Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Toggle for React

A button that stays pressed until it's pressed again, for React on Base UI: bold or bookmark in a toolbar, in two variants and three sizes.

On this page9 sections

A toggle is a button that stays pressed after a click and comes back up on the next one, like bold in a text editor or the bookmark on an article. The button's own look is the state, so it suits a mode that applies to whatever is in front of you.

For a setting that turns a feature on or off, use a Switch. For a row of toggles that work together, such as text alignment, use a Toggle Group.

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.

Anatomy

Toggle anatomy: root, indicator

Usage

import { BookmarkIcon } from "lucide-react";
import { Toggle } from "@/components/ui/toggle";
<Toggle size="sm" variant="outline">
  <BookmarkIcon
    aria-hidden="true"
    className="group-aria-pressed/toggle:fill-foreground"
  />
  Bookmark
</Toggle>

The icon's class fills it while the toggle is pressed: group-aria-pressed/toggle reads the aria-pressed attribute that every version of the button carries.

Accessibility

Toggle renders a <button> with aria-pressed, so a screen reader announces a toggle button that is pressed or not pressed, and Space or Enter flips it. The name is yours to get right. A toggle with visible text is named by that text, and an aria-label replaces it for screen readers, so leave the label off or start it with the visible words. An icon-only toggle needs an aria-label that names the action, such as Bold, and keeps it whether pressed or not.

Space
Toggle the pressed state.
Enter
Toggle the pressed state.

Demos

Outline

Italic and Bold, each an icon with a word, with variant set to outline so the resting button has a border. They are separate toggles, so both can be pressed at once, the way italic and bold combine on one word. Use the outline on a plain surface where a borderless button would read as text.

Preview width

Text

An Italic toggle with its icon and the word, on the default variant, which has no border and takes a muted background once pressed. A word beside the icon helps when the glyph alone could be misread, and it names the button for screen readers too, so the toggle needs no aria-label.

Preview width

Icon only

A bold toggle with nothing but its icon, on the default variant, the button most editor toolbars are built from. With no visible word, aria-label set to Bold names the action, and it stays the same whether the toggle is pressed or not. Use it for actions people know by their glyph, and add the word when they might not.

Preview width

Sizes

Small, Default and Large outline toggles, set with the size values sm, md and lg. md is what you get when you leave the prop out, and default is accepted as another name for it, as in shadcn/ui. Use the small one in a dense editor toolbar and the large one where it's a main touch target.

Preview width

Disabled

Two toggles that can't be pressed, one on the default variant and one outlined, both with disabled. They fade to half opacity and ignore clicks, and the outlined one keeps its border. Disable a toggle when its action doesn't apply yet, such as bold with nothing selected, and keep it in place so the toolbar doesn't shift.

Preview width

RTL

The bookmark toggle with an Arabic label and dir set to rtl. The icon sits on the right, before the word in reading order, and still fills when the toggle is pressed. Use it to check that an icon and its label come in the right order in a right-to-left toolbar.

Preview width

FAQ

What is the difference between a Toggle and a Switch?

A toggle is a button that stays pressed, like bold in an editor toolbar, and a screen reader announces it as a pressed button. A Switch turns a setting on or off, like Wi-Fi, and is announced as on or off.

Pick by what the control changes. A toggle acts on the content in front of you, and a switch changes a preference that outlives the page. A Toggle Group is a row of related toggles, and a Checkbox belongs in a form.

How do I control a Toggle in React?

Pass pressed and update it in onPressedChange, which receives the new boolean first and the event details second. Use defaultPressed instead when the toggle can keep its own state.

Control it when the state belongs to something else, like a bold button that should show whether the selected text is already bold.

How do I make an outline Toggle?

Set variant to outline, as the Outline demo does, for a toggle with a border at rest. The default variant has no border and a transparent background, and ghost is accepted as another name for it.

Both variants show the pressed state the same way, with a muted background, so the outline is only about how the toggle looks before it's pressed.

How do I change the size of a Toggle?

Set size to sm, md or lg, as the Sizes demo does. md is the default, and default is accepted as another name for it, so code copied from shadcn/ui keeps working.

The heights and paddings are classes in components/ui/toggle, under the size key of toggleVariants, so a new size is one more entry under that key.

How do I make an icon-only Toggle accessible?

Give it an aria-label that names the action, such as Bold or Bookmark, as the Icon only demo does, and mark the icon aria-hidden="true" so it isn't read as well. A screen reader then announces the name, the button and whether it's pressed.

Keep the label the same in both states. Switching it to Unbold when pressed makes a screen reader announce Unbold, pressed, which reads as a contradiction.

How do I show a different icon when a Toggle is pressed?

Put both icons in the toggle and switch them with classes keyed on aria-pressed: group-aria-pressed/toggle:hidden on the unpressed one, and hidden group-aria-pressed/toggle:block on the pressed one. ToggleIndicator in this version is a plain <span> and doesn't read the state.

For a single icon that only changes its fill, as the bookmark does in the first demo, one class keyed on aria-pressed is enough.

How do I style the pressed state of a Toggle?

Key the classes on aria-pressed, which every version of the button carries: aria-pressed:bg-accent on the toggle, or group-aria-pressed/toggle: on something inside it. To change every toggle at once, edit toggleVariants in components/ui/toggle.

Base UI also adds data-pressed while it's pressed.

API Reference

Toggle

<button> data-slot="toggle"

PropType
pressed?boolean

Whether the toggle button is currently pressed. This is the controlled counterpart of defaultPressed.

defaultPressed?boolean

Whether the toggle button is currently pressed. This is the uncontrolled counterpart of pressed.

Default
false
onPressedChange?(pressed: boolean, eventDetails: Toggle.ChangeEventDetails) → void

Callback fired when the pressed state is changed.

Type
(pressed: boolean, eventDetails: Toggle.ChangeEventDetails) → void
Parameters

pressed – boolean

eventDetails – Toggle.ChangeEventDetails

Returns
void
disabled?boolean

Whether the component should ignore user interaction.

Default
false
nativeButton?boolean

Whether the component renders a native <button> element when replacing it via the render prop. Set to false if the rendered element is not a button (for example, <div>).

Default
true
size?"sm" | "md" | "default" | "lg"

The size.

Type
"sm" | "md" | "default" | "lg"
Default
"md"
value?Value

A unique string that identifies the toggle when used inside a toggle group.

variant?"default" | "ghost" | "outline"

The visual style.

Type
"default" | "ghost" | "outline"
Default
"default"
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

AttributeType
data-disabled?string

Present when disabled is true.

data-pressed?string

Present when pressed is true.

data-state"on" | "off"

"on" or "off".

Type
"on" | "off"

ToggleIndicator

<span> data-slot="toggle-indicator"

PropType
className?string

Merged with the part's classes.