Toggle
A button that stays pressed or unpressed, for a mode that applies the moment it is pressed.
import { Glyph } from '@slotted/react/icon';import { Toggle } from '@slotted/react/toggle';export default function ToggleBasic() { return <Toggle icon={<Glyph name="pin" />}>Pin header</Toggle>;}Installation
The packages are a 0.x beta on npm, and their public API changes between beta releases without deprecation. One line installs the binding and the default theme.
pnpm add @slotted/react @slotted/theme @slotted/themesimport { Toggle, ToggleDefaultsProvider } from '@slotted/react';A mode that is on or off
A toggle is a native button that carries aria-pressed in both states, so a screen reader announces it as a toggle. Its label never changes with its state: the pressed look and aria-pressed carry the state. A toggle is for a mode that is true the moment it is pressed, such as Pin header or Wrap lines. An action that reaches a server and waits, such as Follow, is ToggleButton from the button family. In React the mark goes in icon. In Angular the mark is an element with slToggleIcon. Both are hidden from the reading order. disabled is the native attribute: a disabled toggle leaves the tab order and keeps showing whether it is on. Hover, press and focus are drawn by the toggle itself.
import { Glyph } from '@slotted/react/icon';import { Inline } from '@slotted/react/inline';import { Toggle } from '@slotted/react/toggle';export default function ToggleStates() { return ( <Inline gap={2}> <Toggle icon={<Glyph name="pin" />}>Pin header</Toggle> <Toggle defaultPressed icon={<Glyph name="bell" />}> Notify me </Toggle> <Toggle disabled icon={<Glyph name="star" />}> Starred only </Toggle> <Toggle defaultPressed disabled icon={<Glyph name="funnel" />}> Carrier filter </Toggle> </Inline> );}Variants
variant sets how the toggle looks before it is on. outline, the default, draws its own edge. ghost is for a toggle inside something already framed, such as a toolbar or a card header. chips draws a rounded item that presses in the neutral ink, which suits a row of filters. The three variants share one geometry, so turning a toggle on changes colors and never its size. The first toggle of each row is pressed.
import { Glyph } from '@slotted/react/icon';import { Inline } from '@slotted/react/inline';import { Stack } from '@slotted/react/stack';import { Toggle } from '@slotted/react/toggle';export default function ToggleVariants() { return ( <Stack gap={3}> <Inline gap={2}> <Toggle defaultPressed icon={<Glyph name="pin" />} variant="outline"> Pin header </Toggle> <Toggle icon={<Glyph name="text-wrap" />} variant="outline"> Wrap lines </Toggle> </Inline> <Inline gap={2}> <Toggle defaultPressed icon={<Glyph name="bell" />} variant="ghost"> Notify me </Toggle> <Toggle icon={<Glyph name="star" />} variant="ghost"> Starred only </Toggle> </Inline> <Inline gap={2}> <Toggle defaultPressed variant="chips"> Late pickups </Toggle> <Toggle variant="chips">Low stock</Toggle> </Inline> </Stack> );}Sizes
size takes sm, md or lg. Each step reads the control scale, so a toggle is as tall as a text field or a button of the same step.
import { Glyph } from '@slotted/react/icon';import { Inline } from '@slotted/react/inline';import { Toggle } from '@slotted/react/toggle';export default function ToggleSizes() { return ( <Inline align="center" gap={2}> <Toggle icon={<Glyph name="text-wrap" />} size="sm"> Dense rows </Toggle> <Toggle icon={<Glyph name="text-wrap" />} size="md"> Default rows </Toggle> <Toggle icon={<Glyph name="text-wrap" />} size="lg"> Roomy rows </Toggle> </Inline> );}Icon only
A toggle with an icon and no words draws a square. The family writes no accessible name, so the application passes aria-label.
import { Glyph } from '@slotted/react/icon';import { Inline } from '@slotted/react/inline';import { Toggle } from '@slotted/react/toggle';export default function ToggleIconOnly() { return ( <Inline gap={2}> <Toggle aria-label="Pin header" icon={<Glyph name="pin" />} /> <Toggle aria-label="Starred only" defaultPressed icon={<Glyph name="star" />} /> <Toggle aria-label="Notify me" icon={<Glyph name="bell" />} /> </Inline> );}Controlled
Pass pressed to own the state. A press calls onPressedChange in React and emits pressedChange in Angular with the new value, and the toggle moves only when the application passes that value back. defaultPressed sets the starting state of a toggle that owns its state, and is read once.
import { useState } from 'react';import { Glyph } from '@slotted/react/icon';import { Inline } from '@slotted/react/inline';import { Stack } from '@slotted/react/stack';import { Toggle } from '@slotted/react/toggle';import { Text } from '@slotted/react/typography';export default function ToggleControlled() { const [wrap, setWrap] = useState(false); return ( <Stack gap={2}> <Inline> <Toggle icon={<Glyph name="text-wrap" />} onPressedChange={(pressed) => setWrap(pressed)} pressed={wrap} > Wrap lines </Toggle> </Inline> <Text size="sm" tone="muted"> {wrap ? 'Long order notes wrap onto further lines.' : 'Long order notes stay on one line.'} </Text> </Stack> );}Reading the reason
The request carries reason, which is pointer for a click and keyboard for Space or Enter. React passes the new value first and the request second: onPressedChange(pressed, request). Angular emits the request, whose value is the new state. A value equal to the current one emits nothing.
import { useState } from 'react';import { Glyph } from '@slotted/react/icon';import { Inline } from '@slotted/react/inline';import { Stack } from '@slotted/react/stack';import { Toggle } from '@slotted/react/toggle';import type { TogglePressedReason } from '@slotted/react/toggle';import { Text } from '@slotted/react/typography';export default function ToggleReason() { const [reason, setReason] = useState<TogglePressedReason | null>(null); return ( <Stack gap={2}> <Inline> <Toggle icon={<Glyph name="rotate-cw" />} onPressedChange={(_pressed, request) => setReason(request.reason)} > Auto-refresh </Toggle> </Inline> <Text size="sm" tone="muted"> {reason === null ? 'No change yet.' : `Last change: ${reason}.`} </Text> </Stack> );}Refusing a change
request.cancel() stops the move before anything commits. A toggle that owns its state stays where it was, and the application can say why. Here a locked period cannot be unlocked from this control.
import { useState } from 'react';import { Glyph } from '@slotted/react/icon';import { Inline } from '@slotted/react/inline';import { Stack } from '@slotted/react/stack';import { Toggle } from '@slotted/react/toggle';import { Text } from '@slotted/react/typography';export default function ToggleGuard() { const [notice, setNotice] = useState('Locking freezes the entries of March 2026.'); return ( <Stack gap={2}> <Inline> <Toggle icon={<Glyph name="lock" />} onPressedChange={(pressed, request) => { if (pressed) { setNotice('March 2026 is locked.'); return; } request.cancel(); setNotice('A locked period is reopened by the finance approver.'); }} > Lock period </Toggle> </Inline> <Text size="sm" tone="muted"> {notice} </Text> </Stack> );}Inside a toolbar
A toolbar is one tab stop and moves between its controls with the arrow keys. A toggle is one of those controls, and Space or Enter presses it. The toggle writes no key listener of its own. ghost fits a toolbar, which is already framed.
import { Glyph } from '@slotted/react/icon';import { Toggle } from '@slotted/react/toggle';import { Toolbar, ToolbarSeparator } from '@slotted/react/toolbar';export default function ToggleToolbar() { return ( <Toolbar aria-label="Order table view"> <Toggle defaultPressed icon={<Glyph name="pin" />} variant="ghost"> Pin header </Toggle> <Toggle icon={<Glyph name="star" />} variant="ghost"> Starred only </Toggle> <ToolbarSeparator /> <Toggle icon={<Glyph name="text-wrap" />} variant="ghost"> Wrap lines </Toggle> </Toolbar> );}Defaults
ToggleDefaultsProvider in React and provideToggleDefaults in Angular set size and variant for every toggle below them. A toggle that states either value keeps its own. Here the filter row is chips at sm, and Archived states ghost.
import { Inline } from '@slotted/react/inline';import { Toggle, ToggleDefaultsProvider } from '@slotted/react/toggle';export default function ToggleDefaults() { return ( <ToggleDefaultsProvider size="sm" variant="chips"> <Inline gap={2}> <Toggle defaultPressed>Late pickups</Toggle> <Toggle>Low stock</Toggle> <Toggle>Backordered</Toggle> <Toggle variant="ghost">Archived</Toggle> </Inline> </ToggleDefaultsProvider> );}Accessibility
| Key | Effect | Focus |
|---|---|---|
| Space | toggles the control | stays |
| Enter | toggles the control | stays |
- The APG Button pattern’s toggle button (w3.org/WAI/ARIA/apg/patterns/button/): a native button with aria-pressed, and no navigation pattern around it — a single control has no arrows of its own.
- aria-pressed is written in both states, so the control is announced as a toggle rather than as a button that happens to be pressed — and the label never changes with the state, which the pattern calls critical. A toggle with only a mark takes its name from an aria-label the consumer writes.
- Inside a toolbar the bar owns the tab stop and the arrows and reaches this control as it reaches every focusable control it holds; the toggle writes no listener. Space and Enter press it, from the platform.
- disabled is the native attribute, because that is also what makes the browser refuse focus and skip the control. The toggle is not a form control: a value a form submits is checkbox or switch.
- The target is the control itself, never narrower than it is tall, so the WCAG 2.2 minimum of 24 by 24 holds at the smallest step and the tightest density — and the theme grows every control to 44 under a coarse pointer.
API
Toggle
Props
pressedboolean- Present means the consumer owns the state, for the life of the component. Absent, the toggle owns it and starts from defaultPressed.
defaultPressedbooleanDefaultfalse- What an uncontrolled toggle starts from, read once. A later change to it does not silently become new state.
size'sm' | 'md' | 'lg'Defaultmd- The control step. It reads the control scale, so the toggle is as tall as the field beside it.
variant'outline' | 'ghost' | 'chips'Defaultoutline- outline draws its own edge, ghost is bare until the toggle is on, and chips is a rounded item that presses in the neutral ink. There is no tone axis: a mode is one color when it is on, and an action that persists, with a tone, is button’s ToggleButton.
iconReactNode- The mark drawn before the words, sized from the control glyph scale and hidden from the reading order. Alone, it makes the toggle square.
childrenReactNode- The words, which never change with the state. Absent, the toggle needs an aria-label.
disabledbooleanDefaultfalse- The native attribute, which is also what makes the browser refuse focus. A toggle that is on and unavailable keeps saying both.
type'button' | 'submit' | 'reset'Defaultbutton- The native type. A toggle in a form is not a form control — checkbox and switch are — so it is a button unless the consumer says otherwise.
Events
| Name | Payload | When |
|---|---|---|
onPressedChange | (pressed: boolean, request: ChangeRequest<boolean>) | Before the state moves. The request carries the new value, the value it replaces, the reason — pointer or keyboard — the originating event, and cancel(), which refuses the move before anything commits. Nothing is emitted for a value equal to the one the toggle already holds. |
onClick, onFocus, onBlur | the element’s own events | Whenever the button fires them. Toggle composes with a consumer’s handler rather than replacing it: the consumer’s runs first, and always runs. Calling preventDefault() in onClick stops the toggle as well. |
Composition
render(props: ToggleRootProps) => ReactNode- Substitutes the root and receives the data attributes, aria-pressed, className, style and the native props, with type, disabled and ref. It stays a button.
refRef<HTMLButtonElement>- Reaches the element. focus() and click() are the element’s own, so no imperative handle is invented for what the platform already ships.
ToggleDefaultsProvider{ size, variant }- On a subtreeThe two decisions an application takes once. A toggle that states either itself still wins.
name, value, form, aria-*the button’s own attributes- Every native attribute reaches the button unchanged and means what a button’s does.
Parts
| Part | Element | Required | Purpose |
|---|---|---|---|
root | button, or the element render returns | Yes | The control. It carries data-ui="toggle", aria-pressed in both states, data-pressed while it is on, and the two axes. Its border is always present, so turning it on changes a color and never the geometry. |
icon | span | No | The mark, sized from the control glyph scale and hidden from the reading order. A toggle with a mark and no words draws square and takes its accessible name from the aria-label the consumer writes. |
label | span | No | The words. A box of their own, so a label a narrow container made too long clips at the ellipsis instead of wrapping — and the string stays whole in the DOM. |
Theming
Each token is a CSS custom property: set it in a theme, or on any element to retune every toggle inside it.
Toggle tokens
| Token | Default |
|---|---|
--slotted-toggle-active-background | var(--slotted-tone-secondary-subtle-active, ButtonFace) |
--slotted-toggle-border-color | var(--slotted-control-border-color, ButtonBorder) |
--slotted-toggle-chips-pressed-background | var(--slotted-tone-neutral-solid, CanvasText) |
--slotted-toggle-chips-pressed-color | var(--slotted-tone-neutral-on-solid, Canvas) |
--slotted-toggle-chips-radius | var(--slotted-radius-full, 9999px) |
--slotted-toggle-color | var(--slotted-text, ButtonText) |
--slotted-toggle-disabled-border-color | var(--slotted-disabled-border, GrayText) |
--slotted-toggle-disabled-color | var(--slotted-disabled-foreground, GrayText) |
--slotted-toggle-disabled-pressed-background | var(--slotted-disabled-background, ButtonFace) |
--slotted-toggle-hover-background | var(--slotted-tone-secondary-subtle-hover, ButtonFace) |
--slotted-toggle-pressed-background | var(--slotted-tone-accent-subtle-active, Highlight) |
--slotted-toggle-pressed-border-color | var(--slotted-tone-accent-border, Highlight) |
--slotted-toggle-pressed-color | var(--slotted-tone-accent-text, HighlightText) |
--slotted-toggle-pressed-hover-background | var(--slotted-tone-accent-subtle-hover, Highlight) |
--slotted-toggle-radius | var(--slotted-control-radius, 4px) |
--slotted-toggle-shadow | var(--slotted-control-shadow, none) |
Theme tokens it reads
| Token | Default |
|---|---|
--slotted-control-border-color | Set per scheme by the theme |
--slotted-control-border-width | var(--slotted-border-width-thin) |
--slotted-control-font-family | var(--slotted-font-family-sans) |
--slotted-control-font-size-lg | comfortable 14px · compact 13px · dense 12px |
--slotted-control-font-size-md | comfortable 13px · compact 12px · dense 12px |
--slotted-control-font-size-sm | comfortable 12px · compact 12px · dense 12px |
--slotted-control-font-weight | var(--slotted-font-weight-semibold) |
--slotted-control-gap-lg | comfortable 8px · compact 6px · dense 6px |
--slotted-control-gap-md | comfortable 8px · compact 6px · dense 4px |
--slotted-control-gap-sm | comfortable 6px · compact 4px · dense 4px |
--slotted-control-height-lg | comfortable 38px · compact 32px · dense 30px |
--slotted-control-height-md | comfortable 34px · compact 28px · dense 26px |
--slotted-control-height-sm | comfortable 30px · compact 26px · dense 24px |
--slotted-control-icon-size-lg | comfortable 18px · compact 16px · dense 14px |
--slotted-control-icon-size-md | comfortable 16px · compact 14px · dense 13px |
--slotted-control-icon-size-sm | comfortable 14px · compact 13px · dense 12px |
--slotted-control-letter-spacing | 0 |
--slotted-control-line-height | 1 |
--slotted-control-padding-inline-lg | comfortable 16px · compact 12px · dense 10px |
--slotted-control-padding-inline-md | comfortable 13px · compact 10px · dense 8px |
--slotted-control-padding-inline-sm | comfortable 10px · compact 8px · dense 6px |
--slotted-control-radius | var(--slotted-radius-sm) |
--slotted-control-shadow | Set per scheme by the theme |
--slotted-control-transition-duration | var(--slotted-duration-short) |
--slotted-control-transition-easing | var(--slotted-easing-standard) |
--slotted-disabled-background | Set per scheme by the theme |
--slotted-disabled-border | Set per scheme by the theme |
--slotted-disabled-foreground | Set per scheme by the theme |
--slotted-duration-instant | 1ms |
--slotted-focus-ring-color | Set per scheme by the theme |
--slotted-focus-ring-offset | var(--slotted-border-width-thick) |
--slotted-focus-ring-width | var(--slotted-border-width-thick) |
--slotted-font-weight-medium | 500 |
--slotted-radius-full | 9999px |
--slotted-text | Set per scheme by the theme |
--slotted-tone-accent-border | Set per scheme by the theme |
--slotted-tone-accent-subtle-active | Set per scheme by the theme |
--slotted-tone-accent-subtle-hover | Set per scheme by the theme |
--slotted-tone-accent-text | Set per scheme by the theme |
--slotted-tone-neutral-on-solid | Set per scheme by the theme |
--slotted-tone-neutral-solid | Set per scheme by the theme |
--slotted-tone-neutral-solid-hover | Set per scheme by the theme |
--slotted-tone-secondary-subtle-active | Set per scheme by the theme |
--slotted-tone-secondary-subtle-hover | Set per scheme by the theme |