-
Notifications
You must be signed in to change notification settings - Fork 297
Expand file tree
/
Copy pathoverlay.taro.tsx
More file actions
110 lines (101 loc) · 2.45 KB
/
Copy pathoverlay.taro.tsx
File metadata and controls
110 lines (101 loc) · 2.45 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
import React, { FunctionComponent, useEffect, useState } from 'react'
import { CSSTransition } from 'react-transition-group'
import { useSpring, animated } from '@react-spring/web'
import classNames from 'classnames'
import { ITouchEvent, View } from '@tarojs/components'
import { ComponentDefaults } from '@/utils/typings'
import { useLockScrollTaro } from '@/hooks/taro/use-lock-scoll'
import { TaroOverlayProps } from '@/types'
export const defaultOverlayProps: TaroOverlayProps = {
...ComponentDefaults,
zIndex: 1000,
duration: 300,
closeOnOverlayClick: true,
visible: false,
lockScroll: true,
onClick: () => {},
afterShow: () => {},
afterClose: () => {},
}
export const Overlay: FunctionComponent<
Partial<TaroOverlayProps> &
Omit<React.HTMLAttributes<HTMLDivElement>, 'onClick'>
> = (props) => {
const {
children,
zIndex,
duration,
className,
closeOnOverlayClick,
visible,
lockScroll,
style,
afterShow,
afterClose,
onClick,
...rest
} = { ...defaultOverlayProps, ...props }
const classPrefix = 'nut-overlay'
const [innerVisible, setInnerVisible] = useState(visible)
const nodeRef = useLockScrollTaro(!!lockScroll && innerVisible)
useEffect(() => {
setInnerVisible(visible)
}, [visible])
const classes = classNames(classPrefix, `${classPrefix}-slide`, className)
const styles = {
zIndex,
...style,
}
const handleClick = (e: ITouchEvent) => {
if (closeOnOverlayClick) {
onClick && onClick(e)
}
}
const springProps = useSpring({
opacity: innerVisible ? 1 : 0,
config: { duration },
onRest: () => {
if (innerVisible) {
afterShow()
} else {
afterClose()
}
},
})
return (
innerVisible && (
<animated.div
ref={nodeRef}
className={classes}
style={{ ...styles, ...springProps }}
{...rest}
onClick={handleClick}
>
{children}
</animated.div>
)
)
return (
<CSSTransition
nodeRef={nodeRef}
classNames={`${classPrefix}-slide`}
unmountOnExit
timeout={duration}
in={innerVisible}
onEntered={afterShow}
onExited={afterClose}
>
<View
ref={nodeRef}
className={classes}
style={styles}
{...(rest as any)}
catchMove={lockScroll}
onClick={handleClick}
>
{children}
</View>
</CSSTransition>
)
}
Overlay.displayName = 'NutOverlay'