"use client"; import { useLayoutEffect, useRef, useState, type CSSProperties } from "react"; import "./follow-toggle.css"; /** * Follow Toggle * Three labels share one pill — Follow / Following / Unfollow — and the pill's * width eases to whichever is showing, measured from hidden copies. Following * is quiet (outlined, a check); hovering it previews the consequence (rose * Unfollow) before you commit. The switch bursts a faint ring outward. */ type Props = { initial?: boolean; name?: string; onChange?: (following: boolean) => void; className?: string }; export function FollowToggle({ initial = false, name, onChange, className = "" }: Props) { const [on, setOn] = useState(initial); const [hover, setHover] = useState(false); const [burst, setBurst] = useState(0); const [w, setW] = useState>({}); const m = useRef>({}); useLayoutEffect(() => { const read = () => setW(Object.fromEntries(Object.entries(m.current).map(([k, el]) => [k, el?.offsetWidth ?? 0]))); read(); document.fonts?.ready.then(read); }, []); const view = !on ? "follow" : hover ? "unfollow" : "following"; const toggle = () => { const next = !on; setOn(next); setHover(false); if (next) setBurst((b) => b + 1); onChange?.(next); }; const label = (k: string) => k === "follow" ? (<>Follow) : k === "following" ? (<>Following) : (<>Unfollow); return ( ); }