Skip to content

Commit e39a5b8

Browse files
committed
refactor(player): simplify popover input focus logic
Removed `isPopoverOpen` state and associated `useEffect` hook. Auto-focus and select logic for the input are now directly handled within `handlePopoverOpen`, reducing state management complexity.
1 parent 7046666 commit e39a5b8

File tree

1 file changed

+34
-52
lines changed

1 file changed

+34
-52
lines changed

src/components/player/Navigator.tsx

Lines changed: 34 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,12 @@ export const Navigator = ({ currentPage, numPages, skipToLocation }: {
99
skipToLocation: (location: string | number, shouldPause?: boolean) => void;
1010
}) => {
1111
const [inputValue, setInputValue] = useState('');
12-
const [isPopoverOpen, setIsPopoverOpen] = useState(false);
1312
const inputRef = useRef<HTMLInputElement>(null);
1413

1514
useEffect(() => {
1615
setInputValue(currentPage.toString());
1716
}, [currentPage]);
1817

19-
// Auto-focus and select input when popover opens
20-
useEffect(() => {
21-
if (isPopoverOpen && inputRef.current) {
22-
// Small delay to ensure the popover is fully rendered
23-
const timer = setTimeout(() => {
24-
inputRef.current?.focus();
25-
inputRef.current?.select();
26-
}, 50);
27-
return () => clearTimeout(timer);
28-
}
29-
}, [isPopoverOpen]);
30-
3118
const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
3219
// Only allow numbers
3320
const value = e.target.value.replace(/[^0-9]/g, '');
@@ -56,6 +43,11 @@ export const Navigator = ({ currentPage, numPages, skipToLocation }: {
5643

5744
const handlePopoverOpen = () => {
5845
setInputValue(''); // Clear input when popup opens
46+
// Auto-focus and select input shortly after opening
47+
setTimeout(() => {
48+
inputRef.current?.focus();
49+
inputRef.current?.select();
50+
}, 50);
5951
};
6052

6153
return (
@@ -73,44 +65,34 @@ export const Navigator = ({ currentPage, numPages, skipToLocation }: {
7365
</Button>
7466

7567
{/* Page number popup */}
76-
<Popover className="relative">
77-
{({ open }) => {
78-
if (open !== isPopoverOpen) {
79-
setIsPopoverOpen(open);
80-
}
81-
82-
return (
83-
<>
84-
<PopoverButton
85-
className="bg-offbase px-2 py-0.5 rounded-full focus:outline-none cursor-pointer hover:bg-offbase transform transition-transform duration-200 ease-in-out hover:scale-[1.04] hover:text-accent"
86-
onClick={handlePopoverOpen}
87-
>
88-
<p className="text-xs whitespace-nowrap">
89-
{currentPage} / {numPages || 1}
90-
</p>
91-
</PopoverButton>
92-
<PopoverPanel anchor="top" className="absolute z-50 bg-base p-3 rounded-md shadow-lg border border-offbase">
93-
<div className="flex flex-col space-y-2">
94-
<div className="text-xs font-medium text-foreground">Go to page</div>
95-
<input
96-
ref={inputRef}
97-
type="text"
98-
inputMode="numeric"
99-
pattern="[0-9]*"
100-
className="w-20 px-2 py-1 text-xs text-accent bg-offbase rounded border-none outline-none appearance-none text-center"
101-
value={inputValue}
102-
onChange={handleInputChange}
103-
onBlur={handleInputConfirm}
104-
onKeyDown={handleInputKeyDown}
105-
placeholder={currentPage.toString()}
106-
aria-label="Page number"
107-
/>
108-
<div className="text-xs text-muted text-center">of {numPages || 1}</div>
109-
</div>
110-
</PopoverPanel>
111-
</>
112-
);
113-
}}
68+
<Popover className="relative mb-1">
69+
<PopoverButton
70+
className="bg-offbase px-2 py-0.5 rounded-full focus:outline-none cursor-pointer hover:bg-offbase transform transition-transform duration-200 ease-in-out hover:scale-[1.04] hover:text-accent"
71+
onClick={handlePopoverOpen}
72+
>
73+
<p className="text-xs whitespace-nowrap">
74+
{currentPage} / {numPages || 1}
75+
</p>
76+
</PopoverButton>
77+
<PopoverPanel anchor="top" className="absolute z-50 bg-base p-3 rounded-md shadow-lg border border-offbase">
78+
<div className="flex flex-col space-y-2">
79+
<div className="text-xs font-medium text-foreground">Go to page</div>
80+
<input
81+
ref={inputRef}
82+
type="text"
83+
inputMode="numeric"
84+
pattern="[0-9]*"
85+
className="w-20 px-2 py-1 text-xs text-accent bg-offbase rounded border-none outline-none appearance-none text-center"
86+
value={inputValue}
87+
onChange={handleInputChange}
88+
onBlur={handleInputConfirm}
89+
onKeyDown={handleInputKeyDown}
90+
placeholder={currentPage.toString()}
91+
aria-label="Page number"
92+
/>
93+
<div className="text-xs text-muted text-center">of {numPages || 1}</div>
94+
</div>
95+
</PopoverPanel>
11496
</Popover>
11597

11698
{/* Page forward */}
@@ -126,4 +108,4 @@ export const Navigator = ({ currentPage, numPages, skipToLocation }: {
126108
</Button>
127109
</div>
128110
);
129-
}
111+
}

0 commit comments

Comments
 (0)