"use client"; /** * toaster.tsx — a chrome toaster that toasts its own label. * Hover and the slot glows. Click: lever down, a moment of heat, then the toast pops with your label burnt in. * Keep going and it gets darker, then charcoal, then you get a fresh slice. * * Part of toy-buttons. MIT licensed. Only depends on React. */ import { useEffect, useId, useImperativeHandle, useRef, useState, type ButtonHTMLAttributes, type CSSProperties, type MouseEvent, type Ref, } from "react"; type Props = Omit, "children"> & { /** stamped on the toaster and burnt into the bread (plain text only) */ children?: string; ref?: Ref; /** ms the bread spends in the heat */ toastFor?: number; }; type Phase = "ready" | "heating" | "popped" | "fresh"; const BREAD = "M14 40C2 32 4 6 30 4 44-1 76-1 90 4c26 2 28 28 16 36l-2 60q0 4-4 4H20q-4 0-4-4Z"; const CHARRED = 3; export function ToasterButton({ children = "toast", toastFor = 1500, onClick, className, type = "button", disabled = false, ref, ...rest }: Props) { const btnRef = useRef(null); useImperativeHandle(ref, () => btnRef.current!, []); const [phase, setPhase] = useState("ready"); const [level, setLevel] = useState(0); // 0 = bread, 3 = charcoal const [slice, setSlice] = useState(0); const toastRef = useRef(null); const bodyRef = useRef(null); const timers = useRef(new Set()); const busy = useRef(false); const uid = `tbt-${useId().replace(/[^a-zA-Z0-9]/g, "")}`; const later = (fn: () => void, ms: number) => { const id = window.setTimeout(() => { timers.current.delete(id); fn(); }, ms); timers.current.add(id); }; useEffect(() => { const t = timers.current; return () => { t.forEach((id) => window.clearTimeout(id)); t.clear(); busy.current = false; }; }, []); useEffect(() => { if (!disabled) return; timers.current.forEach((id) => window.clearTimeout(id)); timers.current.clear(); busy.current = false; setPhase((p) => (p === "heating" ? "ready" : p)); }, [disabled]); const press = (e: MouseEvent) => { if (disabled) return; onClick?.(e); if (e.defaultPrevented || busy.current) return; busy.current = true; const reduced = window.matchMedia("(prefers-reduced-motion: reduce)").matches; if (level >= CHARRED) { // that one's done for. toss it and drop in a fresh slice const toss = toastRef.current?.animate( [ { transform: "translateY(-56px) rotate(0deg)", opacity: 1 }, { transform: "translate(70px,-150px) rotate(40deg)", opacity: 1, offset: 0.45 }, { transform: "translate(150px,0px) rotate(95deg)", opacity: 1, offset: 0.85 }, { transform: "translate(170px,40px) rotate(110deg)", opacity: 0 }, ], { duration: reduced ? 1 : 650, easing: "cubic-bezier(.3,.1,.6,1)", fill: "forwards" }, ); const swap = () => { toss?.cancel(); setLevel(0); setSlice((s) => s + 1); setPhase("fresh"); later(() => { setPhase("ready"); busy.current = false; }, reduced ? 0 : 700); }; if (toss) toss.finished.then(swap, swap); else swap(); return; } setPhase("heating"); later(() => { setLevel((l) => l + 1); setPhase("popped"); if (!reduced) { const spin = Math.random() > 0.5 ? 1 : -1; toastRef.current?.animate( [ { transform: "translateY(22px) rotate(0deg)" }, { transform: `translateY(-170px) rotate(${spin * 200}deg)`, offset: 0.42, easing: "cubic-bezier(.3,0,.7,1)" }, { transform: `translateY(-44px) rotate(${spin * 360}deg)`, offset: 0.78 }, { transform: `translateY(-62px) rotate(${spin * 360}deg)`, offset: 0.88 }, { transform: `translateY(-56px) rotate(${spin * 360}deg)` }, ], { duration: 1050, easing: "cubic-bezier(.2,.8,.4,1)" }, ); bodyRef.current?.animate( [{ transform: "translateY(0)" }, { transform: "translateY(3px) scaleY(.97)" }, { transform: "translateY(0)" }], { duration: 220, easing: "ease-out" }, ); } later(() => (busy.current = false), reduced ? 0 : 900); }, reduced ? 0 : toastFor); }; const burn = Math.min(level, CHARRED) / CHARRED; return ( ); } export default ToasterButton; const css = ` .tbt-wrap{display:inline-flex;padding-top:72px} .tbt-btn{--tbt-burn:0;position:relative;width:190px;height:112px;padding:0;border:0;background:none;cursor:pointer;-webkit-tap-highlight-color:transparent} .tbt-btn:disabled{cursor:not-allowed;opacity:.6} .tbt-btn:focus-visible{outline:none} .tbt-btn:focus-visible .tbt-body svg{outline:3px solid #ff8a3d;outline-offset:4px;border-radius:26px} .tbt-btn svg{display:block;width:100%;height:100%;overflow:visible} .tbt-body{position:absolute;inset:0;z-index:2;transform-origin:50% 100%; filter:drop-shadow(0 14px 14px rgba(30,35,45,.22)) drop-shadow(0 2px 2px rgba(30,35,45,.25))} .tbt-label{text-anchor:middle;font:800 27px/1 ui-rounded,"SF Pro Rounded","Nunito","Segoe UI",system-ui,sans-serif;fill:#2a2d33; paint-order:stroke;stroke:rgba(255,255,255,.55);stroke-width:1.2px} .tbt-lever{transition:transform .5s cubic-bezier(.4,0,.2,1)} .tbt-btn:not(:disabled):hover .tbt-lever{transform:translateY(3px)} .tbt-btn[data-phase=heating] .tbt-lever{transform:translateY(40px)} .tbt-btn[data-phase=popped] .tbt-lever{transition-duration:.12s} .tbt-glow{opacity:0;transition:opacity .4s} .tbt-btn:not(:disabled):hover .tbt-glow{opacity:.45} .tbt-btn[data-phase=heating] .tbt-glow{opacity:1;animation:tbt-heat .5s ease-in-out infinite alternate} /* the bread lives in here, clipped at the toaster's floor so it never pokes out the bottom */ .tbt-slot{position:absolute;left:-140px;right:-140px;top:-240px;bottom:14px;z-index:1;overflow:hidden;pointer-events:none} .tbt-toast{position:absolute;left:176px;top:232px;width:118px;height:106px;z-index:1;transform:translateY(0); transition:transform .45s cubic-bezier(.4,0,.2,1)} .tbt-btn[data-phase=popped] .tbt-toast{transform:translateY(-56px);transition:none} .tbt-btn[data-phase=heating] .tbt-toast{transform:translateY(22px)} .tbt-btn[data-phase=fresh] .tbt-toast{animation:tbt-drop .7s cubic-bezier(.3,1.4,.5,1) both} .tbt-btn:not(:disabled)[data-phase=ready]:hover .tbt-toast{transform:translateY(-4px)} .tbt-burnt{opacity:calc(var(--tbt-burn) * .95);transition:opacity .3s} .tbt-btn[data-phase=heating] .tbt-burnt{transition:none} .tbt-brand{text-anchor:middle;font:900 26px/1 ui-rounded,"SF Pro Rounded","Nunito","Segoe UI",system-ui,sans-serif;fill:#2a1206; opacity:calc(.25 + var(--tbt-burn) * .55);transition:opacity .3s} .tbt-btn[data-phase=ready]:not([data-charred=true]) .tbt-brand,.tbt-btn[data-phase=fresh] .tbt-brand{opacity:0} .tbt-btn[data-charred=true] .tbt-brand{fill:#000;opacity:.5} .tbt-btn[data-charred=true] .tbt-toast svg{filter:brightness(.32) saturate(.6) contrast(1.2)} .tbt-toast svg{transition:filter .4s} .tbt-smoke{position:absolute;left:50%;top:-6px;width:90px;height:0;margin-left:-45px;z-index:3;pointer-events:none} .tbt-smoke i{position:absolute;bottom:0;width:14px;height:14px;border-radius:50%;background:#c9c4be;filter:blur(5px);opacity:0} .tbt-smoke i:nth-child(1){left:14px}.tbt-smoke i:nth-child(2){left:38px;animation-delay:.35s!important}.tbt-smoke i:nth-child(3){left:62px;animation-delay:.7s!important} .tbt-btn[data-phase=heating] .tbt-smoke i{animation:tbt-smoke 1.1s ease-out .5s infinite} .tbt-btn[data-phase=popped] .tbt-smoke i{animation:tbt-smoke 1.4s ease-out .9s 2} .tbt-btn[data-charred=true] .tbt-smoke i{background:#5d5955;animation:tbt-smoke 1.2s ease-out .9s infinite} .tbt-ding{position:absolute;right:-26px;top:22px;z-index:4;pointer-events:none;opacity:0; font:800 14px/1 ui-rounded,"SF Pro Rounded","Nunito","Segoe UI",system-ui,sans-serif;color:#ff7a1a;transform:rotate(12deg) scale(.6)} .tbt-btn[data-phase=popped] .tbt-ding{animation:tbt-ding .9s cubic-bezier(.3,1.6,.5,1) both} @keyframes tbt-heat{from{opacity:.75}to{opacity:1}} @keyframes tbt-smoke{0%{transform:translateY(0) scale(.6);opacity:0}25%{opacity:.7}100%{transform:translateY(-60px) scale(2.2);opacity:0}} @keyframes tbt-ding{0%{opacity:0;transform:rotate(12deg) scale(.4)}25%{opacity:1;transform:rotate(12deg) scale(1.15)}70%{opacity:1;transform:rotate(8deg) scale(1)}100%{opacity:0;transform:rotate(8deg) translateY(-8px)}} @keyframes tbt-drop{0%{transform:translateY(-200px) rotate(-8deg);opacity:0}30%{opacity:1}100%{transform:translateY(0) rotate(0)}} @media (prefers-reduced-motion:reduce){.tbt-toast,.tbt-lever{transition:none}.tbt-glow,.tbt-smoke i,.tbt-ding,.tbt-btn[data-phase=fresh] .tbt-toast{animation:none!important}} `;