R/pronoun-style.R

Defines functions style_class_add style_dots_eval str.yonder_style_pronoun print.yonder_style_pronoun is_style_bare is_style_box is_style_pronoun style_pronoun

style_pronoun <- function(subclass = NULL) {
  structure(list(), class = c(subclass, "yonder_style_pronoun"))
}

is_style_pronoun <- function(x) {
  inherits(x, "yonder_style_pronoun")
}

is_style_box <- function(x) {
  is_box(x) && is_style_pronoun(unbox(x))
}

is_style_bare <- function(x) {
  is_style_pronoun(x) && length(class(x)) == 1
}

print.yonder_style_pronoun <- function(x, ...) {
  cat("<pronoun>\n")
  invisible(x)
}

str.yonder_style_pronoun <- function(object, ...) {
  cat("<pronoun>\n")
  invisible(NULL)
}

style_dots_eval <- function(..., .style = NULL, .mask = NULL) {
  .style <- .style %||% style_pronoun()
  .mask <- list2(.style = .style, !!!.mask)

  # eval_tidy and with_bindings don't seem to affect ... evaluation
  qargs <- enquos(...)

  flatten_if(lapply(qargs, eval_tidy, data = .mask))
}

style_class_add <- function(x, new) {
  if (is_spliced(x)) {
    x <- unbox(x)
  }

  stopifnot(is_style_pronoun(x))

  if (length(new) < 1 || all(!nzchar(new)) || is_na(new)) {
    return(x)
  }

  if (length(new) > 1) {
    new <- paste(new, collapse = " ")
  }

  if (!is.null(x$class)) {
    new <- paste(x$class, new)
  }

  x$class <- new

  rlang::splice(x)
}

#' Style pronoun
#'
#' @description
#'
#' The `.style` pronoun allows you to define styles for a tag element within the
#' context of the element. Prior to the introduction of the `.style` pronoun tag
#' styles were always applied outside or after constructing a tag element.
#'
#' ```R
#' card() %>% background("primary") %>% display("flex")
#' ```
#'
#' However, once the content of a tag element grows to more than a few lines,
#' associating the element's styles with the element becomes increasingly
#' unintuitive. In these situations, make use of the `.style` pronoun.
#'
#' ```R
#' card(
#'   .style %>%
#'     border("primary") %>%
#'     font("primary")
#' )
#' ```
#'
#' @format NULL
#' @include utils.R
#' @export
.style <- style_pronoun()
nteetor/yonder documentation built on June 8, 2022, 1:36 p.m.