A button that switches light and dark and reveals the new theme with a circle growing from where you clicked, a wipe, or a fade. The icon turns from sun to moon. Without View Transitions, or with reduced motion, the theme changes at once.
On this page
- Light and dark switch
- App header control
- Docs site theme
- Settings page theme
Examples
With system
Icon
No flash on load
Installation
npx knowns-ui@latest add theme-togglePaste this into your coding agent (Claude Code, Cursor, Codex). It checks your project, sets up the registry, runs the install and verifies it.
Install Theme toggle from Knowns UI into this project. Work step by step, show me each change, and ask before you edit files outside this project.
Context: Knowns UI (https://ui.knowns.dev) is a shadcn registry. Items land in the project as source files. Docs: https://ui.knowns.dev/docs/theme-toggle
1. Check the project first. Stop and tell me if any of these is missing:
- React 19 and Tailwind CSS v4.
- A components.json at the project root. If there is none, ask me before running `npx knowns-ui@latest init --no-key` (it runs shadcn init when needed, then adds the registries).
2. Run:
```bash
npx knowns-ui@latest add theme-toggle
```
It writes these files:
- lib/knowns/run.ts
- lib/knowns/json.tsx
- lib/knowns/status.ts
- lib/knowns/tone.ts
- lib/knowns/types.ts
- lib/knowns/use-abortable.ts
- lib/knowns/utils.ts
- components/ui/knowns/spinner.tsx
- components/ui/knowns/button.tsx
- lib/knowns/motion.tsx
- lib/knowns/motion-utils.ts
- components/ui/knowns/theme-toggle.tsx
and installs these npm packages: clsx@^2.1.1, tailwind-merge@^3.7.0, class-variance-authority@^0.7.1.
3. While it runs:
- It also adds a few @keyframes (all named kn-*) to the global CSS file (globals.css or the file components.json points at). Let it: they only add animations.
- If it asks to overwrite files under lib/knowns/: these are shared by every Knowns UI item. List them, say whether I changed them (check git), and ask me before you answer.
- If it asks to overwrite any other file, show me the diff and ask me.
4. Verify:
- Every file in the list above exists.
- The project type-checks (for example `npx tsc --noEmit`) and the dev build has no new errors.
5. Use it: follow the examples and props on https://ui.knowns.dev/docs/theme-toggle, and ask me if anything about my data is unclear.
If the Knowns UI MCP server is connected (https://ui.knowns.dev/docs/ai-and-mcp), use get_component({ name: "theme-toggle" }) or search_docs for details.
Finish with a short summary: the files you added or changed, the packages you installed, and anything I still need to do.Props
- theme / onThemeChange
"light"|"dark"|"system"- Controlled. onThemeChange runs inside the transition: change the theme synchronously there.
- defaultTheme
Theme- Default
"system" - Uncontrolled start before anything is saved.
- storageKey / darkClass
string- Default
"theme" / "dark" - Uncontrolled: where it saves, and the class it sets on <html>.
- variant
"circle"|"wipe"|"fade"- Default
"circle" - Circle grows from the click (the button centre for keyboard); the wipe runs away from the button's side.
- duration
number- Default
450 - Reveal length in ms. A one-shot page reveal, so it runs longer than the motion tokens.
- cycle
boolean- Default
false - Light, dark, then system.
- size
"sm"|"md"|"lg"- Default
"md" - Square ghost button sizes; taller on touch screens.
| Prop | Type | Default | Description |
|---|---|---|---|
| theme / onThemeChange | "light" | "dark" | "system" | - | Controlled. onThemeChange runs inside the transition: change the theme synchronously there. |
| defaultTheme | Theme | "system" | Uncontrolled start before anything is saved. |
| storageKey / darkClass | string | "theme" / "dark" | Uncontrolled: where it saves, and the class it sets on <html>. |
| variant | "circle" | "wipe" | "fade" | "circle" | Circle grows from the click (the button centre for keyboard); the wipe runs away from the button's side. |
| duration | number | 450 | Reveal length in ms. A one-shot page reveal, so it runs longer than the motion tokens. |
| cycle | boolean | false | Light, dark, then system. |
| size | "sm" | "md" | "lg" | "md" | Square ghost button sizes; taller on touch screens. |
- transitionTheme(apply, { variant?, origin?, duration? })
Promise<'instant' | 'animated'>- The reveal on its own, for menus and shortcuts: apply must change the theme synchronously.
- originFromElement(el) / originFromEvent(e)
{ x, y }- Where the reveal starts.
- themeScript(storageKey?, darkClass?)
string- The inline no-flash script.
| Prop | Type | Default | Description |
|---|---|---|---|
| transitionTheme(apply, { variant?, origin?, duration? }) | Promise<'instant' | 'animated'> | - | The reveal on its own, for menus and shortcuts: apply must change the theme synchronously. |
| originFromElement(el) / originFromEvent(e) | { x, y } | - | Where the reveal starts. |
| themeScript(storageKey?, darkClass?) | string | - | The inline no-flash script. |
What it handles
- 01Reveals the new theme with a View Transition as a circle that grows from the click point, a wipe, or a fade, chosen with the variant prop.
- 02Starts the circle from the centre of the button when the click comes from the keyboard, where the pointer position would report 0,0.
- 03Keeps the choice in localStorage under storageKey and toggles the dark class on html in uncontrolled mode, and follows the system setting while on system.
- 04Becomes controlled with theme and onThemeChange, where the handler runs inside the transition and must change the theme synchronously.
- 05Exports themeScript for an inline head script so a saved dark theme never flashes light, and transitionTheme for menus and shortcuts.
- 06Applies the theme at once when the browser has no View Transitions or the user prefers reduced motion, and cycles light, dark, system with the cycle prop.
States
- First render
- Nothing. The icon paints for the resolved theme and the label reads Toggle theme until mounted
- none
- Icon morph
- The sun rays turn away and shrink while the core grows and a mask cuts the crescent
- 280ms, ease out
- Reveal
- The new theme is clipped in from the click point, from a side, or by opacity for the length of the duration prop
- none
- System marker
- A small dot appears at the corner while the theme is system
- none, at once
- No View Transitions
- The theme changes at once
- none, at once
- Reduced motion
- The reveal and the icon morph are skipped
- 0ms
| State | What moves | Timing |
|---|---|---|
| First render | Nothing. The icon paints for the resolved theme and the label reads Toggle theme until mounted | none |
| Icon morph | The sun rays turn away and shrink while the core grows and a mask cuts the crescent | 280ms, ease out |
| Reveal | The new theme is clipped in from the click point, from a side, or by opacity for the length of the duration prop | none |
| System marker | A small dot appears at the corner while the theme is system | none, at once |
| No View Transitions | The theme changes at once | none, at once |
| Reduced motion | The reveal and the icon morph are skipped | 0ms |
Works with
- ButtonInstalled with it. The toggle is a square ghost Button, so it shares sizes with the buttons beside it.
- User menuPairs with it: the account panel has its own Light, Dark and System switch for the same setting.
- Segmented controlPairs with it: use three segments in settings when you want the choice visible instead of a cycle.
When to use
When to use
- The light and dark switch in a site or app header.
- Cycling light, dark and system where there is no room for a menu.
When not to use
- Theme as one of several account settings in a menu: use User Menu.
- Choosing between several named themes: use Segmented Control.
Using with AI
Use Copy page for AI above to give your assistant this page, Copy prompt under the install command to have it installed and wired to your data, or connect the Knowns UI MCP server and just ask:
- Add the Knowns UI Theme Toggle to my app and wire it to my own data.
- I need the light and dark switch in a site or app header. Set that up with the Knowns UI Theme Toggle.
- Which Theme Toggle props do I need for a minimal setup? Show me a short example.
With the server connected, your assistant calls get_component({ name: "theme-toggle" }) and search_docs({ query: "theme toggle" }).