R/bulma-panel.R

Defines functions bulma_panel_tabs_item bulma_panel_tabs bulma_panel_icon bulma_panel_block bulma_panel_heading bulma_panel

Documented in bulma_panel bulma_panel_block bulma_panel_heading bulma_panel_icon bulma_panel_tabs bulma_panel_tabs_item

#' @title
#' Bulma Panel
#'
#' @description
#' A composable panel, for compact controls.
#'
#' The `bulma_panel()` contains:
#' * `bulma_panel_heading()` for the first child
#' * `bulma_panel_tabs()` for navigation
#' * `bulma_panel_block()` which can contain other elements
#'
#' [Panel](https://bulma.io/documentation/components/panel/)
#'
#' @param ... content
#'
#' @family Bulma Components
#' @name bulma_panel
NULL

#' @describeIn bulma_panel main container
#'
#' @param color (string) styling
#' @param tag   (fn) tag container
#'
#' @export
bulma_panel <- function(...,
                        color = c("primary", "link", "info",
                                  "success", "warning", "danger",
                                  "white", "black", "light", "dark"),
                        tag = tags$nav) {

  assert_function(tag)
  color <- match_arg(color)

  tag(class = "panel", ...) %>%
    bulma_color(color) %>%
    add_class("bulma_panel")

}

#' @describeIn bulma_panel
#' heading; first child of the panel
#'
#' @export
bulma_panel_heading <- function(..., tag = tags$p) {

  assert_function(tag)

  tag(class = "panel-heading", ...) %>%
    add_class("bulma_panel_heading")

}

#' @describeIn bulma_panel
#' block; contains other elements, including `bulma_control()`, `bulma_input()`,
#' `bulma_button()`, and `bulma_panel_icon()`
#' @param active (flg) whether this is block is a flag
#' @export
bulma_panel_block <- function(..., active = FALSE, tag = tags$div) {

  assert_function(tag)
  walk(tagList(...),
       assert_multi_class,
       c("bulma_form_control", "bulma_input", "bulma_button",
         "bulma_panel_icon", "bulma_form_checkbox",
         "character"))

  tag(class = "panel-block", ...) %>%
    when(active, bulma_is(., "active")) %>%
    add_class("bulma_panel_block")

}

#' @describeIn bulma_panel
#' icon element that appears to the left of each panel-block
#'
#' @param icon (string) icon declaration
#'
#' @export
bulma_panel_icon <- function(icon, tag = tags$span) {

  assert_function(tag)
  assert_string(icon)

  tag(
    class = "panel-icon",
    tags$i(class = icon)
  ) %>%
    add_class("bulma_panel_icon")

}

#' @describeIn bulma_panel
#' tabs for navigation inside the panel; contains `bulma_panel_tabs_item()`s.
#'
#' @export
bulma_panel_tabs <- function(..., tag = tags$p) {

  assert_function(tag)
  walk(tagList(...),
       assert_multi_class,
       "bulma_panel_tabs_item")

  tag(class = "panel-tabs", ...) %>%
    add_class("bulma_panel_tabs")

}

#' @describeIn bulma_panel
#' tab item for inside the `bulma_panel_tabs()`.
#'
#' @param active (flag) whether this one is shown to be selected
#'
#' @export
bulma_panel_tabs_item <- function(..., active = FALSE, tag = tags$a) {

  assert_function(tag)

  tag(class = "panel-tabs-item", ...) %>%
    when(active, bulma_is(., "active")) %>%
    add_class("bulma_panel_tabs_item")

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