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