"use client"; import { useEffect, useRef, useState } from "react"; export default function AutoFitText({ children, className, }: { children: string; className?: string; }) { const containerRef = useRef(null); const textRef = useRef(null); const [fontSize, setFontSize] = useState(10); // will update instantly // biome-ignore lint/correctness/useExhaustiveDependencies: it needs children useEffect(() => { function resize() { const container = containerRef.current; const text = textRef.current; if (!container || !text) return; // start large and shrink until it fits let size = 300; text.style.fontSize = `${size}px`; const { width: cw, height: ch } = container.getBoundingClientRect(); while ((text.scrollWidth > cw || text.scrollHeight > ch) && size > 5) { size -= 2; text.style.fontSize = `${size}px`; } setFontSize(size - 5); } resize(); window.addEventListener("resize", resize); return () => window.removeEventListener("resize", resize); }, [children]); return (
{children}
); }