Skip to content

Commit eea367d

Browse files
committed
migrate scrollcards navigator to typescript
1 parent 843010b commit eea367d

3 files changed

Lines changed: 26 additions & 25 deletions

File tree

src/App.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import React, { useState, useEffect } from 'react'
1+
import { useState, useEffect } from 'react'
22
import 'src/assets/App.css'
33
import { Footer, Header } from 'src/components/Layout'
44
import { BookmarksSidebar } from 'src/features/bookmarks'
55
import { MarketingBanner } from 'src/components/Elements'
6-
import ScrollCardsNavigator from './components/ScrollCardsNavigator'
6+
import { ScrollCardsNavigator } from './components/Layout'
77
import { AppContentLayout } from './components/Layout'
88
import 'react-contexify/dist/ReactContexify.css'
99
import { setupAnalytics, trackPageView, setupIdentification } from 'src/lib/analytics'
Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,31 @@
1-
import React, { useState, useEffect, useLayoutEffect, useRef, useCallback } from 'react'
1+
import { useState, useEffect, useLayoutEffect, useRef, useCallback } from 'react'
22
import { FiChevronLeft, FiChevronRight } from 'react-icons/fi'
33
import { maxCardsPerRow } from 'src/config'
44
import { useUserPreferences } from 'src/stores/preferences'
55
import { 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

src/components/Layout/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./Header"
22
export * from "./Footer"
3-
export * from "./AppContentLayout"
3+
export * from "./AppContentLayout"
4+
export * from "./ScrollCardsNavigator"

0 commit comments

Comments
 (0)