nook
Browse the docs

Components

Theme Toggle

Native

Switches between light and dark mode and remembers the choice.

See the prompt
Switch Button

Try it: this switches the whole page, and your choice is remembered.

Installation

npx nook-ui@latest add theme-toggle

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 "Theme Toggle" component to this project, exactly as Nook ships it.

Switches between light and dark mode and remembers the choice.

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 theme-toggle`. It writes `components/ui/theme-toggle.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/theme-toggle.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
"use client";

import { ThemeToggle } from "@/components/ui/theme-toggle";

/** Not controlled: the toggles change the real page theme, so this whole page switches. */
export default function ThemeToggleDefault() {
  return (
    <div className="grid gap-3 text-sm">
      <div className="flex flex-wrap items-center gap-6">
        <span className="flex items-center gap-2">
          <ThemeToggle appearance="switch" /> Switch
        </span>
        <span className="flex items-center gap-2">
          <ThemeToggle /> Button
        </span>
      </div>
      <p className="text-muted-foreground">Try it: this switches the whole page, and your choice is remembered.</p>
    </div>
  );
}
```

API
- `ThemeToggle`: The control. Uncontrolled, it sets the `dark` class on the page and saves the choice.
  - `appearance` (`"button" | "switch"`, default "button"): One icon, or both sides with a sliding thumb.
  - `dark` (`boolean`): Controlled state.
  - `onDarkChange` (`(dark: boolean) => void`): Called when it is pressed.
- `useTheme, THEME_SCRIPT`: A hook to read and set the theme, and a script for the document head that applies the saved theme before the first paint.

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: The button says what pressing it will do; the switch reports whether dark mode is on. It follows the system setting until the person chooses.

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

Usage

"use client";

import { ThemeToggle } from "@/components/ui/theme-toggle";

/** Not controlled: the toggles change the real page theme, so this whole page switches. */
export default function ThemeToggleDefault() {
  return (
    <div className="grid gap-3 text-sm">
      <div className="flex flex-wrap items-center gap-6">
        <span className="flex items-center gap-2">
          <ThemeToggle appearance="switch" /> Switch
        </span>
        <span className="flex items-center gap-2">
          <ThemeToggle /> Button
        </span>
      </div>
      <p className="text-muted-foreground">Try it: this switches the whole page, and your choice is remembered.</p>
    </div>
  );
}

Examples

In a header

Nook docs

The same toggle you see in this site's header. This switches the whole page.

Light, dark or system

This switches the whole page. System follows your device and updates when it changes.

A setting with a description

Easier on the eyes at night. This switches the whole page.

Props

ThemeToggle

The control. Uncontrolled, it sets the `dark` class on the page and saves the choice.

PropTypeDefaultDescription
appearance"button" | "switch""button"One icon, or both sides with a sliding thumb.
darkboolean–Controlled state.
onDarkChange(dark: boolean) => void–Called when it is pressed.

useTheme, THEME_SCRIPT

A hook to read and set the theme, and a script for the document head that applies the saved theme before the first paint.

Accessibility

The button says what pressing it will do; the switch reports whether dark mode is on. It follows the system setting until the person chooses.

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