rstudio / shinydashboard

Shiny Dashboarding framework
https://rstudio.github.io/shinydashboard/
Other
896 stars 298 forks source link

Toggling boxes to collapse has unexpected results with nested boxes #395

Open rwhitt221 opened 1 year ago

rwhitt221 commented 1 year ago

Good afternoon.

First off this is a beautiful package and is hugely useful.

I like to guide the user by using boxes and collapsing/uncollapsing the next element as they travel through the app.

I also often nest boxes, particularly when I am using modules to develop.

I've noticed that when you nest boxes, you get unexpected results when you toggle their collapsed state. If you toggle the highest level, it will often collapse other boxes but leave the upper box open, but the plus minus sign on the box will flip without actually having an effect.

Interestingly, if the user toggles the box with the plus minus, the "correct" behaviour occurs. I just don't seem to be able to replicate this with code.

Below is a reprex.

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)

ui <- dashboardPage(
  title = "Box API",
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    tags$style("body { background-color: ghostwhite}"),
    fluidRow(
      actionButton("toggle_box", "Toggle Box")
    ),
    br(),
    box(
      title = textOutput("box_state"),
      id = "mybox",
      collapsible = TRUE,
      closable = TRUE,
      box(
        title = textOutput("box_state2"),
        id = "mybox2",
        collapsible = TRUE,
        closable = TRUE
      )
    )
  )
)

server <- function(input, output, session) {
  output$plot <- renderPlot({
    req(!input$mybox$collapsed)
    plot(rnorm(200))
  })

  output$box_state <- renderText({
    state <- if (input$mybox$collapsed) "collapsed" else "uncollapsed"
    paste("My box is", state)
  })

  output$box_state2 <- renderText({
    state <- if (input$mybox2$collapsed) "collapsed" else "uncollapsed"
    paste("My box is", state)
  })

  observeEvent(input$toggle_box, {
    updateBox("mybox", action = "toggle")
  })

  observeEvent(input$update_box, {
    updateBox(
      "mybox",
      action = "update",
      options = list(
        title = h2("New title", dashboardLabel(1, status = "primary")),
        status = "danger",
        solidHeader = TRUE,
        width = 4
      )
    )
  })

  observeEvent(input$mybox$visible, {
    collapsed <- if (input$mybox$collapsed) "collapsed" else "uncollapsed"
    visible <- if (input$mybox$visible) "visible" else "hidden"
    message <- paste("My box is", collapsed, "and", visible)

    showNotification(message, type = "warning", duration = 1)
  })
}

shinyApp(ui, server)

Many thanks and all the best. Keep up the good work.

YongbingDing commented 1 year ago

您好,丁永兵已经收到您的邮件,谢谢。