From f647aa7346eba0f0b9be33ff2a792fcbeac46c30 Mon Sep 17 00:00:00 2001 From: nteetor Date: Thu, 26 Nov 2020 10:23:14 -0600 Subject: [PATCH] feat(visible): small utility function for visibility --- DESCRIPTION | 1 + NAMESPACE | 1 + R/visible.R | 34 ++++++++++++++++++++++++++++++++++ man/visible.Rd | 27 +++++++++++++++++++++++++++ tests/testthat/test-visible.R | 11 +++++++++++ 5 files changed, 74 insertions(+) create mode 100644 R/visible.R create mode 100644 man/visible.Rd create mode 100644 tests/testthat/test-visible.R diff --git a/DESCRIPTION b/DESCRIPTION index 35b9c16..4bb3bff 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -54,5 +54,6 @@ Collate: 'shadow.R' 'text.R' 'utils-docs.R' + 'visible.R' 'width.R' 'zzz.R' diff --git a/NAMESPACE b/NAMESPACE index 7cc36a0..4b964a3 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -22,6 +22,7 @@ export(padding) export(position) export(shadow) export(text) +export(visible) export(width) export(with_style) import(rlang) diff --git a/R/visible.R b/R/visible.R new file mode 100644 index 0000000..013b29b --- /dev/null +++ b/R/visible.R @@ -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) +} diff --git a/man/visible.Rd b/man/visible.Rd new file mode 100644 index 0000000..3b69e8d --- /dev/null +++ b/man/visible.Rd @@ -0,0 +1,27 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/visible.R +\name{visible} +\alias{visible} +\title{Element visibility} +\usage{ +visible(x, value) +} +\arguments{ +\item{x}{A tag element or \link{.style} pronoun.} + +\item{value}{One of \code{TRUE} or \code{FALSE} specifying if the element is visible, +defaults to \code{TRUE}.} +} +\description{ +The \code{visible()} function changes the visibility of a tag element. An +invisible element is both visually hidden and is also hidden from screen +readers. +} +\examples{ + +library(htmltools) + +div("I am hidden") \%>\% + visible(FALSE) + +} diff --git a/tests/testthat/test-visible.R b/tests/testthat/test-visible.R new file mode 100644 index 0000000..cf73a6e --- /dev/null +++ b/tests/testthat/test-visible.R @@ -0,0 +1,11 @@ +context("visible()") + +test_that("adds classes", { + div() %>% + visible(FALSE) %>% + expect_html_class("invisible") + + div() %>% + visible(TRUE) %>% + expect_html_class("visible") +})