R/bulma-icon.R

Defines functions bulma_icon

Documented in bulma_icon

#' @title
#' Bulma Icon
#'
#' @description
#' Bulma is compatible with all icon font libraries: Font Awesome 5,
#' Font Awesome 4, Material Design Icons, Ionicons, etc.
#'
#' The icon element is a container for any type of icon font. Because the icons
#' can take a few seconds to load, and because you want control over the space
#' the icons will take, you can use the icon class as a reliable square
#' container that will prevent the page to "jump" on page load.
#'
#' [Icon](https://bulma.io/documentation/elements/icon/)
#'
#' @family Bulma Elements
#' @name bulma_icon
NULL

#' @describeIn bulma_icon icon container
#'
#' @param icon (string) icon class for the relevant font collection
#' @param size,color style parameters
#' @param stack (str) if this is a stack, the class of the stakc
#' @param ... (tags) content
#' @param tag (fn) default HTML tag
#'
#' @export
bulma_icon <- function(icon,
                       ...,
                       stack = NULL,
                       size = c("small", "normal", "medium", "large"),
                       color = c("primary", "link", "info",
                                 "success", "warning", "danger",
                                 "white", "black", "light", "dark",
                                 "black-bis", "black-ter", "grey-darker",
                                 "grey-dark", "grey-light", "grey-lighter",
                                 "white-ter", "white-bis"),
                       tag = tags$span) {

  assert_character(icon, min.len = 1L)
  assert_function(tag)
  color <- match_arg(color)
  size <- match_arg(size)

  tag(
    class = "icon",
    if (length(icon) == 1) {
      tags$i(class = icon)
    } else {
      exec(
        span,
        class = "fa-stack",
        class = stack,
        !!!map(icon, ~tags$i(class = .))
      )
    },
    ...
  ) %>%
    bulma_text_color(color) %>%
    bulma_size(size) %>%
    add_class("bulma_icon")

}

#' @describeIn bulma_icon text element with icon; contains the icon and the
#' text in a `<span>` element.
#'
#' @param inline (flag) the icon with text is inline or not
#'
#' @export
bulma_icon_text <- function(...,
                            color = c("primary", "link", "info",
                                      "success", "warning", "danger",
                                      "white", "black", "light", "dark",
                                      "black-bis", "black-ter", "grey-darker",
                                      "grey-dark", "grey-light", "grey-lighter",
                                      "white-ter", "white-bis"),
                            inline = TRUE) {

  assert_flag(inline)

  (if (inline) tags$span else tags$div)(
    class = "icon-text",
    ...
  ) %>%
    bulma_text_color(color) %>%
    add_class("bulma_icon_text")

}
tjpalanca/bulma.R documentation built on Dec. 23, 2021, 10:58 a.m.