diff --git a/NEWS.md b/NEWS.md index 44bab132e..f1b5aae2f 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,6 +2,8 @@ ## Bug fixes +* `card()` and `value_box()` no longer register `input$_full_screen` for cards that don't have `full_screen = TRUE`, avoiding unexpected inputs in bookmarking workflows. (#1305) + * Fixed label-to-options spacing on `shiny::radioButtons()` and `shiny::checkboxGroupInput()` in Bootstrap 5, where a Shiny rule was overriding the bslib fix. (#1308) # bslib 0.11.0 diff --git a/R/card.R b/R/card.R index 90498f82a..71645a9eb 100644 --- a/R/card.R +++ b/R/card.R @@ -26,10 +26,9 @@ #' together into one `wrapper` call (e.g. given `card("a", "b", #' card_body("c"), "d")`, `wrapper` would be called twice, once with `"a"` and #' `"b"` and once with `"d"`). -#' @param id Provide a unique identifier for the `card()` or `value_box()` to -#' report its full screen state to Shiny. For example, using `id = "my_card"`, -#' you can observe the card's full screen state with -#' `input$my_card_full_screen`. +#' @param id A unique identifier for the card. Note that full-screen +#' capability cannot be added after card creation; `full_screen` must be set +#' at creation time. #' #' @return A [htmltools::div()] tag. #' @@ -90,7 +89,11 @@ card <- function( children <- as_card_items(args[!nzchar(argnames)], wrapper = wrapper) children <- card_image_add_classes(children) - is_shiny_input <- !is.null(id) + # When full_screen=TRUE and id is provided, the card registers + # input$_full_screen to report its full screen state to Shiny. + # The bslib-card-input class is what triggers this JS-side registration, + # so it must only be added when the card actually has full screen capability. + is_shiny_input <- !is.null(id) && full_screen if (full_screen && is.null(id)) { # a11y: full screen cards need an ID for aria-controls on the toggle button diff --git a/R/sysdata.rda b/R/sysdata.rda index c20a80987..07bacd4a9 100644 Binary files a/R/sysdata.rda and b/R/sysdata.rda differ diff --git a/man/card.Rd b/man/card.Rd index 06f81f4be..ebb90602d 100644 --- a/man/card.Rd +++ b/man/card.Rd @@ -42,10 +42,9 @@ arguments in \code{...} which are not already card item(s) (like together into one \code{wrapper} call (e.g. given \code{card("a", "b", card_body("c"), "d")}, \code{wrapper} would be called twice, once with \code{"a"} and \code{"b"} and once with \code{"d"}).} -\item{id}{Provide a unique identifier for the \code{card()} or \code{value_box()} to -report its full screen state to Shiny. For example, using \code{id = "my_card"}, -you can observe the card's full screen state with -\code{input$my_card_full_screen}.} +\item{id}{A unique identifier for the card. Note that full-screen +capability cannot be added after card creation; \code{full_screen} must be set +at creation time.} } \value{ A \code{\link[htmltools:div]{htmltools::div()}} tag. diff --git a/man/value_box.Rd b/man/value_box.Rd index db141db38..1117d80a7 100644 --- a/man/value_box.Rd +++ b/man/value_box.Rd @@ -90,10 +90,9 @@ container with an opinionated height (e.g., \code{page_fillable()}).} card. Use \verb{bg-*} and \verb{text-*} classes (e.g, \code{"bg-danger"} and \code{"text-light"}) to customize the background/foreground colors.} -\item{id}{Provide a unique identifier for the \code{card()} or \code{value_box()} to -report its full screen state to Shiny. For example, using \code{id = "my_card"}, -you can observe the card's full screen state with -\code{input$my_card_full_screen}.} +\item{id}{A unique identifier for the card. Note that full-screen +capability cannot be added after card creation; \code{full_screen} must be set +at creation time.} \item{theme_color}{\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} Use \code{theme} instead.} diff --git a/tests/testthat/test-card.R b/tests/testthat/test-card.R index f782639a4..289c63686 100644 --- a/tests/testthat/test-card.R +++ b/tests/testthat/test-card.R @@ -40,6 +40,20 @@ test_that("card_image()", { ) }) +test_that("card() only registers as Shiny input when full_screen=TRUE", { + card_no_fs <- card(id = "my_card", "content") + card_with_fs <- card(id = "my_card", full_screen = TRUE, "content") + card_fs_no_id <- card(full_screen = TRUE, "content") + + classes_no_fs <- htmltools::tagGetAttribute(card_no_fs, "class") + classes_with_fs <- htmltools::tagGetAttribute(card_with_fs, "class") + classes_fs_no_id <- htmltools::tagGetAttribute(card_fs_no_id, "class") + + expect_no_match(classes_no_fs, "bslib-card-input") + expect_match(classes_with_fs, "bslib-card-input") + expect_no_match(classes_fs_no_id, "bslib-card-input") +}) + test_that("card_image() input validation", { expect_snapshot( error = TRUE,