Switch
On/off switch for settings that apply immediately; supports required, hint and error.
Code
import * as React from 'react';
import { Switch } from '@fruit-labs/design';
export default function Demo() {
return (
<div
style={{
display: 'flex',
flexDirection: 'column',
gap: 16,
}}
>
<div
style={{
display: 'flex',
gap: 24,
alignItems: 'center',
}}
>
<Switch defaultChecked label="Wi-Fi" />
<Switch label="Bluetooth" />
</div>
<Switch label="Email me product updates" showLabel hint="About once a month." />
<Switch
label="Enable two-factor sign-in"
showLabel
required
error="Turn on two-factor to continue."
/>
</div>
);
}Disabled
import { Switch } from '@fruit-labs/design';
export default function Example() {
return (
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 12, alignItems: 'center' }}>
<Switch label="Backups (managed by your admin)" showLabel disabled defaultChecked />
</div>
);
}Install
From npm:
npm install @fruit-labs/design
import { Switch } from '@fruit-labs/design';Or copy the TypeScript source into your project:
npx shadcn@latest add https://design.fruitlabs.online/r/switch.jsonGuidelines
- Use for instant settings; use a Checkbox inside forms that are submitted.
- Give it a
label;showLabelrenders the label beside it. errorshows a message under it and sets aria-invalid; with useForm use{...f.field(name, 'checked')}.
Props
| Prop | Type | Required |
|---|---|---|
checked | boolean | No |
defaultChecked | boolean | No |
onChange | (checked: boolean) => void | No |
onBlur | () => void | No |
label | string | No |
showLabel | boolean | No |
hint | React.ReactNode | No |
error | React.ReactNode | No |
required | boolean | No |
name | string | No |
disabled | boolean | No |
id | string | No |
className | string | No |