Skip to content

Commit

Permalink
feat(visible): small utility function for visibility
Browse files Browse the repository at this point in the history
  • Loading branch information
nteetor committed Nov 26, 2020
1 parent 83137f0 commit f647aa7
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@ Collate:
'shadow.R'
'text.R'
'utils-docs.R'
'visible.R'
'width.R'
'zzz.R'
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export(padding)
export(position)
export(shadow)
export(text)
export(visible)
export(width)
export(with_style)
import(rlang)
Expand Down
34 changes: 34 additions & 0 deletions R/visible.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
visible_value <- function(value) {
if (is_true(value)) {
"visible"
} else if (is_false(value)) {
"invisible"
}
}

#' Element visibility
#'
#' The `visible()` function changes the visibility of a tag element. An
#' invisible element is both visually hidden and is also hidden from screen
#' readers.
#'
#' @inheritParams background
#'
#' @param value One of `TRUE` or `FALSE` specifying if the element is visible,
#' defaults to `TRUE`.
#'
#' @export
#' @examples
#'
#' library(htmltools)
#'
#' div("I am hidden") %>%
#' visible(FALSE)
#'
visible <- function(x, value) {
assert_subject(x)

class <- visible_value(value)

add_class(x, class)
}
27 changes: 27 additions & 0 deletions man/visible.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions tests/testthat/test-visible.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
context("visible()")

test_that("adds classes", {
div() %>%
visible(FALSE) %>%
expect_html_class("invisible")

div() %>%
visible(TRUE) %>%
expect_html_class("visible")
})

0 comments on commit f647aa7

Please sign in to comment.