nook
Browse the docs

Components

Switch

Built on Base UI

An on or off toggle that takes effect immediately.

See the prompt

Installation

npx nook-ui@latest add switch

Prompt

Paste this into your AI coding assistant. It adds this exact component, the same way the CLI does, and tells the assistant how to use it.

Short. For assistants that can run commands in your project.

Plain text
Add the Nook "Switch" component to this project, exactly as Nook ships it.

An on or off toggle that takes effect immediately.

Steps
1. Check for `nook.json` in the project root. If it is missing, run `npx nook-ui@latest init` first (React or Next.js with Tailwind CSS v4). It adds the Nook tokens, the theme and `cn()`.
2. Run `npx nook-ui@latest add switch`. It writes `components/ui/switch.tsx` together with everything the component needs. Do not write the component from memory.
3. If you cannot run commands, fetch http://localhost:3000/r/switch.json, write every entry of `files` to the path it names, install the packages in `dependencies`, and repeat for each name in `registryDependencies`.
4. Use it where I ask. Start from this example:

```tsx
import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";

export default function SwitchDefault() {
  return (
    <Label>
      <Switch defaultChecked /> Send me weekly summaries
    </Label>
  );
}
```

API
- `Switch`: The switch control.
  - `checked` (`boolean`): Controlled state.
  - `defaultChecked` (`boolean`, default false): Initial state when uncontrolled.
  - `onCheckedChange` (`(checked: boolean) => void`): Called when toggled.
  - `disabled` (`boolean`, default false): Prevents interaction.
  - `name` (`string`): Name submitted with a form.
- Variant `size`: sm, md, lg (default md)

Rules
- Colors, radii, shadows and motion come from Nook tokens and classes such as `bg-primary`, `text-muted-foreground`, `border-border`, `rounded-lg` and `h-control`. Never hard-code them.
- Do not restyle or simplify the file. Change how it looks through `className`, its props and the theme.
- It must keep working with the keyboard, in light and dark mode, and in the sharp, soft and bold styles.
- Accessibility: Renders role=switch. Space toggles. Label it so its purpose is announced.

Done when the component renders like the example in light and dark mode and the project type-checks.

Usage

import { Label } from "@/components/ui/label";
import { Switch } from "@/components/ui/switch";

export default function SwitchDefault() {
  return (
    <Label>
      <Switch defaultChecked /> Send me weekly summaries
    </Label>
  );
}

Examples

Settings list

Each row names the setting and says what it does.

  • A Monday summary of what changed.

  • Email me when someone tags me.

  • Occasional updates, never more than monthly.

On, off and disabled

Saves when flipped

Say what happened, since the switch alone does not.

Your site is live.

Label on the left

For forms where every row lines up on the right.

Sizes

Small, default and large.

Variants

Defined in one place with cva, at the top of the component file.

  • size

    smmd (default)lg

Props

Switch

The switch control.

PropTypeDefaultDescription
checkedboolean–Controlled state.
defaultCheckedbooleanfalseInitial state when uncontrolled.
onCheckedChange(checked: boolean) => void–Called when toggled.
disabledbooleanfalsePrevents interaction.
namestring–Name submitted with a form.

Accessibility

Renders role=switch. Space toggles. Label it so its purpose is announced.

Checked against WCAG 2.1 AA with automated axe tests and keyboard tests on every release.