Skip to content

Commit 34bc1f1

Browse files
authored
feat(Stories): add keyboard navigation with arrow keys (#370)
1 parent b69d7f3 commit 34bc1f1

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

src/components/Stories/Stories.tsx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,28 @@ export function Stories({
9494
});
9595
}, [items, onNextClick]);
9696

97+
React.useEffect(() => {
98+
if (!open) {
99+
return;
100+
}
101+
102+
const handleKeyDown = (event: KeyboardEvent) => {
103+
if (event.key === 'ArrowLeft') {
104+
event.preventDefault();
105+
handleGotoPrevious();
106+
} else if (event.key === 'ArrowRight') {
107+
event.preventDefault();
108+
handleGotoNext();
109+
}
110+
};
111+
112+
window.addEventListener('keydown', handleKeyDown);
113+
114+
return () => {
115+
window.removeEventListener('keydown', handleKeyDown);
116+
};
117+
}, [open, handleGotoPrevious, handleGotoNext]);
118+
97119
if (items.length === 0) {
98120
return null;
99121
}

0 commit comments

Comments
 (0)