Toggle

1 parts

A two-state button that can be pressed on or off, like a bold or italic control in a text editor toolbar. Renders a native <button> by default (handling Space/Enter and the disabled attribute for you), exposes aria-pressed, data-state (on/off), and data-disabled for styling, and works uncontrolled (defaultPressed) or controlled via v-model:pressed. When rendered as a non-button element it synthesizes keyboard activation and the appropriate tabindex/aria-disabled. Provide name to mirror the pressed state into a hidden checkbox so it participates in native form submission (suppressed automatically when nested in a ToggleGroup, which owns the submitted value). Use it for a single standalone toggle; for a set of mutually related toggles use ToggleGroup instead.

Demo

Loading demo…

Anatomy

Import the parts and compose them. Each part forwards attributes to its underlying element.

vue
import {
  Toggle,
} from '@robonen/primitives/forms/toggle';

<Toggle />

API Reference

Toggle

Root

A two-state button that can be pressed on or off, like a bold or italic control in a text editor toolbar. Renders a native `<button>` by default (handling Space/Enter and the `disabled` attribute for you), exposes `aria-pressed`, `data-state` (`on`/`off`), and `data-disabled` for styling, and works uncontrolled (`defaultPressed`) or controlled via `v-model:pressed`. When rendered as a non-button element it synthesizes keyboard activation and the appropriate `tabindex`/`aria-disabled`. Provide `name` to mirror the pressed state into a hidden checkbox so it participates in native form submission (suppressed automatically when nested in a `ToggleGroup`, which owns the submitted value). Use it for a single standalone toggle; for a set of mutually related toggles use `ToggleGroup` instead.

PropTypeDescription
defaultPressed?booleanUncontrolled initial pressed state.
disabled?booleanDisables the toggle.
name?stringName for the hidden form input. When provided (and the toggle lives inside a <form> and is not nested in a ToggleGroup), a hidden checkbox mirrors the pressed state so it is submitted with the owning form.
required?booleanMarks the control as required for form submission (native validation).
value?stringValue submitted by the hidden form input when pressed. Defaults to 'on'.
pressed?booleanTwo-way bound value (v-model:pressed).
Emits
EventPayload
update:pressed[value: boolean]