suiss/ui · Component
Switch
A toggle control that turns a single setting on or off.
Switch is an open-source component from suiss/ui for React, React Native, SwiftUI and Jetpack Compose. The @esuiss/ui CLI copies its source into your project, so you own the code.
Switch wraps the switch primitive of the chosen base and renders the track and thumb together. The thumb stretches while pressed, and the track fills with the primary color when on.
Use a switch for settings that take effect immediately. To select an option in a form that's submitted later, use Checkbox.
Installation
Works in any project with a components.json. See Installation for the one-line registry setup. Or use the suiss/ui CLI:
- Install the following dependencies and add the
cnhelper:
- Copy and paste the following code into your project.
- Update the import paths to match your project setup.
Usage
Examples
Checked
Set defaultChecked to start the switch in the on state.
Set defaultSelected to start the switch in the on state.
Sizes
Use the size prop to choose between sm and default.
With label
Wrap the switch and its text in a Label so the text names and toggles it too.
Disabled
Set disabled to turn the switch off; the example shows the off and on states.
Set isDisabled to turn the switch off; the example shows the off and on states.
Invalid
Mark the switch with aria-invalid to show an error state with a destructive border.
Mark the switch with isInvalid to show an error state with a destructive border.
Accessibility
- Renders with the
switchrole andaria-checked. - Give it a name with
aria-labelor by wrapping it in aLabel. Spacetoggles the focused switch.- A disabled switch ignores clicks and keyboard presses.
API Reference
Switch
The switch track and its sliding thumb.
| Prop | Type | Default |
|---|---|---|
size | "sm" | "default" | "default" |
checked | boolean | — |
defaultChecked | boolean | false |
onCheckedChange | (checked: boolean) => void | — |
disabled | boolean | false |
| Prop | Type | Default |
|---|---|---|
size | "sm" | "default" | "default" |
isSelected | boolean | — |
defaultSelected | boolean | false |
onChange | (isSelected: boolean) => void | — |
isDisabled | boolean | false |
isInvalid | boolean | false |
Also accepts the props of the primitive part it wraps.
Use defaultChecked for an uncontrolled switch, or checked with onCheckedChange to control it. onCheckedChange is called when the switch is toggled.
Use defaultSelected for an uncontrolled switch, or isSelected with onChange to control it. onChange is called when the switch is toggled.
A transparent hit area extends around the track to make it easier to tap, and the thumb's travel flips in right-to-left layouts. The parts set data-slot="switch" and data-slot="switch-thumb", plus data-size and the primitive's state attributes, for styling.
Web preview
Switch is a pressable track with a round thumb that slides between the off and on positions. The thumb animates with Reanimated and stretches slightly while pressed. It comes in two sizes, default and sm, and has an invalid state that adds a destructive border.
Use a switch for settings that take effect immediately. For choices that are submitted later, use Checkbox. The switch has a hit slop around it, so it is easier to press than its visual size.
Installation
- Install the following dependencies:
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/ui/switch.tsx:
- Update the import paths to match your project setup.
- Install the following dependencies:
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/ui/switch.tsx:
- Update the import paths to match your project setup.
- Install the following dependencies:
- Copy the primitives it uses into
components/primitives:
- Copy and paste the following code into
components/ui/switch.tsx:
- Update the import paths to match your project setup.
-
Target iOS 17 or later.
-
Add the Swift package to your app:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Add the files to your app target.
- Apply the Compose compiler plugin, enable Compose, set
compileSdkto 37 or higher andminSdkto 24 or higher, and add the dependencies:
- Copy the theme files every view uses:
- Copy the source and every file it uses:
- Update the
com.esuiss.uipackage names to match your project.
Usage
Examples
Checked
A switch that starts on.
Web preview
Sizes
Set size to sm or default; the example shows both side by side.
Web preview
With label
A controlled switch next to a pressable label.
Web preview
With description
A switch with a label and a line of supporting text.

Disabled
Disabled switches in the off and on states.
Web preview
Invalid
Set invalid to add a destructive border; the example shows the switch next to its label.
Web preview
Accessibility
- The root has the
switchrole and reportscheckedanddisabledin its accessibility state, so VoiceOver and TalkBack announce whether it is on. - The thumb is hidden from the accessibility tree.
- Label every switch with
accessibilityLabeloraccessibilityLabelledBy. - The thumb animation follows the system reduce motion setting.
API Reference
Switch
The switch track and its sliding thumb.
| Prop | Type | Default |
|---|---|---|
size | SwitchSize | "default" |
invalid | boolean | - |
Also accepts the props of React.ComponentProps<typeof SwitchPrimitives.Root>.
Use defaultChecked for an uncontrolled switch, or checked with onCheckedChange to control it. invalid adds a destructive border when the switch is not disabled. A disabled switch is shown at opacity-50.
A switch has no visible text of its own. Pair it with a Label and link them with accessibilityLabelledBy and the label's nativeID, or set an accessibilityLabel. Give the Label an onPress that toggles the value if the label should be pressable too.
.toggleStyle(.switch)
Style that creates SwitchStyle.
| Parameter | Type | Default |
|---|---|---|
size | SwitchStyle.Size: .default | .sm | - |
SwitchSize
Values: Default, Sm.
Switch
The switch track and its sliding thumb.
| Parameter | Type | Default |
|---|---|---|
checked | Boolean | - |
onCheckedChange | ((Boolean) -> Unit)? | - |
modifier | Modifier | Modifier |
size | SwitchSize: Default | Sm | SwitchSize.Default |
enabled | Boolean | LocalFieldState.current.enabled |
invalid | Boolean | LocalFieldState.current.invalid |
interactionSource | MutableInteractionSource? | null |
