Skip to content
switch-example.tsx
A control that allows the user to toggle between checked and not checked.

Installation#
bunx --bun fabricator-ui@latest add switch
Usage#
import { Switch } from "@/components/ui/switch"
import { Text } from "@/components/ui/text"<Switch accessibilityLabel="Off" />
<Switch accessibilityLabel="On" defaultChecked />
<Switch accessibilityLabel="Invalid" aria-invalid />
<Switch accessibilityLabel="Disabled" disabled />
<Switch accessibilityLabel="Disabled on" disabled defaultChecked />
<Switch accessibilityLabel="Read only" readOnly defaultChecked />Examples#
import * as React from "react"
import { View } from "react-native"
import { Switch } from "@/components/ui/switch"
import { Text } from "@/components/ui/text"
function SwitchExample() {
const [isOn, setIsOn] = React.useState(true)
return (
<ExampleWrapper>
<Example title="States" className="flex-row flex-wrap items-center">
<Switch accessibilityLabel="Off" />
<Switch accessibilityLabel="On" defaultChecked />
<Switch accessibilityLabel="Invalid" aria-invalid />
<Switch accessibilityLabel="Disabled" disabled />
<Switch accessibilityLabel="Disabled on" disabled defaultChecked />
<Switch accessibilityLabel="Read only" readOnly defaultChecked />
</Example>
<Example title="Sizes" className="flex-row items-center">
<Switch accessibilityLabel="Small" size="sm" defaultChecked />
<Switch accessibilityLabel="Default" size="default" defaultChecked />
</Example>
<Example title="Controlled">
<View className="flex-row items-center gap-2">
<Switch
accessibilityLabel="Airplane mode"
checked={isOn}
onCheckedChange={setIsOn}
/>
<Text>Airplane mode is {isOn ? "on" : "off"}</Text>
</View>
</Example>
</ExampleWrapper>
)
}
export { SwitchExample }API reference#
Props it adds to what it wraps: size.
Differences from web#
Same. Drawn by Fabricator UI, rather than the system's switch, so it matches the website.
Accessibility#
Parts set role="switch", read by VoiceOver and TalkBack. Give a control with no text an accessibilityLabel.