Skip to content

Commit 2c7801a

Browse files
committed
Adding docs and tests, refined
1 parent e03aada commit 2c7801a

File tree

6 files changed

+103
-7
lines changed

6 files changed

+103
-7
lines changed

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ export(toggle_switch)
164164
export(toggle_tooltip)
165165
export(toolbar)
166166
export(toolbar_input_button)
167+
export(toolbar_input_switch)
167168
export(tooltip)
168169
export(update_popover)
169170
export(update_submit_textarea)

R/toolbar.R

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,27 @@ toolbar_input_button <- function(
148148
}
149149

150150

151+
#' Add toolbar switch input
152+
#'
153+
#' @description
154+
#' A switch input designed to fit well in small places such as in a [toolbar()].
155+
#'
156+
#' @examplesIf rlang::is_interactive()
157+
#' toolbar(
158+
#' align = "right",
159+
#' toolbar_input_switch(id = "toggle", label = "Enable", value = TRUE),
160+
#' toolbar_input_switch(id = "switch", value = FALSE)
161+
#' )
162+
#'
163+
#' @param id The input ID.
164+
#' @param label The label to display next to the switch. If `NULL`, no label
165+
#' is displayed.
166+
#' @param value The initial state of the switch. Default is `FALSE`.
167+
#'
168+
#' @return Returns a switch input suitable for use in a toolbar.
169+
#'
170+
#' @family Toolbar components
171+
#' @export
151172
toolbar_input_switch <- function(
152173
id,
153174
label = NULL,

inst/components/scss/toolbar.scss

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,12 @@
5656
/* ---- Toolbar Input Switch ---- */
5757

5858
.form-group.shiny-input-container:has(.form-switch) {
59-
width: auto;
60-
}
61-
62-
.bslib-input-switch {
63-
margin-inline: 2px;
59+
width: auto; // Overrides default width which adds unnecessary space
6460
}
6561

66-
.form-check {
67-
margin-bottom: 0px;
62+
.bslib-input-switch.form-check {
63+
margin-bottom: 0; // Remove extra form check to ensure alignment with other items
64+
margin-inline: 0.5rem; // Spacing so it's not crunched against other toolbar items
6865
}
6966

7067
/* ---- Adjustments to other elements ---- */

man/toolbar_input_switch.Rd

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

tests/testthat/_snaps/toolbar.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,32 @@
237237
</button>
238238
</bslib-tooltip>
239239

240+
# toolbar_input_switch() has correct attributes
241+
242+
Code
243+
show_raw_html(toolbar_input_switch(id = "switch_with_label", label = "Flip",
244+
value = TRUE))
245+
Output
246+
<div class="form-group shiny-input-container" data-require-bs-version="5" data-require-bs-caller="input_switch()">
247+
<div class="bslib-input-switch form-switch form-check">
248+
<input id="switch_with_label" class="form-check-input" type="checkbox" role="switch" checked/>
249+
<label class="form-check-label" for="switch_with_label">
250+
<span>Flip</span>
251+
</label>
252+
</div>
253+
</div>
254+
255+
---
256+
257+
Code
258+
show_raw_html(toolbar_input_switch(id = "switch_no_label", label = NULL, value = FALSE))
259+
Output
260+
<div class="form-group shiny-input-container" data-require-bs-version="5" data-require-bs-caller="input_switch()">
261+
<div class="bslib-input-switch form-switch form-check">
262+
<input id="switch_no_label" class="form-check-input" type="checkbox" role="switch"/>
263+
<label class="form-check-label" for="switch_no_label">
264+
<span></span>
265+
</label>
266+
</div>
267+
</div>
268+

tests/testthat/test-toolbar.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,14 @@ test_that("toolbar_input_button() markup snapshots", {
337337
)
338338
)
339339
})
340+
341+
342+
# Tests for toolbar_input_switch() #
343+
test_that("toolbar_input_switch() has correct attributes", {
344+
expect_snapshot_html(
345+
toolbar_input_switch(id = "switch_with_label", label = "Flip", value = TRUE)
346+
)
347+
expect_snapshot_html(
348+
toolbar_input_switch(id = "switch_no_label", label = NULL, value = FALSE)
349+
)
350+
})

0 commit comments

Comments
 (0)