Skip to content

React Toggle Button

A React toggle button that stays pressed and is announced as “pressed”, and can be grouped in a glass toolbar.

npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/toggle.json

Format your text

Glass that stays readable.

Standalone toggles

children can be a function of the pressed state, to swap an outline icon for a filled one. The label stays the same: screen readers say “pressed” or “not pressed”, so a label that also changed would contradict it.

Files to copy

3 folders · 1 file · 3 packages
From theme/src/
  • components/toggle/
  • lib/cn.ts
  • styles/
shadcn CLI
npx shadcn@latest add https://lokendra-builds.github.io/sheenui/r/toggle.jsonRegistry item: toggle. The styles need the Tailwind v4 glass UI setup.
Packages
pnpm add @base-ui/react clsx tailwind-merge

Formatting toolbar

Two named groups: B / I / U / S are independent (multiple); alignment is one at a time. The arrow keys move inside a group and Tab moves to the next one. For a single tab stop across several groups, put them in a Toolbar.

Glass that stays readable: every surface keeps its text above 4.5:1, in light and dark.

Toggle

PropTypeDefaultDescription
variant"glass" | "ghost""glass""glass" (default) on its own; "ghost" for toolbars. Inside a ToggleGroup it is always ghost.
size"sm" | "md" | "lg""md""sm" (32px), "md" (40px, default) or "lg" (48px). Inherited from the ToggleGroup when inside one.
iconOnlybooleanfalseSquare button for a lone icon (give it an aria-label).
classNamestringNoneAdds extra classes to the button.
childrenReactNode | ((state: ToggleState) => ReactNode)NoneContent, or a function of the state to swap icons: {({ pressed }) => (pressed ? <StarFilled /> : <Star />)}.
pressed / defaultPressedbooleanfalseControlled or uncontrolled pressed state.
onPressedChange(pressed: boolean) => voidNoneCalled with the new pressed state when the toggle is pressed.
valuestringNoneNames the toggle inside a ToggleGroup.
disabledbooleanfalseMakes the toggle non-interactive.

ToggleGroup

PropTypeDefaultDescription
size"sm" | "md" | "lg""md"Size of every toggle in the group.
classNamestringNoneAdds extra classes to the glass track.
value / defaultValuestring[]NoneValues of the pressed toggles.
onValueChange(value: string[]) => voidNoneCalled when any toggle changes.
multiplebooleanfalseAllow several pressed toggles (otherwise one at a time; pressing it again clears it).
orientation"horizontal" | "vertical""horizontal"Sets the layout and arrow-key direction.
loopFocusbooleantrueArrow keys wrap from the last toggle to the first.
disabledbooleanfalseThe whole group is not interactive.
aria-labelstringNoneName of the group for screen readers.

Keyboard

KeysAction
Tab/Shift + TabToggle: move focus to or from it. ToggleGroup: enter or leave the group once.
Enter/SpacePress or release the focused toggle.
Arrow keysToggleGroup: move between toggles.

Accessibility

  • A native button with aria-pressed; give an icon-only toggle an aria-label.
  • A group has role="group"; name it with aria-label. Pressed state is shown by fill and weight, not only by color.
  • The touch hit area is 44 px; under reduced motion the state changes without a scale.

When to use Toggle

  • Use it as a toggle button for Bold or Italic in a text editor toolbar, with variant="ghost" so there is no surface until pressed and a ToggleGroup holding several together.
  • Use it for a stand-alone Mute, Pin or Favorite button that keeps its pressed state, with iconOnly for a square button and children as a function to swap an outline icon for a filled one.
  • Use a ToggleGroup with the multiple prop for independent options such as text formatting, or without it for a single choice like text alignment where pressing the active one clears it.
  • Skip it when the control is a setting that applies at once, which is what Switch is for, or a form value that submits with the form, where Checkbox is the right element.
  • Pair it with Toolbar for grouped editor actions, Tooltip to name icon-only buttons, and Kbd or KbdShortcut to show the key that triggers the same action elsewhere.

Frequently asked questions

67 components pages in all: browse them on All components.

By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .