Skip to main content

Toggle

StorybookView in Storybook

Explore interactive examples and all component variations in our Storybook.

The Toggle component provides a two-state button for binary choices. Built on Radix UI, it offers consistent styling, accessibility features, and smooth state transitions. Perfect for on/off controls, favorites, bookmarks, and any interface requiring toggle functionality.

Basic Usage

With Text

Controlled Toggle

Different Variants

Default Variant

Outline Variant

Different Sizes

Small

Default

Large

Settings Panel

Settings

Current Settings:

  • Wi-Fi: On
  • Notifications: Off
  • Public Profile: On

Interactive Actions

Article Title

This is a sample article that demonstrates interactive toggle actions.

Like
Star
Save

Text Formatting Toolbar

This text will be formatted based on the toolbar selections above.

Active formatting: none

Properties

Toggle

PropertyTypeDescription
pressedbooleanControlled pressed state of the toggle.
defaultPressedbooleanDefault pressed state when uncontrolled.
onPressedChange(pressed: boolean) => voidCallback fired when pressed state changes.
disabledbooleanWhether the toggle is disabled.
variant"default" | "outline"Visual variant of the toggle.
size"sm" | "default" | "lg"Size variant of the toggle.
classNamestringAdditional CSS classes for the toggle.
childrenReactNodeContent of the toggle button.
asChildbooleanRender as child element instead of button.
aria-labelstringAccessible label for screen readers.

Accessibility

The Toggle component includes comprehensive accessibility features:

  • Keyboard Navigation - Space and Enter keys activate the toggle
  • Focus Management - Proper focus indicators and handling
  • ARIA Support - Full ARIA attributes including pressed state
  • Screen Reader Support - Toggle state changes announced
  • Semantic HTML - Uses proper button semantics
  • High Contrast - Supports high contrast mode and themes

Common Use Cases

The Toggle component is perfect for:

  • Settings Controls - On/off switches for application preferences
  • Text Formatting - Bold, italic, underline controls in editors
  • Interactive Actions - Like, favorite, bookmark buttons
  • Visibility Controls - Show/hide content toggles
  • Feature Flags - Enable/disable feature controls
  • Audio/Video Controls - Mute, play/pause toggles
  • Notification Settings - Enable/disable notification types

Best Practices

  1. Clear Labels - Always provide descriptive aria-labels for screen readers
  2. Visual Feedback - Ensure clear visual indication of pressed/unpressed states
  3. Consistent Behavior - Use consistent toggle behavior throughout your app
  4. Icon Usage - Use recognizable icons that clearly indicate the toggle function
  5. State Management - Handle controlled vs uncontrolled state appropriately
  6. Keyboard Support - Ensure proper keyboard navigation and activation
  7. Touch Targets - Ensure adequate touch target size for mobile devices
  8. Color Contrast - Maintain sufficient color contrast in all states