Daylight switch Free
Free forever. One command, source you own.
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")} />Use with an agent Beta
npx shadcn@latest add https://cottoncandy.dev/r/uisdk/daylight-switch.jsonOptional. An AI agent in your app can read and set this piece by name, and you can ask before anything it can’t undo. The piece itself doesn’t change. Built on UI SDK 0.1.0-beta.23, which is in beta too, so this add-on follows its releases.
Source
How it works
Building with an agent? Hand it a ready prompt for this piece.
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, wherever focus is) 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 (half of it while the knob moves), the body from geometry baked once (at 2/3 of the density while it moves), 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 70 ms a second while it travels (90 before the moving sky went to half its grid), and a drag about 89, of which its own script is about 36 and the same pointer moves over a still item cost about 45 (an A/B on one machine with a graphics card). Both are over the 60 ms a second an active control may take, for as long as the knob moves: an owner-approved exception (27 Nov) that keeps the moon's lit edge crisp.
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. Under 44 its hit area still reaches 44px tall |
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.
A dark-mode toggle with a sky inside. Slide the moon across and the day comes up with it; hold it halfway for dusk. Stars and a rare shooting star by night, three soft clouds drifting by day.
For dark mode and theme togglesMore in Controls & forms