DocsComponentsTemplatesThemesGitHub

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.json

Guidelines

  • Use for instant settings; use a Checkbox inside forms that are submitted.
  • Give it a label; showLabel renders the label beside it.
  • error shows a message under it and sets aria-invalid; with useForm use {...f.field(name, 'checked')}.

Props

PropTypeRequired
checkedbooleanNo
defaultCheckedbooleanNo
onChange(checked: boolean) => voidNo
onBlur() => voidNo
labelstringNo
showLabelbooleanNo
hintReact.ReactNodeNo
errorReact.ReactNodeNo
requiredbooleanNo
namestringNo
disabledbooleanNo
idstringNo
classNamestringNo

Edit this example on GitHub