clickworks
Works / Theme

Burn Switch

The switch is a small firebox with a real flame in it. Flip it and fire catches at the switch and runs across the page, leaving your dark theme behind it. Flip it back and the light opens out again. It works with any site that themes with a data attribute or a dark class.

Free8 files0 dependencies9.7 kB gzippedRenders on the serverKeyboard and screen reader

Install

npx shadcn@latest add https://clickworks.dev/r/burn-switch.json

The files land in components/clickworks/burn-switch/. No other packages are added.

Usage

import { BurnSwitch } from "@/components/clickworks/burn-switch/react/BurnSwitch"

export function ThemeToggle() {
  return <BurnSwitch storageKey="theme" />
}

Props

theme
"light" | "dark"

Start in this theme. Leave it out to read it from the page or the saved choice.

target
string

Selector of the element that carries the theme.

Default "html"

attribute
string

Attribute set on the target. "class" toggles a dark class instead.

Default "data-theme"

storageKey
string

Remembers the choice in localStorage under this key.

duration
number

Milliseconds for the fire to cross the page.

Default 2600

sound
boolean

Synthesized fire sounds.

Default false

size
number

Width in px. Everything scales with it.

Default 116

label
string

Accessible name of the switch.

Default "Dark mode"

Events

onThemeChange
(theme: "light" | "dark") => void

The new theme is fully in place.

Styling

The burn reads your page's own colours, so the char melts into whatever background your dark theme paints. Nothing to configure.

--burn-switch-focusthe focus ring colour

Behaviour and accessibility

  • A real button with role switch: Space and Enter flip it, and screen readers hear the state at once.
  • With reduced motion the theme switches at once and the flame stops moving.
  • Browsers without View Transitions switch without the page burn. The switch still animates.
  • The flame only runs while the switch is on screen and the tab is visible.

Files

react/BurnSwitch.tsxReact component
element/index.tsCustom element
element/burn-switch-element.tsCustom element
element/burn.tsCustom element
element/sound.tsCustom element
core/engine.tsEngine
core/shaders.tsEngine
core/styles.tsEngine