-
Notifications
You must be signed in to change notification settings - Fork 6
/
Overlay.tsx
145 lines (131 loc) · 3.46 KB
/
Overlay.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
137
138
139
140
141
142
143
144
145
import React, {
useRef,
useEffect,
useState,
useCallback,
PropsWithChildren,
} from 'react'
import Portal, { Props as PortalProps } from './components/Portal'
import throttle from 'lodash.throttle'
interface Props extends PortalProps {
fullWindow?: boolean
alignment?: HorizontalAlignment
}
interface Position {
x: number
y: number
}
type HorizontalAlignment = 'right' | 'left' | 'center'
const getXPositionByHorizontalAlignment = (
alignment: HorizontalAlignment,
bounds: ClientRect | DOMRect
) => {
switch (alignment) {
case 'left':
return bounds.left
case 'right':
return bounds.right
case 'center':
return (bounds.right + bounds.left) / 2
default:
return bounds.left
}
}
const getJustifyTypeByHorizontalAlignment = (
alignment: HorizontalAlignment
) => {
switch (alignment) {
case 'left':
return 'justify-start'
case 'right':
return 'justify-end'
case 'center':
return 'justify-center'
default:
return 'justify-start'
}
}
function Overlay({
children,
fullWindow,
target,
alignment = 'left',
}: PropsWithChildren<Props>) {
const container = useRef<HTMLDivElement>(null)
const [position, setPosition] = useState<Position>()
/** updatePosition is throttled due to the use of getBoundingClientRect,
* which triggers a recalculation of the entire page layout, which in
* turn can be quite heavy */
const updatePosition = useCallback(
throttle(
() => {
if (!fullWindow && container.current) {
const bounds = container.current.getBoundingClientRect()
setPosition({
x: getXPositionByHorizontalAlignment(alignment, bounds),
y: bounds.top,
})
}
/** Also, the throttling is set to leading: false (which means that
* the function won't be called right away, but only after X ms),
* to improve hydration performance */
},
200,
{ leading: false }
),
[alignment, fullWindow]
)
useEffect(() => {
updatePosition()
if (window) {
window.addEventListener('scroll', updatePosition)
window.addEventListener('load', updatePosition)
window.addEventListener('resize', updatePosition)
window.addEventListener('visibilitychange', updatePosition)
}
return () => {
if (window) {
window.removeEventListener('scroll', updatePosition)
window.removeEventListener('load', updatePosition)
window.removeEventListener('resize', updatePosition)
window.removeEventListener('visibilitychange', updatePosition)
}
}
}, [alignment, fullWindow, updatePosition, children])
if (!fullWindow) {
return (
// Div used for anchoring the position of the overlay, for popovers etc
<div
ref={container}
style={{
width: 'auto',
height: 1,
marginTop: -1,
}}
>
{position && (
<Portal target={target}>
<div
className={`flex ${getJustifyTypeByHorizontalAlignment(
alignment
)}`}
style={{
position: 'absolute',
left: position.x,
top: position.y,
}}
>
<div className="absolute">{children}</div>
</div>
</Portal>
)}
</div>
)
}
return (
<Portal target={target} cover>
{children}
</Portal>
)
}
export default Overlay