Skip to Content

Built by moveinready.casa  and the community

Shadcn Docs

Radix Docs

Toggle

A two-state button that can be either on or off.

Usage

import {Toggle} from "@/components/ui/toggle"; <Toggle>Toggle</Toggle>;

Components

Toggle

PropTypeDefaultDescription
pressedboolean-Controlled pressed state. When provided, the component becomes controlled.
defaultPressedbooleanfalseUncontrolled initial pressed state. Used when pressed is not provided.
onPressedChange(pressed: boolean) => void-Callback fired when the pressed state should change.
disabledbooleanfalseDisables the toggle. Prevents interaction and reduces opacity.
asChildbooleanfalseIf true, clones props to the nearest child instead of rendering a Pressable. Useful for custom elements.
variant"default" | "outline""default"The visual variant of the toggle. Controls the styling approach.
size"sm" | "md" | "lg" | "xl""md"Sets the size of the toggle. Controls height, padding, and text size.
classNamestring-Custom Tailwind CSS classes to apply to the toggle.
childrenReactNode-Content to display inside the toggle.
aria-labelstring-Web accessibility label for screen readers.
testIDstring-React Native test identifier for testing frameworks.

Hooks

useToggle

The backbone hook for the toggle component that provides accessibility props and state management.

Parameters:

ParameterTypeDefaultDescription
pressedboolean-Controlled pressed state.
defaultPressedbooleanfalseUncontrolled initial pressed state.
onPressedChangefunction-Callback fired when the pressed state should change.
disabledbooleanfalseDisables the toggle.
onPressfunction-Custom onPress handler.
propsToggleProps-Additional props to pass through.

Returns:

{ props: React.ComponentProps<typeof Pressable>; state: { isPressed: boolean; } }
PropertyTypeDescription
propsReact.ComponentProps<typeof Pressable>Accessibility props to pass to the toggle component.
state{isPressed: boolean}The current pressed state of the toggle.

Accessibility

The Toggle component is built with accessibility in mind and includes:

Web Accessibility

  • Uses useToggleButton hook for proper button semantics
  • Supports ARIA attributes: role="button", aria-pressed, aria-disabled
  • Keyboard navigation support (Enter and Space keys)
  • Proper focus management

React Native Accessibility

  • accessibilityRole="button" for proper screen reader announcement
  • accessibilityState={{selected: boolean, disabled: boolean}} to communicate toggle state
  • Proper touch target size (minimum 44x44 points)
  • Screen reader optimized content

Styling

Toggle Classes

Variants:

  • default (default) - Transparent background with hover effects
  • outline - Outlined toggle with border and shadow

Sizes:

  • sm - Small toggle size: h-8 px-1.5 min-w-8
  • md (default) - Medium toggle size: h-9 px-2 min-w-9
  • lg - Large toggle size: h-10 px-2.5 min-w-10
  • xl - Extra large toggle size: h-11 px-3 min-w-11

States:

  • selected - Applied when the toggle is pressed/active
  • disabled - Applied when the toggle is disabled

shadcn-native

Built by moveinready.casa and the community