"use client"; import { RotateCcw, ZoomIn, ZoomOut } from "lucide-react"; import { useEffect, useRef, useState } from "react"; import { useTranslation } from "react-i18next"; import { DEFAULT_PDF_ZOOM, PDF_ZOOM_OPTIONS, type PdfZoomPreference, } from "@/lib/reading-zoom"; import { PreferenceButton } from "./ReaderDisplayControls"; /** * The PDF reader's zoom bar. * * Floats at the bottom of the pane rather than inside the scroll container, so * it does not scroll away with the pages. The percentage is the width that * actually landed: a 400% request clamped by the canvas budget shows 215%, and * the menu marks the stop that produces it. */ export function PdfZoomControls({ preference, effectivePercent, visible, canZoomIn, canZoomOut, percentFor, onZoomIn, onZoomOut, onSelect, onReset, onActiveChange, }: { preference: PdfZoomPreference; /** The percentage actually rendered, which clamping can lower. */ effectivePercent: number; visible: boolean; /** False when a further step would not change the drawn width. */ canZoomIn: boolean; canZoomOut: boolean; /** * The percentage a ladder stop actually lands at on this page. * * The toolbar shows what landed, so a row labelled with the request made the * two disagree: `MIN_PAGE_WIDTH` (240px) is above a Letter page's 25% * (204px), so picking "25%" drew 29% and the toolbar said 29% while the menu * still claimed 25%. Rows print the landed value now. */ percentFor: (scale: number) => number; onZoomIn: () => void; onZoomOut: () => void; onSelect: (next: PdfZoomPreference) => void; onReset: () => void; /** Reports pointer presence so hovering the bar does not hide it. */ onActiveChange: (active: boolean) => void; }) { const { t } = useTranslation(); const [open, setOpen] = useState(false); const rootRef = useRef(null); const menuRef = useRef(null); // The ref wraps the trigger *and* the menu, so a pointerdown on a menu row is // not read as "outside" before that row's click fires. useEffect(() => { if (!open) return; const onPointerDown = (event: PointerEvent) => { const root = rootRef.current; if (root && !root.contains(event.target as Node)) setOpen(false); }; const onKeyDown = (event: KeyboardEvent) => { if (event.key !== "Escape") return; setOpen(false); // `preventDefault()` alone cannot keep the Escape from the Guided // Learning handler: that one listens on `document` and only steps aside // for a visible `[role=dialog]`, while this menu is a `role="menu"`. Both // listeners sit on the same node, and `preventDefault` never stops // delivery to the other. Stopping propagation here is what actually // keeps one Escape from closing the menu and leaving learning mode. event.stopPropagation(); // Focus goes back to the trigger the menu belongs to, so a keyboard user // is not dropped on `` after closing. rootRef.current?.querySelector('[aria-haspopup="menu"]')?.focus(); }; document.addEventListener("pointerdown", onPointerDown); // Capture phase: this runs before the learning-mode listener regardless of // which one registered first. document.addEventListener("keydown", onKeyDown, true); return () => { document.removeEventListener("pointerdown", onPointerDown); document.removeEventListener("keydown", onKeyDown, true); }; }, [open]); // Both limits come from the parent, which knows the page geometry the canvas // budget depends on. Reading them off the requested scale here was wrong: the // budget can clamp a 4× request to 211%, so comparing `preference.scale` // against MAX_PDF_ZOOM left zoom-in enabled while every press was a no-op. const atFloor = !canZoomOut; const atCeiling = !canZoomIn; return ( ); }