Components98 / 118
Eclipse Switch
A toggle that turns into a light show. The white knob winds back, springs across and eclipses into a black disc ringed by a neon corona, while liquid neon floods the track, sparks spray, the colours drift through yellow, green, pink and cyan, and it glows onto the surface with a soft reflection. Off, the colour drains back out.
SwitchSwitchToggleToggleWebGL2WebGL2NeonNeonSquash & stretchSquash & stretchGSAPGSAP
Flip the switch
Tweak
LIVELIVECopy
Look
150px
1.00
Usage
Follows your tweaks.
usage.tsx
import { useState } from "react";
import { EclipseSwitch } from "@/components/inreact/eclipse-switch";
export function VividToggle() {
const [vivid, setVivid] = useState(false);
return <EclipseSwitch label="Vivid mode" checked={vivid} onCheckedChange={setVivid} size={150} />;
}Install with the CLI
TS + Tailwind
npx shadcn@latest add https://inreact.vercel.app/r/eclipse-switch.jsonOr copy the source
Install the dependencies, then save this as components/inreact/eclipse-switch.tsx.
terminal
npm install gsapcomponents/inreact/eclipse-switch.tsx
"use client";
import { gsap } from "gsap";
import { useEffect, useRef, useState } from "react";
/**
* Eclipse Switch: a toggle that turns a plain control into a light show.
*
* Off, it is a quiet dark capsule with a soft white knob. Switch it on and the
* knob winds back, then springs across (stretching as it goes, overshooting
* and settling) and eclipses into a black disc ringed by a neon corona, while
* liquid neon floods the track behind it, sparks spray from where it left,
* the colours keep drifting through yellow, green, pink and cyan, and the
* whole thing glows onto the surface around it with a soft reflection below.
* Switch it off and the colour drains back out behind the white knob.
*
* <EclipseSwitch label="Vivid mode" onCheckedChange={setVivid} />
*
* One WebGL2 shader; a proper role="switch" (Space / Enter); reduced motion
* keeps the colours but drops the springs and sparks.
*/
export interface EclipseSwitchProps {
checked?: boolean;
defaultChecked?: boolean;
onCheckedChange?: (on: boolean) => void;
/** Accessible name (read by screen readers; nothing is printed under the switch). */
label?: string;
/** Track width in px (height follows). */
size?: number;
/** How fast the neon drifts, 0 to 2. */
flow?: number;
className?: string;
}
const reduced = () => typeof window !== "undefined" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
const MAXS = 28;
// the neon palette the colour drifts through: warm first (gold, amber, hot red, magenta), then violet and a cool cyan
const NEON = [
[1.0, 0.84, 0.18],
[1.0, 0.5, 0.08],
[1.0, 0.16, 0.24],
[1.0, 0.12, 0.6],
[0.62, 0.3, 1.0],
[0.22, 0.85, 1.0],
];
const neonAt = (t: number) => {
const n = NEON.length, x = (((t % 1) + 1) % 1) * n, i = Math.floor(x), f = x - i;
const s = f * f * (3 - 2 * f), a = NEON[i], b = NEON[(i + 1) % n];
return a.map((v, k) => v + (b[k] - v) * s);
};
const VERT = `#version 300 es
in vec2 aQ;
void main() { gl_Position = vec4(aQ, 0.0, 1.0); }`;
const FRAG = `#version 300 es
precision highp float;
uniform vec2 uRes;
uniform float uDpr, uTime, uOn, uKnobX, uKnobSX, uKnobSY, uHover, uFloor, uFlash, uSlosh, uBulge, uLit;
uniform vec4 uTrack; // centre x, y, half width, half height (css px)
uniform vec4 uSparks[${MAXS}];
out vec4 o;
const vec3 C0 = vec3(1.0, 0.84, 0.18), C1 = vec3(1.0, 0.5, 0.08), C2 = vec3(1.0, 0.16, 0.24), C3 = vec3(1.0, 0.12, 0.6), C4 = vec3(0.62, 0.3, 1.0), C5 = vec3(0.22, 0.85, 1.0);
vec3 neon(float t) {
t = fract(t) * 6.0;
float f = smoothstep(0.0, 1.0, fract(t));
int i = int(t);
vec3 a = i == 0 ? C0 : i == 1 ? C1 : i == 2 ? C2 : i == 3 ? C3 : i == 4 ? C4 : C5;
vec3 b = i == 0 ? C1 : i == 1 ? C2 : i == 2 ? C3 : i == 3 ? C4 : i == 4 ? C5 : C0;
return mix(a, b, f);
}
float hash(vec2 p) { return fract(sin(dot(p, vec2(127.1, 311.7))) * 43758.5453); }
float noise(vec2 p) {
vec2 i = floor(p), f = fract(p);
vec2 u = f * f * (3.0 - 2.0 * f);
return mix(mix(hash(i), hash(i + vec2(1, 0)), u.x), mix(hash(i + vec2(0, 1)), hash(i + vec2(1, 1)), u.x), u.y);
}
float fbm(vec2 p) { float v = 0.0, a = 0.5; for (int i = 0; i < 4; i++) { v += a * noise(p); p = p * 2.03 + 11.7; a *= 0.5; } return v; }
// a smoother version (fewer, broader layers) for the liquid, so the streaks flow instead of crackling
float fbm3(vec2 p) { float v = 0.0, a = 0.55; for (int i = 0; i < 3; i++) { v += a * noise(p); p = p * 1.8 + 7.3; a *= 0.45; } return v / 0.84; }
// the track, bulging a touch on impact (secondary action)
float sdCap(vec2 p) {
float hw = uTrack.z * (1.0 + 0.025 * uBulge), hh = uTrack.w * (1.0 - 0.05 * uBulge);
vec2 q = abs(p - uTrack.xy);
return length(vec2(max(q.x - (hw - hh), 0.0), q.y)) - hh;
}
// premultiplied colour of the switch at a point (css px); glows add light with no alpha
vec4 scene(vec2 p) {
float hh = uTrack.w, t = uTime, kr = hh * 0.82;
float d = sdCap(p);
float inside = 1.0 - smoothstep(-0.7, 0.7, d);
vec2 q = (p - uTrack.xy) / hh;
// the liquid neon: lava streaks in a dark glass tube. Noise warps them; the slosh
// (the knob's momentum) leans and drags them the way it travelled, then lets go.
vec2 fq = q + vec2(-uSlosh * 0.9 * (1.0 - q.y * q.y), 0.0);
float w = fbm3(fq * vec2(0.6, 1.0) + vec2(t * 0.22, -t * 0.12));
float n = fbm3(fq * vec2(0.8, 1.3) + vec2(-t * 0.75, t * 0.1) + w * 1.6);
float bands = sin((fq.x * 1.55 + n * 3.4 - t * 1.4 + uSlosh * 2.0) * 3.14159);
float body = pow(1.0 - abs(bands), 1.6); // the soft body of each streak
float core = pow(1.0 - abs(bands), 7.0); // its hot centre
float hue = n * 0.55 + t * 0.06 + fq.x * 0.05;
vec3 fl = neon(hue) * (0.07 + 0.95 * body * (0.4 + n)) + neon(hue + 0.12) * core * 1.6 + vec3(1.0, 0.95, 0.85) * pow(core, 3.0) * 0.55 * n;
// more light close to the knob, where the flood comes from
float near = exp(-abs(p.x - uKnobX) / (kr * 1.6));
fl += neon(t * 0.08 + 0.05) * near * 0.28;
// tube shading: light from above, deeper toward the bottom
fl *= 0.72 + 0.45 * smoothstep(0.9, -0.9, q.y);
// the flood follows the knob in, and drains behind it on the way out
float fillMask = smoothstep(uKnobX + kr * 0.25, uKnobX - kr * 0.8, p.x + (n - 0.5) * hh * 1.1) * uLit;
vec3 base = vec3(0.045, 0.045, 0.055);
vec3 col = mix(base, base * 0.6 + fl, fillMask);
// glass: a crisp hairline rim lit by what is inside, and a soft reflection along the top
vec3 rimC = mix(vec3(0.42), neon(t * 0.07 + 0.08) * 1.2 + 0.25, uOn);
col += exp(-pow((d + 0.9) / 0.75, 2.0)) * rimC * mix(0.65, 0.9, uOn);
col += exp(-pow((q.y + 0.6) / 0.16, 2.0)) * smoothstep(0.0, -hh * 0.6, d) * (0.05 + 0.07 * uOn);
col *= inside;
float alpha = inside;
// the knob: a soft white sphere that eclipses into a glossy black one
vec2 kp = (p - vec2(uKnobX, uTrack.y)) / vec2(uKnobSX, uKnobSY);
float kl = length(kp), kd = kl - kr * (1.0 + 0.05 * uHover);
float kin = 1.0 - smoothstep(-0.8, 0.8, kd);
vec2 kn = kp / kr;
vec3 nrm = normalize(vec3(kn, sqrt(max(0.0, 1.0 - dot(kn, kn)))));
vec3 L = normalize(vec3(-0.45, -0.6, 0.66));
float lam = max(dot(nrm, L), 0.0);
float spec = pow(max(dot(reflect(-L, nrm), vec3(0.0, 0.0, 1.0)), 0.0), 40.0);
float fres = pow(1.0 - nrm.z, 2.5);
float ang = atan(kp.y, kp.x);
// colour running round the rim, periodic in the angle so it never jumps where the angle wraps
vec3 cc = neon(t * 0.1 + 0.13 * sin(ang + t * 0.8) + 0.05 * sin(ang * 3.0 + t * 1.7));
vec3 white = vec3(0.78) + vec3(0.24) * lam - 0.24 * (1.0 - nrm.z) + spec * 0.3;
// the black sphere picks up the corona in its rim (fresnel) and a small white highlight
vec3 black = vec3(0.02) + vec3(0.05) * pow(lam, 4.0) + spec * 0.55 + cc * fres * 0.75;
// halfway through the eclipse it passes through a lit grey, tinted by the flood behind it
float ec = smoothstep(0.15, 0.85, uOn);
vec3 kc = mix(white, black, ec) + neon(t * 0.08) * sin(ec * 3.14159) * 0.18;
col = mix(col, kc, kin);
alpha = max(alpha, kin);
// the corona: a crisp saturated ring hugging the knob, a wide halo, and a faint tease on hover while off
float ring = exp(-pow((kd - 1.2) / (1.6 + 0.6 * uHover), 2.0));
float halo = exp(-max(kd, 0.0) / (kr * (0.32 + 0.1 * uHover))) * (1.0 - kin);
float lit = uOn + uHover * (1.0 - uOn) * 0.22;
col += cc * (ring * 2.1 + halo * (1.15 + 0.4 * uHover)) * lit;
// the impact: a flash and a ring of light that runs out from the knob
float sr = kr * (1.05 + (1.0 - uFlash) * 1.9);
col += cc * exp(-pow((kl - sr) / (2.0 + (1.0 - uFlash) * 5.0), 2.0)) * uFlash * 1.4;
col += vec3(1.0, 0.92, 0.8) * kin * uFlash * uFlash * 0.25;
// the whole track glows onto its surroundings, warm and wide
float outer = exp(-max(d, 0.0) / (hh * 1.15)) * (1.0 - inside);
col += mix(neon(t * 0.07 + 0.05), cc, 0.35) * outer * 0.55 * uOn;
// sparks: hot cores with a little light around them
for (int i = 0; i < ${MAXS}; i++) {
vec4 s = uSparks[i];
if (s.z <= 0.0) continue;
float r = length(p - s.xy);
col += (neon(s.w) + vec3(0.3)) * s.z * (exp(-r * r / 2.0) * 1.5 + exp(-r / 4.5) * 0.22);
}
return vec4(col, alpha);
}
void main() {
vec2 p = vec2(gl_FragCoord.x, uRes.y - gl_FragCoord.y) / uDpr;
vec4 c = scene(p);
vec2 cs = uRes / uDpr;
// light dies away in a round falloff centred on the switch (never shaped by the canvas's edges)
vec2 rad = vec2(cs.x * 0.5, p.y < uTrack.y ? uTrack.y : cs.y - uTrack.y);
float fadeOut = 1.0 - smoothstep(0.35, 0.96, length((p - uTrack.xy) / rad));
fadeOut *= fadeOut;
c.rgb = mix(c.rgb * fadeOut, c.rgb, c.a);
// the reflection on a glossy floor: the switch mirrored about the floor line, softening with
// distance (wider blur further down), strongest just below the floor, gone well before the edge
float below = p.y - uFloor;
if (below > 0.0) {
vec2 m = vec2(p.x, uFloor - below);
float b = 1.2 + below * 0.09; // blur radius grows with distance
vec4 r = scene(m) * 0.28
+ (scene(m + vec2(b, 0.0)) + scene(m - vec2(b, 0.0))) * 0.2
+ (scene(m + vec2(0.0, b)) + scene(m - vec2(0.0, b))) * 0.16;
float reach = uTrack.w * 3.4; // how far down the reflection carries
float fade = smoothstep(0.0, uTrack.w * 0.9, below) * pow(1.0 - smoothstep(0.0, reach, below), 1.6);
// a glossy dark floor reflects a little less than it receives, and cooler toward the far edge
// the switch itself reflects clearly; the haze around it only faintly, so the glow never doubles into a band
c.rgb += r.rgb * (0.18 + 0.82 * clamp(r.a, 0.0, 1.0)) * fade * 0.4 * (1.0 - c.a) * fadeOut;
}
o = c;
}`;
export function EclipseSwitch({
checked,
defaultChecked = false,
onCheckedChange,
label = "Vivid mode",
size = 120,
flow = 1,
className,
}: EclipseSwitchProps) {
const [inner, setInner] = useState(defaultChecked);
const on = checked ?? inner;
const btnRef = useRef<HTMLButtonElement>(null);
const hostRef = useRef<HTMLDivElement>(null);
const ambRef = useRef<HTMLDivElement>(null);
const api = useRef({ set: (_on: boolean) => {}, hover: (_h: boolean) => {}, press: (_p: boolean) => {} });
const flowRef = useRef(flow);
flowRef.current = flow;
const W = size, H = Math.round(size * 0.44);
const M = Math.round(H * 2.3); // room around for the glow and sparks to fade out completely
const MB = Math.round(H * 3.6); // more room below, for the whole reflection on the floor
useEffect(() => {
const host = hostRef.current;
if (!host) return;
const still = reduced();
const canvas = document.createElement("canvas");
canvas.setAttribute("aria-hidden", "true");
canvas.style.cssText = `position:absolute;left:${-M}px;top:${-M}px;width:${W + 2 * M}px;height:${H + M + MB}px;pointer-events:none;`;
host.appendChild(canvas);
const gl = canvas.getContext("webgl2", { alpha: true, premultipliedAlpha: true, antialias: false });
if (!gl) {
canvas.remove();
host.dataset.fallback = "1";
return;
}
const sh = (type: number, src: string) => {
const s = gl.createShader(type)!;
gl.shaderSource(s, src);
gl.compileShader(s);
if (!gl.getShaderParameter(s, gl.COMPILE_STATUS)) console.error(gl.getShaderInfoLog(s));
return s;
};
const prog = gl.createProgram()!;
const vs = sh(gl.VERTEX_SHADER, VERT), fs = sh(gl.FRAGMENT_SHADER, FRAG);
gl.attachShader(prog, vs);
gl.attachShader(prog, fs);
gl.bindAttribLocation(prog, 0, "aQ");
gl.linkProgram(prog);
gl.useProgram(prog);
const U = Object.fromEntries(["uRes", "uDpr", "uTime", "uOn", "uLit", "uKnobX", "uKnobSX", "uKnobSY", "uHover", "uFloor", "uFlash", "uSlosh", "uBulge", "uTrack", "uSparks"].map((k) => [k, gl.getUniformLocation(prog, k)]));
const vao = gl.createVertexArray();
gl.bindVertexArray(vao);
const vbo = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vbo);
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([-1, -1, 3, -1, -1, 3]), gl.STATIC_DRAW);
gl.enableVertexAttribArray(0);
gl.vertexAttribPointer(0, 2, gl.FLOAT, false, 0, 0);
const dpr = Math.min(2, window.devicePixelRatio || 1);
canvas.width = Math.round((W + 2 * M) * dpr);
canvas.height = Math.round((H + M + MB) * dpr);
const hh = H / 2, cx = M + W / 2, cy = M + H / 2, kr = hh * 0.82, pad = hh;
const xOff = cx - (W / 2 - pad), xOn = cx + (W / 2 - pad);
// state: the knob on a spring, the eclipse (on) and the flood (lit), hover, press squash,
// the wind-up before launch, the impact flash, the track bulge, the neon slosh, sparks
const s = {
x: on ? xOn : xOff, v: 0, t: on ? xOn : xOff, dir: on ? 1 : -1,
on: on ? 1 : 0, lit: on ? 1 : 0, hover: 0, sq: 0, sqv: 0, wind: 0,
flash: 0, bulge: 0, bulgeV: 0, slosh: 0, sloshV: 0, time: 0, armed: false,
};
type Spark = { x: number; y: number; vx: number; vy: number; life: number; max: number; hue: number };
const sparks: Spark[] = [];
const sbuf = new Float32Array(MAXS * 4);
let awake = 3;
let launch: gsap.core.Timeline | null = null;
const spray = (x: number, dirx: number, count: number, warm: boolean, power = 1) => {
for (let i = 0; i < count; i++) {
const a = (dirx < 0 ? Math.PI : 0) + (Math.random() - 0.5) * 2.4;
const sp = (40 + Math.random() * 150) * power;
sparks.push({ x, y: cy + (Math.random() - 0.5) * hh * 1.2, vx: Math.cos(a) * sp, vy: Math.sin(a) * sp - 40, life: 0, max: 0.35 + Math.random() * 0.6, hue: warm ? Math.random() * 0.35 : 0.02 + Math.random() * 0.08 });
}
while (sparks.length > MAXS) sparks.shift();
};
api.current.set = (next: boolean) => {
const dir = next ? 1 : -1;
const from = s.t;
awake = 3;
launch?.kill();
gsap.killTweensOf(s, "on,lit,wind,flash");
if (still) {
Object.assign(s, { t: next ? xOn : xOff, x: next ? xOn : xOff, v: 0, on: next ? 1 : 0, lit: next ? 1 : 0, dir });
return;
}
s.dir = dir;
// anticipation: the knob squashes back against its wall, then launches
launch = gsap
.timeline()
.to(s, { wind: 1, duration: 0.13, ease: "power2.in" })
.add(() => {
s.t = next ? xOn : xOff;
s.v += dir * 260; // the release
s.armed = true; // the next time it reaches the far wall counts as the impact
spray(from - dir * kr * 0.7, -dir, next ? 16 : 10, next, 0.9);
})
.to(s, { wind: 0, duration: 0.22, ease: "back.out(3)" })
// staging: the eclipse begins in flight and completes on impact; the flood follows the knob in
.to(s, { on: next ? 0.55 : 0, duration: next ? 0.32 : 0.4, ease: "power2.out" }, 0.13)
.to(s, { lit: next ? 1 : 0, duration: next ? 0.95 : 0.6, ease: next ? "power2.out" : "power2.inOut" }, 0.13);
};
api.current.hover = (h) => {
awake = 3;
if (still) return void (s.hover = h ? 1 : 0);
gsap.to(s, { hover: h ? 1 : 0, duration: 0.45, ease: h ? "back.out(2.5)" : "power2.out", overwrite: "auto" });
};
api.current.press = (p) => {
awake = 3;
if (!still) s.sqv += p ? 9 : -4;
};
let visible = true;
const io = new IntersectionObserver(([en]) => {
visible = en.isIntersecting;
awake = 3;
});
io.observe(host);
const tick = (_t: number, ms: number) => {
if (!visible) return;
const dt = Math.min(ms / 1000, 1 / 30);
if (!still)
for (let k = 0; k < 2; k++) {
const h = dt / 2;
// knob: a lively spring that overshoots into the far wall
s.v += (200 * (s.t - s.x) - 17 * s.v) * h;
s.x += s.v * h;
s.sqv += (-260 * s.sq - 13 * s.sqv) * h;
s.sq += s.sqv * h;
// the track's bulge and the neon's slosh: springs that ring on after the knob stops (follow-through)
s.bulgeV += (-210 * s.bulge - 9 * s.bulgeV) * h;
s.bulge += s.bulgeV * h;
const sl = Math.max(-1.3, Math.min(1.3, s.v / 650));
s.sloshV += (38 * (sl - s.slosh) - 4.2 * s.sloshV) * h;
s.slosh += s.sloshV * h;
}
// the impact: the first moment the knob reaches the far wall
const over = Math.max(0, (s.x - s.t) * s.dir);
if (s.armed && over > 0) {
s.armed = false;
const on = s.dir > 0;
gsap.fromTo(s, { flash: 1 }, { flash: 0, duration: on ? 0.7 : 0.4, ease: "power2.out" });
s.bulgeV += on ? 16 : 9;
if (on) gsap.to(s, { on: 1, duration: 0.3, ease: "power3.out" });
spray(s.t + s.dir * kr * 0.9, s.dir, on ? 12 : 5, on, on ? 0.8 : 0.5);
}
if (s.lit > 0.001 || s.on > 0.001) s.time += dt * (still ? 0 : 0.2 + 0.8 * Math.max(0, flowRef.current));
for (let i = sparks.length - 1; i >= 0; i--) {
const p = sparks[i];
p.life += dt;
if (p.life > p.max) {
sparks.splice(i, 1);
continue;
}
p.vy += 170 * dt; // they fall in arcs
p.vx *= 1 - 2.2 * dt;
p.x += p.vx * dt;
p.y += p.vy * dt;
}
const moving = Math.abs(s.v) > 0.5 || Math.abs(s.t - s.x) > 0.05 || Math.abs(s.sqv) > 0.01 || Math.abs(s.bulgeV) > 0.01 || Math.abs(s.sloshV) > 0.005 || sparks.length > 0 || gsap.isTweening(s);
if (moving || s.lit > 0.001) awake = 3;
if (awake <= 0) return;
awake--;
// the knob's shape: stretched by speed in flight, squashed against the far wall on impact,
// squashed against its own wall in the wind-up, and by a press; volume is kept throughout
const wall = Math.min(0.32, (over / kr) * 1.3);
const fly = wall > 0 ? 0 : Math.min(0.26, Math.abs(s.v) / 1500);
const wind = s.wind * 0.16;
let sx = (1 + fly) * (1 - wall) * (1 - wind) * (1 + s.sq * 0.06);
const sy = (1 / Math.sqrt(1 + fly)) * (1 + wall * 0.75) * (1 + wind * 0.7) * (1 - s.sq * 0.07);
sx = Math.max(0.6, sx);
// squashing keeps the knob's outer edge on the wall it is pressed against
let x = Math.max(xOff, Math.min(xOn, s.x));
if (wall > 0) x = s.t + s.dir * kr * (1 - (1 - wall));
if (s.wind > 0) x -= s.dir * kr * wind;
gl.viewport(0, 0, canvas.width, canvas.height);
gl.clearColor(0, 0, 0, 0);
gl.clear(gl.COLOR_BUFFER_BIT);
gl.uniform2f(U.uRes, canvas.width, canvas.height);
gl.uniform1f(U.uDpr, dpr);
gl.uniform1f(U.uTime, s.time);
gl.uniform1f(U.uOn, s.on);
gl.uniform1f(U.uLit, s.lit);
gl.uniform1f(U.uKnobX, x);
gl.uniform1f(U.uKnobSX, sx);
gl.uniform1f(U.uKnobSY, sy);
gl.uniform1f(U.uHover, s.hover);
gl.uniform1f(U.uFlash, s.flash);
gl.uniform1f(U.uSlosh, s.slosh);
gl.uniform1f(U.uBulge, s.bulge);
gl.uniform1f(U.uFloor, cy + hh * 1.3); // a glossy floor just below the switch
gl.uniform4f(U.uTrack, cx, cy, W / 2, hh);
sbuf.fill(0);
sparks.forEach((p, i) => sbuf.set([p.x, p.y, Math.pow(1 - p.life / p.max, 1.5), p.hue + s.time * 0.07], i * 4));
gl.uniform4fv(U.uSparks, sbuf);
gl.drawArrays(gl.TRIANGLES, 0, 3);
// the coloured light it casts on the page behind, swelling with the impact
const amb = ambRef.current;
if (amb) {
const c = neonAt(s.time * 0.07 + 0.08).map((v) => Math.round(v * 255)).join(",");
amb.style.opacity = Math.min(1, s.lit * 0.7 + s.flash * 0.3).toFixed(3);
amb.style.background = `radial-gradient(closest-side, rgba(${c},0.34), rgba(${c},0.1) 50%, transparent)`;
}
};
gsap.ticker.add(tick);
return () => {
gsap.ticker.remove(tick);
io.disconnect();
launch?.kill();
gsap.killTweensOf(s);
gl.deleteProgram(prog);
gl.deleteShader(vs);
gl.deleteShader(fs);
gl.deleteBuffer(vbo);
gl.deleteVertexArray(vao);
gl.getExtension("WEBGL_lose_context")?.loseContext();
canvas.remove();
};
}, [W, H, M, MB]); // eslint-disable-line react-hooks/exhaustive-deps
// follow the value
const first = useRef(true);
useEffect(() => {
if (first.current) return void (first.current = false);
api.current.set(on);
}, [on]);
const toggle = () => {
const next = !on;
if (checked === undefined) setInner(next);
onCheckedChange?.(next);
};
return (
<div className={["relative inline-flex flex-col items-center", className].filter(Boolean).join(" ")}>
{/* the coloured light it throws on the surface behind */}
<div ref={ambRef} aria-hidden="true" className="pointer-events-none absolute top-1/2 left-1/2 -z-10 -translate-x-1/2 -translate-y-1/2 rounded-full opacity-0" style={{ width: W * 3.2, height: W * 2.6 }} />
<button
ref={btnRef}
type="button"
role="switch"
aria-checked={on}
aria-label={label}
onClick={toggle}
onPointerEnter={() => api.current.hover(true)}
onPointerLeave={() => (api.current.hover(false), api.current.press(false))}
onPointerDown={() => api.current.press(true)}
onPointerUp={() => api.current.press(false)}
onFocus={() => api.current.hover(true)}
onBlur={() => api.current.hover(false)}
className="relative block cursor-pointer rounded-full border-0 bg-transparent p-0 outline-none focus-visible:ring-2 focus-visible:ring-white/50 focus-visible:ring-offset-4 focus-visible:ring-offset-black"
style={{ width: W, height: H }}
>
<div ref={hostRef} className="absolute inset-0">
{/* without WebGL: a plain, still switch */}
<span className="absolute inset-0 hidden rounded-full border border-white/25 bg-[#0e0e10] [[data-fallback]_&]:block">
<span
className="absolute top-1/2 size-[82%] -translate-y-1/2 rounded-full transition-all duration-300"
style={{ height: "82%", width: H * 0.82, left: on ? W - H * 0.91 : H * 0.09, background: on ? "#08080a" : "#e9e9ec", boxShadow: on ? "0 0 0 2px #ff3d9e, 0 0 18px 4px rgba(255,61,158,.6)" : "none" }}
/>
</span>
</div>
</button>
</div>
);
}
export default EclipseSwitch;
One prompt, any AI agent
Paste into Claude Code, Codex, Cursor or Windsurf. It carries your tweaks and the full source, so the agent installs it exactly.
Props
7 propscheckedbooleanControlled state.defaultCheckedbooleanStarting state when uncontrolled.default falsedefault falseonCheckedChange(on) => voidCalled when it flips.labelstringAccessible name for screen readers (no text is shown).default "Vivid mode"default "Vivid mode"sizenumberTrack width in px (height follows).default 120default 120flownumberHow fast the neon drifts, 0 to 2.default 1default 1classNamestringExtra classes.