Skip to content

feat(offcanvas): show_offcanvas() accepts a character id - #1346

Merged
gadenbuie merged 8 commits into
mainfrom
show-offcanvas-id
Aug 13, 2026
Merged

feat(offcanvas): show_offcanvas() accepts a character id#1346
gadenbuie merged 8 commits into
mainfrom
show-offcanvas-id

Conversation

@gadenbuie

@gadenbuie gadenbuie commented Aug 12, 2026

Copy link
Copy Markdown
Member

Closes #1345

Summary

show_offcanvas() previously only accepted a bslib_offcanvas object. Any other input, including a bare string, was silently coerced into body content for a new anonymous panel, unlike its id-oriented siblings hide_offcanvas()/toggle_offcanvas(). This made show_offcanvas("panel") do the wrong thing instead of revealing the existing panel.

show_offcanvas() now dispatches on the type of its first argument:

  • character(1) -> the id of a panel already in the UI (reveals it)
  • list-like/tag content (e.g. htmltools::HTML(), tagList()) -> wrapped into a new anonymous panel (today's old behavior, minus bare characters)
  • a bslib_offcanvas object -> rendered + shown (unchanged)

Since a plain character string containing whitespace or being empty cannot be a valid HTML id, and is almost certainly mistaken body text, show_offcanvas() now errors with actionable guidance in that case rather than silently misinterpreting it. htmltools::HTML() is explicitly excluded from the id path (it is a character vector under the hood) so it is still treated as body content.

No client-side (srcts/) changes were needed: showing an existing panel by id sends the same {method: "toggle", value: "show"} message toggle_offcanvas(id, show = TRUE) already sends.

Reproducible Shiny app

library(shiny)
library(bslib)

ui <- page_fluid(
  actionButton("show_existing", "Show existing panel"),
  actionButton("show_html", "Show HTML body"),
  actionButton("show_object", "Show new offcanvas object"),
  offcanvas(
    "This panel is declared in the UI.",
    id = "existing-panel",
    title = "Existing panel"
  )
)

server <- function(input, output, session) {
  observeEvent(input$show_existing, {
    show_offcanvas("existing-panel")
  })

  observeEvent(input$show_html, {
    show_offcanvas(htmltools::HTML("<p>Rendered as an anonymous panel body.</p>"))
  })

  observeEvent(input$show_object, {
    show_offcanvas(offcanvas(
      "Rendered and shown by the server.",
      title = "New panel"
    ))
  })
}

shinyApp(ui, server)

The first button is the behavior introduced by this PR: it opens the predeclared existing-panel by id. The other two confirm that htmltools::HTML() continues to be body content and that passing an offcanvas() object is unchanged.

Breaking change

show_offcanvas("some string") used to create a new anonymous panel with that string as its body. It is now treated as an id lookup instead, and errors if the string looks like body text (contains whitespace, or is empty). This is a behavior change to a function released in bslib 0.12.0.

gadenbuie and others added 7 commits August 12, 2026 17:51
show_offcanvas() previously only accepted a bslib_offcanvas object,
silently coercing any other input (including a bare string) into body
content for a new anonymous panel. This made it inconsistent with
hide_offcanvas()/toggle_offcanvas(), which are id-oriented, and meant
show_offcanvas("panel") did not do what users expected.

Closes #1345
Adds test cases for the new character-id branch and confirms the
list/tagList anonymous-panel path still works. Regenerates
man/show_offcanvas.Rd and documents the behavior change in NEWS.md.
…() id path

htmltools::HTML() is a character vector under the hood, so it needs an
explicit exclusion to route to the body-content path instead of being
treated as an id. Strings containing whitespace or empty strings can't
be valid HTML ids, so treat those as likely-mistaken body text and
error with actionable guidance instead of silently misinterpreting them.
Cite the PR number per convention and note the HTML()/whitespace
error behavior added since the note was first drafted.
@gadenbuie
gadenbuie marked this pull request as ready for review August 13, 2026 13:23
@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

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.

feat(offcanvas): show_offcanvas() should accept a character id

3 participants