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.jsonFormat your text
Glass that stays readable.
ToggleToggleGroupToggleGroupSeparator- +1 more
Using it in a form? See the react-hook-form and zod recipe.
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.
import { useState } from "react";
import { Toggle } from "@/components/sheenui/toggle/toggle";
// Package build (after the first npm release, or a packed tarball): import { Toggle } from "@sheenui/theme";
import { Bell as BellIcon, BellOff as BellOffIcon, Pin as PinIcon, Star as StarIcon } from "lucide-react";
export function Example() {
const [muted, setMuted] = useState(false);
return (
<>
<Toggle iconOnly aria-label="Favorite">
{({ pressed }) => <StarIcon fill={pressed ? "currentColor" : "none"} />}
</Toggle>
<Toggle defaultPressed>
{({ pressed }) => (
<>
<PinIcon fill={pressed ? "currentColor" : "none"} />
Pin
</>
)}
</Toggle>
<Toggle iconOnly variant="ghost" pressed={muted} onPressedChange={setMuted} aria-label="Mute notifications">
{muted ? <BellOffIcon /> : <BellIcon />}
</Toggle>
<Toggle disabled>Disabled</Toggle>
</>
);
}Files to copy
3 folders · 1 file · 3 packages
- From
theme/src/ components/toggle/lib/cn.tsstyles/
- 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.
import { useState } from "react";
import { Toggle, ToggleGroup, ToggleGroupSeparator } from "@/components/sheenui/toggle/toggle";
// Package build (after the first npm release, or a packed tarball): import { Toggle, ToggleGroup, ToggleGroupSeparator } from "@sheenui/theme";
import { AlignCenter as AlignCenterIcon, AlignLeft as AlignLeftIcon, AlignRight as AlignRightIcon, Bold as BoldIcon, Italic as ItalicIcon, Strikethrough as StrikeIcon, Underline as UnderlineIcon } from "lucide-react";
export function Example() {
const [format, setFormat] = useState(["bold"]);
const [align, setAlign] = useState(["left"]);
return (
<div className="flex flex-wrap items-center gap-2">
<ToggleGroup multiple size="sm" aria-label="Style" value={format} onValueChange={setFormat}>
<Toggle value="bold" iconOnly aria-label="Bold"><BoldIcon /></Toggle>
<Toggle value="italic" iconOnly aria-label="Italic"><ItalicIcon /></Toggle>
<Toggle value="underline" iconOnly aria-label="Underline"><UnderlineIcon /></Toggle>
<ToggleGroupSeparator />
<Toggle value="strike" iconOnly aria-label="Strikethrough"><StrikeIcon /></Toggle>
</ToggleGroup>
<ToggleGroup size="sm" aria-label="Alignment" value={align} onValueChange={setAlign}>
<Toggle value="left" iconOnly aria-label="Align left"><AlignLeftIcon /></Toggle>
<Toggle value="center" iconOnly aria-label="Align center"><AlignCenterIcon /></Toggle>
<Toggle value="right" iconOnly aria-label="Align right"><AlignRightIcon /></Toggle>
</ToggleGroup>
</div>
);
}Toggle
ToggleGroup
Keyboard
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
Related pages
67 components pages in all: browse them on All components.
By Lokendra Singh Rathore (GitHub, opens in a new tab). Published , updated .