'use client'; import { Button, Popover, PopoverButton, PopoverPanel } from '@headlessui/react'; import { useState, useEffect, useRef } from 'react'; export const Navigator = ({ currentPage, numPages, skipToLocation }: { currentPage: number; numPages: number | undefined; skipToLocation: (location: string | number, shouldPause?: boolean) => void; }) => { const [inputValue, setInputValue] = useState(''); const inputRef = useRef(null); useEffect(() => { setInputValue(currentPage.toString()); }, [currentPage]); const handleInputChange = (e: React.ChangeEvent) => { // Only allow numbers const value = e.target.value.replace(/[^0-9]/g, ''); setInputValue(value); }; const handleInputConfirm = () => { if (inputValue === '') return; // Don't do anything if input is empty let page = parseInt(inputValue, 10); if (isNaN(page)) return; const maxPage = numPages || 1; if (page < 1) page = 1; if (page > maxPage) page = maxPage; if (page !== currentPage) { skipToLocation(page, true); } setInputValue(''); // Clear input after confirming }; const handleInputKeyDown = (e: React.KeyboardEvent) => { if (e.key === 'Enter') { handleInputConfirm(); inputRef.current?.blur(); } }; const handlePopoverOpen = () => { setInputValue(''); // Clear input when popup opens }; return (
{/* Page back */} {/* Page number popup */}

{currentPage} / {numPages || 1}

Go to page
of {numPages || 1}
{/* Page forward */}
); }