"use client";import * as React from "react";import { Plus, Undo2 } from "lucide-react";import { NumberRoll } from "./number-roll";import { TextMorph } from "./text-morph";import { createSpring, MATERIAL_LIFT, type Spring } from "./ui-interaction-motion";/* ───────────────────────────────────────────────────────── * LIQUID LOG: the card is the vessel you log into * * body the card fills with liquid from the bottom: a live * surface, a pale meniscus, and the liquid itself drawn * in fine engraved lines, deepening toward the bottom * log tap + and the amount leaves it as a drop, arcs up and * falls into the surface: a splash kicks up, the ripple * runs out to the walls and back, a few bubbles rise, * and only then does the level rise and the total roll * read wherever the liquid covers the words they turn light, * cut exactly along the moving waterline, so the total * is never lost * pour hold + and drops keep coming, faster and faster; the * whole hold is one log to undo * over past the goal the card is full and a deeper liquid * rises from the bottom for the second lap * undo a drop lifts out of the surface and falls back into * the button while the level drops * touch a pointer moving along the waterline leaves a small * wake * * One canvas and one frame loop that sleeps when the surface is * still. The total is yours: pass value and add in onLog, or leave * it uncontrolled. * ───────────────────────────────────────────────────────── */export interface LiquidLogProps extends Omit<React.HTMLAttributes<HTMLDivElement>, "title" | "defaultValue" | "onChange"> { value?: number; defaultValue?: number; goal: number; /** What one tap logs. Defaults to an eighth of the goal. */ step?: number; unit?: string; title?: string; icon?: React.ReactNode; /** The liquid's colour: any CSS colour. Defaults to --chart-1. */ color?: string; /** Called for every tap (and every drop of a hold) with the amount. Throw or reject to refuse it. */ onLog?: (amount: number) => Promise<unknown> | void; /** Called with the last log's amount. Without it a controlled card hides Undo. */ onUndo?: (amount: number) => Promise<unknown> | void; format?: Intl.NumberFormatOptions; locales?: string | string[];}const EASE = "cubic-bezier(0.16,1,0.3,1)";const FOCUS = "focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring/50 focus-visible:ring-offset-2 focus-visible:ring-offset-background";const COLS = 40; // surface columnsconst STEP_S = 1 / 120;const FLIGHT = 520;const BUTTON = 52;const INSET = 16;const reducedQuery = "(prefers-reduced-motion: reduce)";const subscribeReduced = (onChange: () => void) => { const query = window.matchMedia(reducedQuery); query.addEventListener("change", onChange); return () => query.removeEventListener("change", onChange);};const useReducedMotion = () => React.useSyncExternalStore(subscribeReduced, () => window.matchMedia(reducedQuery).matches, () => false);const subscribeNothing = () => () => {};const useHydrated = () => React.useSyncExternalStore(subscribeNothing, () => true, () => false);const inOut = (t: number) => (t < 0.5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2);interface Drop { amount: number; start: number; up: boolean; // undo: from the surface back into the button fromX: number; toX: number; x: number; y: number;}interface Particle { x: number; y: number; vx: number; vy: number; r: number; bubble: boolean; born: number;}interface Engine { w: number; h: number; u: Float32Array; // surface displacement, px v: Float32Array; level: Spring | null; drops: Drop[]; bits: Particle[]; frame: number; last: number; spare: number; colors: { liquid: string; card: string; ink: string }; land: (amount: number) => void; clips: { above: HTMLElement | null; below: HTMLElement | null }; pointer: { x: number; y: number } | null; /** On screen and motion allowed: the surface sways gently. */ idle: boolean; t: number;}/** A slow swell on the still surface, so the liquid never reads as a flat block of colour. */const sway = (e: Engine, x: number) => (e.idle ? 1.7 * Math.sin(x * 0.034 + e.t * 1.25) + 1.1 * Math.sin(x * 0.069 - e.t * 0.85) : 0);const surfaceAt = (e: Engine, x: number, base: number) => { const f = (x / Math.max(1, e.w)) * (COLS - 1); const i = Math.max(0, Math.min(COLS - 2, Math.floor(f))); const t = f - i; return base + e.u[i] * (1 - t) + e.u[i + 1] * t + sway(e, x);};const levelY = (e: Engine, f: number) => e.h * (1 - Math.max(0, Math.min(1, f)));function kickSurface(e: Engine, x: number, speed: number, spread = 2.2) { const c = (x / Math.max(1, e.w)) * (COLS - 1); for (let i = 0; i < COLS; i++) e.v[i] += speed * Math.exp(-(((i - c) / spread) ** 2));}/** Step the surface: each column springs back to rest and pulls on its neighbours; the walls reflect. */function stepSurface(e: Engine, dt: number) { e.spare += Math.min(dt, 0.05); while (e.spare >= STEP_S) { e.spare -= STEP_S; for (let i = 0; i < COLS; i++) { const l = e.u[i > 0 ? i - 1 : 0]; const r = e.u[i < COLS - 1 ? i + 1 : COLS - 1]; e.v[i] += ((l + r - 2 * e.u[i]) * 2200 - e.u[i] * 26) * STEP_S; e.v[i] *= 0.991; } for (let i = 0; i < COLS; i++) e.u[i] += e.v[i] * STEP_S; }}function draw(e: Engine, canvas: HTMLCanvasElement | null, now: number) { if (!canvas || !e.w || !e.level) return false; const ctx = canvas.getContext("2d"); if (!ctx) return false; const dpr = Math.min(window.devicePixelRatio || 1, 2); if (canvas.width !== Math.round(e.w * dpr) || canvas.height !== Math.round(e.h * dpr)) { canvas.width = Math.round(e.w * dpr); canvas.height = Math.round(e.h * dpr); } const dt = e.last ? (now - e.last) / 1000 : 0; e.last = now; e.t = now / 1000; stepSurface(e, dt); ctx.setTransform(dpr, 0, 0, dpr, 0, 0); ctx.clearRect(0, 0, e.w, e.h); const { liquid, card } = e.colors; const f = Math.max(0, e.level.get()); const y1 = levelY(e, f); const y2 = f > 1 ? levelY(e, f - 1) : e.h + 4; const calm = Math.min(1, Math.max(0, y1 / 18)); // the surface flattens as it meets the top edge const surface = (base: number, damp: number) => { ctx.beginPath(); ctx.moveTo(0, e.h); const pts: [number, number][] = []; for (let i = 0; i < COLS; i++) { const x = (i / (COLS - 1)) * e.w; pts.push([x, base + (e.u[i] + sway(e, x)) * damp]); } ctx.lineTo(pts[0][0], pts[0][1]); for (let i = 1; i < COLS; i++) { const [px, py] = pts[i - 1]; const [x, y] = pts[i]; ctx.quadraticCurveTo(px, py, (px + x) / 2, (py + y) / 2); } ctx.lineTo(e.w, pts[COLS - 1][1]); ctx.lineTo(e.w, e.h); ctx.closePath(); return pts; }; const body = (base: number, damp: number, deep: number) => { if (base >= e.h + 2) return; ctx.save(); surface(base, damp); ctx.clip(); ctx.globalAlpha = 1; ctx.fillStyle = liquid; ctx.fillRect(0, 0, e.w, e.h); // Light near the surface, depth toward the bottom. const top = Math.max(0, base - 6); const shade = ctx.createLinearGradient(0, top, 0, e.h); shade.addColorStop(0, "rgba(255,255,255,0.26)"); shade.addColorStop(Math.min(1, 70 / Math.max(1, e.h - top)), "rgba(255,255,255,0)"); shade.addColorStop(1, `rgba(0,0,0,${0.16 + deep})`); ctx.fillStyle = shade; ctx.fillRect(0, top, e.w, e.h - top); if (deep) { ctx.fillStyle = `rgba(0,0,0,${deep})`; ctx.fillRect(0, top, e.w, e.h - top); } // The house mark: the liquid is drawn in fine engraved lines. ctx.fillStyle = card; ctx.globalAlpha = 0.11; for (let y = Math.floor(top / 4) * 4 + 3; y < e.h; y += 4) ctx.fillRect(0, y, e.w, 1); ctx.restore(); // The meniscus. ctx.save(); ctx.beginPath(); for (let i = 0; i < COLS; i++) { const x = (i / (COLS - 1)) * e.w; const y = base + (e.u[i] + sway(e, x)) * damp; if (i === 0) ctx.moveTo(x, y); else { const px = ((i - 1) / (COLS - 1)) * e.w; const py = base + (e.u[i - 1] + sway(e, px)) * damp; ctx.quadraticCurveTo(px, py, (px + x) / 2, (py + y) / 2); } } ctx.strokeStyle = card; ctx.globalAlpha = 0.6; ctx.lineWidth = 1.5; ctx.stroke(); ctx.restore(); }; body(y1, calm, 0); body(y2, 1, 0.22); // Splash and bubbles. e.bits = e.bits.filter((p) => { const age = (now - p.born) / 1000; if (p.bubble) { p.y -= 38 * dt; p.x += Math.sin(age * 9 + p.r) * 6 * dt; const s = surfaceAt(e, p.x, y1); if (p.y < s + 2 || age > 2.4) return false; ctx.globalAlpha = 0.55 * Math.min(1, age * 4); ctx.strokeStyle = card; ctx.lineWidth = 1; ctx.beginPath(); ctx.arc(p.x, p.y, p.r, 0, Math.PI * 2); ctx.stroke(); return true; } p.vy += 900 * dt; p.x += p.vx * dt; p.y += p.vy * dt; if (p.vy > 0 && p.y > surfaceAt(e, p.x, y1)) return false; ctx.globalAlpha = 1; ctx.fillStyle = liquid; ctx.beginPath(); ctx.arc(p.x, p.y, p.r, 0, Math.PI * 2); ctx.fill(); return true; }); // Drops in the air, stretched along their path by their speed. const bx = e.w - INSET - BUTTON / 2; const by = e.h - INSET - BUTTON / 2; e.drops = e.drops.filter((d) => { if (d.start < 0) d.start = now; const t = Math.min(1, (now - d.start) / FLIGHT); let x: number; let y: number; if (!d.up) { // Tossed up from the button, falling into the surface. const sx = bx; const sy = by - BUTTON / 2; const ty = surfaceAt(e, d.toX, y1); const apex = Math.min(sy, ty) - Math.max(36, e.h * 0.16); const k = inOut(t * 0.92); x = sx + (d.toX - sx) * k; const a = Math.min(1, t * 1.1); y = (1 - a) ** 2 * sy + 2 * (1 - a) * a * apex + a * a * ty; } else { const sy = surfaceAt(e, d.fromX, y1); const k = inOut(t); x = d.fromX + (bx - d.fromX) * k; const apex = Math.min(sy, by) - Math.max(30, e.h * 0.12); y = (1 - k) ** 2 * sy + 2 * (1 - k) * k * apex + k * k * by; } const vx = x - d.x; const vy = y - d.y; d.x = x; d.y = y; const speed = Math.min(1, Math.hypot(vx, vy) / 10); const size = d.up ? 7 * (1 - t * 0.6) : 3 + 4.5 * Math.min(1, t * 3); ctx.globalAlpha = d.up ? 1 - t ** 3 : Math.min(1, t * 8); ctx.fillStyle = liquid; ctx.save(); ctx.translate(x, y); ctx.rotate(Math.atan2(vy, vx)); ctx.beginPath(); ctx.ellipse(0, 0, size * (1 + speed * 0.8), size / (1 + speed * 0.4), 0, 0, Math.PI * 2); ctx.restore(); ctx.fill(); // A thin light rim so a drop reads against the liquid it falls into. ctx.globalAlpha *= 0.7; ctx.strokeStyle = card; ctx.lineWidth = 1; ctx.stroke(); if (t < 1) return true; if (!d.up) { kickSurface(e, d.toX, 210 + Math.min(120, d.amount)); for (let i = 0; i < 3; i++) e.bits.push({ x: d.toX, y: surfaceAt(e, d.toX, y1), vx: (i - 1) * 70 + (Math.random() - 0.5) * 40, vy: -170 - Math.random() * 90, r: 1.6 + Math.random() * 1.2, bubble: false, born: now }); for (let i = 0; i < 3; i++) e.bits.push({ x: d.toX + (Math.random() - 0.5) * 16, y: surfaceAt(e, d.toX, y1) + 14 + i * 9, vx: 0, vy: 0, r: 1.4 + Math.random() * 1.6, bubble: true, born: now + i * 60 }); e.land(d.amount); } return false; }); ctx.globalAlpha = 1; // The words: dark above the waterline, light below it, cut along the same curve. const clipPts: string[] = []; for (let i = 0; i <= 24; i++) { const x = (i / 24) * e.w; clipPts.push(`${x.toFixed(1)}px ${surfaceAt(e, x, y1).toFixed(1)}px`); } if (e.clips.below) e.clips.below.style.clipPath = f >= 1 ? "none" : `polygon(${clipPts.join(",")}, ${e.w}px ${e.h}px, 0px ${e.h}px)`; if (e.clips.above) e.clips.above.style.clipPath = f >= 1 ? "inset(0 0 100% 0)" : `polygon(0px 0px, ${e.w}px 0px, ${[...clipPts].reverse().join(",")})`; let energy = 0; for (let i = 0; i < COLS; i++) energy += Math.abs(e.u[i]) + Math.abs(e.v[i]) * 0.02; return e.idle || energy > 0.05 * COLS * 0.1 || e.drops.length > 0 || e.bits.length > 0;}export function LiquidLog({ value: valueProp, defaultValue = 0, goal, step: stepProp, unit, title = "Today", icon, color = "var(--chart-1, var(--primary))", onLog, onUndo, format, locales, className = "", style, ...props}: LiquidLogProps) { const reduced = useReducedMotion(); const hydrated = useHydrated(); const locale = locales ?? (hydrated ? undefined : "en-US"); const step = stepProp ?? goal / 8; const [own, setOwn] = React.useState(defaultValue); const controlled = valueProp !== undefined; const value = controlled ? valueProp : own; // While drops are in the air the card shows what has landed; once they're in, it shows your value. const [display, setDisplay] = React.useState(value); const [flying, setFlying] = React.useState(0); const shown = flying > 0 ? display : value; const [history, setHistory] = React.useState<number[]>([]); const [visible, setVisible] = React.useState(false); const [said, setSaid] = React.useState(""); const [shake, setShake] = React.useState(0); const rootRef = React.useRef<HTMLDivElement>(null); const canvasRef = React.useRef<HTMLCanvasElement>(null); const aboveRef = React.useRef<HTMLDivElement>(null); const belowRef = React.useRef<HTMLDivElement>(null); const buttonRef = React.useRef<HTMLButtonElement>(null); const probes = React.useRef<(HTMLSpanElement | null)[]>([]); const live = React.useRef({ value, flying, holding: false, joined: false }); const hold = React.useRef({ timer: 0, gap: 0 }); const engine = React.useRef<Engine>({ w: 0, h: 0, u: new Float32Array(COLS), v: new Float32Array(COLS), level: null, drops: [], bits: [], frame: 0, last: 0, spare: 0, colors: { liquid: "#305dde", card: "#fff", ink: "#454545" }, land: () => {}, clips: { above: null, below: null }, pointer: null, idle: false, t: 0, }); React.useLayoutEffect(() => { live.current.value = value; live.current.flying = flying; }); // One frame loop: it runs while the surface moves, drops fly or the level travels, then sleeps. const kick = React.useCallback(() => { const e = engine.current; if (e.frame) return; const loop = (now: number) => { e.frame = 0; const busy = draw(e, canvasRef.current, now); const moving = !!e.level && (e.level.getVelocity() !== 0 || e.level.get() !== e.level.getTarget()); if (busy || moving) e.frame = requestAnimationFrame(loop); else e.last = 0; }; e.frame = requestAnimationFrame(loop); }, []); React.useLayoutEffect(() => { const e = engine.current; e.clips = { above: aboveRef.current, below: belowRef.current }; e.level = createSpring(0, { stiffness: 60, damping: 15 }, kick, 0.0004); e.land = (amount: number) => { setDisplay((d) => d + amount); setFlying((n) => Math.max(0, n - 1)); }; return () => { e.level?.stop(); e.level = null; if (e.frame) cancelAnimationFrame(e.frame); e.frame = 0; e.drops = []; e.bits = []; }; }, [kick]); React.useLayoutEffect(() => { const root = rootRef.current; if (!root) return; const measure = () => { const e = engine.current; e.w = root.offsetWidth; e.h = root.offsetHeight; kick(); }; measure(); const observer = new ResizeObserver(measure); observer.observe(root); return () => observer.disconnect(); }, [kick]); // The theme's colours, read again whenever the theme changes. React.useEffect(() => { const read = () => { const [liquid, card, ink] = probes.current.map((p) => (p ? getComputedStyle(p).color : "")); engine.current.colors = { liquid: liquid || "#305dde", card: card || "#fff", ink: ink || "#454545" }; kick(); }; read(); const observer = new MutationObserver(read); observer.observe(document.documentElement, { attributes: true, attributeFilter: ["class", "style", "data-theme"] }); const media = window.matchMedia("(prefers-color-scheme: dark)"); media.addEventListener("change", read); return () => { observer.disconnect(); media.removeEventListener("change", read); }; }, [color, kick]); React.useEffect(() => { const e = engine.current; if (!e.level) return; const target = visible && goal > 0 ? Math.max(0, shown) / goal : 0; if (reduced) e.level.jump(Math.min(2, target)); else e.level.set(Math.min(2, target)); }, [shown, goal, visible, reduced]); React.useEffect(() => { const el = rootRef.current; if (!el) return; const observer = new IntersectionObserver( ([entry]) => { if (entry.isIntersecting) { setVisible(true); observer.disconnect(); } }, { rootMargin: "0px 0px -10% 0px" }, ); observer.observe(el); return () => observer.disconnect(); }, []); // The surface sways only while the card is on screen, and never with reduced motion. React.useEffect(() => { const el = rootRef.current; const e = engine.current; if (!el) return; const observer = new IntersectionObserver(([entry]) => { e.idle = entry.isIntersecting && !reduced; kick(); }); observer.observe(el); return () => { observer.disconnect(); e.idle = false; }; }, [reduced, kick]); React.useEffect( () => () => { window.clearTimeout(hold.current.timer); }, [], ); const fmt = (v: number) => new Intl.NumberFormat(locale, format).format(v); const unitText = unit ? ` ${unit}` : ""; const launch = (drop: Omit<Drop, "start" | "x" | "y">) => { const e = engine.current; if (e.drops.length >= 12 || !e.w) return false; e.drops.push({ ...drop, start: -1, x: e.w - INSET - BUTTON / 2, y: e.h - INSET - BUTTON }); kick(); return true; }; // Where a drop lands: a little to the left of the middle, never twice in the same place. const spot = React.useRef(0); const target = () => { spot.current = (spot.current + 0.38) % 1; return engine.current.w * (0.28 + spot.current * 0.3); }; const refuse = () => setShake((n) => n + 1); const log = (amount: number) => { const L = live.current; if (!reduced && visible) { if (L.flying === 0) setDisplay(L.value); if (launch({ amount, up: false, fromX: 0, toX: target() })) { L.flying += 1; setFlying((n) => n + 1); } } if (!controlled) setOwn((v) => v + amount); // A hold is one log to undo. Read the flag now: the updater runs later, after it has moved on. const join = L.joined; setHistory((h) => (join && h.length ? [...h.slice(0, -1), h[h.length - 1] + amount] : [...h, amount])); L.joined = L.holding; let result: Promise<unknown> | void; try { result = onLog?.(amount); } catch { refuse(); if (!controlled) setOwn((v) => v - amount); return; } if (result && typeof (result as Promise<unknown>).then === "function") { (result as Promise<unknown>).then(undefined, () => { refuse(); if (!controlled) setOwn((v) => v - amount); }); } }; const stopHold = () => { window.clearTimeout(hold.current.timer); if (!live.current.holding) return; live.current.holding = false; live.current.joined = false; setSaid(`Logged. ${fmt(live.current.value)}${unitText} of ${fmt(goal)}${unitText}.`); }; const pour = () => { log(step); hold.current.gap = Math.max(140, hold.current.gap * 0.86); hold.current.timer = window.setTimeout(pour, hold.current.gap); }; const canUndo = history.length > 0 && (!controlled || !!onUndo); const undo = () => { const amount = history[history.length - 1]; if (amount === undefined) return; setHistory((h) => h.slice(0, -1)); if (!reduced) { const x = target(); kickSurface(engine.current, x, -160); launch({ amount, up: true, fromX: x, toX: 0 }); } if (!controlled) setOwn((v) => v - amount); const result = onUndo?.(amount); if (result && typeof (result as Promise<unknown>).then === "function") (result as Promise<unknown>).then(undefined, refuse); setSaid(`Removed ${fmt(amount)}${unitText}.`); }; React.useEffect(() => { if (!shake || reduced) return; buttonRef.current?.animate( [{ translate: "0 0" }, { translate: "-4px 0" }, { translate: "4px 0" }, { translate: "-2px 0" }, { translate: "0 0" }], { duration: 380, easing: "ease-out" }, ); }, [shake, reduced]); // A pointer moving along the waterline leaves a small wake. const wake = (e: React.PointerEvent) => { if (reduced || e.pointerType !== "mouse") return; const en = engine.current; const root = rootRef.current; if (!root || !en.level) return; const r = root.getBoundingClientRect(); const x = e.clientX - r.left; const y = e.clientY - r.top; const last = en.pointer; en.pointer = { x, y }; if (!last) return; const surface = levelY(en, Math.max(0, en.level.get())); if (Math.abs(y - surface) > 22 || en.level.get() >= 1) return; const dx = x - last.x; if (Math.abs(dx) < 1) return; kickSurface(en, x, Math.max(-60, Math.min(60, Math.abs(dx) * 5)) * (y < surface ? 1 : -1), 1.6); kick(); }; const pct = goal > 0 ? Math.round((Math.max(0, shown) / goal) * 100) : 0; const last = history[history.length - 1]; const onLiquid = `oklch(from ${color} clamp(0.22, (0.72 - l) * 100, 0.99) calc(c * 0.3) h)`; // The words, drawn twice: once in ink above the waterline, once light below it. const words = (light: boolean) => ( <div className="flex h-full flex-col p-4 @[17rem]:p-5"> <p className="flex h-7 min-w-0 items-center gap-1.5 pr-28 text-[12.5px]" style={{ color: light ? onLiquid : "var(--muted-foreground)" }}> {icon && ( <span className="flex size-3.5 shrink-0 items-center justify-center [&>svg]:size-3.5" style={{ color: light ? onLiquid : color }}> {icon} </span> )} <span className="truncate">{title}</span> </p> <p className="mt-2 text-[40px] font-medium leading-none tracking-[-0.045em] tabular-nums @[17rem]:text-[46px]" style={{ color: light ? onLiquid : "var(--foreground)" }}> <NumberRoll value={visible ? shown : 0} format={format} locales={locale} duration={900} /> {unit && ( <span className="ml-1 text-[15px] font-normal tracking-normal" style={{ opacity: 0.7 }}> {unit} </span> )} </p> <p className="mt-2 text-[12.5px] tabular-nums" style={{ color: light ? onLiquid : "var(--muted-foreground)" }}> <span className="font-medium" style={{ color: light ? onLiquid : color }}> <NumberRoll value={visible ? pct : 0} duration={900} suffix="%" /> </span>{" "} of {fmt(goal)} {unitText} </p> <p className="mt-auto text-[11.5px] font-medium tabular-nums" style={{ color: light ? onLiquid : "var(--muted-foreground)", opacity: 0.85 }}> +{fmt(step)} {unitText} a tap </p> </div> ); return ( <div ref={rootRef} role="group" aria-label={title} onPointerMove={wake} onPointerLeave={() => void (engine.current.pointer = null)} className={`relative isolate aspect-[1/1.02] w-full overflow-clip rounded-[24px] bg-background shadow-[0_0_0_1px_var(--border)] ${className}`} style={style} {...props} > <div aria-hidden="true" data-natural-width className="grid h-0" style={{ gridTemplateColumns: "minmax(0, 300px)" }} /> <canvas ref={canvasRef} aria-hidden="true" className="absolute inset-0 size-full" /> <div className="@container absolute inset-0"> <div ref={aboveRef} role="meter" aria-label={title} aria-valuemin={0} aria-valuemax={goal} aria-valuenow={Math.max(0, shown)} aria-valuetext={`${fmt(shown)}${unitText} of ${fmt(goal)}${unitText}, ${pct}%`} className="absolute inset-0" > {words(false)} </div> <div ref={belowRef} aria-hidden="true" inert className="pointer-events-none absolute inset-0" style={{ clipPath: "inset(100% 0 0 0)" }}> {words(true)} </div> <div className="absolute right-4 top-4 grid transition-[grid-template-columns,opacity] duration-[420ms] @[17rem]:right-5 @[17rem]:top-5" style={{ gridTemplateColumns: canUndo ? "1fr" : "0fr", opacity: canUndo ? 1 : 0, transitionTimingFunction: EASE }} > <div className="flex min-w-0 justify-end overflow-clip"> <button type="button" tabIndex={canUndo ? 0 : -1} aria-hidden={!canUndo} onClick={undo} className={`inline-flex h-7 shrink-0 cursor-pointer items-center gap-1.5 whitespace-nowrap rounded-full bg-background pl-2.5 pr-3 text-[12px] text-muted-foreground shadow-[inset_0_0_0_1px_var(--border)] transition-[color,scale] duration-200 hover:text-foreground active:scale-[0.97] ${FOCUS}`} style={{ transitionTimingFunction: EASE }} > <Undo2 aria-hidden="true" className="size-3.5" /> <TextMorph>{`Undo ${fmt(last ?? step)}${unitText}`}</TextMorph> </button> </div> </div> <button ref={buttonRef} type="button" aria-label={`Log ${fmt(step)}${unitText}`} onPointerDown={(e) => { if (e.button !== 0) return; live.current.holding = true; live.current.joined = false; log(step); hold.current.gap = 320; window.clearTimeout(hold.current.timer); hold.current.timer = window.setTimeout(pour, 460); }} onPointerUp={stopHold} onPointerLeave={stopHold} onPointerCancel={stopHold} onClick={(e) => { // Pointer presses log on the way down; this is the keyboard. if (e.detail !== 0) return; log(step); setSaid(`Logged ${fmt(step)}${unitText}.`); }} onContextMenu={(e) => e.preventDefault()} className={`absolute bottom-4 right-4 flex cursor-pointer touch-manipulation select-none items-center justify-center rounded-full bg-background shadow-[inset_0_0_0_1px_var(--border)] transition-[scale] duration-200 active:scale-[0.9] ${MATERIAL_LIFT} ${FOCUS}`} style={{ width: BUTTON, height: BUTTON, color, transitionTimingFunction: EASE, WebkitTouchCallout: "none" }} > <Plus aria-hidden="true" className="size-6" strokeWidth={2.4} /> </button> </div> <p className="sr-only" aria-live="polite"> {said} </p> {/* Colour probes for the canvas. */} <span ref={(el) => void (probes.current[0] = el)} aria-hidden="true" className="hidden" style={{ color }} /> <span ref={(el) => void (probes.current[1] = el)} aria-hidden="true" className="hidden text-background" /> <span ref={(el) => void (probes.current[2] = el)} aria-hidden="true" className="hidden text-foreground" /> </div> );}