-
Notifications
You must be signed in to change notification settings - Fork 4.1k
/
Copy pathPortal.js
382 lines (310 loc) · 10.4 KB
/
Portal.js
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import EventStack from '@semantic-ui-react/event-stack'
import keyboardKey from 'keyboard-key'
import PropTypes from 'prop-types'
import * as React from 'react'
import {
customPropTypes,
doesNodeContainClick,
makeDebugger,
useAutoControlledValue,
} from '../../lib'
import useTrigger from './utils/useTrigger'
import PortalInner from './PortalInner'
const debug = makeDebugger('portal')
/**
* A component that allows you to render children outside their parent.
* @see Modal
* @see Popup
* @see Dimmer
* @see Confirm
*/
function Portal(props) {
const {
children,
closeOnDocumentClick = true,
closeOnEscape = true,
closeOnPortalMouseLeave,
closeOnTriggerBlur,
closeOnTriggerClick,
closeOnTriggerMouseLeave,
eventPool = 'default',
mountNode,
mouseEnterDelay,
mouseLeaveDelay,
openOnTriggerClick = true,
openOnTriggerFocus,
openOnTriggerMouseEnter,
} = props
const [open, setOpen] = useAutoControlledValue({
state: props.open,
defaultState: props.defaultOpen,
initialState: false,
})
const contentRef = React.useRef()
const [triggerRef, trigger] = useTrigger(props.trigger, props.triggerRef)
const mouseEnterTimer = React.useRef()
const mouseLeaveTimer = React.useRef()
const latestDocumentMouseDownEvent = React.useRef()
// ----------------------------------------
// Behavior
// ----------------------------------------
const openPortal = (e) => {
debug('open()')
setOpen(true)
props.onOpen?.(e, { ...props, open: true })
}
const openPortalWithTimeout = (e, delay) => {
debug('openWithTimeout()', delay)
// React wipes the entire event object and suggests using e.persist() if
// you need the event for async access. However, even with e.persist
// certain required props (e.g. currentTarget) are null so we're forced to clone.
const eventClone = { ...e }
return setTimeout(() => openPortal(eventClone), delay || 0)
}
const closePortal = (e) => {
debug('close()')
setOpen(false)
props.onClose?.(e, { ...props, open: false })
}
const closePortalWithTimeout = (e, delay) => {
debug('closeWithTimeout()', delay)
// React wipes the entire event object and suggests using e.persist() if
// you need the event for async access. However, even with e.persist
// certain required props (e.g. currentTarget) are null so we're forced to clone.
const eventClone = { ...e }
return setTimeout(() => closePortal(eventClone), delay || 0)
}
// ----------------------------------------
// Document Event Handlers
// ----------------------------------------
React.useEffect(() => {
// Clean up timers
clearTimeout(mouseEnterTimer.current)
clearTimeout(mouseLeaveTimer.current)
}, [])
const handleDocumentMouseDown = (e) => {
latestDocumentMouseDownEvent.current = e
}
const handleDocumentClick = (e) => {
const currentMouseDownEvent = latestDocumentMouseDownEvent.current
latestDocumentMouseDownEvent.current = null
// event happened in trigger (delegate to trigger handlers)
const isInsideTrigger = doesNodeContainClick(triggerRef.current, e)
// event originated in the portal but was ended outside
const isOriginatedFromPortal =
currentMouseDownEvent && doesNodeContainClick(contentRef.current, currentMouseDownEvent)
// event happened in the portal
const isInsidePortal = doesNodeContainClick(contentRef.current, e)
if (
!contentRef.current?.contains || // no portal
isInsideTrigger ||
isOriginatedFromPortal ||
isInsidePortal
) {
return
} // ignore the click
if (closeOnDocumentClick) {
debug('handleDocumentClick()')
closePortal(e)
}
}
const handleEscape = (e) => {
if (!closeOnEscape) {
return
}
if (keyboardKey.getCode(e) !== keyboardKey.Escape) {
return
}
debug('handleEscape()')
closePortal(e)
}
// ----------------------------------------
// Component Event Handlers
// ----------------------------------------
const handlePortalMouseLeave = (e) => {
if (!closeOnPortalMouseLeave) {
return
}
// Do not close the portal when 'mouseleave' is triggered by children
if (e.target !== contentRef.current) {
return
}
debug('handlePortalMouseLeave()')
mouseLeaveTimer.current = closePortalWithTimeout(e, mouseLeaveDelay)
}
const handlePortalMouseEnter = () => {
// In order to enable mousing from the trigger to the portal, we need to
// clear the mouseleave timer that was set when leaving the trigger.
if (!closeOnPortalMouseLeave) {
return
}
debug('handlePortalMouseEnter()')
clearTimeout(mouseLeaveTimer.current)
}
const handleTriggerBlur = (e, ...rest) => {
// Call original event handler
trigger.props.onBlur?.(e, ...rest)
// IE 11 doesn't work with relatedTarget in blur events
const target = e.relatedTarget || document.activeElement
// do not close if focus is given to the portal
const didFocusPortal = contentRef.current.contains?.(target)
if (!closeOnTriggerBlur || didFocusPortal) {
return
}
debug('handleTriggerBlur()')
closePortal(e)
}
const handleTriggerClick = (e, ...rest) => {
// Call original event handler
trigger.props.onClick?.(e, ...rest)
if (open && closeOnTriggerClick) {
debug('handleTriggerClick() - close')
closePortal(e)
} else if (!open && openOnTriggerClick) {
debug('handleTriggerClick() - open')
openPortal(e)
}
}
const handleTriggerFocus = (e, ...rest) => {
// Call original event handler
trigger.props.onFocus?.(e, ...rest)
if (!openOnTriggerFocus) {
return
}
debug('handleTriggerFocus()')
openPortal(e)
}
const handleTriggerMouseLeave = (e, ...rest) => {
clearTimeout(mouseEnterTimer.current)
// Call original event handler
trigger.props.onMouseLeave?.(e, ...rest)
if (!closeOnTriggerMouseLeave) {
return
}
debug('handleTriggerMouseLeave()')
mouseLeaveTimer.current = closePortalWithTimeout(e, mouseLeaveDelay)
}
const handleTriggerMouseEnter = (e, ...rest) => {
clearTimeout(mouseLeaveTimer.current)
// Call original event handler
trigger.props.onMouseEnter?.(e, ...rest)
if (!openOnTriggerMouseEnter) {
return
}
debug('handleTriggerMouseEnter()')
mouseEnterTimer.current = openPortalWithTimeout(e, mouseEnterDelay)
}
return (
<>
{open && (
<>
<PortalInner
mountNode={mountNode}
onMount={() => props.onMount?.(null, props)}
onUnmount={() => props.onUnmount?.(null, props)}
ref={contentRef}
>
{children}
</PortalInner>
<EventStack
name='mouseleave'
on={handlePortalMouseLeave}
pool={eventPool}
target={contentRef}
/>
<EventStack
name='mouseenter'
on={handlePortalMouseEnter}
pool={eventPool}
target={contentRef}
/>
<EventStack name='mousedown' on={handleDocumentMouseDown} pool={eventPool} />
<EventStack name='click' on={handleDocumentClick} pool={eventPool} />
<EventStack name='keydown' on={handleEscape} pool={eventPool} />
</>
)}
{trigger &&
React.cloneElement(trigger, {
onBlur: handleTriggerBlur,
onClick: handleTriggerClick,
onFocus: handleTriggerFocus,
onMouseLeave: handleTriggerMouseLeave,
onMouseEnter: handleTriggerMouseEnter,
ref: triggerRef,
})}
</>
)
}
Portal.displayName = 'Portal'
Portal.propTypes = {
/** Primary content. */
children: PropTypes.node.isRequired,
/** Controls whether or not the portal should close when the document is clicked. */
closeOnDocumentClick: PropTypes.bool,
/** Controls whether or not the portal should close when escape is pressed is displayed. */
closeOnEscape: PropTypes.bool,
/**
* Controls whether or not the portal should close when mousing out of the portal.
* NOTE: This will prevent `closeOnTriggerMouseLeave` when mousing over the
* gap from the trigger to the portal.
*/
closeOnPortalMouseLeave: PropTypes.bool,
/** Controls whether or not the portal should close on blur of the trigger. */
closeOnTriggerBlur: PropTypes.bool,
/** Controls whether or not the portal should close on click of the trigger. */
closeOnTriggerClick: PropTypes.bool,
/** Controls whether or not the portal should close when mousing out of the trigger. */
closeOnTriggerMouseLeave: PropTypes.bool,
/** Initial value of open. */
defaultOpen: PropTypes.bool,
/** Event pool namespace that is used to handle component events */
eventPool: PropTypes.string,
/** The node where the portal should mount. */
mountNode: PropTypes.any,
/** Milliseconds to wait before opening on mouse over */
mouseEnterDelay: PropTypes.number,
/** Milliseconds to wait before closing on mouse leave */
mouseLeaveDelay: PropTypes.number,
/**
* Called when a close event happens
*
* @param {SyntheticEvent} event - React's original SyntheticEvent.
* @param {object} data - All props.
*/
onClose: PropTypes.func,
/**
* Called when the portal is mounted on the DOM.
*
* @param {null}
* @param {object} data - All props.
*/
onMount: PropTypes.func,
/**
* Called when an open event happens
*
* @param {SyntheticEvent} event - React's original SyntheticEvent.
* @param {object} data - All props.
*/
onOpen: PropTypes.func,
/**
* Called when the portal is unmounted from the DOM.
*
* @param {null}
* @param {object} data - All props.
*/
onUnmount: PropTypes.func,
/** Controls whether or not the portal is displayed. */
open: PropTypes.bool,
/** Controls whether or not the portal should open when the trigger is clicked. */
openOnTriggerClick: PropTypes.bool,
/** Controls whether or not the portal should open on focus of the trigger. */
openOnTriggerFocus: PropTypes.bool,
/** Controls whether or not the portal should open when mousing over the trigger. */
openOnTriggerMouseEnter: PropTypes.bool,
/** Element to be rendered in-place where the portal is defined. */
trigger: PropTypes.node,
/** Called with a ref to the trigger node. */
triggerRef: customPropTypes.ref,
}
Portal.Inner = PortalInner
export default Portal