File: utils-tags.R

package info (click to toggle)
r-cran-bslib 0.9.0%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,412 kB
  • sloc: javascript: 13,349; makefile: 33; sh: 23
file content (66 lines) | stat: -rw-r--r-- 2,339 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
tag_require <- function(tag, version = version_default(), caller = "") {
  tagAddRenderHook(
    tag,
    replace = FALSE,
    func = function(x) {
      # If we know for sure the version isn't sufficient, it's safe to throw
      current_version <- theme_version(bs_current_theme())
      if (isTRUE(current_version < version)) {
        stop(
          caller,
          " requires Bootstrap ",
          version,
          " or higher. ",
          "To specify the version of Bootstrap, see https://rstudio.github.io/bslib/#basic-usage",
          call. = FALSE
        )
      }
      # We generally don't know the theme/version if any of these conditions are true:
      # 1. We're inside an Rmd output format that doesn't run through rmarkdown::html_document_base
      #    * pkgdown is one known case where the Bootstrap version may be customized, but bslib
      #      doesn't currently have a way to know what the version is.
      # 2. shiny::bootstrapLib() is being called while shiny is not shiny::isRunning()
      #   * At one point I was hoping bootstrapLib() could set the relevant context when
      #     statically rendered, but we didn't end up merging this
      #     https://github.com/rstudio/htmltools/pull/267
      # 3. Someone else is providing the bootstrap dependency
      #   * I currently don't know of any cases where this is relevant, but it might be
      #
      # So, since there are totally legitimate cases where the version requirement
      # could be met, but we don't know for sure what's happening server-side,
      # resort to a client-side check/warning
      return(tag_require_client_side(x, version, caller))
    }
  )
}

tag_require_client_side <- function(
  tag,
  version = version_default(),
  caller = ""
) {
  tagAppendChild(
    tagAppendAttributes(
      tag,
      "data-require-bs-version" = version,
      "data-require-bs-caller" = caller
    ),
    htmlDependency(
      name = "bslib-tag-require",
      version = get_package_version("bslib"),
      package = "bslib",
      src = "components",
      script = "tag-require.js"
    )
  )
}

tag_add_outer_class <- function(x, class = NULL, ...) {
  if (is.null(class)) return(x)

  if (inherits(x, "shiny.tag")) {
    return(tagAppendAttributes(x, class = class, ...))
  }

  as_fill_carrier(div(x, class = class, ...))
}