Theme toggle
Theme toggle is a neobrutalist React component from Cruise UI. The dark mode control: a button that switches light and dark (default), or variant="menu": a square icon button (sun, moon or monitor) opening a Light / Dark / System menu. It switches .nepa-mode on <html> and saves the choice so CruiseThemeScript restores it on the next visit; System follows the device setting, live. Install it with npx shadcn@latest add @cruiseui/theme-toggle.
Install
Terminal
npx shadcn@latest add @cruiseui/theme-toggleFirst time? Installation shows the one-off components.json setup (the @cruiseui registry) and how to turn the theme on.
Example
Menu
Button
"use client";
import { CruiseThemeToggle } from "@/components/cruise/theme-toggle";
export default function ThemeToggleExample() {
return (
<div className="flex flex-col gap-6">
{/* variant="menu": an icon button (sun, moon or monitor for the current
choice) with Light / Dark / System. System follows the device, live. */}
<div className="flex flex-col gap-2">
<p className="field-label">Menu</p>
<div className="flex items-center gap-3">
<CruiseThemeToggle variant="menu" align="start" />
</div>
</div>
{/* variant="button" (default): one button that switches light and dark. */}
<div className="flex flex-col gap-2">
<p className="field-label">Button</p>
<div className="flex flex-wrap items-center gap-4">
{/* Saves the choice; CruiseThemeScript in the root layout restores it on the next visit. */}
<CruiseThemeToggle />
<CruiseThemeToggle iconOnly />
<CruiseThemeToggle buttonVariant="ghost" labels={{ dark: "NEPA mode", light: "Light mode" }} />
</div>
</div>
</div>
);
}
<button class="brutal btn btn-secondary" type="button" aria-label="Toggle dark mode" aria-pressed="false"
onclick="this.setAttribute('aria-pressed', document.documentElement.classList.toggle('nepa-mode'))">
<!-- label: "NEPA mode" in Pidgin mode -->
<span>Dark mode</span>
</button>Needs theme.css. Interactive parts (menus, tabs, drawers) also need the script in the spec for that component.
When to use it
When to use
A site header or settings page that offers dark mode.
When not to
Inside the Dashboard layout (it has the same toggle built in).
Props
CruiseThemeToggle
| Prop | Type | Default | Description |
|---|---|---|---|
variant? | CruiseButtonVariant | ToggleVariant | "button" | "button" (default): one toggle button. "menu": an icon button with a Light / Dark / System menu. (A button style here, e.g. "ghost", still works as before and means "button" with that style.) |
buttonVariant? | CruiseButtonVariant | — | The button's style ("secondary" by default), in both variants. |
labels? | { dark: string; light: string; } | { dark: "Dark mode", light: "Light mode" } | Button variant. dark: the label while it's light ("Dark mode", or "NEPA mode" in Pidgin mode); light: the label while it's dark. |
menuLabels? | { trigger: string; light: string; dark: string; system: string; } | { trigger: "Theme", light: "Light", dark: "Dark", system: "System" } | Menu variant: the trigger's name and tooltip, and the three options. |
iconOnly? | boolean | false | Button variant: a 44px square icon button (the aria-label still names it). |
align? | "start" | "end" | "end" | Menu variant: which edge the menu lines up with (default "end", for a header's right side). |
storageKey? | string | CRUISE_THEME_KEY | Must match CruiseThemeScript's. |
onChange? | ((on: boolean) => void) | — | Called with whether dark mode is on, after it's applied and saved. |
className? | string | — | — |
Usage
- Put CruiseThemeScript in the root layout's <head>, then CruiseThemeToggle wherever the button goes. Every toggle on the page stays in step.
- `variant="menu"`: a 44px square icon button showing the current choice (sun = light, moon = dark, monitor = system), named "Theme" with a "Theme" tooltip. It opens a Dropdown menu with Light, Dark and System as radio items (the chosen one has a check); arrows, Enter and Esc work. System follows prefers-color-scheme and updates live when the device switches. `align` (default "end") lines the menu up with the trigger; `menuLabels` changes the words.
- `buttonVariant` sets the button style in both variants (default "secondary"). Older code that passed a style as `variant` (e.g. variant="ghost") still works.
- `labels` changes the text, e.g. { dark: "NEPA mode", light: "Light mode" } in Pidgin mode. `iconOnly` makes a 44px square icon button.
- `useCruiseDarkMode()` tells a client component whether dark mode is on; `useCruiseThemeChoice()` gives the saved choice ("light", "dark" or "system"); `setCruiseTheme(choice)` and `setCruiseDarkMode(on)` switch and save it from your own code.
Accessibility
- Button: the aria-label is always "Toggle dark mode" (spec rule 11) and aria-pressed says whether it's on.
- Menu: the trigger is named "Theme"; the options are menuitemradio with aria-checked, so the current choice is announced.
- Icons are hidden from screen readers.