A toggle control enabling users to switch between "on" and "off" states.
Overview
The Switch component provides an intuitive and accessible toggle control, allowing users to switch between two states, typically "on" and "off". This component is commonly used for enabling or disabling features, toggling settings, or representing boolean values in forms. The Switch offers a more visual and interactive alternative to traditional checkboxes for binary choices.
Key Features
Accessibility: Built with WAI-ARIA guidelines in mind, ensuring keyboard navigation and screen reader support.
State Management: Internally manages the on/off state, with options for controlled and uncontrolled usage.
Style-able: Data attributes allow for smooth transitions between states and custom styles.
HTML Forms: Can render a hidden input element for form submissions.
Architecture
The Switch component is composed of two main parts:
Root: The main container component that manages the state and behavior of the switch.
Thumb: The "movable" part of the switch that indicates the current state.
Structure
Here's an overview of how the Switch component is structured in code:
Reusable Components
It's recommended to use the Switch primitives to create your own custom switch component that can be used throughout your application.
In the example below, we're using the Checkbox and Label components to create a custom switch component.
You can then use the MySwitch component in your application like so:
Managing Checked State
Bits UI offers several approaches to manage and synchronize the Switch's checked state, catering to different levels of control and integration needs.
1. Two-Way Binding
For seamless state synchronization, use Svelte's bind:checked directive. This method automatically keeps your local state in sync with the switch's internal state.
Key Benefits
Simplifies state management
Automatically updates myChecked when the switch changes (e.g., via clicking on the switch)
Allows external control (e.g., checking via a separate button/programmatically)
2. Change Handler
For more granular control or to perform additional logic on state changes, use the onCheckedChange prop. This approach is useful when you need to execute custom logic alongside state updates.
Use Cases
Implementing custom behaviors on checked/unchecked
Integrating with external state management solutions
Triggering side effects (e.g., logging, data fetching)
3. Fully Controlled
For complete control over the switch's checked state, use the controlledChecked prop. This approach requires you to manually manage the checked state, giving you full control over when and how the component responds to change events.
To implement controlled state:
Set the controlledChecked prop to true on the Switch.Root component.
Provide a checked prop to Switch.Root, which should be a variable holding the current state.
Implement an onCheckedChange handler to update the state when the internal state changes.
When to Use
Implementing complex checked/unchecked logic
Coordinating multiple UI elements
Debugging state-related issues
Note
While powerful, fully controlled state should be used judiciously as it increases complexity and can cause unexpected behaviors if not handled carefully.
For more in-depth information on controlled components and advanced state management techniques, refer to our Controlled State documentation.
Disabled State
You can disable the switch by setting the disabled prop to true.
HTML Forms
If you pass the name prop to Switch.Root, a hidden input element will be rendered to submit the value of the switch to a form.
By default, the input will be submitted with the default checkbox value of 'on' if the switch is checked.
Custom Input Value
If you'd prefer to submit a different value, you can use the value prop to set the value of the hidden input.
For example, if you wanted to submit a string value, you could do the following:
Required
If you want to make the switch required, you can use the required prop.
This will apply the required attribute to the hidden input element, ensuring that proper form submission is enforced.
API Reference
Switch.Root
The root switch component used to set and manage the state of the switch.
Property
Type
Description
checked$bindable
boolean
Whether or not the switch is checked.
Default: false
onCheckedChange
function
A callback function called when the checked state of the switch changes.
Default: ——undefined
controlledChecked
boolean
Whether or not the checked state is controlled or not. If true, the component will not update the checked state internally, instead it will call onCheckedChange when it would have otherwise, and it is up to you to update the checked prop that is passed to the component.
Default: false
disabled
boolean
Whether or not the switch is disabled.
Default: false
name
string
The name of the hidden input element, used to identify the input in form submissions.
Default: ——undefined
required
boolean
Whether or not the switch is required to be checked.
Default: false
value
string
The value of the hidden input element to be used in form submissions when the switch is checked.
Default: ——undefined
ref$bindable
HTMLButtonElement
The underlying DOM element being rendered. You can bind to this to get a reference to the element.
Default: ——undefined
children
Snippet
The children content to render.
Default: ——undefined
child
Snippet
Use render delegation to render your own element. See Child Snippet docs for more information.
Default: ——undefined
Data Attribute
Value
Description
data-state
''
The switch's checked state.
data-checked
''
Present when the switch is checked.
data-disabled
''
Present when the switch is disabled.
data-switch-root
''
Present on the root element.
Switch.Thumb
The thumb on the switch used to indicate the switch's state.
Property
Type
Description
ref$bindable
HTMLSpanElement
The underlying DOM element being rendered. You can bind to this to get a reference to the element.
Default: ——undefined
children
Snippet
The children content to render.
Default: ——undefined
child
Snippet
Use render delegation to render your own element. See Child Snippet docs for more information.