import { useEffect, useRef, useState } from "react"; import { cn } from "~/lib/utils"; const HIDE_AFTER_MS = 1500; const ZOOM_EPSILON = 0.001; interface Props { /** Current zoom factor (1.0 = 100%); changes drive the transient indicator. */ zoomFactor: number; } /** * Floating "X%" pill that surfaces in the top-right of the webview area * whenever the zoom factor changes, then fades out after a short pause. * * Suppressed for the first render's value so we don't flash 100% on mount. */ export function ZoomIndicator({ zoomFactor }: Props) { const [visible, setVisible] = useState(false); const lastFactorRef = useRef(zoomFactor); const timerRef = useRef(null); useEffect(() => { if (Math.abs(lastFactorRef.current - zoomFactor) < ZOOM_EPSILON) return; lastFactorRef.current = zoomFactor; setVisible(true); if (timerRef.current !== null) window.clearTimeout(timerRef.current); timerRef.current = window.setTimeout(() => { setVisible(false); timerRef.current = null; }, HIDE_AFTER_MS); return () => { if (timerRef.current !== null) { window.clearTimeout(timerRef.current); timerRef.current = null; } }; }, [zoomFactor]); const percent = `${Math.round(zoomFactor * 100)}%`; return (
{percent}
); }