Skip to content
Slotted

Toggle

A button that stays pressed or unpressed, for a mode that applies the moment it is pressed.

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.

bash
pnpm add @slotted/react @slotted/theme @slotted/themes
tsx
import { 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.

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.

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.

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.

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.

Long order notes stay on one line.

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.

No change yet.

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.

Locking freezes the entries of March 2026.

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.

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.

Accessibility

KeyEffectFocus
Spacetoggles the controlstays
Entertoggles the controlstays
  • 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.
defaultPressedbooleanDefault false
What an uncontrolled toggle starts from, read once. A later change to it does not silently become new state.
size'sm' | 'md' | 'lg'Default md
The control step. It reads the control scale, so the toggle is as tall as the field beside it.
variant'outline' | 'ghost' | 'chips'Default outline
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.
disabledbooleanDefault false
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'Default button
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

NamePayloadWhen
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, onBlurthe element’s own eventsWhenever 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

PartElementRequiredPurpose
rootbutton, or the element render returnsYesThe 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.
iconspanNoThe 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.
labelspanNoThe 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

TokenDefault
--slotted-toggle-active-backgroundvar(--slotted-tone-secondary-subtle-active, ButtonFace)
--slotted-toggle-border-colorvar(--slotted-control-border-color, ButtonBorder)
--slotted-toggle-chips-pressed-backgroundvar(--slotted-tone-neutral-solid, CanvasText)
--slotted-toggle-chips-pressed-colorvar(--slotted-tone-neutral-on-solid, Canvas)
--slotted-toggle-chips-radiusvar(--slotted-radius-full, 9999px)
--slotted-toggle-colorvar(--slotted-text, ButtonText)
--slotted-toggle-disabled-border-colorvar(--slotted-disabled-border, GrayText)
--slotted-toggle-disabled-colorvar(--slotted-disabled-foreground, GrayText)
--slotted-toggle-disabled-pressed-backgroundvar(--slotted-disabled-background, ButtonFace)
--slotted-toggle-hover-backgroundvar(--slotted-tone-secondary-subtle-hover, ButtonFace)
--slotted-toggle-pressed-backgroundvar(--slotted-tone-accent-subtle-active, Highlight)
--slotted-toggle-pressed-border-colorvar(--slotted-tone-accent-border, Highlight)
--slotted-toggle-pressed-colorvar(--slotted-tone-accent-text, HighlightText)
--slotted-toggle-pressed-hover-backgroundvar(--slotted-tone-accent-subtle-hover, Highlight)
--slotted-toggle-radiusvar(--slotted-control-radius, 4px)
--slotted-toggle-shadowvar(--slotted-control-shadow, none)

Theme tokens it reads

TokenDefault
--slotted-control-border-colorSet per scheme by the theme
--slotted-control-border-widthvar(--slotted-border-width-thin)
--slotted-control-font-familyvar(--slotted-font-family-sans)
--slotted-control-font-size-lgcomfortable 14px · compact 13px · dense 12px
--slotted-control-font-size-mdcomfortable 13px · compact 12px · dense 12px
--slotted-control-font-size-smcomfortable 12px · compact 12px · dense 12px
--slotted-control-font-weightvar(--slotted-font-weight-semibold)
--slotted-control-gap-lgcomfortable 8px · compact 6px · dense 6px
--slotted-control-gap-mdcomfortable 8px · compact 6px · dense 4px
--slotted-control-gap-smcomfortable 6px · compact 4px · dense 4px
--slotted-control-height-lgcomfortable 38px · compact 32px · dense 30px
--slotted-control-height-mdcomfortable 34px · compact 28px · dense 26px
--slotted-control-height-smcomfortable 30px · compact 26px · dense 24px
--slotted-control-icon-size-lgcomfortable 18px · compact 16px · dense 14px
--slotted-control-icon-size-mdcomfortable 16px · compact 14px · dense 13px
--slotted-control-icon-size-smcomfortable 14px · compact 13px · dense 12px
--slotted-control-letter-spacing0
--slotted-control-line-height1
--slotted-control-padding-inline-lgcomfortable 16px · compact 12px · dense 10px
--slotted-control-padding-inline-mdcomfortable 13px · compact 10px · dense 8px
--slotted-control-padding-inline-smcomfortable 10px · compact 8px · dense 6px
--slotted-control-radiusvar(--slotted-radius-sm)
--slotted-control-shadowSet per scheme by the theme
--slotted-control-transition-durationvar(--slotted-duration-short)
--slotted-control-transition-easingvar(--slotted-easing-standard)
--slotted-disabled-backgroundSet per scheme by the theme
--slotted-disabled-borderSet per scheme by the theme
--slotted-disabled-foregroundSet per scheme by the theme
--slotted-duration-instant1ms
--slotted-focus-ring-colorSet per scheme by the theme
--slotted-focus-ring-offsetvar(--slotted-border-width-thick)
--slotted-focus-ring-widthvar(--slotted-border-width-thick)
--slotted-font-weight-medium500
--slotted-radius-full9999px
--slotted-textSet per scheme by the theme
--slotted-tone-accent-borderSet per scheme by the theme
--slotted-tone-accent-subtle-activeSet per scheme by the theme
--slotted-tone-accent-subtle-hoverSet per scheme by the theme
--slotted-tone-accent-textSet per scheme by the theme
--slotted-tone-neutral-on-solidSet per scheme by the theme
--slotted-tone-neutral-solidSet per scheme by the theme
--slotted-tone-neutral-solid-hoverSet per scheme by the theme
--slotted-tone-secondary-subtle-activeSet per scheme by the theme
--slotted-tone-secondary-subtle-hoverSet per scheme by the theme