Join our Discord Community

Toggle

A two-state button that can be either on or off. Perfect for binary options, formatting controls, and feature toggles in React applications with Next.js, TypeScript, and Tailwind CSS.

You know those buttons in text editors that stay pressed when you click them? Like bold, italic, or underline buttons that show whether formatting is active? Toggles give users that same clear visual feedback for any on/off option.

Basic toggle button

Simple bold formatting toggle:

Loading component...

Clean pressed/unpressed states with visual feedback. This free open source React component handles all the accessibility requirements and keyboard navigation for two-state controls.

npx shadcn@latest add toggle

Why toggles beat regular buttons

Toggles solve specific interaction problems:

  • State visibility - Users instantly see what's active
  • Persistent feedback - Button stays pressed while feature is on
  • Less cognitive load - No guessing about current state
  • Familiar pattern - Everyone knows how toggle buttons work
  • Space efficient - One button handles both on and off
  • Immediate response - Visual feedback happens instantly

Real-world toggle patterns

Text formatting group

Multiple toggles working together:

Loading component...

Rich text toolbar

Complete editor toolbar with grouped toggles:

Loading component...

These examples show toggles in their natural habitat - toolbars, formatting controls, and settings where users need to toggle features on and off.

Perfect for binary controls

Toggles work best for features that have clear on/off states. Text formatting, view modes, filter options, feature flags, display settings. Much clearer than buttons that don't show their current state or switches that might be confusing in toolbar contexts.

Built on Radix UI Toggle primitive. Full TypeScript support. Styled with Tailwind CSS to match the shadcn design system.

API Reference

Toggle

A two-state button component.

PropTypeDefaultDescription
pressedboolean-The controlled pressed state of the toggle
defaultPressedbooleanfalseThe default pressed state (uncontrolled)
onPressedChange(pressed: boolean) => void-Event handler called when the pressed state changes
disabledbooleanfalseWhen true, prevents the user from interacting with the toggle
variant"default" | "outline""default"The visual style variant
size"default" | "sm" | "lg""default"The size of the toggle
classNamestring-Additional CSS classes

Data Attributes

The toggle automatically applies data attributes for styling:

AttributeValuesDescription
[data-state]"on" | "off"Indicates whether the toggle is pressed
[data-disabled]Present when disabledApplied when the toggle is disabled

Size Variants

SizeDescription
smSmaller toggle for compact toolbars
defaultStandard size for most use cases
lgLarger toggle for primary actions

Style Variants

VariantDescription
defaultFilled style with solid background when pressed
outlineOutlined style with border emphasis

Usage Patterns

Controlled toggle

Loading component...

Toggle group state management

Loading component...

Complete toolbar with grouped toggles

Loading component...

Keyboard Navigation

The toggle supports full keyboard interaction:

KeyDescription
SpaceActivates/deactivates the toggle
EnterActivates/deactivates the toggle

Accessibility Features

Built-in accessibility support includes:

  • ARIA attributes - Proper aria-pressed state communication
  • Keyboard support - Space and Enter keys toggle state
  • Focus management - Clear focus indicators
  • Screen reader support - State changes are announced
  • Semantic button - Uses proper button semantics
  • Label association - Connects with aria-label or external labels

Styling Examples

The toggle component supports different variants and sizes. Check the preview examples above to see various styling options in action, including outline variants, different sizes, and text labels.

Common use cases

Toggles work great for these scenarios:

  • Text formatting - Bold, italic, underline controls
  • View modes - List vs grid, expanded vs collapsed
  • Filter states - Show/hide categories, active filters
  • Feature flags - Enable/disable functionality
  • Layout options - Sidebar visibility, panel states
  • Tool selection - Active drawing tools, editing modes
  • Status indicators - Favorite, bookmark, follow states

Design guidelines

Keep these principles in mind when using toggles:

  • Clear labeling - Use aria-label or visible text
  • Visual distinction - Pressed state should be obvious
  • Group logically - Related toggles should be visually connected
  • Consistent sizing - Use same size within groups
  • Icon choice - Icons should clearly represent the action
  • State persistence - Toggle state should persist appropriately
  • Loading states - Show loading during async toggle operations
  • Error handling - Revert state if toggle action fails