Basic Usage
Controlled State
Event Handlers
Props Reference
Current toggle state (controlled)
Initial toggle state (uncontrolled)
Event fired when switch is toggled
Disables switch interaction
Makes switch required for form submission
Prevents changing the switch state
Form field name for submission
Value submitted when switch is checked
Value submitted when switch is unchecked
Additional CSS classes for styling
With Label
Form Integration
Real Example
From demo atdemo/demo.py:44:
Compositional API
For custom layouts:Switch Components
switch.root
The main switch container with background.switch.thumb
The sliding thumb element.Styling Classes
Access predefined styles viaui.switch.class_names:
ROOT: Switch track/background styles with checked statesTHUMB: Sliding thumb styles with transform animations
Visual States
The switch automatically handles:- Unchecked: Gray background, thumb on left
- Checked: Primary color background, thumb slides right
- Disabled: Reduced opacity, cursor not-allowed
- Focus: Outline visible for keyboard navigation
Animation
Smooth transitions powered by CSS:- 200ms ease-out for background color changes
- 200ms ease-out for thumb translation
- Shadow effects for depth
Accessibility
- Keyboard accessible (Space/Enter to toggle)
- Proper ARIA attributes
- Focus indicators
- Screen reader support
Implementation Details
From source code atreflex_ui/components/base/switch.py:94:
- Built on Base UI Switch primitives
- High-level wrapper auto-creates root with thumb
- Uses data attributes for styling states
- Renders hidden checkbox input for form submission
- Thumb translates 3 units (0.75rem) when checked