-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbslib-utils.R
More file actions
341 lines (305 loc) · 7.72 KB
/
Copy pathbslib-utils.R
File metadata and controls
341 lines (305 loc) · 7.72 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#' Bootstrap 4 toggle input
#'
#' A Bootstrap 4 toggle input similar to a checkbox
#'
#' @export
theme_toggle <- function() {
div(
class = "custom-control custom-switch",
tags$input(
id = "custom_mode",
type = "checkbox",
class = "custom-control-input",
onclick = HTML(
"Shiny.setInputValue(
'dark_mode',
document.getElementById('custom_mode').value
);"
)
),
tags$label(
"Custom mode?",
`for` = "custom_mode",
class = "custom-control-label"
)
)
}
#' Custom card component
#'
#' @param ... Card content.
#' @export
super_card <- function(...) {
div(
class = "supercard",
div(class = "supercard_body", ...),
bslib::bs_dependency_defer(super_card_dependency)
)
}
super_card_dependency <- function(theme) {
sass_str <- "
.supercard {
box-shadow: 0 4px 10px 0 rgb(0, 0, 0), 0 4px 20px 0
rgb(0, 0, 0);
width: 50%;
height: 200px;
background-color: $primary;
.supercard_body {
padding: 0.01em 16px;
}
}
"
dep_name <- "supercard"
dep_version <- "1.0.0"
if (bslib::is_bs_theme(theme)) {
bslib::bs_dependency(
input = sass_str,
theme = theme,
name = dep_name,
version = dep_version
)
} else {
htmlDependency(
name = dep_name,
version = dep_version,
src = "supercard-1.0.0/css",
stylesheet = "super-card.css",
package = "OSUICode"
)
}
}
#' Neon theme for bslib demo apps
#'
#' @export
bslib_neon_theme <- bslib::bs_theme(
version = 4,
bg = "#000000",
fg = "#FFFFFF",
primary = "#9600FF",
secondary = "#1900A0",
success = "#38FF12",
info = "#00F5FB",
warning = "#FFF100",
danger = "#FF00E3",
base_font = "Marker Felt",
heading_font = "Marker Felt",
code_font = "Chalkduster"
)
#' Dark theme for bslib demo apps
#'
#' @export
bslib_dark_theme <- bslib::bs_theme_update(
bslib::bs_theme(version = 4),
bg = "black",
fg = "white",
primary = "orange"
)
#' Unified Bootstrap badge
#'
#' Badge providing support for BS3, BS4 and BS5
#'
#' @param text Badge text.
#' @param color Badge color.
#'
#' @return A Bootstrap badge with different class depending on
#' the currently used Bootstrap theme.
#' @export
bs_badge <- function(text, color = NULL) {
# Create common badge skeleton for BS3/4/5
badge_skeleton <- tags$span(class = "badge", text)
# Handle BS4 and BS5 extra class
if (!is.null(color)) {
badge_skeleton <- tagAddRenderHook(
badge_skeleton, function(x) {
# get theme and version
theme <- getCurrentTheme()
version <- if (bslib::is_bs_theme(theme)) {
bslib::theme_version(theme)
}
switch(
version,
# stop if color is used with BS3
"3" = stop(
sprintf(
"color is not available for Bootstrap %s",
version
)
),
"4" = tagQuery(x)$
addClass(sprintf("badge-%s", color))$
allTags(),
"5" = tagQuery(x)$
addClass(sprintf("rounded-pill bg-%s", color))$
allTags()
)
})
}
badge_skeleton
}
#' Bootstrap accordion unified wrapper
#'
#' Provide support for BS4 and BS5
#'
#' @param id Accordion unique id.
#' @param items Slot for \link{bs_accordion_item}.
#'
#' @export
bs_accordion <- function(id, items) {
accordion_tag <- tags$div(
class = "accordion",
id = id,
items
)
tagAddRenderHook(accordion_tag, function(x) {
# get theme and version
theme <- bslib::bs_current_theme()
version <- if (bslib::is_bs_theme(theme)) {
bslib::theme_version(theme)
}
if (version == "3") {
stop(
sprintf(
"accordion is not available for Bootstrap %s",
version
)
)
}
# process accordion items to add
# missing attributes
new_items <- lapply(seq_along(items), function(i) {
# temp ids based on the parent id
heading_id <- paste(id, "heading", i, sep = "_")
controls_id <- paste0(id, "_collapse_", i)
target_id <- paste0("#", controls_id)
# resolve bs_according_item
items[[i]] <- as.tags(items[[i]])
# BS4 and BS5 have minor differences
switch(
version,
"4" = tagQuery(items[[i]])$
find(".card-header")$
addAttrs("id" = heading_id)$
find(".btn")$
addAttrs(
"data-target" = target_id,
"aria-controls" = controls_id
)$
resetSelected()$
find(".collapse")$
addAttrs(
"id" = controls_id,
"aria-labelledby" = heading_id,
"data-parent" = paste0("#", id)
)$
allTags(),
"5" = tagQuery(items[[i]])$
find(".accordion-header")$
addAttrs("id" = heading_id)$
children()$
addAttrs(
"data-bs-target" = target_id,
"aria-controls" = controls_id
)$
resetSelected()$
find(".accordion-collapse")$
addAttrs(
"id" = controls_id,
"aria-labelledby" = heading_id,
"data-bs-parent" = paste0("#", id)
)$
allTags()
)
})
# alter main tag structure
tagQuery(x)$
# replace accordion items processed above
empty()$
append(new_items)$
allTags()
})
}
#' Bootstrap unified accordion item wrapper
#'
#' Provide support for BS4 and BS5
#'
#' @param title Item title.
#' @param content Item content.
#' @param active Whether to open the itm at start. Default to FALSE.
#'
#' @export
bs_accordion_item <- function(title, content, active = FALSE) {
item_body <- tags$div(
# id will be added from bs_accordion
# aria-labelledby also added from bs_accordion
# class differs between BS4 and BS5
# data parent differs between BS4 and BS5
class = paste("collapse", if (active) "show"),
tags$div(
# class differs between BS4 and BS5
content
)
)
# accordion item wrapper
accordion_item_tag <- tags$div(
# class differs between BS4 and BS5
item_body
)
tagAddRenderHook(accordion_item_tag, function(x) {
# get theme and version
theme <- bslib::bs_current_theme()
version <- if (bslib::is_bs_theme(theme)) {
bslib::theme_version(theme)
}
# create accordion item header
item_header <- if (version == "4") {
tags$div(
class = "card-header",
# id will be added from bs_accordion
tags$h2(
class = "mb-0",
tags$button(
class = "btn btn-link btn-block text-left",
type = "button",
`data-toggle` = "collapse",
# data-target will be added from bs_accordion
`aria-expanded` = tolower(active),
# aria-controls will be added from bs_accordion
title
)
)
)
} else if (version == "5") {
tags$h2(
class = "accordion-header",
tags$button(
class = "accordion-button",
type = "button",
`data-bs-toggle` = "collapse",
`aria-expanded` = tolower(active),
title
)
)
}
# alter tag structure
switch(
version,
# don't need to handle BS3
"4" = tagQuery(x)$
addClass("card")$
# prepend header tag
prepend(item_header)$
find(".collapse")$
children()$
# add class to item body
addClass("card-body")$
allTags(),
"5" = tagQuery(x)$
addClass("accordion-item")$
prepend(item_header)$
find(".collapse")$
addClass("accordion-collapse")$
children()$
addClass("accordion-body")$
allTags()
)
})
}