"use client"; /** * ants.tsx — a little crew steals the letters, then the entire button. * Hover to put the label back; click and watch them carry it away and home. * * Part of toy-buttons. MIT licensed. Only depends on React. */ import { useEffect, useId, useImperativeHandle, useRef, useState, type ButtonHTMLAttributes, type CSSProperties, type MouseEvent, type ReactNode, type Ref, } from "react"; type Props = Omit, "children"> & { children?: ReactNode; ref?: Ref; }; type Variables = CSSProperties & Record<`--${string}`, string | number>; /** Two alternating tripods leave each set of feet planted between steps. */ function Ant({ className = "", style }: { className?: string; style?: CSSProperties }) { const id = `tba-ant-${useId().replace(/[^a-zA-Z0-9_-]/g, "")}`; return ( ); } export function AntsButton({ children = "submit", onClick, className, type = "button", disabled = false, ref, style, ...rest }: Props) { const buttonRef = useRef(null); useImperativeHandle(ref, () => buttonRef.current!, []); const [running, setRunning] = useState(false); const [paused, setPaused] = useState(false); const [travel, setTravel] = useState({ left: -520, right: 520, clip: 700 }); const runningRef = useRef(false); useEffect(() => { const button = buttonRef.current; if (!button) return; let visible = true; const update = () => setPaused(disabled || !visible || document.hidden); const observer = typeof IntersectionObserver === "undefined" ? null : new IntersectionObserver(([entry]) => { visible = entry.isIntersecting; update(); }); observer?.observe(button); document.addEventListener("visibilitychange", update); update(); return () => { observer?.disconnect(); document.removeEventListener("visibilitychange", update); }; }, [disabled]); useEffect(() => { const reset = () => { runningRef.current = false; setRunning(false); }; const media = window.matchMedia("(prefers-reduced-motion: reduce)"); media.addEventListener("change", reset); if (disabled) reset(); return () => { runningRef.current = false; media.removeEventListener("change", reset); }; }, [disabled]); const handleClick = (event: MouseEvent) => { onClick?.(event); if (event.defaultPrevented || disabled || runningRef.current || window.matchMedia("(prefers-reduced-motion: reduce)").matches) return; const button = event.currentTarget; const rect = button.getBoundingClientRect(); const scale = rect.width / button.offsetWidth || 1; const right = Math.ceil((window.innerWidth - rect.left) / scale + 80); const left = -Math.ceil(rect.right / scale + 80); setTravel({ left, right, clip: Math.max(right, -left) + 120 }); runningRef.current = true; setRunning(true); }; const text = typeof children === "string" || typeof children === "number" ? String(children) : null; const letters = text === null ? [] : Array.from(text); // Leave the first half in the button; at most three characters hitch a ride. const stolen = Math.min(3, Math.floor(letters.length / 2)); const buttonStyle = { "--tba-width": `${Math.max(252, letters.length * 18 + 104)}px`, "--tba-exit-left": `${travel.left}px`, "--tba-exit-right": `${travel.right}px`, "--tba-clip": `${travel.clip}px`, ...style, } as Variables; return ( ); } export default AntsButton; const css = ` .tba-btn{position:relative;display:inline-block;box-sizing:border-box;width:var(--tba-width,252px);height:82px;padding:0;border:0;border-radius:24px;background:transparent;color:#18191b;cursor:pointer;vertical-align:middle;-webkit-tap-highlight-color:transparent;font:900 27px/1 ui-sans-serif,system-ui,-apple-system,"Segoe UI",sans-serif;letter-spacing:-.035em} @supports (overflow:clip) and (overflow-clip-margin:1px){.tba-btn{overflow:clip;overflow-clip-margin:var(--tba-clip,700px)}} .tba-btn:focus-visible{outline:3px solid #555d68;outline-offset:6px} .tba-btn:disabled{cursor:default} .tba-sr{position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border:0} .tba-scene,.tba-rig,.tba-load,.tba-face,.tba-outline{position:absolute;inset:0;display:block;border-radius:inherit;pointer-events:none} .tba-scene{overflow:visible} .tba-outline{width:100%;height:100%;opacity:0;overflow:visible} .tba-rig{transform-origin:50% 80%;z-index:2} .tba-face{display:flex;align-items:center;justify-content:center;border:1px solid #d9dade;background:linear-gradient(155deg,#fff 0%,#fefefe 32%,#f8f8f8 66%,#ebebec 100%);box-shadow:inset 0 1.5px 1px #fff,inset 1px 1px 5px rgba(255,255,255,.95),inset 0 -4px 6px rgba(161,164,167,.19),0 5px 0 -2px #d5d6d7,0 9px 9px -6px rgba(39,42,47,.22),0 18px 24px -14px rgba(39,42,47,.24)} .tba-face:before{content:"";position:absolute;inset:2px 7px auto;height:30%;border-radius:30px 30px 50% 50%;background:linear-gradient(180deg,rgba(255,255,255,.98),rgba(255,255,255,0));pointer-events:none} .tba-label{position:relative;display:flex;align-items:center;justify-content:center;white-space:nowrap;text-shadow:0 1px 0 rgba(255,255,255,.95)} .tba-slot{position:relative;display:block;text-align:center} .tba-glyph,.tba-ink{position:relative;display:block} .tba-glyph-cargo{transition:transform .65s cubic-bezier(.22,.7,.28,1);transition-delay:var(--tba-delay,0ms)} .tba-glyph-cargo{transform:translate(var(--tba-shift),37px) rotate(var(--tba-tilt));z-index:3} .tba-glyph-cargo:after{content:"";position:absolute;left:8%;right:7%;bottom:-2px;height:4px;border-radius:50%;background:rgba(15,16,18,.32);filter:blur(2px);z-index:-1;transition:opacity .25s} .tba-glyph-cargo .tba-ink{animation:tba-cargo-bob 1.6s ease-in-out infinite alternate;animation-delay:calc(var(--tba-delay,0ms) * -1)} .tba-carrier{position:absolute;left:50%;top:52px;width:var(--tba-ant-width,48px);margin-left:-9px;transform:translateX(var(--tba-shift));transition:transform .65s cubic-bezier(.22,.7,.28,1),opacity .2s ease;transition-delay:var(--tba-delay,0ms),calc(var(--tba-delay,0ms) + 340ms)} .tba-btn:enabled:is(:hover,:focus-visible) .tba-glyph-cargo,.tba-btn[data-running=true] .tba-glyph-cargo{transform:translate(0,0) rotate(0)} .tba-btn:enabled:is(:hover,:focus-visible) .tba-glyph-cargo,.tba-btn[data-running=true] .tba-glyph-cargo{transition-duration:.68s;transition-timing-function:cubic-bezier(.25,1.35,.46,1);transition-delay:calc(240ms + var(--tba-delay,0ms))} .tba-btn:enabled:is(:hover,:focus-visible) .tba-glyph-cargo:after,.tba-btn[data-running=true] .tba-glyph-cargo:after{opacity:0;transition-delay:.72s} .tba-btn:enabled:is(:hover,:focus-visible) .tba-carrier,.tba-btn[data-running=true] .tba-carrier{transform:translate(0,-37px);opacity:0;transition-duration:.68s,.18s;transition-timing-function:cubic-bezier(.25,1.35,.46,1),ease;transition-delay:calc(240ms + var(--tba-delay,0ms)),calc(780ms + var(--tba-delay,0ms))} .tba-btn:enabled:is(:hover,:focus-visible) .tba-glyph-cargo .tba-ink,.tba-btn[data-running=true] .tba-glyph-cargo .tba-ink{animation:tba-drop .34s ease-out both;animation-delay:calc(870ms + var(--tba-delay,0ms))} .tba-dust{position:absolute;z-index:4;left:50%;bottom:0;width:3px;height:3px;border-radius:50%;background:#a6a8aa;box-shadow:-5px -1px 0 -0.2px #babcbf,5px -1px 0 -0.2px #babcbf,-8px 0 0 -0.7px #c6c7c9,8px 0 0 -0.7px #c6c7c9;opacity:0;pointer-events:none} .tba-btn:enabled:is(:hover,:focus-visible) .tba-dust,.tba-btn[data-running=true] .tba-dust{animation:tba-puff .45s ease-out both;animation-delay:calc(900ms + var(--tba-delay,0ms))} .tba-ant{display:block;width:100%;height:auto;overflow:visible} .tba-carrier .tba-ant{transform-origin:50% 50%;transition:transform .18s ease-in-out} .tba-btn:enabled:is(:hover,:focus-visible) .tba-carrier .tba-ant,.tba-btn[data-running=true] .tba-carrier .tba-ant{transform:rotateY(180deg);transition-delay:190ms} .tba-leg-a{animation:tba-tripod-a var(--tba-gait,340ms) ease-in-out infinite} .tba-leg-b{animation:tba-tripod-b var(--tba-gait,340ms) ease-in-out infinite} .tba-antennae{transform-origin:55px 22px;animation:tba-antenna-idle 2.4s ease-in-out infinite;animation-delay:calc(var(--tba-delay,0ms) * -1)} .tba-btn:enabled:is(:hover,:focus-visible) .tba-carrier .tba-leg-a,.tba-btn[data-running=true] .tba-carrier .tba-leg-a{animation:tba-panic-a .9s ease-in-out both;animation-delay:var(--tba-delay,0ms)} .tba-btn:enabled:is(:hover,:focus-visible) .tba-carrier .tba-leg-b,.tba-btn[data-running=true] .tba-carrier .tba-leg-b{animation:tba-panic-b .9s ease-in-out both;animation-delay:var(--tba-delay,0ms)} .tba-btn:enabled:is(:hover,:focus-visible) .tba-carrier .tba-antennae,.tba-btn[data-running=true] .tba-carrier .tba-antennae{animation:tba-antenna-alert .45s ease-in-out 2} .tba-squad{position:absolute;inset:0;opacity:0;pointer-events:none} .tba-worker{position:absolute;top:calc(100% - 20px);width:48px;animation:tba-tiny-bob var(--tba-gait,340ms) ease-in-out infinite} .tba-leader{left:calc(100% + 3px);width:42px} .tba-strays{position:absolute;inset:0;opacity:0;pointer-events:none;z-index:1} .tba-stray{position:absolute;left:73%;top:calc(100% - 2px);width:39px;transform-origin:50% 60%;animation:tba-look 2.2s ease-in-out infinite} .tba-stray .tba-leg-a,.tba-stray .tba-leg-b{animation-play-state:paused!important} .tba-squad .tba-ant,.tba-squad .tba-ant *,.tba-strays .tba-ant,.tba-strays .tba-ant *{animation-play-state:paused} .tba-btn[data-running=true] .tba-squad .tba-ant,.tba-btn[data-running=true] .tba-squad .tba-ant *,.tba-btn[data-running=true] .tba-strays .tba-ant,.tba-btn[data-running=true] .tba-strays .tba-ant *{animation-play-state:running} .tba-btn[data-running=true] .tba-rig{animation:tba-march 4.55s linear both} .tba-btn[data-running=true] .tba-load{animation:tba-load-bob 4.55s ease-in-out both} .tba-btn[data-running=true] .tba-squad{animation:tba-squad 4.55s ease-in-out both} .tba-btn[data-running=true] .tba-outline{animation:tba-vacant 4.55s ease-in-out both} .tba-btn[data-running=true] .tba-strays{animation:tba-stray-appear 4.55s ease-in-out both} .tba-btn:disabled .tba-carrier *{animation-play-state:paused} .tba-btn[data-paused=true] *{animation-play-state:paused!important} @keyframes tba-cargo-bob{from{transform:translateY(0) rotate(-1deg)}to{transform:translateY(-1px) rotate(1deg)}} @keyframes tba-drop{0%{transform:translateY(-1px)}35%{transform:translateY(2px) scaleY(.92)}65%{transform:translateY(-2px)}100%{transform:none}} @keyframes tba-puff{0%{opacity:0;transform:translate(-50%,0) scale(.4)}20%{opacity:.55}100%{opacity:0;transform:translate(-50%,-5px) scale(1.8)}} @keyframes tba-tripod-a{0%,40%{transform:translate(0,0)}50%{transform:translate(1.5px,-3px)}60%,90%{transform:translate(3px,0)}95%{transform:translate(1.5px,-3px)}100%{transform:translate(0,0)}} @keyframes tba-tripod-b{0%,15%{transform:translate(0,0)}25%{transform:translate(1.5px,-3px)}35%,65%{transform:translate(3px,0)}75%{transform:translate(1.5px,-3px)}85%,100%{transform:translate(0,0)}} @keyframes tba-panic-a{0%,26%{transform:translate(0,0)}32%{transform:translate(1.5px,-3px)}38%,42%{transform:translate(3px,0)}48%{transform:translate(1.5px,-3px)}54%,58%{transform:translate(0,0)}64%{transform:translate(1.5px,-3px)}70%,74%{transform:translate(3px,0)}80%{transform:translate(1.5px,-3px)}86%,100%{transform:translate(0,0)}} @keyframes tba-panic-b{0%,34%{transform:translate(3px,0)}40%{transform:translate(1.5px,-3px)}46%,50%{transform:translate(0,0)}56%{transform:translate(1.5px,-3px)}62%,66%{transform:translate(3px,0)}72%{transform:translate(1.5px,-3px)}78%,82%{transform:translate(0,0)}88%{transform:translate(1.5px,-3px)}94%,100%{transform:translate(3px,0)}} @keyframes tba-antenna-idle{0%,70%,100%{transform:rotate(0)}80%{transform:rotate(-6deg)}90%{transform:rotate(5deg)}} @keyframes tba-antenna-alert{0%,100%{transform:rotate(0)}20%{transform:rotate(-17deg)}45%{transform:rotate(12deg)}70%{transform:rotate(-9deg)}} @keyframes tba-tiny-bob{0%,100%{transform:translateY(0)}50%{transform:translateY(-1px)}} @keyframes tba-load-bob{0%,12%{transform:none}18%,26%,34%,42%,50%,58%,66%{transform:translateY(-2px) rotate(-.7deg)}22%,30%,38%,46%,54%,62%,70%{transform:translateY(0) rotate(.7deg)}75%,100%{transform:none}} @keyframes tba-march{0%,4%{transform:translate(0,0) rotate(0);animation-timing-function:cubic-bezier(.3,.8,.3,1)}13%{transform:translate(0,-15px) rotate(-2deg);animation-timing-function:ease-in-out}18%{transform:translate(0,-15px) rotate(1deg);animation-timing-function:cubic-bezier(.32,0,.28,1)}39%,49%{transform:translate(var(--tba-exit-right,520px),-15px) rotate(1deg)}49.01%{transform:translate(var(--tba-exit-left,-520px),-15px) rotate(-1deg);animation-timing-function:cubic-bezier(.32,0,.28,1)}72%,77%{transform:translate(0,-15px) rotate(-1deg);animation-timing-function:cubic-bezier(.45,0,.8,.6)}82%{transform:translate(0,2px) rotate(.4deg);animation-timing-function:ease-out}86%{transform:translate(0,-4px) rotate(-.2deg);animation-timing-function:ease-in}91%,100%{transform:translate(0,0) rotate(0)}} @keyframes tba-squad{0%,3%{opacity:0;transform:translateY(0)}8%{opacity:1;transform:translateY(6px)}13%,77%{opacity:1;transform:translateY(15px)}82%{opacity:.8;transform:translateY(-2px)}86%{opacity:.25;transform:translateY(4px)}91%,100%{opacity:0;transform:translateY(0)}} @keyframes tba-vacant{0%,7%{opacity:0}16%,70%{opacity:1}84%,100%{opacity:0}} @keyframes tba-stray-appear{0%,23%{opacity:0}30%,58%{opacity:1}68%,100%{opacity:0}} @keyframes tba-look{0%,20%{transform:rotateY(0)}30%,60%{transform:rotateY(180deg)}72%,100%{transform:rotateY(360deg)}} @media (prefers-reduced-motion:reduce){.tba-btn .tba-glyph-cargo{transform:none;transition:none}.tba-btn .tba-glyph-cargo:after,.tba-btn .tba-dust,.tba-btn .tba-carrier,.tba-btn .tba-squad,.tba-btn .tba-strays,.tba-btn .tba-outline{display:none}.tba-btn *{animation:none!important}} `;