R/contrast.R

Defines functions a11y_highContrastButton

Documented in a11y_highContrastButton

#' Accessible high-contrast toggle button
#'
#' A toggle button that switches high-contrast mode by adding/removing the CSS
#' class `.high-contrast` on the `<body>` element, with ARIA attributes
#' according to BITV 2.0.
#'
#' - Uses [a11y_actionButton()] internally (visible label **or** `aria-label`).
#' - Sets `aria-pressed` and toggles this state via JS.
#' - Adds the class `.a11y-high-contrast-toggle` to which the JS is attached.
#'
#' @param inputId Button ID (default: `"toggle_contrast"`)
#' @param label Visible label (optional, e.g. `"High Contrast"`)
#' @param icon Optional icon, e.g. `shiny::icon("adjust")`
#' @param aria_label ARIA label (required if `label` is missing/empty)
#' @param ... Additional arguments for [a11y_actionButton()]
#'
#' @return HTML tag of the button component
#'
#' @examples
#' # Default high-contrast toggle button
#' a11y_highContrastButton()
#'
#' # Custom label in English
#' a11y_highContrastButton(
#'   label      = "High Contrast",
#'   aria_label = "Toggle high-contrast mode"
#' )
#'
#' # Icon-only toggle (no visible label)
#' a11y_highContrastButton(
#'   label      = NULL,
#'   icon       = shiny::icon("adjust"),
#'   aria_label = "Toggle high-contrast mode"
#' )
#'
#' @export
a11y_highContrastButton <- function(
  inputId = "toggle_contrast",
  label = "Contrast Mode",
  icon = shiny::icon("adjust"),
  aria_label = "Toggle high-contrast mode on or off",
  ...
) {
  # Base: a11y_actionButton
  btn <- a11y_actionButton(
    inputId    = inputId,
    label      = label,
    icon       = icon,
    aria_label = aria_label,
    ...
  )

  # Add aria-pressed
  btn <- add_aria(btn, pressed = "false")

  # Add class for JS
  btn <- htmltools::tagAppendAttributes(
    btn,
    class = "a11y-high-contrast-toggle"
  )

  # Attach JS/CSS dependency
  htmltools::attachDependencies(btn, list(use_a11y()))
}

Try the a11yShiny package in your browser

Any scripts or data that you put into this service are public.

a11yShiny documentation built on April 1, 2026, 5:07 p.m.