forked from rstudio/bslib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbs-global.R
140 lines (129 loc) · 3.6 KB
/
bs-global.R
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#' Global theming
#'
#' `bs_global_theme()` creates and sets the global Bootstrap Sass theme. This
#' theme is typically found by [bs_theme_dependencies()] in the app or document
#' where the global theme is being used. You can obtain the current global theme
#' with [bs_global_get()] or directly set the global theme with
#' [bs_global_set()].
#'
#' @inheritParams bs_theme
#'
#' @return Functions that modify the global theme (e.g., `bs_global_set()`)
#' invisibly return the previously set theme. `bs_global_get()` returns the
#' current global theme.
#'
#' @family Bootstrap theme functions
#'
#' @examples
#' # Remember the global state now (so we can restore later)
#' theme <- bs_global_get()
#'
#' # Use Bootstrap 3 (globally) with some theme customization
#' bs_global_theme(3, bg = "#444", fg = "#e4e4e4", primary = "#e39777")
#' if (rlang::is_interactive()) {
#' bs_theme_preview(with_themer = FALSE)
#' }
#'
#' # If no global theme is active, bs_global_get() returns NULL
#' bs_global_clear()
#' bs_global_get()
#'
#' # Restore the original state
#' bs_global_set(theme)
#'
#' @export
bs_global_theme <- function(
version = version_default(),
preset = NULL,
bg = NULL,
fg = NULL,
primary = NULL,
secondary = NULL,
success = NULL,
info = NULL,
warning = NULL,
danger = NULL,
base_font = NULL,
code_font = NULL,
heading_font = NULL,
...,
bootswatch = NULL
) {
bs_global_set(bs_theme(
version,
preset = preset,
bootswatch = bootswatch,
bg = bg,
fg = fg,
primary = primary,
secondary = secondary,
success = success,
info = info,
warning = warning,
danger = danger,
base_font = base_font,
code_font = code_font,
heading_font = heading_font,
...
))
}
#' @rdname bs_global_theme
#' @inheritParams bs_theme_update
#' @export
bs_global_set <- function(theme = bs_theme()) {
if (!is.null(theme)) {
assert_bs_theme(theme)
}
# In addition to setting a bslib global option, also set shiny's
# current theme if this code is running in an `runtime: shiny` doc
if (is_shiny_runtime() && is_installed("shiny", "1.6")) {
warning(
"bs_global_set() may not work as expected inside runtime: shiny documents. ",
"To update the document's theme, use `session$setCurrentTheme()` instead.",
call. = FALSE
)
}
old_theme <- options(bslib_theme = theme)
invisible(old_theme[["bslib_theme"]])
}
#' @rdname bs_global_theme
#' @export
bs_global_get <- function() {
getOption("bslib_theme")
}
#' @rdname bs_global_theme
#' @export
bs_global_clear <- function() {
old_theme <- options(bslib_theme = NULL)
invisible(old_theme[["bslib_theme"]])
}
#' @rdname bs_global_theme
#' @inheritParams bs_add_variables
#' @export
bs_global_add_variables <- function(..., .where = "defaults",
.default_flag = identical(.where, "defaults")) {
theme <- assert_global_theme("bs_global_add_variables()")
theme <- bs_add_variables(theme, ..., .where = .where, .default_flag = .default_flag)
bs_global_set(theme)
}
#' @rdname bs_global_theme
#' @export
bs_global_add_rules <- function(...) {
theme <- assert_global_theme("bs_global_add_rules()")
theme <- bs_add_rules(theme, ...)
bs_global_set(theme)
}
#' @rdname bs_global_theme
#' @export
bs_global_bundle <- function(...) {
theme <- assert_global_theme("bs_global_bundle()")
theme <- bs_bundle(theme, ...)
bs_global_set(theme)
}
assert_global_theme <- function(calling_func) {
theme <- bs_global_get()
if (is.null(theme)) {
stop("`", calling_func, "` requires that a global theme is first set (do you want to call `bs_global_theme()`?)")
}
theme
}