rstudio / bslib

Tools for theming Shiny and R Markdown via Bootstrap 3, 4, or 5.
https://rstudio.github.io/bslib/
Other
443 stars 49 forks source link

sidebar_toggle doesn't close a sidebar #1042

Closed fredericva closed 2 months ago

fredericva commented 2 months ago

We just updated bslib from 0.6.1 to 0.7 and noticed a change (possibly a bug) in the behaviour of toggle_sidebar()

We develop an app in shiny using bslib and rhino, In our code, we have a global sidebar which is closed by default and opened only in certain pages. With the new version the sidebar starts closed, but as soon as it opens, it doesn't close when we navigate to a page where it should be.

UI <- function(id){
  page_navbar(
    ...
    sidebar = = sidebar(
      id = ns("global_sidebar"),
      width = 300,
      open = "closed",
      globalSidebarModule$ui(ns("globalSidebarModule"))
    ),
    <rest of the UI>
}

server <- function(id){
  <...>
  observe({
    print(input$main_nav)
    if (input$main_nav %in% c("page2", "page3", "Page4")){
       print("open")
       sidebar_toggle(
          id = "global_sidebar",
          open = "open"
        )
     } else {
       print("closed")
       sidebar_toggle(
          id = "global_sidebar",
          open = "closed"
       )
     }
   })
<...>

when we loanch the app (in page1), the sidebar is closed, when we navigate to page2, sidebar opens, but when we go back in page1, even if the "closed" part of the code runs, the sidebar stays open.

gadenbuie commented 2 months ago

Thanks for the report @fredericva! The bug is that we refactored the code to clean up the internal implementation and are now sending a method = "closed" message to the server when it should be method = "close". I'll have a PR to fix this up shortly.

Here's a complete, minimal reprex:

library(shiny)
library(bslib)
# pkgload::load_all()

ui <- page_navbar(
  id = "page",
  sidebar = sidebar(
    id = "global",
    open = "closed"
  ),
  nav_panel("one", "Page one"),
  nav_panel("two", "Page two"),
  nav_panel("three", "Page three")
)

server <- function(input, output, session) {
  observeEvent(input$page, {
    toggle_sidebar("global", input$page != "one")
  })
}

shinyApp(ui, server)
github-actions[bot] commented 1 week ago

This issue has been automatically locked. If you have found a related problem, please open a new issue (with a reproducible example or feature request) and link to this issue. :raising_hand: Need help? Connect with us on Discord or Posit Community.