Skip to content

Commit f1fd230

Browse files
committed
feat: Make scrolling thumbnails to view keyboard-compatible
1 parent 75172d9 commit f1fd230

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

src/ImageGallery.tsx

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ export function ImageGallery({
6060
dialogRef.current?.showModal();
6161
}
6262

63-
function changeSlide(directionNumber: number) {
63+
function changeSlide(direction: number) {
6464
const totalImages = imagesInfoArray.length;
65-
let newSlideNumber = slideNumber + directionNumber;
65+
let newSlideNumber = slideNumber + direction;
6666

6767
newSlideNumber < 1 && (newSlideNumber = totalImages);
6868
newSlideNumber > totalImages && (newSlideNumber = 1);
@@ -73,14 +73,6 @@ export function ImageGallery({
7373
}
7474
}
7575

76-
function scrollActiveThumbImgIntoView() {
77-
activeThumbImgRef.current?.scrollIntoView({
78-
behavior: "smooth",
79-
block: "nearest",
80-
inline: "center",
81-
});
82-
}
83-
8476
function switchFullScreen(on: boolean) {
8577
if (on) {
8678
lightboxRef.current?.requestFullscreen().catch((error) => {
@@ -94,9 +86,18 @@ export function ImageGallery({
9486
}
9587
}
9688

89+
function scrollImage(direction: number) {
90+
flushSync(() => changeSlide(direction));
91+
activeThumbImgRef.current?.scrollIntoView({
92+
behavior: "smooth",
93+
block: "nearest",
94+
inline: "center",
95+
});
96+
}
97+
9798
function handleKeyDownOnModal(e: React.KeyboardEvent<HTMLElement>) {
98-
e.key === "ArrowLeft" && changeSlide(-1);
99-
e.key === "ArrowRight" && changeSlide(1);
99+
e.key === "ArrowLeft" && scrollImage(-1);
100+
e.key === "ArrowRight" && scrollImage(1);
100101
e.key === "f" && fullscreen && switchFullScreen(false);
101102
e.key === "f" && !fullscreen && switchFullScreen(true);
102103
}
@@ -245,10 +246,7 @@ export function ImageGallery({
245246
...modalSlideBtnStyle,
246247
}}
247248
title="Previous image"
248-
onClick={() => {
249-
flushSync(() => changeSlide(-1));
250-
scrollActiveThumbImgIntoView();
251-
}}
249+
onClick={() => scrollImage(-1)}
252250
>
253251
{SvgElement(
254252
<path
@@ -271,10 +269,7 @@ export function ImageGallery({
271269
...modalSlideBtnStyle,
272270
}}
273271
title="Next image"
274-
onClick={() => {
275-
flushSync(() => changeSlide(1));
276-
scrollActiveThumbImgIntoView();
277-
}}
272+
onClick={() => scrollImage(1)}
278273
>
279274
{SvgElement(
280275
<path

0 commit comments

Comments
 (0)