Slide Switch
React Native (Expo)
A React Native switch whose thumb can be dragged as well as tapped, stretching while held and settling on the nearer side.
A switch whose thumb can be dragged as well as tapped. It stretches while held, follows the finger, and settles on whichever side it is nearer to (a flick decides it sooner). The track cross-fades between the two colors, and screen readers get a standard switch with its state. Built for React Native and Expo with react-native only; this page previews it in the browser through react-native-web.
Installation
CLI
npx @modus-ui/cli add slide-switchManual
- Copy the code from the component tab.
- Create a file at
components/ui/slide-switch.tsx. - Paste the code.
It needs no packages beyond react-native, which your Expo or React Native app already has.
Usage
tsx
The switch is controlled: it shows value, and a tap or drag calls onValueChange with the new one. Update value there to accept the change.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
value | boolean | - | Whether the switch is on. |
onValueChange | (value: boolean) => void | - | Called with the new value after a tap or a drag. |
disabled | boolean | false | Dims the switch and ignores touches. |
size | number | 32 | Height of the track. The track is 1.8 times as wide. |
label | string | - | Spoken name of the switch. |
colors | Partial<SlideSwitchColors> | - | Overrides off, on and thumb. |
style | StyleProp<ViewStyle> | - | Style for the switch's container. |
Comments