01 · press · controls
Daylight switch Free
Slide the moon across and the day comes up with it: one lit sphere in a glass well, its terminator sweeping from moon to sun, with stars and a rare shooting star by night and sunlit clouds by day. Hold it halfway for dusk.
Install
npx shadcn@latest add https://cottoncandy.dev/r/daylight-switch.jsonFree. Lands in components/mu/daylight-switch/ with the shared foundation. Needs React 19, Tailwind v4 and motion; reads your shadcn theme, and dark mode follows .dark.
Usage
import { DaylightSwitch } from "@/components/mu/daylight-switch/DaylightSwitch";
<DaylightSwitch label="Light mode" onCheckedChange={(day) => setTheme(day ? "light" : "dark")} />How it works
Input and reduced motion
- Pointer / touch: press to flip it, or drag the knob. The state changes as the knob crosses the middle, so the page's theme follows your finger; stop anywhere to see that time of day, and when you let go it carries on to that side at the speed you threw it. A vertical swipe that starts on the switch still scrolls the page, and a cancelled drag (the page scrolled, or Escape) puts it back as it was.
- Hover (mouse): the knob leans a couple of pixels toward the side it would go.
- Keyboard: Tab to focus, Space or Enter to flip.
- Reduced motion: it changes instantly, the knob doesn't squeeze or lean, and the scene is still: no glint, no shooting star, no drifting clouds.
- Ambient loops (the glint and shooting star by night, the cloud drift by day) only run while the switch is at rest and on screen; a press, a drag or the run between sides stops them at once, and one sparkle shows at a time.
Cost
The scene is painted per pixel only when the light changes, at most once a frame and never at rest: the sky on a coarse grid, the body from geometry baked once, the stars and clouds from sprites baked once (the clouds drift on the compositor). At a 4× slower CPU on a 3× phone: about 4 ms a second at rest by night, under 1 by day, 0 off-screen; the run between sides is the busy part, about 84–123 ms a second while it travels.
Props
| Prop | Default | What it does |
|---|---|---|
checked | — | true / false | controlled state: true is day |
defaultChecked | false | true / false | starting state when uncontrolled |
onCheckedChange | — | — | called with true for day, false for night |
size | 56 | any px; reads from 44 | height in px, rounded to a whole px; the switch is twice as wide |
label | "Daylight" | — | accessible name |
disabled | false | true / false | dims it and ignores input |
sound | off | true or 0–1 | click as it flips (see Sound) |
className | — | — | added to the root |
Sound
Off by default. With sound (or a volume, sound={0.5}) the switch clicks where it flips, a shade darker going to night: on a press, a key, or under your finger as a drag crosses the middle. Never on hover, and never when checked changes from your code. setMuted(true) from components/mu/sounds.ts silences every cottoncandy item at once.
Licence
Free to use in your projects, under the terms on the licence.