Skip to content

Commit c670743

Browse files
authored
Merge pull request #861 from remap-keys/support-wheel-event-on-layer-paging-ui
Support a wheel event to change the page on a layer paging UI.
2 parents 1492c2c + 693f0c3 commit c670743

File tree

1 file changed

+40
-2
lines changed

1 file changed

+40
-2
lines changed

src/components/common/layer/LayerPagination.tsx

+40-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
MoreHoriz,
1010
MoreVert,
1111
} from '@mui/icons-material';
12-
import React from 'react';
12+
import React, { useEffect, useRef } from 'react';
1313

1414
const useLayerPaginationStyles = makeStyles({
1515
ulVertical: {
@@ -49,6 +49,8 @@ type LayerPaginationProps = {
4949
orientation: LayerPaginationOrientation;
5050
};
5151

52+
const LAYER_PAGINATION_WHEEL_TIMEOUT = 500;
53+
5254
export default function LayerPagination(props: LayerPaginationProps) {
5355
const StyledBadge = withStyles(() => ({
5456
badge: {
@@ -66,6 +68,41 @@ export default function LayerPagination(props: LayerPaginationProps) {
6668
props.onClickPage(page);
6769
},
6870
});
71+
72+
const pageRefs: Map<number, React.RefObject<HTMLDivElement>> = new Map();
73+
for (const item of items) {
74+
if (item.type === 'page' && item.page !== null) {
75+
pageRefs.set(item.page, useRef<HTMLDivElement>(null));
76+
}
77+
}
78+
79+
useEffect(() => {
80+
let processing = false;
81+
const handleWheel = (event: WheelEvent) => {
82+
event.preventDefault();
83+
if (processing) {
84+
return;
85+
}
86+
processing = true;
87+
if (event.deltaY < 0) {
88+
props.onClickPage(Math.min(props.page! + 1, props.count));
89+
} else if (event.deltaY > 0) {
90+
props.onClickPage(Math.max(1, props.page! - 1));
91+
}
92+
setTimeout(() => {
93+
processing = false;
94+
}, LAYER_PAGINATION_WHEEL_TIMEOUT);
95+
};
96+
for (const ref of pageRefs.values()) {
97+
ref.current?.addEventListener('wheel', handleWheel, { passive: false });
98+
}
99+
return () => {
100+
for (const ref of pageRefs.values()) {
101+
ref.current?.removeEventListener('wheel', handleWheel);
102+
}
103+
};
104+
});
105+
69106
return (
70107
<nav>
71108
<ul
@@ -90,10 +127,11 @@ export default function LayerPagination(props: LayerPaginationProps) {
90127
label={page - 1}
91128
color={selected ? 'primary' : undefined}
92129
clickable={!selected}
93-
onClick={() => {
130+
onClick={(): void => {
94131
props.onClickPage(page);
95132
}}
96133
className={selected ? '' : classes.unselected}
134+
ref={pageRefs.get(page)!}
97135
/>
98136
</StyledBadge>
99137
);

0 commit comments

Comments
 (0)