-
Notifications
You must be signed in to change notification settings - Fork 331
/
Copy pathbackdrop.tsx
136 lines (130 loc) · 3.92 KB
/
backdrop.tsx
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import React, { MouseEvent } from 'react'
import useTheme from '../use-theme'
import CssTransition from './css-transition'
import useCurrentState from '../utils/use-current-state'
import useClasses from '../use-classes'
interface Props {
onClick?: (event: MouseEvent<HTMLElement>) => void
visible?: boolean
width?: string
onContentClick?: (event: MouseEvent<HTMLElement>) => void
backdropClassName?: string
positionClassName?: string
layerClassName?: string
}
const defaultProps = {
onClick: () => {},
visible: false,
onContentClick: () => {},
backdropClassName: '',
positionClassName: '',
layerClassName: '',
}
type NativeAttrs = Omit<React.HTMLAttributes<any>, keyof Props>
export type BackdropProps = Props & NativeAttrs
const Backdrop: React.FC<React.PropsWithChildren<BackdropProps>> = React.memo(
({
children,
onClick,
visible,
width,
onContentClick,
backdropClassName,
positionClassName,
layerClassName,
...props
}: React.PropsWithChildren<BackdropProps> & typeof defaultProps) => {
const theme = useTheme()
const [, setIsContentMouseDown, IsContentMouseDownRef] = useCurrentState(false)
const clickHandler = (event: MouseEvent<HTMLElement>) => {
if (IsContentMouseDownRef.current) return
onClick && onClick(event)
}
const mouseUpHandler = () => {
if (!IsContentMouseDownRef.current) return
const timer = setTimeout(() => {
setIsContentMouseDown(false)
clearTimeout(timer)
}, 0)
}
return (
<CssTransition name="backdrop-wrapper" visible={visible} clearTime={300}>
<div
className={useClasses('backdrop', backdropClassName)}
onClick={clickHandler}
onMouseUp={mouseUpHandler}
{...props}
>
<div className={useClasses('layer', layerClassName)} />
<div
onClick={onContentClick}
className={useClasses('position', positionClassName)}
onMouseDown={() => setIsContentMouseDown(true)}
>
{children}
</div>
<style jsx>{`
.backdrop {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
overflow: auto;
z-index: 1000;
-webkit-overflow-scrolling: touch;
box-sizing: border-box;
text-align: center;
}
.position {
position: relative;
z-index: 1001;
outline: none;
max-width: 90%;
width: ${width};
margin: 20px auto;
vertical-align: middle;
display: inline-block;
}
.backdrop:before {
display: inline-block;
width: 0;
height: 100%;
vertical-align: middle;
content: '';
}
.layer {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
width: 100%;
height: 100%;
opacity: ${theme.expressiveness.portalOpacity};
background-color: black;
transition: opacity 0.35s cubic-bezier(0.4, 0, 0.2, 1);
pointer-events: none;
z-index: 1000;
}
.backdrop-wrapper-enter .layer {
opacity: 0;
}
.backdrop-wrapper-enter-active .layer {
opacity: ${theme.expressiveness.portalOpacity};
}
.backdrop-wrapper-leave .layer {
opacity: ${theme.expressiveness.portalOpacity};
}
.backdrop-wrapper-leave-active .layer {
opacity: 0;
}
`}</style>
</div>
</CssTransition>
)
},
)
Backdrop.defaultProps = defaultProps
Backdrop.displayName = 'GeistBackdrop'
export default Backdrop