Skip to content

Commit

Permalink
feat: Enable bookmarking in input_switch() and input_dark_mode() (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
gadenbuie authored Jan 14, 2025
1 parent 7b1f15a commit 7594d67
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

* Optimized for better performance the internal functions that compile Sass to call the `color-contrast()` algorithm. (#1140)

* `input_switch()` and `input_dark_mode()` can be included in Shiny's [bookmarking feature](https://shiny.posit.co/r/articles/share/bookmarking-state/). (#1166)

# bslib 0.8.0

## Breaking changes
Expand Down
10 changes: 9 additions & 1 deletion R/input-dark-mode.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ input_dark_mode <- function(..., id = NULL, mode = NULL) {
mode <- rlang::arg_match(mode, c("light", "dark"))
}

if (!is.null(id)) {
mode <- shiny::restoreInput(id, default = mode)
}

if (any(!nzchar(rlang::names2(rlang::list2(...))))) {
abort("All arguments in `...` must be named.")
}
Expand Down Expand Up @@ -63,7 +67,11 @@ input_dark_mode <- function(..., id = NULL, mode = NULL) {
#' @param session A Shiny session object (the default should almost always be
#' used).
#' @export
toggle_dark_mode <- function(mode = NULL, ..., session = get_current_session()) {
toggle_dark_mode <- function(
mode = NULL,
...,
session = get_current_session()
) {
rlang::check_dots_empty(
error = function(err) rlang::warn(rlang::cnd_message(err))
)
Expand Down
9 changes: 8 additions & 1 deletion R/input-switch.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@
#' @family input controls
#' @export
input_switch <- function(id, label, value = FALSE, width = NULL) {
tag <- input_checkbox(id, label, class = "bslib-input-switch form-switch", value = value, width = width)
value <- shiny::restoreInput(id, default = value)
tag <- input_checkbox(
id,
label,
class = "bslib-input-switch form-switch",
value = value,
width = width
)
tag <- tag_require(tag, version = 5, caller = "input_switch()")
as_fragment(tag)
}
Expand Down

0 comments on commit 7594d67

Please sign in to comment.