-
Notifications
You must be signed in to change notification settings - Fork 299
Expand file tree
/
Copy pathoverlay.tsx
More file actions
92 lines (84 loc) · 1.95 KB
/
Copy pathoverlay.tsx
File metadata and controls
92 lines (84 loc) · 1.95 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
import React, {
FunctionComponent,
MouseEvent,
useEffect,
useRef,
useState,
} from 'react'
import { useSpring, animated } from '@react-spring/web'
import classNames from 'classnames'
import { ComponentDefaults } from '@/utils/typings'
import { useLockScroll } from '@/hooks/use-lock-scroll'
import { WebOverlayProps } from '@/types'
export const defaultOverlayProps: WebOverlayProps = {
...ComponentDefaults,
zIndex: 1000,
duration: 300,
closeOnOverlayClick: true,
visible: false,
lockScroll: true,
onClick: () => {},
afterShow: () => {},
afterClose: () => {},
}
export const Overlay: FunctionComponent<
Partial<WebOverlayProps> & React.HTMLAttributes<HTMLDivElement>
> = (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 = useRef(null)
useEffect(() => {
setInnerVisible(visible)
}, [visible])
const shouldLockScroll = !innerVisible ? false : lockScroll
useLockScroll(nodeRef, shouldLockScroll)
const classes = classNames(classPrefix, `${classPrefix}-slide`, className)
const styles = {
...style,
zIndex,
}
const handleClick = (e: MouseEvent) => {
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>
)
)
}
Overlay.displayName = 'NutOverlay'