Skip to content

Theme Toggle

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
Used for
  • Light and dark switch
  • App header control
  • Docs site theme
  • Settings page theme

Examples

Reveal
Speed

With system

Icon

No flash on load

Installation

npx knowns-ui@latest add theme-toggle
For Claude Code, Cursor or Codex: installs it and wires the props to your own data.

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
Default450
Reveal length in ms. A one-shot page reveal, so it runs longer than the motion tokens.
cycle
boolean
Defaultfalse
Light, dark, then system.
size
"sm" | "md" | "lg"
Default"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.

What it handles

  1. 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.
  2. 02Starts the circle from the centre of the button when the click comes from the keyboard, where the pointer position would report 0,0.
  3. 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.
  4. 04Becomes controlled with theme and onThemeChange, where the handler runs inside the transition and must change the theme synchronously.
  5. 05Exports themeScript for an inline head script so a saved dark theme never flashes light, and transitionTheme for menus and shortcuts.
  6. 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

Works with

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

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" }).

Need help?

Stuck on Theme Toggle, or found something off? We read every message.