@@ -119,6 +119,7 @@ sidebar <- function(
119119 id = id ,
120120 role = " complementary" ,
121121 class = c(" sidebar" , class ),
122+ hidden = if (open == " closed" ) NA ,
122123 style = css(background_color = bg , color = fg ),
123124 tags $ div(
124125 class = " sidebar-content" ,
@@ -201,20 +202,13 @@ layout_sidebar <- function(
201202 max_height_mobile <- sidebar $ max_height_mobile %|| %
202203 if (is.null(height )) " 250px" else " 50%"
203204
204- if (identical(sidebar $ open , " always" )) {
205- # sidebar initialization is only needed if collapsible
206- sidebar_id <- NULL
207- sidebar_init_js <- NULL
208- } else {
209- sidebar_id <- tagGetAttribute(sidebar $ tag , " id" )
210- sidebar_init_js <- sidebar_js_init(sidebar_id )
211- }
205+ sidebar_init <- if (! identical(sidebar $ open , " always" )) TRUE
212206
213207 res <- div(
214208 class = " bslib-sidebar-layout" ,
215209 class = if (right ) " sidebar-right" ,
216210 class = if (identical(sidebar $ open , " closed" )) " sidebar-collapsed" ,
217- `data-bslib-sidebar-init` = sidebar_id ,
211+ `data-bslib-sidebar-init` = sidebar_init ,
218212 `data-bslib-sidebar-open` = sidebar $ open ,
219213 `data-bslib-sidebar-border` = if (! is.null(border )) tolower(border ),
220214 `data-bslib-sidebar-border-radius` = if (! is.null(border_radius )) tolower(border_radius ),
@@ -226,7 +220,7 @@ layout_sidebar <- function(
226220 ),
227221 !!! contents ,
228222 sidebar_dependency(),
229- sidebar_init_js
223+ sidebar_init_js()
230224 )
231225
232226 res <- bindFillRole(res , item = fill )
@@ -238,17 +232,6 @@ layout_sidebar <- function(
238232 )
239233}
240234
241- sidebar_js_init <- function (id ) {
242- # Requires Shiny >= 1.7.2 and works thanks to rstudio/shiny#3630 (2022-05-09)
243- tags $ script(
244- " document.currentScript.parentElement.removeChild(document.currentScript);\n " ,
245- sprintf(
246- " window.bslib.Sidebar.initCollapsible(document.querySelector('[data-bslib-sidebar-init=\" %s\" ]'));" ,
247- id
248- )
249- )
250- }
251-
252235# ' @describeIn sidebar Toggle a `sidebar()` state during an active Shiny user
253236# ' session.
254237# ' @param session A Shiny session object (the default should almost always be
@@ -270,6 +253,7 @@ sidebar_toggle <- function(id, open = NULL, session = get_current_session()) {
270253 abort(' `open` must be `NULL`, `TRUE` (or "open"), or `FALSE` (or "closed").' )
271254 }
272255
256+ force(id )
273257 callback <- function () {
274258 session $ sendInputMessage(id , list (method = method ))
275259 }
@@ -293,3 +277,13 @@ sidebar_dependency <- function() {
293277 script = " sidebar.min.js"
294278 )
295279}
280+
281+ sidebar_init_js <- function () {
282+ # Note: if we want to avoid inline `<script>` tags in the future for
283+ # initialization code, we might be able to do so by turning the sidebar layout
284+ # container into a web component
285+ tags $ script(
286+ `data-bslib-sidebar-init` = NA ,
287+ HTML(" bslib.Sidebar.initCollapsibleAll()" )
288+ )
289+ }
0 commit comments