Toggle
A button that stays pressed until it is pressed again, such as Bold in a text editor. It renders a native button with aria-pressed. Keep its label the same in both states: a Mute toggle stays Mute while pressed, and screen readers announce it as pressed.
- Size
- 1.0 kB
Example
import { Toggle } from "menhir/toggle";
export default function ToggleExample() {
return (
<Toggle.Root
aria-label="Bold"
class="size-9 cursor-pointer rounded-md border border-stone-300 font-bold focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500 data-pressed:border-transparent data-pressed:bg-stone-800 data-pressed:text-white dark:border-stone-700 dark:data-pressed:bg-stone-300 dark:data-pressed:text-stone-900"
>
B
</Toggle.Root>
);
}
Anatomy
<Toggle.Root />API reference
Root
<button type="button" aria-pressed>. Name it with its content, or with aria-label when it shows only an icon. Its render function must render a <button>.
| Prop | Type | Default | Description |
|---|---|---|---|
pressed | boolean | — | Whether the toggle is pressed, when controlled. Pair it with onPressedChange. |
defaultPressed | boolean | false | Whether the toggle starts pressed, when uncontrolled. |
onPressedChange | (pressed: boolean) => void | — | Called with the new value when the user presses the toggle. A controlled toggle changes only if pressed does. |
disabled | boolean | false | Whether the toggle ignores the user. It cannot be focused either. |
render | (props) => JSX.Element | — | Renders the toggle with your own button component. Read once. |
Keyboard
| Keys | Action |
|---|---|
| Space | Presses or releases the toggle. |
| Enter | Presses or releases the toggle. |
| Tab | Moves focus to or away from the toggle. |