"use client"; /** * stamp.tsx — a chunky wooden rubber stamp resting on a cardboard card. * Hover lifts it to show the inked face; click slams down a crooked red APPROVED mark that fades before the next one. * * Part of toy-buttons. MIT licensed. Only depends on React. */ import { useEffect, useId, useImperativeHandle, useRef, useState, type ButtonHTMLAttributes, type MouseEvent, type Ref } from "react"; type Palette = { card: string; wood: string; ink: string }; type Props = Omit, "children"> & { children?: string; ref?: Ref; stampText?: string; palette?: Palette; }; const W = 250; const H = 84; const PADX = 20; const PADT = 204; const PADB = 30; const SW = W + 2 * PADX; const SH = PADT + H + PADB; const CX = PADX + W / 2; // card centre x const CARD_TOP = PADT; const SLAB = 7; const STAMP_Y = CARD_TOP + 6; // origin of the stamp art: bottom of the wood block const IMPACT_Y = CARD_TOP + H / 2 - SLAB / 2; // card centre const DEFAULT_PALETTE: Palette = { card: "#e0d3bf", wood: "#9a5b29", ink: "#d42a2a" }; const REST = { y: 0, a: 0 }; const HOVER = { y: -56, a: -14 }; const LIFT = { y: -42, a: -12 }; const pose = (p: { y: number; a: number }) => `translate(0px,${p.y}px) rotate(${p.a}deg)`; const BW = 160; const BH = 44; const NECK = "M-20 -45C-20 -50 -10 -51 -10 -55C-10 -57 -13.5 -57.5 -13.5 -60.5C-13.5 -63.5 -9 -64 -9 -67L9 -67C9 -64 13.5 -63.5 13.5 -60.5C13.5 -57.5 10 -57 10 -55C10 -51 20 -50 20 -45Z"; const PEAR = "M0 -119C15.5 -119 25 -108 25 -95C25 -84 18 -76 11 -71C8.5 -69.2 8 -68 8 -67L-8 -67C-8 -68 -8.5 -69.2 -11 -71C-18 -76 -25 -84 -25 -95C-25 -108 -15.5 -119 0 -119Z"; const TOPF = 9; const T_RESET = 2300; const T_END = 3000; export function StampButton({ children = "approve", stampText = "APPROVED", palette = DEFAULT_PALETTE, onClick, className, type = "button", disabled = false, ref, ...rest }: Props) { const btnRef = useRef(null); const wrapRef = useRef(null); const liftRef = useRef(null); const cardRef = useRef(null); const labelRef = useRef(null); const ticksRef = useRef(null); const dieRef = useRef(null); const shadowRef = useRef(null); const wobRef = useRef(null); const slot0 = useRef(null); const slot1 = useRef(null); const slotIn0 = useRef(null); const slotIn1 = useRef(null); const playRef = useRef<() => void>(() => {}); const [playing, setPlaying] = useState(false); const uid = `tb-stamp-${useId().replace(/[^a-zA-Z0-9_-]/g, "")}`; useImperativeHandle(ref, () => btnRef.current!, []); useEffect(() => { const el = btnRef.current; const host = wrapRef.current; if (!el || !host) return; const media = window.matchMedia("(prefers-reduced-motion: reduce)"); const anims = new Map(); const timers = new Set(); let epoch = 0; let busy = false; let nextSlot = 0; const slotUsed = [false, false]; let visible = true; const run = (key: string, target: Element | null, frames: Keyframe[], opts: KeyframeAnimationOptions) => { if (!target) return null; anims.get(key)?.cancel(); const a = target.animate(frames, opts); anims.set(key, a); return a; }; const later = (fn: () => void, ms: number) => { const ep = epoch; const id = window.setTimeout(() => { timers.delete(id); if (ep === epoch) fn(); }, ms); timers.add(id); }; const clearAll = () => { epoch++; delete host.dataset.tbLand; timers.forEach((id) => window.clearTimeout(id)); timers.clear(); anims.forEach((a) => a.cancel()); anims.clear(); busy = false; slotUsed[0] = slotUsed[1] = false; setPlaying(false); }; const refresh = () => { host.dataset.tbPaused = String(disabled || !visible || document.hidden); }; const io = typeof IntersectionObserver === "undefined" ? null : new IntersectionObserver(([en]) => { visible = en.isIntersecting; refresh(); }); io?.observe(host); document.addEventListener("visibilitychange", refresh); const mediaChange = () => { clearAll(); }; media.addEventListener("change", mediaChange); refresh(); playRef.current = () => { if (disabled || el.matches(":disabled")) return; const reduced = media.matches; const again = busy; // a repeat click restarts the slam; the older impression drops to half strength epoch++; delete host.dataset.tbLand; timers.forEach((id) => window.clearTimeout(id)); timers.clear(); busy = true; setPlaying(true); const slot = nextSlot; nextSlot = 1 - nextSlot; const slots = [slot0.current, slot1.current]; const inner = [slotIn0.current, slotIn1.current]; const old = 1 - slot; if (again && slotUsed[old] === false && slotUsed[slot]) { // the previous impression sits in `slot`'s partner; handled below by index swap } // older visible impression const other = slotUsed[old] ? old : slotUsed[slot] ? slot : -1; if (other >= 0 && other !== slot) { run(`imp${other}`, inner[other], [{ opacity: 0.5 }, { opacity: 0.5, offset: T_RESET / T_END }, { opacity: 0 }], { duration: T_END, fill: "both" }); } else if (other === slot) { anims.get(`imp${slot}`)?.cancel(); } slotUsed[slot] = true; const rot = -9 + Math.random() * 5; const ox = (Math.random() - 0.5) * 8, oy = (Math.random() - 0.5) * 4; slots[slot]?.setAttribute("transform", `translate(${CX + ox} ${CARD_TOP + H / 2 - 2 + oy}) rotate(${rot.toFixed(2)})`); // label hides under the ink and comes back at the end const lblDelay = reduced ? 0 : 110; const lblDur = T_END - lblDelay; run("label", labelRef.current, [ { opacity: 1 }, { opacity: 0, offset: (reduced ? 60 : 50) / lblDur }, { opacity: 0, offset: (2600 - lblDelay) / lblDur }, { opacity: 1 }, ], { duration: lblDur, delay: lblDelay, fill: "both" }); if (reduced) { const dur = T_RESET; run(`imp${slot}`, inner[slot], [ { opacity: 0 }, { opacity: 1, offset: 150 / dur }, { opacity: 1, offset: 1 }, ], { duration: dur, delay: 0, fill: "both" }); later(() => { run(`imp${slot}`, inner[slot], [{ opacity: 1 }, { opacity: 0 }], { duration: 600, fill: "both" }); }, T_RESET); later(() => { delete host.dataset.tbLand; busy = false; slotUsed[0] = slotUsed[1] = false; setPlaying(false); }, T_END); return; } const hov = el.matches(":hover"); const from = again ? LIFT : hov ? HOVER : REST; const fy = from.y; const D = 560; run("stamp", liftRef.current, [ { transform: pose(from), easing: "cubic-bezier(.3,.8,.4,1)" }, { transform: pose({ y: fy - 8, a: -18 }), offset: 70 / D, easing: "cubic-bezier(.7,0,1,.6)" }, { transform: pose({ y: 30, a: -7 }), offset: 170 / D, easing: "ease-in-out" }, { transform: pose({ y: 30, a: -5 }), offset: 215 / D, easing: "ease-in-out" }, { transform: pose({ y: 30, a: -7 }), offset: 260 / D, easing: "cubic-bezier(.2,.9,.3,1)" }, { transform: pose({ y: -49, a: -13 }), offset: 420 / D, easing: "ease-in-out" }, { transform: pose(LIFT) }, ], { duration: D, fill: "forwards" }); run("card", cardRef.current, [ { transform: "scale(1,1)" }, { transform: "scale(1.02,.94)", offset: 0.4 }, { transform: "scale(1,1)" }, ], { duration: 140, delay: 170, easing: "ease-out" }); run("die", dieRef.current, [ { transform: "scaleY(1)" }, { transform: "scaleY(1)", offset: 70 / 560 }, { transform: "scaleY(.1)", offset: 165 / 560 }, { transform: "scaleY(.1)", offset: 265 / 560 }, { transform: "scaleY(1)", offset: 380 / 560 }, { transform: "scaleY(1)" }, ], { duration: 560 }); const sh = (o: number, sc: string) => ({ opacity: o, transform: `translate(0px,32px) rotate(-7deg) ${sc}` }); run("shadow", shadowRef.current, [ sh(0, "scale(1.5,3)"), { ...sh(0.08, "scale(1.5,3)"), offset: 70 / 560, easing: "cubic-bezier(.7,0,1,.6)" }, { ...sh(0.55, "scale(1,1)"), offset: 170 / 560 }, { ...sh(0.55, "scale(1,1)"), offset: 260 / 560, easing: "ease-out" }, { ...sh(0, "scale(1.4,3)"), offset: 420 / 560 }, sh(0, "scale(1.4,3)"), ], { duration: 560 }); run("squash", wobRef.current, [ { transform: "scale(1,1)" }, { transform: "scale(1.025,.93)", offset: 0.3 }, { transform: "scale(.995,1.012)", offset: 0.7 }, { transform: "scale(1,1)" }, ], { duration: 150, delay: 170, easing: "ease-out" }); ticksRef.current?.querySelectorAll(".tb-stamp-tick").forEach((ln, i) => { run(`tick${i}`, ln, [ { transform: "translateX(-2px) scaleX(.3)", opacity: 0 }, { transform: "translateX(4px) scaleX(1)", opacity: 1, offset: 0.22 }, { transform: "translateX(16px) scaleX(.6)", opacity: 0 }, ], { duration: 280, delay: 170, easing: "cubic-bezier(.2,.7,.3,1)", fill: "forwards" }); }); const dur = T_END - 250; run(`imp${slot}`, inner[slot], [ { opacity: 0 }, { opacity: 1, offset: 70 / dur }, { opacity: 1, offset: (T_RESET - 250) / dur }, { opacity: 0 }, ], { duration: dur, delay: 250, fill: "both" }); later(() => { const target = el.matches(":hover") ? HOVER : REST; const settle = { y: target.y + (target === REST ? 2 : 0), a: target.a }; const a = run("stamp", liftRef.current, [ { transform: pose(LIFT), easing: "cubic-bezier(.3,.7,.3,1)" }, { transform: pose(settle), offset: 0.72, easing: "ease-out" }, { transform: pose(target) }, ], { duration: 420, fill: "forwards" }); if (a) a.onfinish = () => { if (anims.get("stamp") === a) { a.cancel(); anims.delete("stamp"); } }; }, T_RESET); later(() => { host.dataset.tbLand = "1"; }, T_RESET + 140); later(() => { delete host.dataset.tbLand; busy = false; slotUsed[0] = slotUsed[1] = false; setPlaying(false); }, T_END); }; return () => { playRef.current = () => {}; io?.disconnect(); document.removeEventListener("visibilitychange", refresh); media.removeEventListener("change", mediaChange); epoch++; timers.forEach((id) => window.clearTimeout(id)); timers.clear(); anims.forEach((a) => a.cancel()); anims.clear(); }; }, [disabled]); const click = (e: MouseEvent) => { if (disabled || e.currentTarget.matches(":disabled")) return; onClick?.(e); if (e.defaultPrevented) return; playRef.current(); }; const ink = palette.ink; const bw = 152; const bh = 44; const tw = bw - 26; const fillInk = `url(#${uid}-ink)`; const specks: [number, number, number][] = [[-bw / 2 - 3, -8, 0.9], [-bw / 2 + 22, bh / 2 + 2.6, 0.7], [bw / 2 + 2.4, 6, 1.1], [bw / 2 - 30, -bh / 2 - 2.6, 0.8], [-12, bh / 2 + 3, 0.6], [36, -bh / 2 - 3.2, 0.9], [-bw / 2 + 6, -bh / 2 - 1.8, 0.6], [bw / 2 - 8, bh / 2 + 2, 0.7], [4, -bh / 2 - 2, 0.5]]; const impression = ( <> {specks.map(([x, y, r], i) => )} {stampText} ); const ticks: [number, number, number, number][] = [[CX - 96, CARD_TOP - 4, -150, 18], [CX - 96, CARD_TOP - 4, -122, 14], [CX - 96, CARD_TOP - 4, 168, 10], [CX + 96, CARD_TOP - 10, -30, 18], [CX + 96, CARD_TOP - 10, -58, 14], [CX + 96, CARD_TOP - 10, 12, 10]]; const labelStyle = { font: '800 21px ui-sans-serif,system-ui,-apple-system,"Segoe UI",sans-serif', letterSpacing: ".08em", textTransform: "uppercase" as const }; const FH = H - SLAB; return ( ); } export default StampButton; const css = ` .tb-stamp-wrap{display:inline-flex;position:relative;vertical-align:middle} .tb-stamp-btn{position:relative;display:block;box-sizing:border-box;width:${W}px;height:${H}px;padding:0;border:0;border-radius:${H / 2}px; background:transparent;overflow:visible;cursor:pointer;-webkit-tap-highlight-color:transparent;color:#2b2b2b} .tb-stamp-btn:focus-visible{outline:3px solid #e8603c;outline-offset:6px} .tb-stamp-btn:disabled{cursor:not-allowed} .tb-stamp-btn:disabled .tb-stamp-art{opacity:.6;filter:saturate(.5)} .tb-stamp-art{position:absolute;left:-${PADX}px;top:-${PADT}px;pointer-events:none;overflow:visible;max-width:none} .tb-stamp-card{transform-origin:${CX}px ${CARD_TOP + H}px} .tb-stamp-lift{transform-origin:0 0;transform:${pose(REST)};transition:transform 340ms cubic-bezier(.34,1.45,.5,1)} .tb-stamp-wob{transform-origin:0 0} .tb-stamp-die{opacity:0;transform:scaleY(.3);transform-origin:0 4px;transition:opacity 220ms ease-out,transform 340ms cubic-bezier(.34,1.45,.5,1)} .tb-stamp-shadow{opacity:.34;transform-box:fill-box;transform-origin:center;transition:opacity 280ms ease-out,transform 340ms cubic-bezier(.34,1.45,.5,1)} .tb-stamp-hsh{opacity:0;transition:opacity 300ms ease-out} .tb-stamp-btn:not(:disabled):hover .tb-stamp-hsh{opacity:.26} .tb-stamp-imp{opacity:0;transform-box:fill-box;transform-origin:center} .tb-stamp-tick{opacity:0;transform-box:fill-box;transform-origin:left center} .tb-stamp-btn:not(:disabled):hover .tb-stamp-lift{transform:${pose(HOVER)}} .tb-stamp-btn:not(:disabled):hover .tb-stamp-die,.tb-stamp-wrap[data-tb-state=playing]:not([data-tb-land]) .tb-stamp-die{opacity:1;transform:scaleY(1)} .tb-stamp-btn:not(:disabled):hover .tb-stamp-shadow{opacity:0;transform:translate(6px,4px) scale(1.15,1.6)} .tb-stamp-btn:not(:disabled):hover .tb-stamp-wob{animation:tb-stamp-wob 1.6s ease-in-out infinite} .tb-stamp-wrap[data-tb-state=playing] .tb-stamp-wob{animation:none} .tb-stamp-wrap[data-tb-state=playing]:not([data-tb-land]) .tb-stamp-shadow,.tb-stamp-wrap[data-tb-state=playing] .tb-stamp-hsh{opacity:0} @keyframes tb-stamp-wob{0%,100%{transform:rotate(-1.5deg)}50%{transform:rotate(1.5deg)}} .tb-stamp-wrap[data-tb-paused=true] *{animation-play-state:paused!important} @media (prefers-reduced-motion:reduce){.tb-stamp-wrap *,.tb-stamp-wrap *::before,.tb-stamp-wrap *::after{animation:none!important;transition:none!important} .tb-stamp-btn:not(:disabled):hover .tb-stamp-lift{transform:${pose(REST)}} .tb-stamp-btn:not(:disabled):hover .tb-stamp-die{opacity:0;transform:scaleY(.3)} .tb-stamp-btn:not(:disabled):hover .tb-stamp-shadow{opacity:.34;transform:none} .tb-stamp-btn:not(:disabled):hover .tb-stamp-hsh{opacity:0} .tb-stamp-wrap[data-tb-state=playing] .tb-stamp-die{opacity:0;transform:scaleY(.3)}} `;