"use client"; /** * pullchain.tsx — a porcelain wall light with a ball chain on a verlet spring. * Brush the chain and it swings; click to tug it and the light comes on, then it switches itself off again. * * Part of toy-buttons. MIT licensed. Only depends on React. */ import { useEffect, useImperativeHandle, useRef, useState, type ButtonHTMLAttributes, type CSSProperties, type MouseEvent, type ReactNode, type Ref } from "react"; type Palette = { porcelain: string; glow: string; metal: string }; type Props = Omit, "children" | "onChange"> & { children?: ReactNode; ref?: Ref; lit?: boolean; onLitChange?: (lit: boolean) => void; autoOff?: number; palette?: Palette; }; const W = 210; const H = 88; const PADX = 90; const PADT = 120; const PADB = 150; const CW = W + PADX * 2; const CH = H + PADT + PADB; const CX = CW / 2; const HB = PADT + H; // housing bottom in canvas coordinates const BALLS = 9; const SEG = 7.6; const LAST_SEG = 14; const REST_LEN = (BALLS) * SEG + LAST_SEG; const BALL_R = 3.7; const BEAD_R = 12.5; const STEP = 0.008; const ITER = 8; const GRAV = 1400; const DAMP = 0.985; const DEFAULT_PALETTE: Palette = { porcelain: "#e9e6df", glow: "#ffc45a", metal: "#a78353" }; const clamp = (n: number, lo = 0, hi = 1) => Math.max(lo, Math.min(hi, n)); const hexRgb = (hex: string): [number, number, number] => { const h = hex.replace("#", ""); const f = h.length === 3 ? h.split("").map((c) => c + c).join("") : h.padEnd(6, "0"); return [parseInt(f.slice(0, 2), 16) || 0, parseInt(f.slice(2, 4), 16) || 0, parseInt(f.slice(4, 6), 16) || 0]; }; type Pt = { x: number; y: number; px: number; py: number; w: number }; export function PullChainButton({ children = "on", lit, onLitChange, autoOff = 2400, palette = DEFAULT_PALETTE, onClick, className, type = "button", disabled = false, ref, ...rest }: Props) { const btnRef = useRef(null); const wrapRef = useRef(null); const canvasRef = useRef(null); const houseRef = useRef(null); const playRef = useRef<(next: boolean) => void>(() => {}); const visRef = useRef<(v: boolean, delay: number) => void>(() => {}); const autoRef = useRef<() => void>(() => {}); const autoMs = useRef(autoOff); const mounted = useRef(false); const [inner, setInner] = useState(false); const isLit = lit ?? inner; autoMs.current = autoOff; autoRef.current = () => { if (lit === undefined) setInner(false); onLitChange?.(false); }; useImperativeHandle(ref, () => btnRef.current!, []); const glow = hexRgb(palette.glow); const glowCss = `${glow[0]},${glow[1]},${glow[2]}`; useEffect(() => { const btn = btnRef.current, wrap = wrapRef.current, cv = canvasRef.current, house = houseRef.current; if (!btn || !wrap || !cv || !house) return; const ctx = cv.getContext("2d"); if (!ctx) return; const media = window.matchMedia("(prefers-reduced-motion: reduce)"); let reduced = media.matches; let visible = true; let frame = 0, last = 0, acc = 0, calm = 0; let rect: DOMRect | undefined; let rectDirty = true; let hoverBtn = false, hoverChain = false; let cursor = { x: -9999, y: -9999, t: 0 }; let bbox = { x0: 0, x1: 0, y1: 0 }; // pull spring let mode: 0 | 1 | 2 = 0, driveT = 0, ext = 0, extV = 0, amt = 34; // light let visLit = false, litMix = 0, spillCur = 0, flash = 0; let swayT = 0, hoverT0 = 0; const timers = new Set(); const anims = new Set(); let epoch = 0; const [gr, gg, gb] = glow; const pts: Pt[] = []; const ghost: { x: number; y: number }[] = []; let swayDir = 1, kickOn = true; const resetChain = () => { pts.length = 0; ghost.length = 0; pts.push({ x: CX, y: HB + 11, px: CX, py: HB + 11, w: 0 }); for (let i = 1; i <= BALLS; i++) pts.push({ x: CX, y: HB + 11 + i * SEG, px: CX, py: HB + 11 + i * SEG, w: 1 }); const by = HB + 11 + BALLS * SEG + LAST_SEG; pts.push({ x: CX, y: by, px: CX, py: by, w: 1 / 6 }); }; resetChain(); const segLen = (i: number) => (i === BALLS ? LAST_SEG : SEG); // sprites let sprite: HTMLCanvasElement | null = null, spriteLit: HTMLCanvasElement | null = null; let spriteS = 1; const makeSprite = (warm: boolean, dpr: number) => { const s = Math.ceil((BALL_R + 2) * 2 * dpr); const c = document.createElement("canvas"); c.width = s; c.height = s; const k = c.getContext("2d"); if (!k) return c; const m = s / 2; const r = BALL_R * dpr; const g = k.createRadialGradient(m - r * 0.35, m - r * 0.4, 0.2, m, m, r); if (warm) { g.addColorStop(0, "#fff8e0"); g.addColorStop(0.25, "#ffd98a"); g.addColorStop(0.6, "rgba(240,170,70,.6)"); g.addColorStop(1, "rgba(240,170,70,0)"); } else { g.addColorStop(0, "#fffdf6"); g.addColorStop(0.15, "#f3e6c8"); g.addColorStop(0.45, "#c7ad7c"); g.addColorStop(0.75, "#6e5733"); g.addColorStop(1, "#2b2114"); } k.fillStyle = g; k.beginPath(); k.arc(m, m, r, 0, 6.2832); k.fill(); if (!warm) { k.strokeStyle = "rgba(30,20,8,.55)"; k.lineWidth = 0.6 * dpr; k.beginPath(); k.arc(m, m, r, 0, 6.2832); k.stroke(); k.fillStyle = "rgba(255,230,180,.22)"; k.beginPath(); k.arc(m + r * 0.25, m + r * 0.5, r * 0.3, 0, 6.2832); k.fill(); } return c; }; const setup = () => { const scale = btn.getBoundingClientRect().width / (btn.offsetWidth || 1) || 1; const dpr = Math.min(2, (window.devicePixelRatio || 1) * scale); cv.width = Math.round(CW * dpr); cv.height = Math.round(CH * dpr); ctx.setTransform(dpr, 0, 0, dpr, 0, 0); sprite = makeSprite(false, dpr); spriteLit = makeSprite(true, dpr); spriteS = dpr; rectDirty = true; }; const draw = () => { ctx.clearRect(0, 0, CW, CH); if (spillCur > 0.003) { const sp = spillCur; ctx.save(); ctx.translate(CX, PADT + H * 0.62); ctx.scale(1, 0.8); const g1 = ctx.createRadialGradient(0, 0, 0, 0, 0, 90); g1.addColorStop(0, `rgba(255,196,96,${(0.55 * sp).toFixed(3)})`); g1.addColorStop(1, "rgba(255,196,96,0)"); ctx.fillStyle = g1; ctx.fillRect(-90, -90, 180, 180); const g2 = ctx.createRadialGradient(0, 0, 0, 0, 0, 190); g2.addColorStop(0, `rgba(255,180,80,${(0.26 * sp).toFixed(3)})`); g2.addColorStop(0.45, `rgba(255,180,80,${(0.12 * sp).toFixed(3)})`); g2.addColorStop(1, "rgba(255,180,80,0)"); ctx.fillStyle = g2; ctx.fillRect(-190, -190, 380, 380); ctx.restore(); ctx.save(); ctx.translate(CX, HB + 30); ctx.scale(1.25, 1); const g3 = ctx.createRadialGradient(0, 0, 0, 0, 0, 120); g3.addColorStop(0, `rgba(255,190,90,${(0.22 * sp).toFixed(3)})`); g3.addColorStop(1, "rgba(255,190,90,0)"); ctx.fillStyle = g3; ctx.fillRect(-120, -120, 240, 240); ctx.restore(); } // chain shadow on the wall ctx.save(); ctx.lineCap = "round"; ctx.lineJoin = "round"; for (const [lw, al] of [[5, 0.07 + 0.05 * litMix], [2.5, 0.16 + 0.10 * litMix]] as const) { ctx.strokeStyle = `rgba(0,0,0,${al.toFixed(3)})`; ctx.lineWidth = lw; ctx.beginPath(); for (let i = 0; i < pts.length; i++) { const p = pts[i]; if (i) ctx.lineTo(p.x + 5, p.y + 5); else ctx.moveTo(p.x + 5, p.y + 5); } ctx.stroke(); } { const bs = pts[BALLS + 1]; ctx.fillStyle = `rgba(0,0,0,${(0.10 + 0.05 * litMix).toFixed(3)})`; ctx.beginPath(); ctx.arc(bs.x + 4, bs.y + 6, BEAD_R + 1.5, 0, 6.2832); ctx.fill(); ctx.fillStyle = `rgba(0,0,0,${(0.16 + 0.08 * litMix).toFixed(3)})`; ctx.beginPath(); ctx.arc(bs.x + 4, bs.y + 6, BEAD_R - 1, 0, 6.2832); ctx.fill(); } ctx.restore(); // links ctx.strokeStyle = "#5a4a32"; ctx.lineWidth = 1.4; ctx.beginPath(); for (let i = 0; i < pts.length; i++) { const p = pts[i]; if (i) ctx.lineTo(p.x, p.y); else ctx.moveTo(p.x, p.y); } ctx.stroke(); // balls if (sprite && spriteLit) { const half = sprite.width / spriteS / 2; const sw = sprite.width / spriteS; for (let i = 1; i <= BALLS; i++) { const p = pts[i]; ctx.drawImage(sprite, p.x - half, p.y - half, sw, sw); } if (litMix > 0.01) { for (let i = 1; i <= BALLS; i++) { const p = pts[i]; ctx.globalAlpha = litMix * (1 - 0.45 * i / BALLS); ctx.drawImage(spriteLit, p.x - half - 0.3, p.y - half - 0.9, sw, sw); } ctx.globalAlpha = 1; } } // pull bead const b = pts[BALLS + 1]; if (!reduced && ghost.length >= 2) { const l1 = ghost[ghost.length - 1], l2 = ghost[ghost.length - 2]; if (Math.hypot(l1.x - l2.x, l1.y - l2.y) > 2) { const als = [0.06, 0.10, 0.16]; const off = ghost.length - 4; for (let j = 0; j < 3; j++) { const gp = ghost[Math.max(0, off) + j]; if (!gp) continue; ctx.fillStyle = `rgba(199,173,124,${als[j]})`; ctx.beginPath(); ctx.arc(gp.x, gp.y, BEAD_R, 0, 6.2832); ctx.fill(); } } } const cg = ctx.createLinearGradient(b.x - 2.2, 0, b.x + 2.2, 0); cg.addColorStop(0, "#4d3d24"); cg.addColorStop(0.35, "#e8d3a6"); cg.addColorStop(0.65, "#8a7148"); cg.addColorStop(1, "#3a2e1b"); ctx.fillStyle = cg; ctx.beginPath(); ctx.roundRect(b.x - 2.2, b.y - BEAD_R - 4, 4.4, 5.5, 1.2); ctx.fill(); const bg = ctx.createRadialGradient(b.x - 4.5, b.y - 5, 0.5, b.x, b.y, BEAD_R); bg.addColorStop(0, "#ffffff"); bg.addColorStop(0.10, "#f6efe2"); bg.addColorStop(0.38, "#bfae90"); bg.addColorStop(0.72, "#5f5240"); bg.addColorStop(1, "#2a231a"); ctx.fillStyle = bg; ctx.beginPath(); ctx.arc(b.x, b.y, BEAD_R, 0, 6.2832); ctx.fill(); ctx.strokeStyle = "rgba(0,0,0,.55)"; ctx.lineWidth = 1; ctx.stroke(); ctx.fillStyle = "rgba(255,255,255,.95)"; ctx.beginPath(); ctx.ellipse(b.x - 4.5, b.y - 5.5, 2.8, 1.8, -0.6, 0, 6.2832); ctx.fill(); ctx.strokeStyle = "rgba(255,236,200,.38)"; ctx.lineWidth = 1.3; ctx.lineCap = "round"; ctx.beginPath(); ctx.arc(b.x, b.y, BEAD_R - 1.4, 0.15 * Math.PI, 0.6 * Math.PI); ctx.stroke(); if (litMix > 0.01) { const wg = ctx.createRadialGradient(b.x - 4.5, b.y - 5, 1, b.x, b.y, BEAD_R); wg.addColorStop(0, `rgba(255,236,180,${(0.75 * litMix).toFixed(3)})`); wg.addColorStop(0.6, `rgba(240,200,120,${(0.25 * litMix).toFixed(3)})`); wg.addColorStop(1, "rgba(216,185,122,0)"); ctx.fillStyle = wg; ctx.beginPath(); ctx.arc(b.x, b.y, BEAD_R, 0, 6.2832); ctx.fill(); } // ferrule ctx.fillStyle = "#6b5534"; ctx.fillRect(CX - 7, HB - 1, 14, 3); ctx.fillStyle = "rgba(255,255,255,.5)"; ctx.fillRect(CX - 7, HB - 1, 14, 1); const fg = ctx.createLinearGradient(CX - 5, 0, CX + 5, 0); fg.addColorStop(0, "#3e3122"); fg.addColorStop(0.22, "#9c8258"); fg.addColorStop(0.38, "#f4e5c2"); fg.addColorStop(0.55, "#b4976a"); fg.addColorStop(1, "#3a2d1c"); ctx.fillStyle = fg; ctx.beginPath(); ctx.roundRect(CX - 5, HB + 2, 10, 9, 1.5); ctx.fill(); ctx.fillStyle = "rgba(0,0,0,.4)"; ctx.fillRect(CX - 5, HB + 10.5, 10, 1); }; const stepOnce = () => { const h = STEP; // pull spring if (mode === 1) { const k = 2200; const a = k * (amt - ext) - 2 * Math.sqrt(k) * extV; extV += a * h; ext += extV * h; driveT += h * 1000; if (driveT >= 110) { mode = 2; const bd = pts[BALLS + 1]; if (kickOn) bd.px -= 40 * h; // sideways wobble on release } } else if (mode === 2) { const w = 20, z = 0.3; const a = -w * w * ext - 2 * z * w * extV; extV += a * h; ext += extV * h; driveT += h * 1000; if (driveT > 1300 || (driveT > 400 && Math.abs(ext) < 0.15 && Math.abs(extV) < 1)) { mode = 0; ext = 0; extV = 0; } } const sc = 1 + Math.max(ext, 0) / REST_LEN; const n = pts.length; const dmp = mode === 0 && !hoverBtn && !hoverChain ? 0.976 : DAMP; for (let i = 1; i < n; i++) { const p = pts[i]; const vx = (p.x - p.px) * dmp, vy = (p.y - p.py) * dmp; p.px = p.x; p.py = p.y; p.x += vx; p.y += vy + GRAV * h * h; } // soft lean of the bead towards a hovering cursor if (hoverChain && !disabled) { const bd = pts[n - 1]; const tx = CX + clamp(cursor.x - CX, -18, 18); bd.x += 1800 * (tx - bd.x) * h * h; } else if (hoverBtn && mode === 0 && !disabled) { const bd = pts[n - 1]; swayT += h; const ramp = clamp((performance.now() - hoverT0) / 220); const tx = CX + swayDir * (16 * ramp + 5 * Math.sin(swayT * 2 * Math.PI / 1.6)); bd.x += 1800 * (tx - bd.x) * h * h; } for (let it = 0; it < ITER; it++) { for (let i = 0; i < n - 1; i++) { const a = pts[i], b = pts[i + 1]; const L = segLen(i) * sc; const dx = b.x - a.x, dy = b.y - a.y; const d = Math.hypot(dx, dy) || 1e-6; if (d <= L) continue; // a rope: slack is allowed const wsum = a.w + b.w; const diff = (d - L) / d / wsum; a.x += dx * diff * a.w; a.y += dy * diff * a.w; b.x -= dx * diff * b.w; b.y -= dy * diff * b.w; } } if (mode !== 0 || Math.abs(ext) > 0.01) { const bd = pts[n - 1]; bd.y = pts[0].y + REST_LEN + ext; bd.py = bd.y - extV * h; } }; const simulate = (dtMs: number) => { acc += Math.min(32, dtMs) / 1000; let steps = 0; while (acc >= STEP && steps < 4) { stepOnce(); acc -= STEP; steps++; } if (acc > STEP) acc = 0; }; const isMoving = () => { let maxV = 0, maxOff = 0; for (let i = 1; i < pts.length; i++) { maxV = Math.max(maxV, Math.abs(pts[i].x - pts[i].px)); maxOff = Math.max(maxOff, Math.abs(pts[i].x - CX)); } return maxV > 0.12 || maxOff > 1.2; }; const tick = (now: number) => { frame = 0; if (disabled || btn.matches(":disabled") || !visible || document.hidden || reduced) { last = 0; return; } const dt = last ? Math.min(48, now - last) : 16; last = now; simulate(dt); { const bp = pts[BALLS + 1]; ghost.push({ x: bp.x, y: bp.y }); if (ghost.length > 4) ghost.shift(); } // light follows the mechanism const target = visLit ? 1 : 0; litMix += (target - litMix) * (1 - Math.exp(-dt / 110)); spillCur += (target - spillCur) * (1 - Math.exp(-dt / (visLit ? 70 : 150))); if (flash > 0) { flash = Math.max(0, flash - dt / 90); } const sp = spillCur; spillCur = Math.min(1.1, sp + flash * 0.1); draw(); spillCur = sp; let minX = 1e9, maxX = -1e9, maxY = 0; for (const p of pts) { minX = Math.min(minX, p.x); maxX = Math.max(maxX, p.x); maxY = Math.max(maxY, p.y); } bbox = { x0: minX - BEAD_R - 12, x1: maxX + BEAD_R + 12, y1: maxY + BEAD_R + 12 }; const moving = isMoving(); const settling = Math.abs(litMix - target) > 0.01 || Math.abs(spillCur - target) > 0.01 || flash > 0; if (mode !== 0 || moving || hoverChain || hoverBtn || settling) { calm = 0; frame = requestAnimationFrame(tick); } else if (++calm < 12) frame = requestAnimationFrame(tick); else { last = 0; litMix = target; spillCur = target; resetChain(); draw(); } }; const wake = () => { if (!frame && !disabled && visible && !document.hidden && !reduced) { last = 0; calm = 0; frame = requestAnimationFrame(tick); } }; const refreshHover = () => { wrap.dataset.tbHover = String(hoverBtn || hoverChain); }; const onMove = (e: PointerEvent) => { if (disabled || reduced) return; if (rectDirty || !rect) { rect = cv.getBoundingClientRect(); rectDirty = false; } const scale = rect.width / CW || 1; const x = (e.clientX - rect.left) / scale, y = (e.clientY - rect.top) / scale; const t = e.timeStamp; const dtc = cursor.t ? Math.max(8, t - cursor.t) / 1000 : 0; const vx = dtc ? (x - cursor.x) / dtc : 0; cursor = { x, y, t }; const inBox = x >= bbox.x0 && x <= bbox.x1 && y >= HB && y <= bbox.y1; const was = hoverChain; hoverChain = inBox; if (inBox) { if (Math.abs(vx) > 5 && vx < 5000) { for (let i = 1; i < pts.length; i++) { const p = pts[i]; const d = Math.hypot(p.x - x, p.y - y); if (d < 40) p.px -= vx * 0.25 * (1 - d / 40) * STEP * p.w; } } wake(); } if (was !== hoverChain) { refreshHover(); wake(); } }; const enter = () => { hoverBtn = true; hoverT0 = performance.now(); swayT = 0; const dir = Math.abs(cursor.x - CX) < 20 ? 1 : (cursor.x < CX ? 1 : -1); swayDir = dir; pts[BALLS + 1].px -= dir * 260 * STEP; refreshHover(); wake(); }; const leave = () => { hoverBtn = false; refreshHover(); }; const dirty = () => { rectDirty = true; }; const leaveDoc = () => { hoverChain = false; cursor.t = 0; refreshHover(); }; const later = (fn: () => void, ms: number) => { const e = epoch; const id = window.setTimeout(() => { timers.delete(id); if (e === epoch) fn(); }, ms); timers.add(id); }; const clearWork = () => { epoch++; timers.forEach((id) => window.clearTimeout(id)); timers.clear(); anims.forEach((a) => a.cancel()); anims.clear(); }; const setVis = (v: boolean) => { visLit = v; wrap.dataset.tbVis = v ? "lit" : "off"; if (reduced) { litMix = v ? 1 : 0; spillCur = litMix; draw(); return; } if (v) { flash = 1; const on = wrap.querySelector(".tb-pullchain-on"); if (on) { const an = on.animate([{ opacity: 1, filter: "brightness(1.8) saturate(.75)" }, { opacity: 1, filter: "brightness(.82) saturate(1.1)", offset: 0.22 }, { opacity: 1, filter: "brightness(1.35)", offset: 0.45 }, { opacity: 1, filter: "brightness(1)" }], { duration: 240, easing: "cubic-bezier(.2,.7,.3,1)" }); anims.add(an); an.onfinish = () => anims.delete(an); } } else if (litMix > 0.5) { const on = wrap.querySelector(".tb-pullchain-on"); if (on) { const an = on.animate([{ opacity: 1, filter: "none" }, { opacity: 0.85, filter: "sepia(.6) saturate(2.2) hue-rotate(-18deg) brightness(.8)", offset: 0.35 }, { opacity: 0, filter: "sepia(1) saturate(3) hue-rotate(-25deg) brightness(.45)" }], { duration: 380, easing: "ease-in" }); anims.add(an); an.onfinish = () => anims.delete(an); } } wake(); }; const tug = (a: number, kick = true) => { kickOn = kick; amt = a; mode = 1; driveT = 0; const an = house.animate([{ transform: "translateY(0)" }, { transform: "translateY(1.5px)", offset: 0.45 }, { transform: "translateY(0)" }], { duration: 220, easing: "ease-out" }); anims.add(an); an.onfinish = () => anims.delete(an); wake(); }; const refresh = () => { wrap.dataset.tbPaused = String(disabled || !visible || document.hidden); if (disabled || !visible || document.hidden || reduced) { cancelAnimationFrame(frame); frame = 0; last = 0; resetChain(); mode = 0; ext = 0; extV = 0; litMix = visLit ? 1 : 0; spillCur = litMix; draw(); } else wake(); }; const mediaChange = () => { reduced = media.matches; clearWork(); refresh(); }; const io = typeof IntersectionObserver === "undefined" ? null : new IntersectionObserver(([en]) => { visible = en.isIntersecting; refresh(); }); const ro = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(() => { setup(); if (reduced || !frame) draw(); }); setup(); io?.observe(wrap); ro?.observe(btn); window.addEventListener("pointermove", onMove, { passive: true }); window.addEventListener("resize", dirty); window.addEventListener("scroll", dirty, true); document.documentElement.addEventListener("pointerleave", leaveDoc); btn.addEventListener("pointerenter", enter); btn.addEventListener("pointerleave", leave); document.addEventListener("visibilitychange", refresh); media.addEventListener("change", mediaChange); visRef.current = (v, delay) => { if (delay <= 0 || reduced) { setVis(v); return; } later(() => setVis(v), delay); }; playRef.current = (next) => { if (disabled || btn.matches(":disabled")) return; // a new toggle cancels a pending auto-off and any pending mechanism click epoch++; timers.forEach((id) => window.clearTimeout(id)); timers.clear(); if (reduced) { if (next && autoMs.current > 0) later(() => { autoRef.current(); }, autoMs.current); return; } tug(18); if (next && autoMs.current > 0) later(() => { tug(8, false); autoRef.current(); }, autoMs.current); }; refresh(); draw(); return () => { playRef.current = () => {}; visRef.current = () => {}; cancelAnimationFrame(frame); clearWork(); io?.disconnect(); ro?.disconnect(); window.removeEventListener("pointermove", onMove); window.removeEventListener("resize", dirty); window.removeEventListener("scroll", dirty, true); document.documentElement.removeEventListener("pointerleave", leaveDoc); btn.removeEventListener("pointerenter", enter); btn.removeEventListener("pointerleave", leave); document.removeEventListener("visibilitychange", refresh); media.removeEventListener("change", mediaChange); }; // eslint-disable-next-line react-hooks/exhaustive-deps }, [disabled, palette.glow, palette.metal]); // the window and spill follow the toggle state, 110 ms after the tug (the mechanism click) useEffect(() => { const first = !mounted.current; mounted.current = true; visRef.current(isLit, first ? 0 : 110); }, [isLit, disabled, palette.glow, palette.metal]); const click = (e: MouseEvent) => { if (disabled || e.currentTarget.matches(":disabled")) return; onClick?.(e); if (e.defaultPrevented) return; const next = !isLit; if (lit === undefined) setInner(next); onLitChange?.(next); playRef.current(next); }; const vars = { "--tb-pullchain-porcelain": palette.porcelain, "--tb-pullchain-glow": palette.glow, "--tb-pullchain-glow-rgb": glowCss } as CSSProperties; return ( ); } export default PullChainButton; const css = ` .tb-pullchain-wrap{position:relative;display:inline-block;width:${W}px;height:${H}px;vertical-align:middle} .tb-pullchain-btn{position:relative;display:block;box-sizing:border-box;width:${W}px;height:${H}px;padding:0;margin:0;border:0;border-radius:${H / 2}px;background:transparent; overflow:visible;cursor:pointer;-webkit-tap-highlight-color:transparent; font:700 27px ui-rounded,"SF Pro Rounded","Nunito","Segoe UI",system-ui,sans-serif} .tb-pullchain-btn:focus-visible{outline:3px solid #f2c75a;outline-offset:5px} .tb-pullchain-btn:disabled{cursor:default;opacity:.6;filter:saturate(.5)} .tb-pullchain-canvas{position:absolute;left:-${PADX}px;top:-${PADT}px;width:${CW}px;height:${CH}px;pointer-events:none} .tb-pullchain-house{position:absolute;inset:0;border-radius:${H / 2}px;pointer-events:none; overflow:hidden; background:linear-gradient(180deg,#fdfcf8 0,#f1ede5 22%,var(--tb-pullchain-porcelain,#e9e6df) 50%,#d9d3c8 78%,#bfb8ab 100%); box-shadow:0 0 0 1px rgba(70,58,40,.22),0 1px 1px rgba(0,0,0,.35),0 4px 6px -1px rgba(0,0,0,.30),0 16px 28px -8px rgba(0,0,0,.55),inset 0 1.5px 0 #fff,inset 0 -2px 0 rgba(255,255,255,.35),inset 0 -9px 12px -4px rgba(90,70,40,.22),inset -4px 0 8px -2px rgba(0,0,0,.10)} .tb-pullchain-house::before{content:"";position:absolute;left:44px;right:30px;top:3px;height:15px;border-radius:8px 40px 8px 8px / 8px 14px 8px 8px;background:linear-gradient(180deg,rgba(255,255,255,.9),rgba(255,255,255,0));opacity:.75;pointer-events:none} .tb-pullchain-house::after{content:"";position:absolute;inset:0;border-radius:inherit;background:radial-gradient(ellipse 55% 75% at 52% 50%,rgba(255,200,110,.38),rgba(255,200,110,0) 72%);opacity:0;transition:opacity .4s;pointer-events:none} .tb-pullchain-wrap[data-tb-vis=lit] .tb-pullchain-house::after{opacity:1;transition-duration:.18s} .tb-pullchain-cap{position:absolute;left:0;top:0;width:40px;height:100%;border-radius:${H / 2}px 0 0 ${H / 2}px;box-sizing:border-box; background:radial-gradient(85% 75% at 60% 30%,#ffffff 0,#ebe7df 30%,#cbc5ba 72%,#a59e92 100%);border-right:1.5px solid #8e897f; box-shadow:1px 0 0 rgba(255,255,255,.55),inset 0 1px 0 rgba(255,255,255,.8),inset 6px 0 10px -6px rgba(0,0,0,.18)} .tb-pullchain-win{position:absolute;left:30px;top:18px;width:150px;height:52px;box-sizing:border-box;border-radius:26px;border:3px solid;border-color:#8f8b85 #c9c5bf #f4f2ee #c9c5bf;overflow:hidden; box-shadow:0 1px 0 rgba(255,255,255,.85),0 -1px 0 rgba(0,0,0,.12),0 0 0 1px rgba(0,0,0,.10);transition:border-color .15s,box-shadow .18s} .tb-pullchain-wrap[data-tb-hover=true] .tb-pullchain-win{border-color:#a5a19b #dcd9d4 #ffffff #dcd9d4} .tb-pullchain-wrap[data-tb-vis=lit] .tb-pullchain-win{box-shadow:0 0 0 1px rgba(255,214,130,.6),0 0 14px 2px rgba(255,190,80,.55),0 1px 0 rgba(255,255,255,.85)} .tb-pullchain-glass{position:absolute;inset:0;background:linear-gradient(180deg,#2b2d2e,#1c1d1e);box-shadow:inset 0 0 0 1px rgba(0,0,0,.55),inset 0 3px 6px rgba(0,0,0,.6)} .tb-pullchain-glass::before{content:"";position:absolute;inset:0;border-radius:inherit;background:radial-gradient(ellipse 60% 70% at 50% 55%,rgba(255,176,72,.30),rgba(255,176,72,0) 70%);opacity:0;transition:opacity .22s ease-out} .tb-pullchain-wrap[data-tb-hover=true] .tb-pullchain-glass::before{opacity:1} .tb-pullchain-glass::after{content:"";position:absolute;left:16px;right:16px;top:4px;height:11px;border-radius:8px;background:linear-gradient(180deg,rgba(255,255,255,.16),rgba(255,255,255,.02))} .tb-pullchain-on{position:absolute;inset:0;opacity:0;transition:opacity .26s ease-out; background:radial-gradient(ellipse 70% 80% at 50% 50%,#fffbe0 0,#fff1a8 35%,#fbdc78 70%,#eeb94a 100%);box-shadow:inset 0 0 10px rgba(255,170,40,.55),inset 0 3px 4px rgba(120,70,0,.25)} .tb-pullchain-wrap[data-tb-vis=lit] .tb-pullchain-on{opacity:1;transition-duration:0s} .tb-pullchain-label{position:absolute;left:33px;top:21px;width:144px;height:46px;box-sizing:border-box;display:flex;align-items:center;justify-content:center;pointer-events:none; letter-spacing:.12em;padding-left:.12em;text-transform:uppercase;line-height:1;color:#cfccc5;text-shadow:0 -1px 0 rgba(0,0,0,.7),0 0 6px rgba(255,255,255,.06);transition:color .26s} .tb-pullchain-wrap[data-tb-hover=true] .tb-pullchain-label{color:#e8d6b0} .tb-pullchain-wrap[data-tb-vis=lit] .tb-pullchain-label{color:#46351a;text-shadow:0 1px 0 rgba(255,250,225,.6),0 0 8px rgba(255,230,160,.55);transition-duration:0s} .tb-pullchain-wrap[data-tb-paused=true] *{animation-play-state:paused!important} @media (prefers-reduced-motion:reduce){.tb-pullchain-wrap *,.tb-pullchain-wrap *::before,.tb-pullchain-wrap *::after{animation:none!important;transition:none!important}} `;