Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit 8b4b8b4

Browse files
authored
chore(js->ts): covert scrollbar && re-org/fix (#1025)
1 parent f9e089e commit 8b4b8b4

File tree

12 files changed

+169
-160
lines changed

12 files changed

+169
-160
lines changed

src/components/CustomScroller/HorizontalScroller.js renamed to src/components/CustomScroller/HorizontalScroller.tsx

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
import React, { useState, useRef, useCallback } from 'react'
88
import { useTheme } from 'styled-components'
99
import { Waypoint } from 'react-waypoint'
10-
import T from 'prop-types'
10+
import type { TSIZE_SML, TThemeMap } from '@/spec'
1111

1212
import { buildLog } from '@/utils'
1313
import { SIZE } from '@/constant'
1414
import { useCustomScroll } from '@/hooks'
1515

16+
import type { TProps as TScrollProps } from './index'
17+
1618
import {
1719
Wrapper,
1820
//
@@ -25,17 +27,27 @@ import {
2527
/* eslint-disable-next-line */
2628
const log = buildLog('c:CustomScroller:index')
2729

30+
type TProps = Omit<
31+
TScrollProps,
32+
| 'direction'
33+
| 'onTopEnter'
34+
| 'onTopLeave'
35+
| 'onBottomEnter'
36+
| 'onBottomLeave'
37+
| 'onScrollDirectionChange'
38+
>
39+
2840
// horizontal version
29-
const HorizontalScroller = ({
30-
height,
31-
width,
32-
innerHeight,
33-
showShadow,
34-
shadowSize,
35-
barSize,
41+
const HorizontalScroller: React.FC<TProps> = ({
42+
height = '100%',
43+
width = '100%',
44+
innerHeight = '100%',
45+
showShadow = true,
46+
shadowSize = SIZE.SMALL,
47+
barSize = SIZE.SMALL,
3648
children,
37-
autoHide,
38-
withBorder,
49+
autoHide = true,
50+
withBorder = false,
3951
}) => {
4052
const [showLeftShadow, setShowLeftShadow] = useState(false)
4153
const [showRightShadow, setShowRightShadow] = useState(true)
@@ -46,9 +58,8 @@ const HorizontalScroller = ({
4658
const handleShowRightShadow = useCallback(() => setShowRightShadow(true), [])
4759
const handleHideRightShadow = useCallback(() => setShowRightShadow(false), [])
4860

49-
const {
50-
_meta: { category: themeCategory },
51-
} = useTheme()
61+
const { _meta: themeMeta }: TThemeMap = useTheme()
62+
const { category: themeCategory } = themeMeta
5263

5364
const ref = useRef(null)
5465
useCustomScroll(ref, {
@@ -57,12 +68,7 @@ const HorizontalScroller = ({
5768
})
5869

5970
return (
60-
<Wrapper
61-
height={height}
62-
width={width}
63-
shadowSize={shadowSize}
64-
barSize={barSize}
65-
>
71+
<Wrapper height={height} width={width} barSize={barSize}>
6672
{showShadow && (
6773
<LeftShadowBar
6874
show={showLeftShadow}
@@ -99,28 +105,4 @@ const HorizontalScroller = ({
99105
)
100106
}
101107

102-
HorizontalScroller.propTypes = {
103-
children: T.node.isRequired,
104-
height: T.string,
105-
width: T.string,
106-
showShadow: T.bool,
107-
shadowSize: T.oneOf([SIZE.SMALL, SIZE.MEDIUM, SIZE.LARGE]),
108-
barSize: T.oneOf([SIZE.SMALL, SIZE.MEDIUM, SIZE.LARGE]),
109-
// hack for custom scrollbar
110-
innerHeight: T.string,
111-
autoHide: T.bool,
112-
withBorder: T.bool,
113-
}
114-
115-
HorizontalScroller.defaultProps = {
116-
height: '100%',
117-
width: '100%',
118-
showShadow: true,
119-
shadowSize: SIZE.SMALL,
120-
barSize: SIZE.SMALL,
121-
innerHeight: '100%',
122-
autoHide: false,
123-
withBorder: false,
124-
}
125-
126108
export default React.memo(HorizontalScroller)

src/components/CustomScroller/VerticalScroller.js renamed to src/components/CustomScroller/VerticalScroller.tsx

Lines changed: 32 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77
import React, { useState, useRef, useCallback } from 'react'
88
import { useTheme } from 'styled-components'
99
import { Waypoint } from 'react-waypoint'
10-
import T from 'prop-types'
10+
import type { TSIZE_SML, TThemeMap } from '@/spec'
11+
import type { TScrollDirection } from './spec'
1112

1213
import { buildLog, debounce } from '@/utils'
1314
import { SIZE } from '@/constant'
1415
import { useCustomScroll } from '@/hooks'
1516

17+
import type { TProps as TScrollProps } from './index'
18+
1619
import {
1720
Wrapper,
1821
//
@@ -24,17 +27,19 @@ import {
2427
/* eslint-disable-next-line */
2528
const log = buildLog('c:CustomScroller:index')
2629

27-
// horizontal version
28-
const VerticalScroller = ({
29-
height,
30-
width,
31-
showShadow,
32-
shadowSize,
33-
barSize,
30+
type TProps = Omit<TScrollProps, 'direction' | 'innerHeight'>
31+
32+
// vertical version
33+
const VerticalScroller: React.FC<TProps> = ({
34+
height = '100%',
35+
width = '100%',
36+
showShadow = true,
37+
shadowSize = SIZE.SMALL,
38+
barSize = SIZE.SMALL,
3439
children,
35-
autoHide,
36-
showOnHover,
37-
withBorder,
40+
autoHide = true,
41+
showOnHover = false,
42+
withBorder = false,
3843
onTopEnter,
3944
onTopLeave,
4045
onBottomEnter,
@@ -68,25 +73,28 @@ const VerticalScroller = ({
6873
onBottomEnter?.()
6974
}, [onBottomEnter])
7075

71-
const {
72-
_meta: { category: themeCategory },
73-
} = useTheme()
76+
const { _meta: themeMeta }: TThemeMap = useTheme()
77+
const { category: themeCategory } = themeMeta
7478

7579
const ref = useRef(null)
7680
const scrollInstance = useCustomScroll(ref, {
7781
scrollbars: { autoHide: autoHide ? 'scroll' : 'never' },
7882
themeCategory,
7983
callbacks: {
80-
onScroll: debounce(() => {
81-
const position = scrollInstance?.scroll().position
82-
if (position) {
83-
const currentY = position.y
84-
85-
currentY > lastYPosition
86-
? onScrollDirectionChange?.('up')
87-
: onScrollDirectionChange?.('down')
88-
}
89-
}, 100),
84+
onScroll: debounce(
85+
() => {
86+
const position = scrollInstance?.scroll().position
87+
if (position) {
88+
const currentY = position.y
89+
90+
currentY > lastYPosition
91+
? onScrollDirectionChange?.('up')
92+
: onScrollDirectionChange?.('down')
93+
}
94+
},
95+
100,
96+
true,
97+
),
9098
onScrollStart: () => {
9199
const position = scrollInstance?.scroll().position
92100
if (position) {
@@ -148,40 +156,4 @@ const VerticalScroller = ({
148156
)
149157
}
150158

151-
VerticalScroller.propTypes = {
152-
children: T.node.isRequired,
153-
height: T.string,
154-
width: T.string,
155-
showShadow: T.bool,
156-
shadowSize: T.oneOf([SIZE.SMALL, SIZE.MEDIUM, SIZE.LARGE]),
157-
barSize: T.oneOf([SIZE.SMALL, SIZE.MEDIUM, SIZE.LARGE]),
158-
// hack for custom scrollbar
159-
autoHide: T.bool,
160-
showOnHover: T.bool,
161-
withBorder: T.bool,
162-
onTopEnter: T.oneOfType([T.func, T.instanceOf(null)]),
163-
onTopLeave: T.oneOfType([T.func, T.instanceOf(null)]),
164-
onBottomEnter: T.oneOfType([T.func, T.instanceOf(null)]),
165-
onBottomLeave: T.oneOfType([T.func, T.instanceOf(null)]),
166-
167-
// scroll direction
168-
onScrollDirectionChange: T.oneOfType([T.func, T.instanceOf(null)]),
169-
}
170-
171-
VerticalScroller.defaultProps = {
172-
height: '100%',
173-
width: '100%',
174-
showShadow: true,
175-
shadowSize: SIZE.SMALL,
176-
barSize: SIZE.SMALL,
177-
autoHide: true,
178-
showOnHover: false,
179-
withBorder: false,
180-
onTopEnter: null,
181-
onTopLeave: null,
182-
onBottomEnter: null,
183-
onBottomLeave: null,
184-
onScrollDirectionChange: null,
185-
}
186-
187159
export default React.memo(VerticalScroller)

src/components/CustomScroller/index.js

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React from 'react'
2+
3+
import type { TSIZE_SML } from '@/spec'
4+
import type { TDirection, TScrollDirection } from './spec'
5+
6+
import HorizontalScroller from './HorizontalScroller'
7+
import VerticalScroller from './VerticalScroller'
8+
9+
export type TProps = {
10+
direction: TDirection
11+
children: React.ReactNode
12+
height?: string
13+
innerHeight?: string
14+
width?: string
15+
showShadow?: boolean
16+
shadowSize?: TSIZE_SML
17+
barSize?: TSIZE_SML
18+
// hack for custom scrollbar
19+
autoHide?: boolean
20+
showOnHover?: boolean
21+
withBorder?: boolean
22+
onTopEnter?: () => void
23+
onTopLeave?: () => void
24+
onBottomEnter?: () => void
25+
onBottomLeave?: () => void
26+
27+
// scroll direction
28+
onScrollDirectionChange?: (dir: TScrollDirection) => void
29+
}
30+
31+
const CustomScroller: React.FC<TProps> = ({
32+
children,
33+
direction = 'vertical',
34+
...restProps
35+
}) => {
36+
return direction === 'vertical' ? (
37+
<VerticalScroller {...restProps}>{children}</VerticalScroller>
38+
) : (
39+
<HorizontalScroller {...restProps}>{children}</HorizontalScroller>
40+
)
41+
}
42+
43+
export default React.memo(CustomScroller)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type TDirection = 'vertical' | 'horizontal'
2+
3+
export type TScrollDirection = 'up' | 'down'

src/components/CustomScroller/styles/horizontal_scroller.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import { getShadowBackground, getShadowSize, getScrollbarThin } from './metrics'
66
import { WrapperBase, ScrollWrapperBase, ShadowBarBase } from './index'
77

88
type TBar = {
9-
showOnHover: boolean
10-
barSize: string
11-
withBorder: boolean
12-
shadowSize: string
13-
height: string
14-
innerHeight: string
9+
showOnHover?: boolean
10+
barSize?: string
11+
withBorder?: boolean
12+
shadowSize?: string
13+
height?: string
14+
innerHeight?: string
1515
}
1616

1717
export const Wrapper = styled(WrapperBase)<{ barSize: string }>`

src/components/CustomScroller/styles/vertical_scroller.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import { WrapperBase, ScrollWrapperBase, ShadowBarBase } from './index'
55
import { getShadowBackground, getShadowSize, getScrollbarThin } from './metrics'
66

77
type TBar = {
8-
showOnHover: boolean
9-
barSize: string
10-
withBorder: boolean
8+
width?: string
9+
height: string
10+
barSize?: string
11+
showOnHover?: boolean
12+
withBorder?: boolean
1113
shadowSize: string
1214
}
1315
export const Wrapper = styled(WrapperBase)<TBar>`

src/containers/layout/GlobalLayout/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import React, { useEffect } from 'react'
88

99
import type { Nullable, TSEO } from '@/spec'
10-
import { ANCHOR } from '@/constant'
10+
import { ANCHOR, SIZE } from '@/constant'
1111
import AnalysisService from '@/services/Analysis'
1212
import { useNetwork, useShortcut, usePlatform, useDevice } from '@/hooks'
1313
import { pluggedIn } from '@/utils'
@@ -102,7 +102,7 @@ const GlobalLayoutContainer: React.FC<TProps> = ({
102102
<CustomScroller
103103
direction="vertical"
104104
height="100vh"
105-
barSize="medium"
105+
barSize={SIZE.MEDIUM}
106106
showShadow={false}
107107
onScrollDirectionChange={(direction) =>
108108
bodyScrollDirectionOnChange(direction)

0 commit comments

Comments
 (0)