Skip to content

Commit 8751c7c

Browse files
authored
feat(card): Report full screen state as a Shiny input (#1006)
* refactor(card): style * feat(card): Report full screen state to Shiny * feat(value_box): Add `id` parameter * feat(card): Report state on page load, too * chore: Add a comment about recursive calls in _setShinyInput() * chore: yarn build * refactor(card.ts): Simplify recursive setShinyInput * docs(news): Add item
1 parent be3d84a commit 8751c7c

File tree

11 files changed

+101
-28
lines changed

11 files changed

+101
-28
lines changed

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
* Added `input_task_button()`, a replacement for `shiny::actionButton()` that automatically prevents an operation from being submitted multiple times. It does this by, upon click, immediately transitioning to a "Processing..." visual state that does not let the button be clicked again. The button resets to its clickable state automatically after the reactive flush it causes is complete; or, for advanced scenarios, `update_task_button()` can be used to manually control when the button resets.
1414

15+
* Both `card()` and `value_box()` now take an `id` argument that, when provided, is used to report the full screen state of the card or value box to the server. For example, when using `card(id = "my_card", full_screen = TRUE)` you can determine if the card is currently in full screen mode by reading the boolean value of `input$my_card$full_screen`. (#1006)
16+
1517
## Improvements
1618

1719
* `layout_columns()` was rewritten in Typescript as a custom element to improve the portability of the component. (#931)

R/card.R

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@
2828
#' together into one `wrapper` call (e.g. given `card("a", "b",
2929
#' card_body("c"), "d")`, `wrapper` would be called twice, once with `"a"` and
3030
#' `"b"` and once with `"d"`).
31+
#' @param id Provide a unique identifier for the `card()` or `value_box()` to
32+
#' report its state to Shiny. For example, using `id = "my_card"`, you can
33+
#' observe the card's full screen state with `input$my_card$full_screen`.
3134
#'
3235
#' @return A [htmltools::div()] tag.
3336
#'
@@ -70,21 +73,34 @@
7073
#' )
7174
#' )
7275
#'
73-
card <- function(..., full_screen = FALSE, height = NULL, max_height = NULL, min_height = NULL, fill = TRUE, class = NULL, wrapper = card_body) {
74-
76+
card <- function(
77+
...,
78+
full_screen = FALSE,
79+
height = NULL,
80+
max_height = NULL,
81+
min_height = NULL,
82+
fill = TRUE,
83+
class = NULL,
84+
wrapper = card_body,
85+
id = NULL
86+
) {
7587
args <- rlang::list2(...)
7688
argnames <- rlang::names2(args)
7789

7890
attribs <- args[nzchar(argnames)]
7991
children <- as_card_items(args[!nzchar(argnames)], wrapper = wrapper)
8092

81-
if (full_screen && is.null(attribs$id)) {
93+
is_shiny_input <- !is.null(id)
94+
95+
if (full_screen && is.null(id)) {
8296
# a11y: full screen cards need an ID for aria-controls on the toggle button
83-
attribs$id <- paste0("bslib-card-", p_randomInt(1000, 10000))
97+
id <- paste0("bslib-card-", p_randomInt(1000, 10000))
8498
}
8599

86100
tag <- div(
101+
id = id,
87102
class = "card bslib-card bslib-mb-spacing",
103+
class = if (is_shiny_input) "bslib-card-input",
88104
style = css(
89105
height = validateCssUnit(height),
90106
max_height = validateCssUnit(max_height),

R/value-box.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ value_box <- function(
9999
max_height = NULL,
100100
fill = TRUE,
101101
class = NULL,
102+
id = NULL,
102103
theme_color = deprecated()
103104
) {
104105
dots <- separate_arguments(...)
@@ -158,6 +159,7 @@ value_box <- function(
158159
class,
159160
if (!is.null(showcase)) showcase_layout$class
160161
),
162+
id = id,
161163
full_screen = full_screen,
162164
height = height,
163165
max_height = max_height,

inst/components/dist/components.js

Lines changed: 29 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

inst/components/dist/components.js.map

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)