bslib
bslib copied to clipboard
Make `sidebar()`'s collapse icon configurable
For example:
layout_sidebar(
"Main content",
sidebar = sidebar(
"Controls",
collapse_icon = bsicons::bs_icon("sliders")
)
)
I completely recognize that collapse_icon
is a very simple solution to make the icon customizable. I'm realizing, though, that we're very likely to end up wanting to allow customizations in other aspects of the collapse toggle, like the label or the position, and we are facing a similar problem with the full screen toggle as well.
Given this, what if we were to encapsulate the collapse options into a single function, like sidebar_collapse_options()
? This would give us some flexibility to change the collapse options in the future without changing the sidebar()
API.
Such a function might look like this. Its arguments consult global options that can be set individually and it returns a named list so that we could have sidebar(collapse_options = sidebar_collapse_options())
as the default.
sidebar_collapse_options <- function(
icon = getOption("bslib.sidebar.collapse_icon", NULL),
label = getOption("bslib.sidebar.collapse_label", "Toggle Sidebar"),
...
) {
opts <- list(icon = icon, label = label)
names(opts) <- paste0("bslib.sidebar.collapse_", names(opts))
class(opts) <- c("sidebar_collapse", "bslib_options")
opts
}
sidebar_collapse_options()
#> $bslib.sidebar.collapse_icon
#> NULL
#>
#> $bslib.sidebar.collapse_label
#> [1] "Toggle Sidebar"
#>
#> attr(,"class")
#> [1] "sidebar_collapse" "bslib_options"
The list names are a little verbose, but that's to support passing sidebar_collapse_options()
to options()
to set the values globally:
options(
sidebar_collapse_options(
icon = bsicons::bs_icon("sliders"),
label = "Toggle settings"
)
)
getOption("bslib.sidebar.collapse_icon")
#> <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" class="bi bi-sliders " style="height:1em;width:1em;fill:currentColor;vertical-align:-0.125em;" aria-hidden="true" role="img" ><path fill-rule="evenodd" d="M11.5 2a1.5 1.5 0 1 0 0 3 1.5 1.5 0 0 0 0-3zM9.05 3a2.5 2.5 0 0 1 4.9 0H16v1h-2.05a2.5 2.5 0 0 1-4.9 0H0V3h9.05zM4.5 7a1.5 1.5 0 1 0 0 3 1.5 1.5 0 0 0 0-3zM2.05 8a2.5 2.5 0 0 1 4.9 0H16v1H6.95a2.5 2.5 0 0 1-4.9 0H0V8h2.05zm9.45 4a1.5 1.5 0 1 0 0 3 1.5 1.5 0 0 0 0-3zm-2.45 1a2.5 2.5 0 0 1 4.9 0H16v1h-2.05a2.5 2.5 0 0 1-4.9 0H0v-1h9.05z"></path></svg>
sidebar_collapse_options()
#> $bslib.sidebar.collapse_icon
#> <svg ... >
#>
#> $bslib.sidebar.collapse_label
#> [1] "Toggle settings"
#>
#> attr(,"class")
#> [1] "sidebar_collapse" "bslib_options"
This is also what I'm envisioning for full_screen_options()
.
For posterity, I'm in agreement that we need a way to customize the label (mainly for internationalization purposes), but I'm not convinced the cost of adding sidebar_collapse_options()
is worth the benefit -- we already have a sidebar()
for customizing the sidebar, and so it feels weird to adopt a different pattern for a specific subset of options.
That said, I do, in general, like the idea, and I'd probably be in favor of adopting it for card(fill_screen = full_screen_options())
. However, if internationalization is the primary/sole motivating factor here, I may prefer to support something like options(bslib.sidebar_toggle_label = ..., bslib.card_full_screen_labels = )
for now.
And, since it seems logical to consider this in tandem with full_screen_options()
, I'm thinking we'll stash this until the next release