"use client"; /** * ferro.tsx — a glass capsule filled with magnetic ferrofluid. * Move toward it to draw up spikes; click to make a liquid crown. * * Part of toy-buttons. MIT licensed. Only depends on React. */ import { useEffect, useId, useImperativeHandle, useRef, type ButtonHTMLAttributes, type MouseEvent, type ReactNode, type Ref, } from "react"; type Props = Omit, "children"> & { children?: ReactNode; ref?: Ref; }; const SPIKES = [48, 79, 109, 140, 171, 201, 232]; const CROWN = [25, 43, 59, 77, 59, 43, 25]; const DROPS = [ { x: 49, y: 72, dx: -4, rise: 20, r: 2.0 }, { x: 79, y: 56, dx: -6, rise: 31, r: 2.5 }, { x: 109, y: 41, dx: -4, rise: 44, r: 2.3 }, { x: 140, y: 24, dx: 4, rise: 28, r: 2.8 }, { x: 171, y: 41, dx: 5, rise: 44, r: 2.3 }, { x: 201, y: 56, dx: 6, rise: 31, r: 2.5 }, { x: 231, y: 72, dx: 4, rise: 20, r: 2.0 }, ]; const clamp = (n: number, min: number, max: number) => Math.max(min, Math.min(max, n)); // The inside of the rounded glass, used both to limit tips and catch droplets. function ceiling(x: number) { const r = 67; const offset = x < 77 ? 77 - x : x > 203 ? x - 203 : 0; return 78 - Math.sqrt(Math.max(0, r * r - offset * offset)); } /** One outline keeps the cones joined to the pool; the faces only add light. */ function surface(pointer: number, attraction: number, crown: number, wobble = 0) { const drift = (pointer - 140) / 140 * attraction * 4; const top = (x: number) => 99 - 2.4 * Math.cos((x - 140) / 140 * Math.PI / 2) - attraction * 2 * Math.exp(-Math.pow((x - pointer) / 90, 2)) + wobble * (x - 140) / 140; let d = `M 18 111 C 20 104 25 101 33 ${top(33).toFixed(2)}`; const faces: string[] = []; const streaks: string[] = []; for (let i = 0; i < SPIKES.length; i++) { const base = SPIKES[i] + drift; const influence = Math.exp(-Math.pow((SPIKES[i] - pointer) / 49, 2)); const y = top(base); const tipX = base + clamp((pointer - base) * attraction * .25, -10, 10); const height = Math.max(0, Math.min(attraction * 61 * influence + crown * CROWN[i], y - ceiling(tipX) - 5.2)); const tipY = y - height; const left = base - 14.7; const right = base + 14.7; d += ` L ${left.toFixed(2)} ${top(left).toFixed(2)}`; d += ` C ${(base - 6.5).toFixed(2)} ${y.toFixed(2)} ${(tipX - 3).toFixed(2)} ${(tipY + height * .43).toFixed(2)} ${tipX.toFixed(2)} ${tipY.toFixed(2)}`; d += ` C ${(tipX + 3).toFixed(2)} ${(tipY + height * .43).toFixed(2)} ${(base + 6.5).toFixed(2)} ${y.toFixed(2)} ${right.toFixed(2)} ${top(right).toFixed(2)}`; // The broad, feathered face and thin taper follow the same concave flank. faces.push(height > 2.5 ? `M ${left.toFixed(2)} ${top(left).toFixed(2)} C ${(base - 6.5).toFixed(2)} ${y.toFixed(2)} ${(tipX - 3).toFixed(2)} ${(tipY + height * .43).toFixed(2)} ${tipX.toFixed(2)} ${tipY.toFixed(2)} C ${(tipX + 3).toFixed(2)} ${(tipY + height * .43).toFixed(2)} ${(base + 6.5).toFixed(2)} ${y.toFixed(2)} ${right.toFixed(2)} ${top(right).toFixed(2)} Q ${base.toFixed(2)} ${(y + 5).toFixed(2)} ${left.toFixed(2)} ${top(left).toFixed(2)} Z` : ""); streaks.push(height > 4 ? `M ${(tipX - .15).toFixed(2)} ${(tipY + height * .09).toFixed(2)} C ${(tipX - 2.1).toFixed(2)} ${(tipY + height * .47).toFixed(2)} ${(base - 7).toFixed(2)} ${(y - 4).toFixed(2)} ${(base - 11).toFixed(2)} ${(y - .4).toFixed(2)} C ${(base - 8.2).toFixed(2)} ${(y - 1.8).toFixed(2)} ${(tipX - .5).toFixed(2)} ${(tipY + height * .44).toFixed(2)} ${(tipX + .45).toFixed(2)} ${(tipY + height * .11).toFixed(2)} Z` : ""); } d += ` Q 251 ${top(251).toFixed(2)} 259 106 C 265 110 267 118 261 126 C 254 138 237 141 217 142 C 171 145 109 145 63 142 C 43 141 26 138 19 127 C 14 121 14 116 18 111 Z`; return { d, faces, streaks, drift }; } export function FerroButton({ children = "attract", className, type = "button", disabled = false, onClick, ref, ...rest }: Props) { const button = useRef(null); const pool = useRef(null); const poolClip = useRef(null); const refractedPool = useRef(null); const faces = useRef<(SVGPathElement | null)[]>([]); const streaks = useRef<(SVGPathElement | null)[]>([]); const droplets = useRef<(SVGGElement | null)[]>([]); const smears = useRef<(SVGEllipseElement | null)[]>([]); const reflections = useRef(null); const wake = useRef(() => {}); const clickAt = useRef(-Infinity); useImperativeHandle(ref, () => button.current!, []); const uid = `tbf-${useId().replace(/[^a-zA-Z0-9_-]/g, "")}`; const initial = surface(140, 0, 0); useEffect(() => { const el = button.current; if (!el) return; const motion = window.matchMedia("(prefers-reduced-motion: reduce)"); let visible = true, frame = 0, last = 0; let targetX = 140, targetAttraction = 0; let x = 140, vx = 0, attraction = 0, va = 0, crown = 0, vc = 0; const render = (elapsed: number) => { const settle = clamp((elapsed - 850) / 1650, 0, 1); const wobble = elapsed >= 850 && elapsed < 2500 ? Math.sin(settle * Math.PI * 2) * Math.sin(settle * Math.PI) * 2.2 : 0; const shape = surface(x, attraction, crown, wobble); pool.current?.setAttribute("d", shape.d); poolClip.current?.setAttribute("d", shape.d); refractedPool.current?.setAttribute("d", shape.d); faces.current.forEach((path, i) => path?.setAttribute("d", shape.faces[i])); streaks.current.forEach((path, i) => path?.setAttribute("d", shape.streaks[i])); reflections.current?.setAttribute("transform", `translate(${shape.drift.toFixed(2)} ${(wobble * .22).toFixed(2)})`); const t = (elapsed - 290) / 1100; droplets.current.forEach((drop, i) => { const smear = smears.current[i]; if (!drop) return; if (t <= 0 || t >= 1) { drop.setAttribute("opacity", "0"); smear?.setAttribute("opacity", "0"); return; } const p = DROPS[i]; const dx = p.x + p.dx * t; const rawY = p.y - p.rise * 4 * t * (1 - t) + (98 - p.y) * t * t; const limit = ceiling(dx) + p.r * 1.25 + .6; const dy = Math.max(rawY, limit); drop.setAttribute("transform", `translate(${dx.toFixed(2)} ${dy.toFixed(2)})`); drop.setAttribute("opacity", Math.min(1, t * 8, (1 - t) * 5).toFixed(2)); if (smear) { const touched = i >= 2 && i <= 4 && t > .2 && t < .95; const opacity = touched ? Math.min(.24, (t - .2) * 1.4, (.95 - t) * .8) : 0; const smearX = p.x + p.dx * .4; smear.setAttribute("transform", `translate(${smearX.toFixed(2)} ${(ceiling(smearX) + p.r * .6).toFixed(2)})`); smear.setAttribute("opacity", opacity.toFixed(2)); } }); }; const allowed = () => !disabled && !el.matches(":disabled") && visible && !document.hidden && !motion.matches; const tick = (now: number) => { frame = 0; if (!allowed()) return; const dt = last ? clamp((now - last) / 16.667, .5, 2) : 1; last = now; const elapsed = now - clickAt.current; if (elapsed >= 2600) delete el.dataset.tbfSettling; let crownTarget = 0; if (elapsed >= 0 && elapsed < 700) crownTarget = 1; const hoverTarget = elapsed >= 0 && elapsed < 2600 ? 0 : targetAttraction; vx = (vx + (targetX - x) * .075 * dt) * Math.pow(.76, dt); va = (va + (hoverTarget - attraction) * .075 * dt) * Math.pow(.76, dt); vc = (vc + (crownTarget - crown) * .085 * dt) * Math.pow(.76, dt); x += vx * dt; attraction = Math.max(0, attraction + va * dt); crown = Math.max(0, crown + vc * dt); render(elapsed); const active = (elapsed >= 0 && elapsed < 2600) || Math.abs(targetX - x) > .03 || Math.abs(vx) > .02 || Math.abs(hoverTarget - attraction) > .001 || Math.abs(va) > .001 || Math.abs(crownTarget - crown) > .001 || Math.abs(vc) > .001; if (active) frame = requestAnimationFrame(tick); else last = 0; }; const start = () => { if (!frame && allowed()) { last = 0; frame = requestAnimationFrame(tick); } }; wake.current = start; const reset = () => { cancelAnimationFrame(frame); frame = 0; last = 0; x = targetX = 140; vx = 0; attraction = targetAttraction = va = crown = vc = 0; clickAt.current = -Infinity; delete el.dataset.tbfSettling; render(Infinity); }; const pause = () => { el.dataset.tbfPaused = String(!visible || document.hidden || disabled); if (!allowed()) reset(); else start(); }; const move = (event: PointerEvent) => { if (!allowed() || event.pointerType === "touch") return; const rect = el.getBoundingClientRect(); const px = (event.clientX - rect.left) * 280 / rect.width; const py = (event.clientY - rect.top) * 156 / rect.height; const dx = Math.max(0, Math.abs(px - 140) - 110); const reach = clamp(1 - Math.hypot(dx, py - 82) / 135, 0, 1); targetX = clamp(px, 35, 245); targetAttraction = reach; start(); }; const leaveWindow = () => { targetAttraction = 0; targetX = 140; start(); }; const observer = typeof IntersectionObserver === "undefined" ? null : new IntersectionObserver(([entry]) => { visible = entry.isIntersecting; pause(); }); observer?.observe(el); window.addEventListener("pointermove", move, { passive: true }); document.documentElement.addEventListener("pointerleave", leaveWindow); document.addEventListener("visibilitychange", pause); motion.addEventListener("change", pause); pause(); return () => { wake.current = () => {}; cancelAnimationFrame(frame); observer?.disconnect(); window.removeEventListener("pointermove", move); document.documentElement.removeEventListener("pointerleave", leaveWindow); document.removeEventListener("visibilitychange", pause); motion.removeEventListener("change", pause); }; }, [disabled]); const activate = (event: MouseEvent) => { if (disabled || event.currentTarget.matches(":disabled")) return; onClick?.(event); if (event.defaultPrevented || window.matchMedia("(prefers-reduced-motion: reduce)").matches) return; clickAt.current = performance.now(); event.currentTarget.dataset.tbfSettling = "true"; wake.current(); }; return ( ); } export default FerroButton; const css = ` .tbf-btn{position:relative;display:inline-flex;align-items:center;justify-content:center;width:280px;height:156px; padding:0;border:0;border-radius:78px;appearance:none;background:transparent;color:#fff;cursor:pointer; -webkit-tap-highlight-color:transparent;font:400 22px/1 ui-rounded,"SF Pro Rounded","Segoe UI",system-ui,sans-serif;letter-spacing:.04em} .tbf-btn::before{content:"";position:absolute;left:8%;right:8%;bottom:0;height:17px;border-radius:50%;pointer-events:none; background:rgba(31,45,47,.32);filter:blur(10px);transform:translateY(8px);box-shadow:0 5px 14px rgba(35,48,50,.16)} .tbf-btn:focus-visible{outline:3px solid #596d73;outline-offset:5px} .tbf-btn:disabled{cursor:default} .tbf-art{position:absolute;inset:0;width:100%;height:100%;overflow:visible;pointer-events:none} .tbf-label{position:absolute;top:111px;left:0;width:100%;padding:0 24px;text-align:center;white-space:nowrap; color:rgba(250,255,255,.9);text-shadow:0 1px 1px rgba(10,20,22,.48),0 -1px 1px rgba(255,255,255,.18);pointer-events:none} .tbf-liquid{transform-origin:140px 126px;animation:tbf-slosh 5.2s ease-in-out infinite} @keyframes tbf-slosh{0%,100%{transform:translate(0,0) scale(1,1)}25%{transform:translate(-1px,.35px) scale(1.004,.996)}75%{transform:translate(1px,-.35px) scale(.996,1.004)}} .tbf-btn[data-tbf-paused=true] .tbf-liquid,.tbf-btn[data-tbf-settling=true] .tbf-liquid,.tbf-btn:disabled .tbf-liquid{animation-play-state:paused} @media (prefers-reduced-motion:reduce){.tbf-liquid{animation:none}} `;