rstudio / bslib

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

feat: Add option to place sidebar above main content on mobile #1088

Closed gadenbuie closed 1 month ago

gadenbuie commented 1 month ago

Closes #1010

This PR adds a new "always-above" option for the mobile component of sidebar(open=), in other words:

# Page with sidebar permantenly open below main content on mobile
page_sidebar(
  sidebar = sidebar("Sidebar"),
  "Main content"
)

# Page with sidebar permanently open **above** main content on mobile
page_sidebar(
  sidebar = sidebar("Sidebar", open = list(mobile = "always-above")),
  "Main content"
)

This feature works by:

  1. Teaching sidebar_open_on() that "always-above" is a valid choice for the mobile component.
  2. Reversing the order of main and sidebar in the markup when open$mobile == "always-above".

This won't change visual order for screens above mobile size, where the layout is determined by grid. It will, however, result in the sidebar receiving tab focus before the main content area, across all screen sizes. That seems a reasonable trade-off and in-line with the types of UIs where users would want the sidebar to be shown before the main content on mobile devices.


Fixes #1081

Incidentally, I ran into the problem described in #1081 while working on this. The issue turns out to be that we manually provide values to rlang::arg_match(), so calling sidebar_open_on() without arguments could potentially throw. The fix is to use scalar default values in that helper function.