Skip to content

Commit 6c6960c

Browse files
committed
feat: create a new button to switch theme
1 parent f9ca75f commit 6c6960c

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ export(set_golem_name)
7676
export(set_golem_options)
7777
export(set_golem_version)
7878
export(set_golem_wd)
79+
export(switch_theme_button)
7980
export(use_external_css_file)
8081
export(use_external_file)
8182
export(use_external_html_template)

R/switch_theme_button.R

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#' @export
2+
switch_theme_button <- function(inputId, label = NULL) {
3+
if (!rlang::is_installed("bslib")) {
4+
stop("You need to install bslib to use this function")
5+
}
6+
7+
button <- bslib::input_switch(
8+
id = inputId,
9+
label = label,
10+
width = "fit-content"
11+
) |>
12+
shiny::tagAppendAttributes(
13+
class = "golem-switch-themer"
14+
)
15+
16+
css_file_path <- system.file(
17+
"utils",
18+
"golem_button_theme.css",
19+
package = "golem"
20+
)
21+
22+
css_dependency <- htmltools::htmlDependency(
23+
name = "golem-switch-themer-css",
24+
version = "1.0.0",
25+
src = dirname(css_file_path),
26+
stylesheet = basename(css_file_path)
27+
)
28+
29+
button_with_dependency <- htmltools::attachDependencies(button, css_dependency)
30+
31+
button_with_dependency
32+
}

inst/utils/golem_button_theme.css

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
.golem-switch-themer .form-switch .form-check-input:checked {
3+
background-position: right center;
4+
--bs-form-switch-bg: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyMCIgaGVpZ2h0PSIyMCIgdmlld0JveD0iMCAwIDI0IDI0IiBmaWxsPSJub25lIiBzdHJva2U9IiNmZmZmZmYiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBjbGFzcz0ibHVjaWRlIGx1Y2lkZS1tb29uIj48cGF0aCBkPSJNMTIgM2E2IDYgMCAwIDAgOSA5IDkgOSAwIDEgMS05LTlaIi8+PC9zdmc+) !important;
5+
background-color: var(--golem-switcher);
6+
}
7+
8+
.golem-switch-themer .form-switch .form-check-input {
9+
--bs-form-switch-bg: url(data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIyNCIgaGVpZ2h0PSIyNCIgdmlld0JveD0iMCAwIDI0IDI0IiBmaWxsPSJub25lIiBzdHJva2U9IiNmZmZmZmYiIHN0cm9rZS13aWR0aD0iMiIgc3Ryb2tlLWxpbmVjYXA9InJvdW5kIiBzdHJva2UtbGluZWpvaW49InJvdW5kIiBjbGFzcz0ibHVjaWRlIGx1Y2lkZS1zdW4tbWVkaXVtIj48Y2lyY2xlIGN4PSIxMiIgY3k9IjEyIiByPSI0Ii8+PHBhdGggZD0iTTEyIDN2MSIvPjxwYXRoIGQ9Ik0xMiAyMHYxIi8+PHBhdGggZD0iTTMgMTJoMSIvPjxwYXRoIGQ9Ik0yMCAxMmgxIi8+PHBhdGggZD0ibTE4LjM2NCA1LjYzNi0uNzA3LjcwNyIvPjxwYXRoIGQ9Im02LjM0MyAxNy42NTctLjcwNy43MDciLz48cGF0aCBkPSJtNS42MzYgNS42MzYuNzA3LjcwNyIvPjxwYXRoIGQ9Im0xNy42NTcgMTcuNjU3LjcwNy43MDciLz48L3N2Zz4=) !important;
10+
width: 2.6em !important;
11+
height: 1.6em;
12+
background-image: var(--bs-form-switch-bg);
13+
border-radius: 2em;
14+
transition: background-position 0.15s ease-in-out;
15+
background-repeat: no-repeat;
16+
background-size: 18px;
17+
--golem-switcher: var(--bs-primary);
18+
background-color: var(--golem-switcher);
19+
cursor: pointer;
20+
}

0 commit comments

Comments
 (0)