"use client"; /** * plane.tsx — a notebook-paper "send" button. * Click it and it folds itself into a paper plane, loops around, and lands back as "sent". * * Part of toy-buttons. MIT licensed. Only depends on React. */ import { useEffect, useId, useRef, useState, useImperativeHandle, type ButtonHTMLAttributes, type Ref, type MouseEvent, type ReactNode, } from "react"; type Props = Omit, "children"> & { children?: ReactNode; ref?: Ref; sentLabel?: ReactNode; /** SVG path the plane flies, relative to the button's center. Must start and end at 0,0. */ flightPath?: string; }; const FLIGHT = "M0 0C60-4 120-30 160-80C200-130 200-190 150-195C100-200 90-130 150-115" + "C230-95 300-130 310-190C320-250-280-275-320-175C-350-90-170-4 0 0"; export function PlaneButton({ children = "send", sentLabel = "sent", flightPath = FLIGHT, onClick, className, type = "button", disabled = false, ref, ...rest }: Props) { const btnRef = useRef(null); useImperativeHandle(ref, () => btnRef.current!, []); const [phase, setPhase] = useState<"idle" | "folded" | "flying" | "sent">("idle"); const busy = useRef(false); const work = useWork(disabled, () => { busy.current = false; setPhase("idle"); if (trailSvgRef.current) trailSvgRef.current.style.opacity = "0"; }); const flightRef = useRef(null); const trailRef = useRef(null); const maskRef = useRef(null); const trailSvgRef = useRef(null); const uid = `tbp-${useId()}`; const send = (e: MouseEvent) => { if (disabled || e.currentTarget.matches(":disabled")) return; onClick?.(e); if (e.defaultPrevented) return; const plane = flightRef.current; const path = trailRef.current; const mask = maskRef.current; const trail = trailSvgRef.current; if (busy.current || phase !== "idle" || !plane || !path || !mask || !trail) return; busy.current = true; const reduced = work.reduced(); if (reduced) { setPhase("sent"); work.later(() => { setPhase("idle"); busy.current = false; }, 1400); return; } setPhase("folded"); work.later(() => { setPhase("flying"); // sample the path into keyframes, rotating the nose along the tangent const len = path.getTotalLength(); const N = 120; const pts = Array.from({ length: N + 1 }, (_, i) => path.getPointAtLength((len * i) / N)); let angle = 0; let prev = 0; const frames: Keyframe[] = pts.map((p, i) => { const a = pts[Math.min(i + 1, N)]; const b = pts[Math.max(i - 1, 0)]; let raw = (Math.atan2(a.y - b.y, a.x - b.x) * 180) / Math.PI; if (i === 0) prev = raw; while (raw - prev > 180) raw -= 360; while (raw - prev < -180) raw += 360; prev = raw; angle = i === N ? 0 : raw; const lift = 1 + 0.35 * Math.sin((Math.PI * i) / N); return { transform: `translate(${p.x.toFixed(1)}px,${p.y.toFixed(1)}px) rotate(${angle.toFixed(1)}deg) scale(${lift.toFixed(3)})` }; }); // little wind-up before takeoff frames.unshift({ transform: "translate(0px,0px) rotate(0deg) scale(1)", offset: 0 }); frames.splice(1, 0, { transform: "translate(-12px,3px) rotate(6deg) scale(.96)", offset: 0.04 }); frames.forEach((f, i) => i > 1 && (f.offset = 0.08 + ((i - 2) / (frames.length - 3)) * 0.92)); const timing: KeyframeAnimationOptions = { duration: 2900, easing: "cubic-bezier(.45,.05,.4,1)" }; mask.style.strokeDasharray = `${len}`; work.cancelAnimations(trail); trail.style.opacity = "1"; work.animate(mask, [ { strokeDashoffset: len, offset: 0 }, { strokeDashoffset: len, offset: 0.08 }, { strokeDashoffset: 0 }, ], { ...timing, fill: "forwards" }, ); work.animate(plane, frames, timing, () => { work.animate(trail, [{ opacity: 1 }, { opacity: 0 }], { duration: 500, fill: "forwards" }); setPhase("sent"); work.later(() => { setPhase("idle"); busy.current = false; }, 1500); }); }, 420); }; const folded = phase === "folded" || phase === "flying"; return ( ); } export default PlaneButton; // Each file stays copyable: all animation ownership lives with this instance. function useWork(disabled: boolean, reset: () => void) { const state = useRef({ epoch: 0, active: false, timers: new Set(), animations: new Map() }); const resetRef = useRef(reset); resetRef.current = reset; useEffect(() => { const s = state.current; const clear = () => { s.epoch++; s.timers.forEach((id) => window.clearTimeout(id)); s.timers.clear(); s.animations.forEach(({ el, remove }, a) => { a.cancel(); if (remove) el.remove(); }); s.animations.clear(); }; s.active = !disabled; if (disabled) resetRef.current(); const media = window.matchMedia("(prefers-reduced-motion: reduce)"); const change = () => { clear(); resetRef.current(); }; media.addEventListener("change", change); return () => { s.active = false; clear(); media.removeEventListener("change", change); }; }, [disabled]); const later = (done: () => void, ms: number) => { const s = state.current; const epoch = s.epoch; const id = window.setTimeout(() => { s.timers.delete(id); if (s.active && s.epoch === epoch) done(); }, ms); s.timers.add(id); return id; }; const animate = (el: Element | null, frames: Keyframe[], timing: KeyframeAnimationOptions, done?: () => void, remove = false) => { const s = state.current; if (!s.active) return; s.animations.forEach((entry, a) => { if (entry.el === el) { a.cancel(); s.animations.delete(a); } }); if (!el) { done?.(); return; } const epoch = s.epoch; const a = el.animate(frames, timing); s.animations.set(a, { el, remove }); const finish = () => { if (remove) { el.remove(); a.cancel(); } if (remove || done || (timing.fill !== "forwards" && timing.fill !== "both")) s.animations.delete(a); }; a.finished.then(() => { finish(); if (s.active && s.epoch === epoch) done?.(); if (done) a.cancel(); }, () => { if (remove) el.remove(); s.animations.delete(a); }); }; const cancelAnimations = (el: Element) => { state.current.animations.forEach((entry, a) => { if (entry.el === el) { a.cancel(); if (entry.remove) el.remove(); state.current.animations.delete(a); } }); }; return { later, animate, cancelAnimations, cancel: (id: number) => { window.clearTimeout(id); state.current.timers.delete(id); }, reduced: () => window.matchMedia("(prefers-reduced-motion: reduce)").matches, }; } const css = ` .tbp-wrap{position:relative;display:inline-flex} .tbp-btn{position:relative;width:168px;height:56px;padding:0;border:0;background:none;cursor:pointer;-webkit-tap-highlight-color:transparent; font:650 18px/1 ui-rounded,"SF Pro Rounded","Nunito","Segoe UI",system-ui,sans-serif;color:#24304a;z-index:1} .tbp-btn::before{content:"";position:absolute;inset:0;border-radius:10px;border:1.5px dashed rgba(36,48,74,.22);opacity:0;transition:opacity .3s} .tbp-btn[data-phase=flying]::before{opacity:1} .tbp-btn:focus-visible{outline:2px solid #5b8def;outline-offset:3px;border-radius:10px} .tbp-btn:focus-visible .tbp-paper{outline:2px solid #5b8def;outline-offset:3px} .tbp-flight{position:absolute;inset:0;filter:drop-shadow(0 0 .6px rgba(36,48,74,.45)) drop-shadow(0 8px 10px rgba(30,50,90,.18)) drop-shadow(0 1px 1px rgba(30,50,90,.18)); transition:transform .25s cubic-bezier(.3,1.6,.5,1)} .tbp-btn[data-phase=idle]:enabled:hover .tbp-flight{transform:translateY(-2px) rotate(-1deg)} .tbp-btn[data-phase=idle]:enabled:active .tbp-flight{transform:translateY(1px) scale(.98)} .tbp-paper{position:absolute;inset:0;border-radius:10px;display:flex;align-items:center;justify-content:center; clip-path:polygon(0% 0%,50% 0%,100% 0%,100% 100%,0% 100%,0% 50%); background:linear-gradient(90deg,transparent 20px,rgba(232,96,96,.6) 20px 21.5px,transparent 21.5px), repeating-linear-gradient(180deg,transparent 0 11px,rgba(80,140,220,.26) 11px 12px),#fffdf7; transition:clip-path .38s cubic-bezier(.6,0,.3,1),transform .38s cubic-bezier(.6,0,.3,1.25),border-radius .2s} .tbp-paper.is-folded{clip-path:polygon(0% 0%,50% 25%,100% 50%,100% 50%,0% 100%,22% 50%);transform:scale(.44,.8);border-radius:0} .tbp-shade{position:absolute;inset:0;opacity:0;transition:opacity .3s .1s; background:linear-gradient(180deg,rgba(255,255,255,0) 49%,rgba(36,60,110,.35) 49.5% 50.5%,rgba(36,60,110,.16) 51%)} .tbp-paper.is-folded .tbp-shade{opacity:1} .tbp-label{position:relative;display:inline-flex;align-items:center;gap:7px;transition:opacity .15s} .tbp-paper.is-folded .tbp-label{opacity:0} .tbp-icon{width:19px;height:19px} .tbp-icon-plane{transition:transform .3s cubic-bezier(.3,1.6,.5,1)} .tbp-btn[data-phase=idle]:enabled:hover .tbp-icon-plane{transform:translate(2px,-2px) rotate(-8deg)} .tbp-trail{position:absolute;left:50%;top:50%;width:1px;height:1px;overflow:visible;pointer-events:none;opacity:0;z-index:0} .tbp-trail>path{fill:none;stroke:rgba(36,48,74,.35);stroke-width:2.2;stroke-linecap:round;stroke-dasharray:.1 9} .tbp-btn:disabled{cursor:default;transition:none} .tbp-wrap[data-tb-paused=true] *{animation-play-state:paused!important} .tbp-btn:disabled *{transition:none!important;animation:none!important} @media (prefers-reduced-motion:reduce){.tbp-wrap *, .tbp-wrap *::before, .tbp-wrap *::after{animation:none!important;transition:none!important}} `;