1- import React , { useState , useEffect , useLayoutEffect , useRef , useCallback } from 'react'
1+ import { useState , useEffect , useLayoutEffect , useRef , useCallback } from 'react'
22import { FiChevronLeft , FiChevronRight } from 'react-icons/fi'
33import { maxCardsPerRow } from 'src/config'
44import { useUserPreferences } from 'src/stores/preferences'
55import { trackPageScroll } from 'src/lib/analytics'
66
7- function ScrollCardsNavigator ( ) {
7+ export const ScrollCardsNavigator = ( ) => {
88 const { cards } = useUserPreferences ( )
99 const [ leftButtonVisible , setLeftButtonVisible ] = useState ( true )
1010 const [ rightButtonVisible , setRightButtonVisible ] = useState ( true )
11- const scrollBarContainer = useRef ( null )
11+ const scrollBarContainer = useRef < HTMLElement | null > ( null )
1212
13- const handleScroll = ( e ) => {
14- const { scrollLeft, scrollWidth, offsetWidth } = e . target
15- setLeftButtonVisible ( scrollLeft > 0 )
16- const scrollRight = scrollWidth - offsetWidth - Math . abs ( scrollLeft )
17- setRightButtonVisible ( scrollRight > 0 )
13+ const handleScroll = ( e : Event ) => {
14+ if ( cards . length <= maxCardsPerRow ) {
15+ setLeftButtonVisible ( false )
16+ setRightButtonVisible ( false )
17+ } else {
18+ const { scrollLeft, scrollWidth, offsetWidth } = e . target as HTMLElement
19+ setLeftButtonVisible ( scrollLeft > 100 )
20+ const scrollRight = scrollWidth - offsetWidth - Math . abs ( scrollLeft )
21+ setRightButtonVisible ( scrollRight > 100 )
22+ }
1823 }
1924
20- const handleKeyboardKeys = useCallback ( ( e ) => {
21- if ( e . keyCode === 37 ) {
25+ const handleKeyboardKeys = useCallback ( ( e : KeyboardEvent ) => {
26+ if ( e . key === 'ArrowLeft' ) {
2227 scrollTo ( 'left' )
23- } else if ( e . keyCode === 39 ) {
28+ } else if ( e . key === 'ArrowRight' ) {
2429 scrollTo ( 'right' )
2530 }
2631 } , [ ] )
@@ -30,32 +35,29 @@ function ScrollCardsNavigator() {
3035 } , [ ] )
3136
3237 useEffect ( ( ) => {
33- scrollBarContainer . current . addEventListener ( 'scroll' , handleScroll , true )
38+ scrollBarContainer . current ? .addEventListener ( 'scroll' , handleScroll , true )
3439 window . addEventListener ( 'keydown' , handleKeyboardKeys )
3540 return ( ) => {
3641 window . removeEventListener ( 'keydown' , handleKeyboardKeys )
37- scrollBarContainer . current . removeEventListener ( 'scroll' , handleScroll )
42+ scrollBarContainer . current ? .removeEventListener ( 'scroll' , handleScroll )
3843 }
44+ // eslint-disable-next-line react-hooks/exhaustive-deps
3945 } , [ handleKeyboardKeys ] )
4046
4147 useEffect ( ( ) => {
4248 setLeftButtonVisible ( false )
4349 setRightButtonVisible ( cards . length > maxCardsPerRow )
4450 } , [ cards ] )
4551
46- const scrollTo = ( direction ) => {
52+ const scrollTo = ( direction : 'left' | 'right' ) => {
4753 if ( ! scrollBarContainer . current ) {
4854 return
4955 }
5056 trackPageScroll ( direction )
5157 const { scrollLeft } = scrollBarContainer . current
52- const { offsetWidth } = scrollBarContainer . current . children [ 0 ]
53- let extraPadding = 32 // Should be calculated dynamically
58+ const { offsetWidth } = scrollBarContainer . current ?. firstChild as HTMLElement
5459
55- const position =
56- direction === 'left'
57- ? scrollLeft - offsetWidth - extraPadding
58- : scrollLeft + offsetWidth + extraPadding
60+ const position = direction === 'left' ? scrollLeft - offsetWidth : scrollLeft + offsetWidth
5961
6062 scrollBarContainer . current . scrollTo ( {
6163 left : position ,
@@ -78,5 +80,3 @@ function ScrollCardsNavigator() {
7880 </ >
7981 )
8082}
81-
82- export default ScrollCardsNavigator
0 commit comments