# Switch

- **[Atoms](/components/)**

An on/off control for settings that apply right away. Built on Radix Switch.

## When to use

- **Use for**: A setting that takes effect immediately.
- **Not for**: Choices that wait for a Save button. Use a checkbox.
- **Pairs with**: Settings lists, a visible label.

## Preview

switch.tsx

```
import { Theme } from '@/components/klarhimmel/theme/style';
import { Switch } from '@/components/klarhimmel/ui/switch';

export default function Example() {
  return (
    <Theme theme="brand">
      <label className="flex items-center gap-3">
        <Switch defaultChecked /> Email me when something ships
      </label>
    </Theme>
  );
}
```

## Import

- **CLI**

  ```
  npx shadcn add @klarhimmel/switch
  ```
- **Import**

  ```
  import { Switch } from '@/components/klarhimmel/ui/switch';
  ```

- **Files**: switch.tsx, in components/klarhimmel/ui/. With it: theme, utils.
- **Render in**: Render it inside <Theme>, once near the root of your app. Tokens and fonts are scoped to it, so outside it the component is unstyled.

## Examples

- **Off, and disabled**

  ```
  import { useId } from 'react';
  import { Switch } from '@/components/klarhimmel/ui/switch';
  
  export default function SwitchStates() {
    const a = useId();
    const b = useId();
    const c = useId();
    return (
      <div className="flex flex-col gap-3">
        <div className="flex items-center gap-3"><Switch id={a} /><label htmlFor={a} className="text-kh-small">Off</label></div>
        <div className="flex items-center gap-3"><Switch id={b} defaultChecked disabled /><label htmlFor={b} className="text-kh-small">On, disabled (managed by your plan)</label></div>
        <div className="flex items-center gap-3"><Switch id={c} disabled /><label htmlFor={c} className="text-kh-small">Off, disabled</label></div>
      </div>
    );
  }
  ```

## Props

- **name**
- **defaultChecked**: Controlled or uncontrolled state.
- **checked**: Controlled or uncontrolled state.
- **onCheckedChange**: Called when the state changes.
- **value**
- **required**
- **native attributes**: Every native attribute of the element passes through, className included.

## Accessibility

- Space · Turns it on or off
- Tab · Moves focus

[Accessibility](/get-started/accessibility)

[Previous](/components/textarea)

[Next](/components/settings-group)
