"use client"; /** * coin.tsx — an arcade coin door with a glowing slot and a red START button. * Hover and a gold coin floats over the slot; click and it drops in, and START lights up as READY. * * Part of toy-buttons. MIT licensed. Only depends on React. */ import { useEffect, useId, useImperativeHandle, useRef, type ButtonHTMLAttributes, type CSSProperties, type MouseEvent, type ReactNode, type Ref } from "react"; type Palette = { panel: string; button: string; lit: string; coin: string }; type Props = Omit, "children"> & { children?: ReactNode; ref?: Ref; readyText?: string; palette?: Palette; }; const W = 220; const H = 200; const SLOT_X = 78; // slot centre x const SLOT_TOP = 42; // top of the glowing slot const HOVER_Y = -52; // coin centre y while hovering const START_Y = -64; // coin centre y when dropped without hover const COIN = 64; const READY_MS = 2400; const EXTRA_MS = 2000; const DEFAULT_PALETTE: Palette = { panel: "#2a2a2c", button: "#b3161a", lit: "#ff2a1f", coin: "#e8b923" }; const STAR = ["....#....", "....#....", "...###...", "#########", ".#######.", "..#####..", "..##.##..", ".##...##.", ".#.....#."]; const PX = 2.4; const starRects = STAR.flatMap((row, y) => row.split("").map((c, x) => (c === "#" ? [x, y] : null))).filter((v): v is number[] => v !== null); export function CoinButton({ children = "start", readyText = "ready", palette = DEFAULT_PALETTE, onClick, className, type = "button", disabled = false, ref, ...rest }: Props) { const uid = `tb-coin-${useId().replace(/[^a-zA-Z0-9_-]/g, "")}`; const btnRef = useRef(null); const wrapRef = useRef(null); const bodyRef = useRef(null); const coinRef = useRef(null); const litRef = useRef(null); const bloomRef = useRef(null); const coinSvgRef = useRef(null); const redRef = useRef(null); const slotRef = useRef(null); const streakRef = useRef(null); const ticksRef = useRef(null); const creditRef = useRef(null); const playRef = useRef<() => void>(() => {}); useImperativeHandle(ref, () => btnRef.current!, []); useEffect(() => { const btn = btnRef.current, wrap = wrapRef.current, body = bodyRef.current, coin = coinRef.current; const red = redRef.current, slot = slotRef.current, ticks = ticksRef.current, credit = creditRef.current, streaks = streakRef.current, lit = litRef.current, bloom = bloomRef.current, coinSvg = coinSvgRef.current; if (!bloom || !coinSvg || !lit || !streaks || !btn || !wrap || !body || !coin || !red || !slot || !ticks || !credit) return; let epoch = 0; let credits = 0; let endAt = 0; let endTimer = 0; const timers = new Set(); const anims = new Set(); const media = window.matchMedia("(prefers-reduced-motion: reduce)"); const setPhase = (p: "idle" | "play" | "ready") => { wrap.dataset.tbPhase = p; }; const later = (fn: () => void, ms: number) => { const e = epoch; const id = window.setTimeout(() => { timers.delete(id); if (e === epoch) fn(); }, ms); timers.add(id); return id; }; const animate = (el: Element, frames: Keyframe[], opts: KeyframeAnimationOptions) => { const a = el.animate(frames, opts); anims.add(a); a.onfinish = () => { if (opts.fill !== "forwards") anims.delete(a); }; return a; }; const clearAll = () => { epoch++; timers.forEach((id) => window.clearTimeout(id)); timers.clear(); window.clearTimeout(endTimer); anims.forEach((a) => a.cancel()); anims.clear(); }; const toIdle = () => { clearAll(); credits = 0; setPhase("idle"); delete wrap.dataset.tbCredit; delete wrap.dataset.tbCoin; }; const scheduleEnd = () => { window.clearTimeout(endTimer); endTimer = window.setTimeout(toIdle, Math.max(0, endAt - performance.now())); }; const observer = typeof IntersectionObserver === "undefined" ? null : new IntersectionObserver(([en]) => { wrap.dataset.tbPaused = String(!en.isIntersecting || document.hidden); }); const vis = () => { wrap.dataset.tbPaused = String(document.hidden); }; observer?.observe(wrap); document.addEventListener("visibilitychange", vis); const mediaChange = () => toIdle(); media.addEventListener("change", mediaChange); toIdle(); if (disabled) wrap.dataset.tbPaused = "true"; const dropCoin = () => { const startY = btn.matches(":hover") && wrap.dataset.tbPhase === "idle" ? HOVER_Y : START_Y; wrap.dataset.tbCoin = "drop"; const at = (c: number) => `translateY(${c - COIN / 2}px)`; animate(coin, [ { transform: at(startY), opacity: 1, easing: "cubic-bezier(.5,0,1,.6)" }, { transform: at(SLOT_TOP - COIN / 2), opacity: 1, offset: 180 / 440, easing: "cubic-bezier(.25,.1,.6,1)" }, { transform: at(SLOT_TOP + COIN / 2 + 4), opacity: 1 }, ], { duration: 440, fill: "forwards" }); animate(bloom, [{ opacity: 1 }, { opacity: 0, offset: 0.36 }, { opacity: 0 }], { duration: 440, fill: "forwards" }); animate(coinSvg, [{ transform: "perspective(240px) rotateY(58deg)", easing: "cubic-bezier(.4,0,.6,1)" }, { transform: "perspective(240px) rotateY(72deg)", offset: 180 / 440 }, { transform: "perspective(240px) rotateY(72deg)" }], { duration: 440, fill: "forwards" }); animate(streaks, [{ opacity: 0 }, { opacity: 1, offset: 0.12 }, { opacity: 1, offset: 0.6 }, { opacity: 0 }], { duration: 440 }); animate(red, [{ opacity: 0 }, { opacity: 0, offset: 0.45 }, { opacity: 1 }], { duration: 180, fill: "forwards" }); animate(slot, [ { opacity: 1, filter: "brightness(1)" }, { opacity: 1, filter: "brightness(1.9)", offset: 0.08 }, { opacity: 1, filter: "brightness(1.9)", offset: 0.36 }, { opacity: 0.4, filter: "brightness(1)", offset: 0.38 }, { opacity: 0.4, filter: "brightness(1)", offset: 0.85 }, { opacity: 1, filter: "brightness(1)" }, ], { duration: 640, delay: 180 }); animate(body, [ { transform: "translateY(0)" }, { transform: "translateY(3px)", offset: 0.4 }, { transform: "translateY(0)" }, ], { duration: 100, delay: 420, easing: "cubic-bezier(.3,1.6,.5,1)" }); animate(ticks, [{ opacity: 0 }, { opacity: 1, offset: 0.25 }, { opacity: 0 }], { duration: 160, delay: 420 }); later(() => { delete wrap.dataset.tbCoin; red.style.opacity = "0"; }, 760); }; playRef.current = () => { if (disabled || btn.matches(":disabled")) return; const now = performance.now(); if (media.matches) { clearAll(); setPhase("ready"); endAt = now + 1800; scheduleEnd(); return; } if (wrap.dataset.tbPhase !== "idle") { credits++; endAt += EXTRA_MS; scheduleEnd(); credit.textContent = `CREDIT ${credits}`; wrap.dataset.tbCredit = "true"; anims.forEach((a) => a.cancel()); anims.clear(); dropCoin(); return; } credits = 1; setPhase("play"); endAt = now + READY_MS; scheduleEnd(); dropCoin(); later(() => { setPhase("ready"); animate(lit, [{ opacity: 0 }, { opacity: 1, offset: 0.12 }, { opacity: 0.2, offset: 0.24 }, { opacity: 1, offset: 0.4 }, { opacity: 0.55, offset: 0.52 }, { opacity: 1 }], { duration: 300, easing: "linear" }); animate(body, [{ filter: "brightness(1)" }, { filter: "brightness(1.12)", offset: 0.3 }, { filter: "brightness(1)" }], { duration: 360 }); }, 440); }; return () => { playRef.current = () => {}; clearAll(); observer?.disconnect(); document.removeEventListener("visibilitychange", vis); media.removeEventListener("change", mediaChange); }; }, [disabled]); const click = (e: MouseEvent) => { if (disabled || e.currentTarget.matches(":disabled")) return; onClick?.(e); if (e.defaultPrevented) return; playRef.current(); }; const vars = { "--tb-coin-panel": palette.panel, "--tb-coin-button": palette.button, "--tb-coin-lit": palette.lit, "--tb-coin-coin": palette.coin } as CSSProperties; return ( ); } const Chev = () => ( ); export default CoinButton; const css = ` .tb-coin-wrap{position:relative;display:inline-block;width:${W}px;height:${H}px;vertical-align:middle} .tb-coin-btn{position:relative;display:block;box-sizing:border-box;width:${W}px;height:${H}px;padding:0;margin:0;border:0;border-radius:22px; background:transparent;overflow:visible;cursor:pointer;-webkit-tap-highlight-color:transparent; font:900 28px "Impact","Haettenschweiler","Arial Narrow Bold",ui-sans-serif,sans-serif} .tb-coin-btn:focus-visible{outline:3px solid #e6b450;outline-offset:4px} .tb-coin-btn:disabled{cursor:default;opacity:.6;filter:saturate(.5)} .tb-coin-body{position:absolute;inset:0;border-radius:22px;pointer-events:none; background:linear-gradient(180deg,#3a3a3d,#0c0c0d);box-shadow:0 1px 0 rgba(255,255,255,.06) inset,0 -2px 0 rgba(0,0,0,.6) inset,0 2px 3px rgba(0,0,0,.6),0 12px 18px -6px rgba(0,0,0,.55),0 28px 36px -20px rgba(0,0,0,.6)} .tb-coin-inner{position:absolute;inset:4px;border-radius:18px; background:repeating-linear-gradient(90deg,rgba(255,255,255,.018) 0 1px,transparent 1px 3px),radial-gradient(circle at 20% 10%,rgba(255,255,255,.07),transparent 40%),radial-gradient(circle at 85% 90%,rgba(255,255,255,.04),transparent 30%), linear-gradient(180deg,var(--tb-coin-panel,#2a2a2c),#161617);box-shadow:inset 0 0 0 1px rgba(255,255,255,.08)} .tb-coin-svg{position:absolute;inset:0;width:100%;height:100%;overflow:visible} .tb-coin-slot{position:absolute;left:${SLOT_X - 4}px;top:42px;width:8px;height:60px;border-radius:4px; background:linear-gradient(90deg,#ff2a1f,#ffb3a0 50%,#ff2a1f);box-shadow:0 0 10px rgba(255,40,30,.6),0 0 26px 6px rgba(255,40,30,.22);transition:filter .22s ease-out} .tb-coin-wrap[data-tb-phase=idle] .tb-coin-btn:hover .tb-coin-slot{filter:brightness(1.3) saturate(1.1)} .tb-coin-wrap[data-tb-phase=idle] .tb-coin-btn:hover .tb-coin-glow{animation-duration:1.1s} .tb-coin-spill{position:absolute;left:${SLOT_X - 40}px;top:4px;width:80px;height:26px;border-radius:50%;background:radial-gradient(closest-side,rgba(255,200,80,.24),transparent);opacity:0;transition:opacity .2s} .tb-coin-wrap[data-tb-phase=idle] .tb-coin-btn:hover .tb-coin-spill{opacity:1} .tb-coin-glow{position:absolute;inset:-8px;border-radius:12px;background:radial-gradient(ellipse,rgba(255,40,30,.55),transparent 70%); animation:tb-coin-pulse 2.4s ease-in-out infinite} .tb-coin-arrow{transform-box:fill-box;transform-origin:center} .tb-coin-wrap[data-tb-phase=idle] .tb-coin-btn:hover .tb-coin-arrow{animation:tb-coin-nudge .7s ease-in-out infinite} @keyframes tb-coin-nudge{0%,100%{transform:translateY(0)}50%{transform:translateY(2px)}} .tb-coin-credit{position:absolute;left:131px;top:86px;width:52px;height:16px;border-top:1px solid #7a5a1e;box-sizing:border-box;text-align:center;font:700 8px/16px ui-sans-serif,system-ui,sans-serif; letter-spacing:.04em;color:#e6b450;background:#141414;opacity:0} .tb-coin-wrap[data-tb-credit=true] .tb-coin-credit{opacity:1} .tb-coin-start{position:absolute;left:20px;top:130px;width:180px;height:58px;box-sizing:border-box;border-radius:29px;padding:3px; background:linear-gradient(180deg,#9a9aa0,#3c3c40 48%,#1c1c1e 52%,#6a6a70);transition:transform 180ms cubic-bezier(.3,1.6,.5,1); box-shadow:0 0 0 1px #0c0c0d,0 0 0 4px #6c6c72,0 0 0 5px #0a0a0b,0 -1px 0 5px rgba(255,255,255,.18),0 6px 10px 4px rgba(0,0,0,.55)} .tb-coin-btn:active:not(:disabled) .tb-coin-start{transform:translateY(2px);transition:transform 60ms ease-out} .tb-coin-btn:active:not(:disabled) .tb-coin-face-idle{box-shadow:inset 0 3px 6px rgba(40,0,0,.6),inset 0 -2px 4px rgba(255,255,255,.08);filter:brightness(.88)} .tb-coin-face-idle::after{content:"";position:absolute;left:10%;right:10%;top:3px;height:19px;border-radius:50%;background:radial-gradient(100% 120% at 50% 0%,rgba(255,255,255,.36) 0%,rgba(255,255,255,.1) 50%,rgba(255,255,255,0) 75%)} .tb-coin-face-idle::before{content:"";position:absolute;left:26%;right:26%;bottom:3px;height:5px;border-radius:50%;background:radial-gradient(closest-side,rgba(255,150,130,.45),rgba(255,150,130,0))} .tb-coin-face{position:absolute;inset:3px;border-radius:26px;overflow:hidden} .tb-coin-face-idle{background:repeating-linear-gradient(115deg,rgba(0,0,0,.05) 0 1px,transparent 1px 4px),radial-gradient(120% 70% at 50% 0%,rgba(255,255,255,.22),transparent 55%),linear-gradient(180deg,#c81d1f 0%,var(--tb-coin-button,#b3161a) 40%,#7d0a0b 100%);box-shadow:inset 0 2px 1px rgba(255,255,255,.28),inset 0 -6px 10px rgba(40,0,0,.55),inset 0 0 0 1px rgba(0,0,0,.4);transition:filter .18s} .tb-coin-label{position:absolute;inset:0;display:flex;align-items:center;justify-content:center;font-size:27px;letter-spacing:.07em;text-transform:uppercase;line-height:1; color:#f2e2c2;text-shadow:0 1px 0 #5a0506,0 2px 3px rgba(0,0,0,.45);transition:opacity .2s} .tb-coin-face-lit{background:radial-gradient(ellipse 70% 80% at 50% 50%,#a8120c 0%,#6e0806 70%,#4a0403 100%);opacity:0;transition:opacity .2s;overflow:visible; box-shadow:inset 0 0 0 2px #ff6a48,inset 0 0 10px 2px rgba(255,70,40,.75),0 0 16px rgba(255,40,30,.75),0 0 40px rgba(255,40,30,.35)} .tb-coin-face-lit::before{content:"";position:absolute;inset:-10px -16px;border-radius:40px;background:repeating-conic-gradient(from 0deg,rgba(255,80,40,0) 0 8deg,rgba(255,80,40,.35) 8deg 9deg); -webkit-mask:radial-gradient(closest-side,transparent 78%,#000 82%,transparent 100%);mask:radial-gradient(closest-side,transparent 78%,#000 82%,transparent 100%);pointer-events:none} .tb-coin-ready{position:absolute;inset:0;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:2px;color:#ffe2b0} .tb-coin-ready-sub{font:700 10px/1 ui-sans-serif,system-ui,sans-serif;letter-spacing:.14em;color:#ffd6a0;text-shadow:0 0 3px rgba(255,120,50,.7);margin-bottom:1px} .tb-coin-ready-main{font:900 22px/1 "Impact","Haettenschweiler","Arial Narrow Bold",ui-sans-serif,sans-serif;letter-spacing:.12em;text-transform:uppercase;color:#ffeccc;text-shadow:0 0 3px rgba(255,170,80,.85),0 0 10px rgba(255,70,30,.5)} .tb-coin-chev{position:absolute;top:50%;margin-top:-6px;width:14px;height:12px;filter:drop-shadow(0 0 3px #ff7a2a)} .tb-coin-chev svg{display:block} .tb-coin-chev-l{left:18px}.tb-coin-chev-r{right:18px;transform:scaleX(-1)} .tb-coin-wrap[data-tb-phase=ready] .tb-coin-chev{animation:tb-coin-blink .6s steps(1,end) infinite} .tb-coin-wrap[data-tb-phase=ready] .tb-coin-face-lit,.tb-coin-wrap[data-tb-phase=play] .tb-coin-face-lit{opacity:0} .tb-coin-wrap[data-tb-phase=ready] .tb-coin-face-lit{opacity:1;animation:tb-coin-breathe .8s ease-in-out infinite alternate} .tb-coin-wrap[data-tb-phase=ready] .tb-coin-label{opacity:0} .tb-coin-btn:hover .tb-coin-face-idle{filter:brightness(1.1)} .tb-coin-layer{position:absolute;inset:0;pointer-events:none;clip-path:inset(-140px -40px ${H - SLOT_TOP}px -40px)} .tb-coin-ring{position:absolute;left:${SLOT_X - 24}px;top:-13px;width:48px;height:11px;border-radius:50%;background:none;border:1.5px solid rgba(255,226,140,1);box-sizing:border-box; box-shadow:0 0 6px rgba(255,200,80,.85),0 0 14px rgba(255,170,40,.45),inset 0 0 4px rgba(255,210,110,.6);opacity:0;transition:opacity .16s} .tb-coin-ring::after{content:"";position:absolute;left:7px;right:7px;top:5px;height:6px;border-radius:50%;border:1px solid rgba(255,226,140,.75)} .tb-coin-ring::before{content:"";position:absolute;left:10px;right:10px;bottom:50%;height:16px;background:linear-gradient(to top,rgba(255,214,110,.38),rgba(255,214,110,0));clip-path:polygon(0 100%,100% 100%,78% 0,22% 0)} .tb-coin-drop{position:absolute;left:${SLOT_X - COIN / 2}px;top:0;width:${COIN}px;height:${COIN}px;opacity:0;scale:.78;translate:0 14px; transition:opacity .16s,scale .32s cubic-bezier(.34,1.56,.64,1),translate .32s cubic-bezier(.34,1.56,.64,1); transform:translateY(${HOVER_Y - COIN / 2}px)} .tb-coin-streaks{position:absolute;left:50%;bottom:100%;width:0;height:0;opacity:0} .tb-coin-streaks i{position:absolute;bottom:2px;width:2px;border-radius:1px;background:linear-gradient(to top,rgba(255,218,110,.9),rgba(255,218,110,0));box-shadow:0 0 4px rgba(255,200,80,.6)} .tb-coin-streaks i:nth-child(1){left:-10px;height:24px}.tb-coin-streaks i:nth-child(2){left:-1px;height:40px}.tb-coin-streaks i:nth-child(3){left:8px;height:28px} .tb-coin-bob{display:block;position:relative;width:100%;height:100%} .tb-coin-bloom{position:absolute;left:50%;top:50%;width:108px;height:108px;margin:-54px 0 0 -54px;border-radius:50%;pointer-events:none;background:radial-gradient(closest-side,rgba(255,220,110,.55) 0%,rgba(255,180,50,.24) 42%,rgba(255,160,40,.08) 70%,rgba(255,160,40,0) 100%)} .tb-coin-bob svg{display:block;overflow:visible} .tb-coin-bob svg{transform:perspective(240px) rotateY(-30deg);animation:tb-coin-rock 2.4s ease-in-out infinite alternate paused} .tb-coin-lip{transform:translateX(2.6px);animation:tb-coin-lip 2.4s ease-in-out infinite alternate paused} .tb-coin-wrap[data-tb-phase=idle] .tb-coin-btn:hover .tb-coin-drop,.tb-coin-wrap[data-tb-phase=idle] .tb-coin-btn:hover .tb-coin-ring{opacity:1} .tb-coin-wrap[data-tb-phase=idle] .tb-coin-btn:hover .tb-coin-bob{animation:tb-coin-bob 1.4s ease-in-out infinite} .tb-coin-wrap[data-tb-phase=idle] .tb-coin-btn:hover .tb-coin-drop{scale:1;translate:0 0} .tb-coin-wrap[data-tb-phase=idle] .tb-coin-btn:hover .tb-coin-bob svg,.tb-coin-wrap[data-tb-phase=idle] .tb-coin-btn:hover .tb-coin-lip{animation-play-state:running} .tb-coin-wrap[data-tb-phase=idle] .tb-coin-btn:hover .tb-coin-ring{animation:tb-coin-ringpulse 1.4s ease-in-out infinite} .tb-coin-wrap[data-tb-coin=drop] .tb-coin-drop{scale:1;translate:0 0;transition:none} .tb-coin-wrap[data-tb-coin=drop] .tb-coin-bob{animation:none} .tb-coin-wrap[data-tb-coin=drop] .tb-coin-bob svg{animation:none;transform:perspective(240px) rotateY(58deg)} .tb-coin-wrap[data-tb-coin=drop] .tb-coin-lip{animation:none;transform:translateX(-4px)} @keyframes tb-coin-pulse{0%,100%{opacity:.75}50%{opacity:1}} @keyframes tb-coin-blink{0%,60%{opacity:1}61%,100%{opacity:.35}} @keyframes tb-coin-breathe{from{filter:brightness(.92)}to{filter:brightness(1.12)}} @keyframes tb-coin-bob{0%,100%{transform:translateY(-4px)}50%{transform:translateY(4px)}} @keyframes tb-coin-rock{from{transform:perspective(240px) rotateY(-30deg)}to{transform:perspective(240px) rotateY(30deg)}} @keyframes tb-coin-lip{from{transform:translateX(2.6px)}to{transform:translateX(-2.6px)}} @keyframes tb-coin-ringpulse{0%,100%{transform:scale(.8);opacity:.55}50%{transform:scale(1);opacity:1}} .tb-coin-wrap[data-tb-paused=true] *{animation-play-state:paused!important} @media (prefers-reduced-motion:reduce){.tb-coin-wrap *,.tb-coin-wrap *::before,.tb-coin-wrap *::after{animation:none!important;transition:none!important} .tb-coin-wrap .tb-coin-drop,.tb-coin-wrap .tb-coin-ring{opacity:0!important}} `;