Open History to see the last commit
1import { useEffect, useRef, useState } from "react";
2
3import { cn } from "~/lib/utils";
4
5const HIDE_AFTER_MS = 1500;
6const ZOOM_EPSILON = 0.001;
7
8interface Props {
9 zoomFactor: number;
10}
11
12export function ZoomIndicator({ zoomFactor }: Props) {
13 const [visible, setVisible] = useState(false);
14 const lastFactorRef = useRef(zoomFactor);
15 const timerRef = useRef<number | null>(null);
16
17 useEffect(() => {
18 if (Math.abs(lastFactorRef.current - zoomFactor) < ZOOM_EPSILON) return;
19 lastFactorRef.current = zoomFactor;
20 setVisible(true);
21 if (timerRef.current !== null) window.clearTimeout(timerRef.current);
22 timerRef.current = window.setTimeout(() => {
23 setVisible(false);
24 timerRef.current = null;
25 }, HIDE_AFTER_MS);
26 return () => {
27 if (timerRef.current !== null) {
28 window.clearTimeout(timerRef.current);
29 timerRef.current = null;
30 }
31 };
32 }, [zoomFactor]);
33
34 const percent = `${Math.round(zoomFactor * 100)}%`;
35
36 return (
37 <div
38 aria-hidden={!visible}
39 className={cn(
40 "pointer-events-none absolute top-3 right-3 z-20 select-none rounded-full border border-border/70 bg-popover/95 px-2.5 py-1 text-xs font-medium text-foreground shadow-md/10 backdrop-blur transition-all duration-200 ease-out",
41 visible ? "opacity-100 translate-y-0" : "opacity-0 -translate-y-1",
42 )}
43 >
44 {percent}
45 </div>
46 );
47}