File: bs-remove.R

package info (click to toggle)
r-cran-bslib 0.4.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,332 kB
  • sloc: javascript: 10,075; makefile: 30; sh: 23
file content (63 lines) | stat: -rw-r--r-- 1,598 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
#' Remove or retrieve Sass code from a theme
#'
#' @inheritParams bs_theme_update
#' @param ids a character vector of ids
#'
#' @return a modified [bs_theme()] object.
#'
#' @export
#' @examples
#'
#' # Remove CSS rules for print and carousels
#' bs4 <- bs_theme(version = 4)
#' bs_remove(bs4, c("_print", "_carousel"))
#'
#' # Remove BS3 compatibility layer
#' bs_remove(bs4, "bs3compat")
#'
bs_remove <- function(theme, ids = character(0)) {
  ids <- retain_valid_ids(theme, ids)
  sass_bundle_remove(theme, ids)
}

#' @rdname bs_remove
#' @param include_unnamed whether or not to include unnamed [sass::sass_layer()]s
#' (e.g., Bootstrap Sass variables, functions, and mixins).
#' @export
bs_retrieve <- function(theme, ids = character(0), include_unnamed = TRUE) {
  ids <- retain_valid_ids(theme, ids)
  if (isTRUE(include_unnamed)) {
    ids <- c(ids, "")
  }
  layers <- theme$layers[names(theme$layers) %in% ids]
  structure(
    sass_bundle(!!!layers),
    class = class(theme)
  )
}


retain_valid_ids <- function(theme, ids) {
  assert_bs_theme(theme)
  stopifnot(is.character(ids))

  bundle_ids <- names(theme$layers)
  if (!length(ids)) {
    stop(
      "Provide one or more of the following `ids`: ",
      paste(bundle_ids, collapse = ", ")
    )
  }

  missing_ids <- setdiff(ids, bundle_ids)
  if (length(missing_ids)) {
    warning(
      "The following `ids` weren't found in `theme`: ",
      paste(missing_ids, collapse = ", "), ".\n\n",
      "Possible `ids` include: ",
      paste(bundle_ids[nzchar(bundle_ids)], collapse = ", ")
    )
  }

  intersect(ids, bundle_ids)
}