Skip to content

Type to search the docs, API, and blog.

↑↓ navigate↵ openesc close
  • Zazz 0.14 alpha

    API

    Toggle

    A pressed or unpressed button over a checkbox or radio.

    Installation

    Source
    pnpm dlx zazz-ui add toggle
    npx zazz-ui add toggle
    yarn dlx zazz-ui add toggle
    bunx zazz-ui add toggle

    Also adds fields, kbd, button, which it depends on. Run zazz-ui init first if the project has no zazz/ folder yet.

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- Paint the right theme (background, form controls, scrollbars) before CSS loads -->
    <meta name="color-scheme" content="light dark">
    <!-- Zazz styles: base layers, then fields, kbd, button, toggle in cascade order -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_layers.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_variables.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_shadows.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_breakpoints.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_reset.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_typography.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_view-transitions.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/primitives/fields/fields.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/primitives/kbd/kbd.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/primitives/button/button.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/primitives/toggle/toggle.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-sm.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-md.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-lg.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-xl.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-2xl.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-disabled.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-active.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-focus-visible.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-focus-within.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-hover.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-checked.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-current.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-open.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-stuck.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_properties-starting.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-sm.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-md.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-lg.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-xl.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-2xl.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-disabled.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-active.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-focus-visible.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-focus-within.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-hover.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-checked.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-current.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-open.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-stuck.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-tier-starting.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-flow.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-grid.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-spacing.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-margin.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-sizing.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-typography.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-color.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-effects.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-box.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-pseudo.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_defaults.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/_utilities-sr.css">
    <!-- Import map: bare specifiers in Zazz scripts resolve to pinned, SRI-checked files -->
    <script type="importmap">
    {
      "imports": {
        "signal-polyfill": "https://cdn.jsdelivr.net/npm/signal-polyfill@0.2.2/dist/index.js"
      },
      "integrity": {
        "https://cdn.jsdelivr.net/npm/signal-polyfill@0.2.2/dist/index.js": "sha384-0OqM7OXGT6Oz1z98u/geiFn/FUyz4iWexkXHCvbIGXRRDXl6bp6IKc0j78AQ6DEK"
      }
    }
    </script>
    <!-- Polyfill: Interest Invokers (interestfor) — native in Chromium only -->
    <script type="module" src="https://cdn.jsdelivr.net/npm/invokers@2.2.2/dist/esm/production/interest.js" integrity="sha384-hR2BVNtsS7fIIMwOm+f7MY0JZfuByGhQQfevCBB6evFATKzBsTw0JzH/RxD94z2T" crossorigin="anonymous"></script>
    <!-- Zazz behavior: side-effect modules by tag; the rest via module imports -->
    <script type="module" src="https://cdn.jsdelivr.net/npm/@zazz-ui/core@0.14.2/src/base/dialog-lifecycle.js"></script>
    <!-- Theme: apply the persisted (or preferred) scheme before first paint -->
    <script>
      (() => {
        try {
          // A stored choice pins the scheme with data-theme; with no choice the
          // page follows the system scheme.
          const storedTheme = localStorage.getItem("theme");
          if (storedTheme === "dark" || storedTheme === "light") {
            document.documentElement.dataset.theme = storedTheme;
          }
        } catch {
          /* no storage access: follow the system scheme */
        }
      })();
    </script>

    A <head> that loads the base layers and toggle with its dependencies from jsDelivr, pinned to 0.14.2. To load the whole kit in two requests, see the CDN install.

    Examples

    <div
      style="
        --display: flex-col;
        --items: start;
        --gap: 6;
      "
    >
      <div
        style="
          --display: flex;
          --items: center;
          --gap: 4;
        "
      >
        <label data-ui="toggle" data-toggle-size="icon">
          <input type="checkbox" name="bold" aria-label="Bold" />
          <svg
            xmlns="http://www.w3.org/2000/svg"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
            aria-hidden="true"
          >
            <path d="M6 4h8a4 4 0 0 1 0 8H6z" />
            <path d="M6 12h9a4 4 0 0 1 0 8H6z" />
          </svg>
        </label>
        <label data-ui="toggle" data-toggle-size="icon">
          <input type="checkbox" name="italic" aria-label="Italic" checked />
          <svg
            xmlns="http://www.w3.org/2000/svg"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
            aria-hidden="true"
          >
            <line x1="19" y1="4" x2="10" y2="4" />
            <line x1="14" y1="20" x2="5" y2="20" />
            <line x1="15" y1="4" x2="9" y2="20" />
          </svg>
        </label>
        <label data-ui="toggle" data-toggle-size="icon" data-toggle-variant="ghost">
          <input type="checkbox" name="underline" aria-label="Underline" />
          <svg
            xmlns="http://www.w3.org/2000/svg"
            viewBox="0 0 24 24"
            fill="none"
            stroke="currentColor"
            stroke-width="2"
            stroke-linecap="round"
            stroke-linejoin="round"
            aria-hidden="true"
          >
            <path d="M6 4v6a6 6 0 0 0 12 0V4" />
            <line x1="4" y1="20" x2="20" y2="20" />
          </svg>
        </label>
      </div>
    
      <div
        style="
          --display: flex;
          --items: center;
          --gap: 4;
        "
      >
        <label data-ui="toggle">
          <input type="checkbox" name="bold" />
          <span>Bold</span>
        </label>
        <label data-ui="toggle">
          <input type="checkbox" name="italic" checked />
          <span>Italic</span>
        </label>
        <label data-ui="toggle" data-toggle-variant="ghost">
          <input type="checkbox" name="disabled" disabled />
          <span>Disabled</span>
        </label>
      </div>
    </div>
    /**
     * toggle.css: Toggle ([data-ui~="toggle"]), a button-styled checkbox/radio
     *
     * Same look as the button (hooks default to the --ui-button-* family), but it wraps a
     * native checkbox or radio inside a <label>. State is driven from the nested input
     * via :has(): the checked state takes the primary role, so an "on" toggle reads
     * like a primary button. Because the state lives on the input,
     * the label's own state tiers (`--bg--checked`) do not fire; theme the checked
     * look through the `--ui-toggle-*--checked` hooks.
     *
     * @layer      variables, components
     * @requires   _layers.css, _variables.css, _properties.css (--_*-resolved, --_focus-shadow), button.css, kbd.css (--ui-kbd-*)
     * @uses       :has(): drive label state from the nested input (checked/focus/disabled)
     * @uses       color-mix(), oklch(from …): variant hover/active tints, the same steps
     *             as the button's variants
     * @uses       text-box: vertical trim (patchy browser support)
     * @uses       corner-shape — superellipse corners; unsupported browsers keep plain
     *             rounded corners
     * @uses       --_touch-min (touch screens) — the default and icon sizes grow to the touch
     *             floor, and each inline side gains half of what the height grew; sm and
     *             icon-sm keep their box and extend an invisible ::after hit area
     * @tokens     --ui-toggle-* (@layer variables;
     *             default to the --ui-button-* family)
     * @consumedby toggle-group.css
     */
    
    @layer variables {
      :root {
        --ui-toggle-ring-color: var(--ui-button-ring-color);
    
        --ui-toggle-font-family: var(--ui-button-font-family);
        --ui-toggle-font-size: var(--ui-button-font-size);
        --ui-toggle-font-weight: var(--ui-button-font-weight);
        --ui-toggle-leading: var(--ui-button-leading);
    
        --ui-toggle-bg: var(--ui-button-bg);
        --ui-toggle-bg--hover: var(--ui-button-bg--hover);
        --ui-toggle-bg--active: var(--ui-button-bg--active);
    
        --ui-toggle-bg--checked: var(--color-primary);
        --ui-toggle-bg--checked-hover: oklch(from var(--color-primary) l c h / 0.9);
        --ui-toggle-bg--checked-active: oklch(from var(--color-primary) l c h / 0.8);
    
        --ui-toggle-text: var(--ui-button-text);
        --ui-toggle-text--hover: var(--ui-button-text--hover);
        --ui-toggle-text--active: var(--ui-button-text--active);
    
        --ui-toggle-text--checked: var(--color-primary-foreground);
        --ui-toggle-text--checked-hover: var(--color-primary-foreground);
        --ui-toggle-text--checked-active: var(--color-primary-foreground);
    
        --ui-toggle-border-width: var(--ui-button-border-width);
        --ui-toggle-border-style: var(--ui-button-border-style);
        --ui-toggle-border-color: var(--ui-button-border-color);
        --ui-toggle-border-color--hover: var(--ui-button-border-color--hover);
        --ui-toggle-border-color--active: var(--ui-button-border-color--active);
    
        --ui-toggle-border-color--checked: var(--color-border);
        --ui-toggle-border-color--checked-hover: var(--color-border);
        --ui-toggle-border-color--checked-active: var(--color-border);
    
        /* dual-mode hooks: a scale number or a length */
        --ui-toggle-gap: var(--ui-button-gap);
        --ui-toggle-py: var(--ui-button-py);
        --ui-toggle-px: var(--ui-button-px);
        --ui-toggle-min-h: var(--ui-button-min-h);
        --ui-toggle-radius: var(--ui-button-radius);
        --ui-toggle-corner-shape: var(--ui-button-corner-shape);
        --ui-toggle-background-clip: border-box;
    
        --ui-toggle-icon-size: var(--ui-button-icon-size);
    
        /* composes into the toggle's box-shadow list: use a transparent shadow,
           never `none`, so the focus ring layers stay valid */
        --ui-toggle-shadow: var(--ui-button-shadow);
      }
    }
    
    /* variant privates: one per hook, non-inheriting */
    @property --_toggle-ring-color {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-font-family {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-font-size {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-font-weight {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-leading {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-bg {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-bg--hover {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-bg--active {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-bg--checked {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-bg--checked-hover {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-bg--checked-active {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-text {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-text--hover {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-text--active {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-text--checked {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-text--checked-hover {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-text--checked-active {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-border-width {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-border-style {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-border-color {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-border-color--hover {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-border-color--active {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-border-color--checked {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-border-color--checked-hover {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-border-color--checked-active {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-gap {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-py {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-px {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-min-h {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-radius {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-corner-shape {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-background-clip {
      syntax: "*";
      inherits: false;
    }
    
    @property --_toggle-shadow {
      syntax: "*";
      inherits: false;
    }
    
    @layer ui.components {
      /* The label wraps a visually-hidden checkbox/radio that stays focusable, so
         :focus-visible on the input still drives the ring. */
      :where([data-ui~="toggle"]) {
        font-family: var(
          --_font-family-resolved,
          var(--_toggle-font-family, var(--ui-toggle-font-family))
        );
        font-size: var(--_font-size-resolved, var(--_toggle-font-size, var(--ui-toggle-font-size)));
        font-weight: var(
          --_font-weight-resolved,
          var(--_toggle-font-weight, var(--ui-toggle-font-weight))
        );
        line-height: var(--_leading-resolved, var(--_toggle-leading, var(--ui-toggle-leading)));
    
        position: relative;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-shrink: 0;
    
        /* dual-mode metrics go through the registered typed pair */
        --_gap-len: var(--_gap-resolved, var(--_toggle-gap, var(--ui-toggle-gap)));
        --_gap-num: var(--_gap-resolved, var(--_toggle-gap, var(--ui-toggle-gap)));
        column-gap: calc(var(--_gap-len) + var(--_gap-num) * var(--spacing));
    
        --_min-h-len: var(--_min-h-resolved, var(--_toggle-min-h, var(--ui-toggle-min-h)));
        --_min-h-num: var(--_min-h-resolved, var(--_toggle-min-h, var(--ui-toggle-min-h)));
        /* the touch floor: 0 on a mouse; a size that must stay small sets it to 0px */
        --_touch-floor: var(--_touch-min);
        min-block-size: max(
          calc(var(--_min-h-len) + var(--_min-h-num) * var(--spacing)),
          var(--_touch-floor)
        );
        min-inline-size: max-content;
    
        --_py-len: var(--_py-resolved, var(--_toggle-py, var(--ui-toggle-py)));
        --_py-num: var(--_py-resolved, var(--_toggle-py, var(--ui-toggle-py)));
        padding-block: calc(var(--_py-len) + var(--_py-num) * var(--spacing));
    
        --_px-len: var(--_px-resolved, var(--_toggle-px, var(--ui-toggle-px)));
        --_px-num: var(--_px-resolved, var(--_toggle-px, var(--ui-toggle-px)));
        /* each inline side gains half of what the touch floor added to the height, so the
           label's margins grow evenly; the kbd ends below add it too */
        --_touch-px: max(
          0px,
          (var(--_touch-floor) - calc(var(--_min-h-len) + var(--_min-h-num) * var(--spacing))) / 2
        );
        padding-inline: calc(var(--_px-len) + var(--_px-num) * var(--spacing) + var(--_touch-px));
    
        border-width: var(
          --_border-width-resolved,
          var(--_toggle-border-width, var(--ui-toggle-border-width))
        );
        border-style: var(
          --_border-style-resolved,
          var(--_toggle-border-style, var(--ui-toggle-border-style))
        );
        border-color: var(
          --_border-color-resolved,
          var(--_toggle-border-color, var(--ui-toggle-border-color))
        );
        --_radius-len: var(--_radius-resolved, var(--_toggle-radius, var(--ui-toggle-radius)));
        --_radius-num: var(--_radius-resolved, var(--_toggle-radius, var(--ui-toggle-radius)));
        border-radius: calc(
          var(--_radius-len) + var(--_radius-num) * var(--spacing) * var(--radius-multiplier)
        );
        corner-shape: var(
          --_corner-shape-resolved,
          var(--_toggle-corner-shape, var(--ui-toggle-corner-shape))
        );
    
        color: var(--_text-resolved, var(--_toggle-text, var(--ui-toggle-text)));
        background-color: var(--_bg-resolved, var(--_toggle-bg, var(--ui-toggle-bg)));
        background-clip: var(
          --_bg-clip-resolved,
          var(--_toggle-background-clip, var(--ui-toggle-background-clip))
        );
        white-space-collapse: collapse;
    
        /* published to --_focus-shadow so a --shadow or --ring utility keeps it; the
           widths grow from 0 on :focus-visible so the ring eases in */
        --_ring-offset-width: 0px;
        --_ring-width: 0px;
        --_focus-in: max(0, -1 * sign(var(--_focus-ring-w, 1px)));
        --_ring-fill: color-mix(
          in oklch,
          var(--_ring-color-resolved, var(--_toggle-ring-color, var(--ui-toggle-ring-color)))
            var(--ring-opacity),
          transparent
        );
        --_focus-shadow:
          0 0 0 calc(var(--_ring-offset-width) * (1 - var(--_focus-in))) var(--ring-offset-shadow-color),
          0 0 0 calc((var(--_ring-offset-width) + var(--_ring-width)) * (1 - var(--_focus-in)))
            var(--_ring-fill, var(--ring-shadow-color)),
          inset 0 0 0 calc(var(--_ring-offset-width) * var(--_focus-in)) var(--ring-offset-shadow-color),
          inset 0 0 0 calc((var(--_ring-offset-width) + var(--_ring-width)) * var(--_focus-in))
            var(--_ring-fill, var(--ring-shadow-color));
        box-shadow:
          var(--_focus-shadow, 0 0 #0000),
          var(--_shadow-resolved, var(--_toggle-shadow, var(--ui-toggle-shadow)));
    
        cursor: default;
        text-decoration: none;
    
        /* transparent twin of the ring for forced-colors / high-contrast modes */
        outline: abs(var(--_focus-ring-w, var(--outline-width))) var(--outline-style) transparent;
        outline-offset: calc(
          var(--outline-offset) - var(--_focus-in) *
            (2 * var(--outline-offset) + abs(var(--_focus-ring-w, var(--outline-width))))
        );
    
        transition: var(--default-transition);
    
        user-select: none;
        text-box: trim-both ex alphabetic;
      }
    
      /* visually hidden but still in the tab order and focusable */
      :where([data-ui~="toggle"]) > input:is([type="checkbox"], [type="radio"]) {
        position: absolute;
        clip-path: inset(50%);
        opacity: 0;
        pointer-events: none;
      }
    
      /* ===========================================================================
      STATES — driven from the nested input
      =========================================================================== */
    
      @media (hover: hover) {
        :where([data-ui~="toggle"]):hover:not(:has(> input:disabled)) {
          color: var(--_text-resolved, var(--_toggle-text--hover, var(--ui-toggle-text--hover)));
          background-color: var(--_bg-resolved, var(--_toggle-bg--hover, var(--ui-toggle-bg--hover)));
          border-color: var(
            --_border-color-resolved,
            var(--_toggle-border-color--hover, var(--ui-toggle-border-color--hover))
          );
        }
      }
    
      :where([data-ui~="toggle"]):active:not(:has(> input:disabled)) {
        color: var(--_text-resolved, var(--_toggle-text--active, var(--ui-toggle-text--active)));
        background-color: var(--_bg-resolved, var(--_toggle-bg--active, var(--ui-toggle-bg--active)));
        border-color: var(
          --_border-color-resolved,
          var(--_toggle-border-color--active, var(--ui-toggle-border-color--active))
        );
      }
    
      /* the "on" state reuses the button's active look */
      :where([data-ui~="toggle"]):has(> input:checked) {
        color: var(--_text-resolved, var(--_toggle-text--checked, var(--ui-toggle-text--checked)));
        background-color: var(--_bg-resolved, var(--_toggle-bg--checked, var(--ui-toggle-bg--checked)));
        border-color: var(
          --_border-color-resolved,
          var(--_toggle-border-color--checked, var(--ui-toggle-border-color--checked))
        );
      }
    
      @media (hover: hover) {
        :where([data-ui~="toggle"]):has(> input:checked):hover:not(:has(> input:disabled)) {
          color: var(
            --_text-resolved,
            var(--_toggle-text--checked-hover, var(--ui-toggle-text--checked-hover))
          );
          background-color: var(
            --_bg-resolved,
            var(--_toggle-bg--checked-hover, var(--ui-toggle-bg--checked-hover))
          );
          border-color: var(
            --_border-color-resolved,
            var(--_toggle-border-color--checked-hover, var(--ui-toggle-border-color--checked-hover))
          );
        }
      }
    
      :where([data-ui~="toggle"]):has(> input:checked):active:not(:has(> input:disabled)) {
        color: var(
          --_text-resolved,
          var(--_toggle-text--checked-active, var(--ui-toggle-text--checked-active))
        );
        background-color: var(
          --_bg-resolved,
          var(--_toggle-bg--checked-active, var(--ui-toggle-bg--checked-active))
        );
        border-color: var(
          --_border-color-resolved,
          var(--_toggle-border-color--checked-active, var(--ui-toggle-border-color--checked-active))
        );
      }
    
      :where([data-ui~="toggle"]):has(> input:focus-visible) {
        --_ring-offset-width: var(--ring-offset-width);
        --_ring-width: abs(var(--_focus-ring-w, var(--ring-width)));
    
        outline-color: transparent;
      }
    
      /* before the variants at the same specificity: a disabled primary keeps its color; only the default surface goes muted */
      :where([data-ui~="toggle"]:has(> input:disabled)) {
        --_toggle-text: var(--color-muted-foreground);
        --_toggle-bg: var(--color-muted);
        --_toggle-shadow: 0 0 transparent;
        --decoration-line: none;
    
        cursor: default;
        opacity: 0.6;
        outline: none;
      }
    
      /* ===========================================================================
      ICONS
      =========================================================================== */
    
      :where([data-ui~="toggle"]) > svg {
        inline-size: var(--ui-toggle-icon-size);
        block-size: var(--ui-toggle-icon-size);
        pointer-events: none;
        justify-content: center;
        align-items: center;
        display: flex;
        padding: 0;
      }
    
      /* ===========================================================================
      VARIANTS
      =========================================================================== */
    
      :where([data-ui~="toggle"][data-toggle-variant="primary"]) {
        --_toggle-bg: var(--color-primary);
        --_toggle-bg--hover: light-dark(
          oklch(from var(--color-primary) calc(l + (1 - l) * 0.2) c h),
          oklch(from var(--color-primary) calc(l * 0.8) c h)
        );
        --_toggle-bg--active: light-dark(
          oklch(from var(--color-primary) calc(l + (1 - l) * 0.3) c h),
          oklch(from var(--color-primary) calc(l * 0.7) c h)
        );
    
        --_toggle-text: var(--color-primary-foreground);
        --_toggle-text--hover: var(--color-primary-foreground);
        --_toggle-text--active: var(--color-primary-foreground);
    
        --_toggle-border-color: transparent;
        --_toggle-border-color--hover: transparent;
        --_toggle-border-color--active: transparent;
    
        --_toggle-shadow: 0 0 transparent;
      }
    
      :where([data-ui~="toggle"][data-toggle-variant="secondary"]) {
        --_toggle-bg: var(--color-secondary);
        --_toggle-bg--hover: light-dark(
          oklch(from var(--color-secondary) calc(l + (1 - l) * 0.2) c h),
          oklch(from var(--color-secondary) calc(l * 0.8) c h)
        );
        --_toggle-bg--active: light-dark(
          oklch(from var(--color-secondary) calc(l + (1 - l) * 0.3) c h),
          oklch(from var(--color-secondary) calc(l * 0.7) c h)
        );
    
        --_toggle-text: var(--color-secondary-foreground);
        --_toggle-text--hover: var(--color-secondary-foreground);
        --_toggle-text--active: var(--color-secondary-foreground);
    
        --_toggle-border-color: transparent;
        --_toggle-border-color--hover: transparent;
        --_toggle-border-color--active: transparent;
    
        --_toggle-shadow: 0 0 transparent;
      }
    
      :where([data-ui~="toggle"][data-toggle-variant="tertiary"]) {
        --_toggle-bg: var(--color-tertiary);
        --_toggle-bg--hover: light-dark(
          oklch(from var(--color-tertiary) calc(l + (1 - l) * 0.2) c h),
          oklch(from var(--color-tertiary) calc(l * 0.8) c h)
        );
        --_toggle-bg--active: light-dark(
          oklch(from var(--color-tertiary) calc(l + (1 - l) * 0.3) c h),
          oklch(from var(--color-tertiary) calc(l * 0.7) c h)
        );
    
        --_toggle-text: var(--color-tertiary-foreground);
        --_toggle-text--hover: var(--color-tertiary-foreground);
        --_toggle-text--active: var(--color-tertiary-foreground);
    
        --_toggle-border-color: transparent;
        --_toggle-border-color--hover: transparent;
        --_toggle-border-color--active: transparent;
    
        --_toggle-shadow: 0 0 transparent;
      }
    
      :where([data-ui~="toggle"][data-toggle-variant="muted"]) {
        /* the translucent muted role: fades toward the surface beneath */
        --_toggle-bg: var(--color-muted);
        --_toggle-bg--hover: oklch(from var(--_toggle-bg) l c h / calc(alpha * 0.66));
        --_toggle-bg--active: oklch(from var(--_toggle-bg) l c h / calc(alpha * 0.33));
    
        --_toggle-text: var(--color-foreground);
        --_toggle-text--hover: var(--color-foreground);
        --_toggle-text--active: var(--color-foreground);
    
        --_toggle-border-color: transparent;
        --_toggle-border-color--hover: transparent;
        --_toggle-border-color--active: transparent;
    
        --_toggle-shadow: 0 0 transparent;
      }
    
      /* the "ghost" toggle reads as bare until on/hovered */
      :where([data-ui~="toggle"][data-toggle-variant="ghost"]) {
        --_toggle-bg: transparent;
        /* the translucent muted role: shows the surface beneath and follows a retheme */
        --_toggle-bg--hover: var(--color-muted);
        --_toggle-bg--active: oklch(from var(--_toggle-bg--hover) l c h / calc(alpha * 1.5));
    
        --_toggle-text: var(--color-foreground);
        --_toggle-text--hover: var(--color-foreground);
        --_toggle-text--active: var(--color-foreground);
    
        --_toggle-border-color: transparent;
        --_toggle-border-color--hover: transparent;
        --_toggle-border-color--active: transparent;
    
        --_toggle-shadow: 0 0 transparent;
      }
    
      :where([data-ui~="toggle"][data-toggle-variant="destructive"]) {
        --_toggle-ring-color: var(--color-destructive-foreground);
    
        --_toggle-bg: var(--color-destructive);
        --_toggle-bg--hover: oklch(from var(--_toggle-bg) calc(l - 0.025) c h);
        --_toggle-bg--active: oklch(from var(--_toggle-bg) calc(l - 0.05) c h);
    
        --_toggle-text: var(--color-destructive-foreground);
        --_toggle-text--hover: var(--_toggle-text);
        --_toggle-text--active: var(--_toggle-text);
    
        --_toggle-border-color: transparent;
        --_toggle-border-color--hover: transparent;
        --_toggle-border-color--active: transparent;
    
        --_toggle-shadow: 0 0 transparent;
      }
    
      /* ===========================================================================
      SIZES
      =========================================================================== */
    
      :where([data-ui~="toggle"][data-toggle-size="sm"]) {
        --_toggle-min-h: calc(var(--spacing) * 6);
        /* a 16px icon, this, and the border fit the 24px box */
        --_toggle-py: calc(var(--spacing) * 0.75);
        --_toggle-px: calc(var(--spacing) * 2);
        --_toggle-radius: var(--radius-sm);
        --_toggle-font-size: var(--font-size-xs);
        /* dense by choice: the box stays small and ::after takes the touch floor */
        --_touch-floor: 0px;
      }
    
      :where([data-ui~="toggle"][data-toggle-size="icon"]) {
        block-size: max(
          calc(var(--_min-h-len) + var(--_min-h-num) * var(--spacing)),
          var(--_touch-floor)
        );
        inline-size: max(
          calc(var(--_min-h-len) + var(--_min-h-num) * var(--spacing)),
          var(--_touch-floor)
        );
        padding: 0;
        aspect-ratio: 1 / 1;
      }
    
      :where([data-ui~="toggle"][data-toggle-size="icon-sm"]) {
        --_toggle-min-h: calc(var(--spacing) * 6);
        --_toggle-px: calc(var(--spacing) * 2);
        --_toggle-radius: var(--radius-sm);
        /* the hook, not a private: it must inherit to reach the icon */
        --ui-toggle-icon-size: calc(var(--spacing) * 3.5);
        --_touch-floor: 0px;
    
        block-size: calc(var(--_min-h-len) + var(--_min-h-num) * var(--spacing));
        inline-size: calc(var(--_min-h-len) + var(--_min-h-num) * var(--spacing));
        padding: 0;
        aspect-ratio: 1 / 1;
      }
    
      /* touch: sm and icon-sm keep their box; a transparent ::after centred on the toggle
         (already position: relative) is at least --_touch-min square */
      :where([data-ui~="toggle"]:is([data-toggle-size="sm"], [data-toggle-size="icon-sm"]))::after {
        content: "";
        position: absolute;
        inset: min(0px, calc((100% - var(--_touch-min)) / 2));
      }
    
      /* ===========================================================================
      KBD
      =========================================================================== */
    
      /* Descendant (not child) so keys wrapped in a ui-kbd-group still resize */
      :where([data-ui~="toggle"]) :where(kbd) {
        --ui-kbd-min-w: calc(var(--spacing) * 4.5);
        --ui-kbd-min-h: calc(var(--spacing) * 4.5);
        --ui-kbd-icon-size: calc(var(--spacing) * 3);
      }
    
      /* the button's kbd ends, counted without the hidden input: a kbd at either end
         of the visible content, but not alone, tightens that side */
      :where([data-ui~="toggle"]):has(
        > :where(kbd, ui-kbd-group, [data-ui~="kbd-group"]):nth-last-child(1 of :not(input)):not(
            :nth-child(1 of :not(input))
          )
      ) {
        padding-inline-end: calc(var(--spacing) * 1.5 + var(--_touch-px));
      }
    
      :where([data-ui~="toggle"]):has(
        > :where(kbd, ui-kbd-group, [data-ui~="kbd-group"]):nth-child(1 of :not(input)):not(
            :nth-last-child(1 of :not(input))
          )
      ) {
        padding-inline-start: calc(var(--spacing) * 1.5 + var(--_touch-px));
      }
    
      :where([data-ui~="toggle"][data-toggle-size="sm"]):has(
        > :where(kbd, ui-kbd-group, [data-ui~="kbd-group"]):nth-last-child(1 of :not(input)):not(
            :nth-child(1 of :not(input))
          )
      ) {
        padding-inline-end: calc(var(--spacing) * 0.5 + var(--_touch-px));
      }
    
      :where([data-ui~="toggle"][data-toggle-size="sm"]):has(
        > :where(kbd, ui-kbd-group, [data-ui~="kbd-group"]):nth-child(1 of :not(input)):not(
            :nth-last-child(1 of :not(input))
          )
      ) {
        padding-inline-start: calc(var(--spacing) * 0.5 + var(--_touch-px));
      }
    }

    Attributes

    toggle attributes
    AttributeKindValuesNotes
    data-ui="toggle"Identityfree valueIdentity token in data-ui.
    data-toggle-sizePreseticon icon-sm smPreset: a named bundle of values.
    data-toggle-variantPresetdestructive ghost muted primary secondary tertiaryPreset: a named bundle of values.

    Requires: button.

    Hooks

    Set a hook on :root, on a subtree, or inline on one element. A state hook (--…--hover) themes that state only; the primitive keeps its other states.

    toggle hooks
    HookDefaultState
    --ui-toggle-background-clipborder-box—
    --ui-toggle-bgvar(--ui-button-bg)—
    --ui-toggle-bg--activevar(--ui-button-bg--active)active
    --ui-toggle-bg--checkedvar(--color-primary)checked
    --ui-toggle-bg--checked-activeoklch(from var(--color-primary) l c h / 0.8)checked-active
    --ui-toggle-bg--checked-hoveroklch(from var(--color-primary) l c h / 0.9)checked-hover
    --ui-toggle-bg--hovervar(--ui-button-bg--hover)hover
    --ui-toggle-border-colorvar(--ui-button-border-color)—
    --ui-toggle-border-color--activevar(--ui-button-border-color--active)active
    --ui-toggle-border-color--checkedvar(--color-border)checked
    --ui-toggle-border-color--checked-activevar(--color-border)checked-active
    --ui-toggle-border-color--checked-hovervar(--color-border)checked-hover
    --ui-toggle-border-color--hovervar(--ui-button-border-color--hover)hover
    --ui-toggle-border-stylevar(--ui-button-border-style)—
    --ui-toggle-border-widthvar(--ui-button-border-width)—
    --ui-toggle-corner-shapevar(--ui-button-corner-shape)—
    --ui-toggle-font-familyvar(--ui-button-font-family)—
    --ui-toggle-font-sizevar(--ui-button-font-size)—
    --ui-toggle-font-weightvar(--ui-button-font-weight)—
    --ui-toggle-gapvar(--ui-button-gap)—
    --ui-toggle-icon-sizevar(--ui-button-icon-size)—
    --ui-toggle-leadingvar(--ui-button-leading)—
    --ui-toggle-min-hvar(--ui-button-min-h)—
    --ui-toggle-pxvar(--ui-button-px)—
    --ui-toggle-pyvar(--ui-button-py)—
    --ui-toggle-radiusvar(--ui-button-radius)—
    --ui-toggle-ring-colorvar(--ui-button-ring-color)—
    --ui-toggle-shadowvar(--ui-button-shadow)—
    --ui-toggle-textvar(--ui-button-text)—
    --ui-toggle-text--activevar(--ui-button-text--active)active
    --ui-toggle-text--checkedvar(--color-primary-foreground)checked
    --ui-toggle-text--checked-activevar(--color-primary-foreground)checked-active
    --ui-toggle-text--checked-hovervar(--color-primary-foreground)checked-hover
    --ui-toggle-text--hovervar(--ui-button-text--hover)hover

    API