Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introducing navtreePanel() #3326

Open
wants to merge 25 commits into
base: main
Choose a base branch
from
Open

Introducing navtreePanel() #3326

wants to merge 25 commits into from

Conversation

cpsievert
Copy link
Collaborator

@cpsievert cpsievert commented Mar 9, 2021

This PR introduces navtreePanel() -- a modern drop-in replacement for navlistPanel() which offers two main benefits:

  1. Handles arbitrarily nested navbarMenu()s via collapsing submenus (you're only allowed one in navlistPanel(), tabsetPanel(), etc)
  2. Navigation labels remain displayed after selection.

For a basic example:

shinyApp(
  fluidPage(
    theme = bslib::bs_theme(),
    navtreePanel(
      tabPanel("A", "a"),
      tabPanel("B", "b"),
      navbarMenu(
        "More letters",
        tabPanel("C", "c"),
        tabPanel("D", "d"),
        navbarMenu(
          "More letters",
          tabPanel("E", "e"),
          tabPanel("F", "f")
        )
      )
    )
  ),
  server = function(input, output) {}
)

Screen Shot 2021-03-08 at 6 39 06 PM

navtreePanel() works with all the tab updating functions, here's a small demo:

library(shiny)

ui <- fluidPage(
  theme = bslib::bs_theme(),
  p("Press add, then select, then remove"),
  actionButton("add", "Add 'Dynamic' menu"),
  actionButton("select", "Select 'Dynamic' menu"),
  actionButton("remove", "Remove 'Dynamic' menu"),
  br(), br(),
  navtreePanel(
    id = "treelist",
    tabPanel("A", "a"),
    tabPanel("B", "b"),
    navbarMenu(
      "More letters",
      tabPanel("C", "c"),
      tabPanel("D", "d"),
      navbarMenu(
        "More letters",
        tabPanel("E", "e"),
        tabPanel("F", "f")
      )
    )
  )
)

server <- function(input, output) {
  observe(print(input$treelist))
  
  observeEvent(input$add, {
    appendTab(
      inputId = "treelist",
      navbarMenu(
        "Dynamic Menu",
        tabPanel("Dynamic", "Dynamic tab")
      )
    )
  })
  observeEvent(input$select, {
    updateTabsetPanel(inputId = "treelist", selected = "Dynamic")
  })
  observeEvent(input$remove, {
    removeTab(inputId = "treelist", target = "Dynamic Menu")
  })
}

shinyApp(ui, server)

Testing notes

Install remotes::install_github("rstudio/shiny") and run the app above. You should see a collapsed menu appear after clicking "Add"

First, click the "Add" button and a collapsed menu should appear. Then, click "select" and that collapsed menu should expand and the item named "Dynamic" tab should be highlighted. Then, click "Remove" and the menu should disappear.

TODO

  • Make it easy to put content in the nav
  • Can we think of this more generally as being integrated with a collapsible sidebar?

@cpsievert cpsievert requested a review from wch March 9, 2021 00:46
@cpsievert cpsievert changed the base branch from master to bs4-tab-panels March 9, 2021 17:30
@cpsievert cpsievert added this to the 1.7 milestone Mar 12, 2021
Base automatically changed from bs4-tab-panels to master March 22, 2021 17:38
@cpsievert
Copy link
Collaborator Author

Note to self: all data-target uses should also have a data-bs-target for BS5 support

@cpsievert cpsievert removed this from the 1.7 milestone Jun 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant