forked from nodejs/nodejs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.tsx
More file actions
130 lines (105 loc) · 3.42 KB
/
index.tsx
File metadata and controls
130 lines (105 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import { ChevronDownIcon, ChevronUpIcon } from '@heroicons/react/24/outline';
import { useEffect, useRef, useState } from 'react';
import { type FC, type RefObject } from 'react';
import styles from '@node-core/ui-components/Common/Select/index.module.css';
type SelectScrollButtonProps = {
direction: 'up' | 'down';
selectContentRef?: RefObject<HTMLDivElement | null>;
scrollAmount?: number;
scrollInterval?: number;
};
const SelectScrollButton: FC<SelectScrollButtonProps> = ({
direction,
selectContentRef,
scrollAmount = 35,
scrollInterval = 50,
}) => {
const DirectionComponent =
direction === 'down' ? ChevronDownIcon : ChevronUpIcon;
const [isVisible, setIsVisible] = useState(false);
const [hasOverflow, setOverflow] = useState(false);
const intervalRef = useRef<number | null>(null);
const isScrollingRef = useRef(false);
const clearScrollInterval = () => {
if (intervalRef.current !== null) {
window.clearInterval(intervalRef.current);
intervalRef.current = null;
}
};
const startScrolling = () => {
if (!selectContentRef?.current || !isVisible || !hasOverflow) return;
clearScrollInterval();
intervalRef.current = window.setInterval(() => {
if (!selectContentRef.current || !isScrollingRef.current) return;
const container = selectContentRef.current;
if (direction === 'down') {
container.scrollBy({ top: scrollAmount, behavior: 'smooth' });
if (
container.scrollTop >=
container.scrollHeight - container.clientHeight
) {
clearScrollInterval();
setIsVisible(false);
}
} else {
container.scrollBy({
top: -Math.abs(scrollAmount),
behavior: 'smooth',
});
if (container.scrollTop <= 0) {
clearScrollInterval();
setIsVisible(false);
}
}
}, scrollInterval);
};
useEffect(() => {
if (!selectContentRef?.current) return;
const container = selectContentRef.current;
setOverflow(container.scrollHeight > container.clientHeight);
const updateButtonVisibility = () => {
if (!container) return;
if (direction === 'down') {
setIsVisible(
container.scrollTop < container.scrollHeight - container.clientHeight
);
} else {
setIsVisible(container.scrollTop > 0);
}
};
updateButtonVisibility();
const handleScroll = () => {
updateButtonVisibility();
if (!isScrollingRef.current && intervalRef.current !== null) {
clearScrollInterval();
}
};
container.addEventListener('scroll', handleScroll);
window.addEventListener('resize', updateButtonVisibility);
return () => {
container.removeEventListener('scroll', handleScroll);
window.removeEventListener('resize', updateButtonVisibility);
clearScrollInterval();
};
}, [direction, selectContentRef]);
const handleMouseEnter = () => {
isScrollingRef.current = true;
startScrolling();
};
const handleMouseLeave = () => {
isScrollingRef.current = false;
clearScrollInterval();
};
if (!isVisible) return null;
return (
<div
className={styles.scrollBtn}
data-direction={direction}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
>
<DirectionComponent className={styles.scrollBtnIcon} aria-hidden="true" />
</div>
);
};
export default SelectScrollButton;