Swarm
const css = `
@keyframes toggle-swarm-drift { to { transform: translate(var(--dx), var(--dy)); } }
.toggle-swarm { position: relative; display: inline-flex; align-items: center; gap: 12px; color: light-dark(#42454c, #e5e6e9); font-size: 14px; cursor: pointer; user-select: none; }
.toggle-swarm__input { position: absolute; width: 1px; height: 1px; margin: 0; opacity: 0; }
.toggle-swarm__track {
position: relative;
width: 84px;
height: 32px;
border-radius: 999px;
background: light-dark(#f3f4f7, #1a1c1f);
box-shadow: inset 0 0 0 1px light-dark(#dbdde1, #2e3035);
}
.toggle-swarm__input:focus-visible + .toggle-swarm__track { outline: 2px solid light-dark(#5f626a, #a7abb2); outline-offset: 3px; }
.toggle-swarm__fly {
position: absolute;
left: -2px;
top: -2px;
width: 4px;
height: 4px;
transform: translate(var(--x0), var(--y0));
transition: transform 0.9s cubic-bezier(0.5, 0, 0.2, 1) var(--d);
}
.toggle-swarm__fly i {
display: block;
width: 4px;
height: 4px;
border-radius: 50%;
background: light-dark(#6d7179, #7d8087);
animation: toggle-swarm-drift 1.6s ease-in-out var(--d) infinite alternate;
transition: background-color 0.4s, box-shadow 0.4s;
}
/* On: they fly over and gather into the thumb, glowing. */
.toggle-swarm__input:checked + .toggle-swarm__track .toggle-swarm__fly { transform: translate(var(--x1), var(--y1)); }
.toggle-swarm__input:checked + .toggle-swarm__track .toggle-swarm__fly i { background: light-dark(#373a40, #f1f2f3); box-shadow: 0 0 5px 1px color-mix(in oklab, light-dark(#373a40, #f1f2f3) 60%, transparent); transition-delay: 0.5s; }
@media (prefers-reduced-motion: reduce) {
.toggle-swarm__fly { transition-duration: 0.01s; transition-delay: 0s; }
.toggle-swarm__fly i { animation: none; }
}
`;
const SCATTER = [
[8, 9], [14, 21], [20, 12], [24, 25], [29, 7], [33, 17], [38, 26], [42, 10], [12, 15], [27, 19], [36, 5], [46, 20],
];
// Gathered positions: a small ring round the on-side centre, with a few inside it.
const FLIES = SCATTER.map(([x, y], i) => {
const a = (i / SCATTER.length) * Math.PI * 2;
const r = i % 3 === 0 ? 3 : 8;
return {
x0: x,
y0: y,
x1: 66 + Math.cos(a) * r,
y1: 16 + Math.sin(a) * r,
d: ((i * 7) % 12) * 0.035,
dx: (((i * 5) % 7) - 3) * 0.9,
dy: (((i * 3) % 7) - 3) * 0.9,
};
});
export function SwarmToggle() {
return (
<>
<style>
{css}
</style>
<label className="toggle-swarm">
<input className="toggle-swarm__input" type="checkbox" role="switch" />
<span className="toggle-swarm__track" aria-hidden="true">
{FLIES.map((f, i) => (
<span
key={i}
className="toggle-swarm__fly"
style={
{
'--x0': `${f.x0}px`,
'--y0': `${f.y0}px`,
'--x1': `${f.x1.toFixed(1)}px`,
'--y1': `${f.y1.toFixed(1)}px`,
'--d': `${f.d.toFixed(2)}s`,
'--dx': `${f.dx.toFixed(1)}px`,
'--dy': `${f.dy.toFixed(1)}px`,
} as React.CSSProperties
}
>
<i />
</span>
))}
</span>
Night lights
</label>
</>
);
}Markup where the component goes, CSS in your stylesheet. No JavaScript.
<label class="toggle-swarm"><input class="toggle-swarm__input" type="checkbox" role="switch"/><span class="toggle-swarm__track" aria-hidden="true"><span class="toggle-swarm__fly" style="--x0:8px;--y0:9px;--x1:69.0px;--y1:16.0px;--d:0.00s;--dx:-2.7px;--dy:-2.7px"><i></i></span><span class="toggle-swarm__fly" style="--x0:14px;--y0:21px;--x1:72.9px;--y1:20.0px;--d:0.25s;--dx:1.8px;--dy:0.0px"><i></i></span><span class="toggle-swarm__fly" style="--x0:20px;--y0:12px;--x1:70.0px;--y1:22.9px;--d:0.07s;--dx:0.0px;--dy:2.7px"><i></i></span><span class="toggle-swarm__fly" style="--x0:24px;--y0:25px;--x1:66.0px;--y1:19.0px;--d:0.32s;--dx:-1.8px;--dy:-0.9px"><i></i></span><span class="toggle-swarm__fly" style="--x0:29px;--y0:7px;--x1:62.0px;--y1:22.9px;--d:0.14s;--dx:2.7px;--dy:1.8px"><i></i></span><span class="toggle-swarm__fly" style="--x0:33px;--y0:17px;--x1:59.1px;--y1:20.0px;--d:0.39s;--dx:0.9px;--dy:-1.8px"><i></i></span><span class="toggle-swarm__fly" style="--x0:38px;--y0:26px;--x1:63.0px;--y1:16.0px;--d:0.21s;--dx:-0.9px;--dy:0.9px"><i></i></span><span class="toggle-swarm__fly" style="--x0:42px;--y0:10px;--x1:59.1px;--y1:12.0px;--d:0.04s;--dx:-2.7px;--dy:-2.7px"><i></i></span><span class="toggle-swarm__fly" style="--x0:12px;--y0:15px;--x1:62.0px;--y1:9.1px;--d:0.28s;--dx:1.8px;--dy:0.0px"><i></i></span><span class="toggle-swarm__fly" style="--x0:27px;--y0:19px;--x1:66.0px;--y1:13.0px;--d:0.11s;--dx:0.0px;--dy:2.7px"><i></i></span><span class="toggle-swarm__fly" style="--x0:36px;--y0:5px;--x1:70.0px;--y1:9.1px;--d:0.35s;--dx:-1.8px;--dy:-0.9px"><i></i></span><span class="toggle-swarm__fly" style="--x0:46px;--y0:20px;--x1:72.9px;--y1:12.0px;--d:0.18s;--dx:2.7px;--dy:1.8px"><i></i></span></span>Night lights</label>@keyframes toggle-swarm-drift { to { transform: translate(var(--dx), var(--dy)); } }
.toggle-swarm { position: relative; display: inline-flex; align-items: center; gap: 12px; color: light-dark(#42454c, #e5e6e9); font-size: 14px; cursor: pointer; user-select: none; }
.toggle-swarm__input { position: absolute; width: 1px; height: 1px; margin: 0; opacity: 0; }
.toggle-swarm__track {
position: relative;
width: 84px;
height: 32px;
border-radius: 999px;
background: light-dark(#f3f4f7, #1a1c1f);
box-shadow: inset 0 0 0 1px light-dark(#dbdde1, #2e3035);
}
.toggle-swarm__input:focus-visible + .toggle-swarm__track { outline: 2px solid light-dark(#5f626a, #a7abb2); outline-offset: 3px; }
.toggle-swarm__fly {
position: absolute;
left: -2px;
top: -2px;
width: 4px;
height: 4px;
transform: translate(var(--x0), var(--y0));
transition: transform 0.9s cubic-bezier(0.5, 0, 0.2, 1) var(--d);
}
.toggle-swarm__fly i {
display: block;
width: 4px;
height: 4px;
border-radius: 50%;
background: light-dark(#6d7179, #7d8087);
animation: toggle-swarm-drift 1.6s ease-in-out var(--d) infinite alternate;
transition: background-color 0.4s, box-shadow 0.4s;
}
/* On: they fly over and gather into the thumb, glowing. */
.toggle-swarm__input:checked + .toggle-swarm__track .toggle-swarm__fly { transform: translate(var(--x1), var(--y1)); }
.toggle-swarm__input:checked + .toggle-swarm__track .toggle-swarm__fly i { background: light-dark(#373a40, #f1f2f3); box-shadow: 0 0 5px 1px color-mix(in oklab, light-dark(#373a40, #f1f2f3) 60%, transparent); transition-delay: 0.5s; }
@media (prefers-reduced-motion: reduce) {
.toggle-swarm__fly { transition-duration: 0.01s; transition-delay: 0s; }
.toggle-swarm__fly i { animation: none; }
}Goal: Create a "Swarm" toggle.
Visual behavior: A night-lights switch: fireflies drift loosely around the track; switching on, they fly over and gather into a glowing ring that becomes the thumb.
Technique:
- A real native checkbox with role="switch" (visually hidden: absolute, 1x1, opacity 0) inside a <label>; every visual is aria-hidden and styled from the input with :checked + sibling selectors.
- Each firefly has two positions in CSS variables: scattered (off) and on a small ring (on). The outer element transitions between them with its own delay.
- An inner dot drifts on an alternate keyframe at all times, so the swarm stays alive in both states; on, the dots brighten and glow.
Accessibility: A native checkbox with role="switch" inside a <label>, so Space toggles it and screen readers announce a switch; the visible label names it and :focus-visible on the input outlines the visual. Under prefers-reduced-motion they move instantly and do not drift.
My stack: {{USER_STACK}}
My styling: {{USER_STYLING}}
My constraints: {{USER_CONSTRAINTS}}
My colors: neutral only. Black, white and grays, no hues. Use the color values above as written, or my project's closest neutral tokens.
Return a single self-contained component. No dependencies.Goal: Create a "Swarm" toggle.
Visual behavior: A night-lights switch: fireflies drift loosely around the track; switching on, they fly over and gather into a glowing ring that becomes the thumb.
Technique:
- A real native checkbox with role="switch" (visually hidden: absolute, 1x1, opacity 0) inside a <label>; every visual is aria-hidden and styled from the input with :checked + sibling selectors.
- Each firefly has two positions in CSS variables: scattered (off) and on a small ring (on). The outer element transitions between them with its own delay.
- An inner dot drifts on an alternate keyframe at all times, so the swarm stays alive in both states; on, the dots brighten and glow.
Accessibility: A native checkbox with role="switch" inside a <label>, so Space toggles it and screen readers announce a switch; the visible label names it and :focus-visible on the input outlines the visual. Under prefers-reduced-motion they move instantly and do not drift.
My stack: {{USER_STACK}}
My styling: {{USER_STYLING}}
My constraints: {{USER_CONSTRAINTS}}
My colors: use my project's existing color tokens. The color values above are grayscale placeholders. Map each one by its role (surface, text, border, accent, glow) to my theme, and keep their relative lightness so contrast and the effect still read.
Return a single self-contained component. No dependencies.Paste into Claude, ChatGPT or Cursor after filling in {{USER_STACK}}, {{USER_STYLING}} and {{USER_CONSTRAINTS}}. Neutral keeps the greys you see here; Match my project asks the model to map them onto your own colour tokens.
Free to use in any project, commercial included. MIT-0, no attribution required. Licence
About this component
A night-lights switch: fireflies drift loosely around the track; switching on, they fly over and gather into a glowing ring that becomes the thumb.
- Dependencies
- None
- Difficulty
- Intermediate
- Triggers
- click, state change, glow, delay stagger
- Techniques
- css variables, css transition, keyframes
- Reduced motion
- Respected
- Theme
- Light and dark (follows your page's color-scheme)
A native checkbox with role="switch" inside a <label>, so Space toggles it and screen readers announce a switch; the visible label names it and :focus-visible on the input outlines the visual. Under prefers-reduced-motion they move instantly and do not drift.