Skip to content

Toggle

A two-state button that turns a setting or a filter on and off and stays pressed.

Installation

pnpm dlx shadcn@latest add @silkware/toggle

To copy the component by hand, install its dependencies:

pnpm add react-aria-components class-variance-authority cn

Then copy the source into components/ui/toggle.tsx:

components/ui/toggle.tsx
"use client"
import { cva, type VariantProps } from "class-variance-authority"import { cn } from "cn"import {  ToggleButton as TogglePrimitive,  type ToggleButtonProps,} from "@animate-ui/react/primitives/react-aria"
const toggleVariants = cva(  "group/toggle inline-flex items-center justify-center gap-1 rounded-md text-sm font-medium whitespace-nowrap transition-[color,box-shadow] outline-none hover:bg-muted hover:text-foreground focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 disabled:pointer-events-none disabled:opacity-50 aria-invalid:border-destructive aria-invalid:ring-destructive/20 aria-pressed:bg-muted dark:aria-invalid:ring-destructive/40 data-selected:bg-muted [&_svg]:pointer-events-none [&_svg]:shrink-0 [&_svg:not([class*='size-'])]:size-4",  {    variants: {      variant: {        default: "bg-transparent",        outline: "border border-input bg-transparent shadow-xs hover:bg-muted",      },      size: {        default:          "h-9 min-w-9 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",        sm: "h-8 min-w-8 px-2.5 has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5",        lg: "h-10 min-w-10 px-2.5 has-data-[icon=inline-end]:pr-2 has-data-[icon=inline-start]:pl-2",      },    },    defaultVariants: {      variant: "default",      size: "default",    },  })
function Toggle({  className,  variant = "default",  size = "default",  ...props}: ToggleButtonProps & VariantProps<typeof toggleVariants>) {  return (    <TogglePrimitive      data-slot="toggle"      className={cn(toggleVariants({ variant, size, className }))}      {...props}    />  )}
export { Toggle, toggleVariants }

Usage

import { Toggle } from "@/components/ui/toggle"
<Toggle aria-label="Pin order" onChange={(isPinned) => pinOrder(isPinned)}>  <PinIcon /></Toggle>

A toggle is a button with a state. It reads as pressed when selected and not pressed otherwise. The selected state is shown with a soft grey fill, so for a toggle that is the only control of its kind, use variant="outline" and a label that names the thing that is on.

Use onChange, which receives the new boolean, not onPress. The state is uncontrolled unless you pass isSelected.

Examples

Outline

variant="outline" adds a border so the toggle stands apart on a plain surface. The default variant has no border and is meant for toolbars, where neighbouring toggles share the space.

With text

Add text to a toggle when an icon alone would not say what it does. Put data-icon="inline-start" on the icon so the padding tightens.

Size

sm, default and lg set the height to 32, 36 and 40 pixels. Keep one size within a toolbar.

Disabled

isDisabled keeps the current state visible but ignores presses and removes the toggle from the tab order.

Controlled

Use isSelected and onChange when the state lives elsewhere, for example in a filter or a saved preference. Use defaultSelected for an initial state that only the toggle changes.

Order 4821 is pinned

Accessibility

KeyAction
SpaceToggles the button on or off.
EnterToggles the button on or off.
TabMoves focus to the next control. A disabled toggle is skipped.

React Aria provides:

  • A button with aria-pressed that reflects the state, and the native keyboard behaviour above.
  • Press events that behave the same for mouse, touch and keyboard.
  • data-selected and data-pressed states for styling, and a focus ring for keyboard focus only.

You provide:

  • An aria-label for an icon-only toggle. The label names the setting, not the state: "Pin order", not "Pinned".
  • A label that stays the same when the state changes, because aria-pressed already tells the state.
  • A visible difference between on and off that does not depend on colour alone.

Guidelines

When to use

  • To switch a view option or a filter on and off, such as "Show late orders".
  • To mark an item with a state that people change often, such as pinned or flagged.
  • Inside a toolbar, where each button has two states.

When not to use

Content

  • Name the thing the toggle controls: "Pin order", "Show late orders".
  • Keep the same label in both states. Do not swap "Show" for "Hide".
  • Use an icon only when it is widely understood, and always give it an aria-label.

Labels

Do

Give an icon-only toggle an aria-label that names the setting.

Don’t

Don’t use a toggle for an action. A toggle that says "Delete order" stays pressed and does nothing the second time.

API reference

Toggle

Renders a <button> through the React Aria ToggleButton. It accepts every prop of it and the props below.

PropTypeDefaultDescription
variant?"default" | "outline""default"Without a border, or with a border and a shadow.
size?"sm" | "default" | "lg""default"The height and minimum width: 32, 36 or 40 pixels.
isSelected?booleanNoneWhether the toggle is on, for a controlled toggle.
defaultSelected?booleanfalseWhether the toggle starts on, for an uncontrolled toggle.
onChange?(isSelected: boolean) => voidNoneCalled when the state changes.
isDisabled?booleanfalseIgnores presses and removes the toggle from the tab order.
aria-label?stringNoneThe accessible name, required when the toggle has no visible text.
className?stringNoneClasses merged after the variant classes.

toggleVariants

The class function behind Toggle and ToggleGroupItem, from class-variance-authority. Call toggleVariants({ variant: "outline", size: "sm" }) to give another element the toggle styles.