Skip to content
shadcn.io

shadcn.io is not affiliated with official shadcn/ui

Toggle examples · Standard

Default Toggle for React

An icon-only toggle that turns bold on and off.

Framework
UI library

A toolbar button has to show what's active: bold, italic or underline. This React example is a single icon-only Toggle for bold, built on Base UI. It keeps its own pressed state, so one click turns it on and the next turns it off, and the aria-label names it for screen readers. Use it for text formatting, view options, filters or any on/off action where an icon is enough.

Preview

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

FAQ

How does the toggle track its pressed state without a value prop?

The Toggle is uncontrolled by default: it stores its own pressed state, flips it on a click or on Space and Enter, and exposes it as aria-pressed. It starts unpressed. Pass defaultPressed to start it pressed.

Why is aria-label required for icon-only toggles?

An icon has no text, so without a label a screen reader announces only "toggle button, not pressed". aria-label="Toggle bold" gives the button its name, and the icon is marked aria-hidden="true" so it isn't read as well. When the label is visible, as in the Toggle with Text example, the text names the button and no aria-label is needed.

When should I use Toggle instead of Switch?

A Toggle is a button that stays pressed, like Bold in an editor toolbar, and is announced as a pressed button. A Switch turns a setting on or off, like notifications or dark mode, and is announced as a switch. Use the Toggle when the button is the state of the thing you're working on, and the Switch for a preference that applies elsewhere.

Can I change the pressed state styling?

Yes. Every Toggle sets aria-pressed while it's on, so style that attribute: for example aria-pressed:bg-primary aria-pressed:text-primary-foreground on the Toggle. To change every Toggle in your app, edit the Toggle in your ui folder instead of repeating the classes.

How do I control the pressed state of the toggle?

Keep the pressed state in your own state and pass it back to the Toggle, when other parts of the page need to read or set it. Pass pressed; onPressedChange receives the new boolean first, then event details.

How do I create a toggle group with multiple toggles?

Use the Toggle Group: wrap a ToggleGroupItem for each option in a ToggleGroup, which tracks which items are pressed and makes the row one Tab stop with arrow-key navigation. One item is pressed at a time by default; add multiple to let several be pressed together.