-
Notifications
You must be signed in to change notification settings - Fork 4k
Expand file tree
/
Copy pathPortalInner.js
More file actions
62 lines (49 loc) · 1.45 KB
/
PortalInner.js
File metadata and controls
62 lines (49 loc) · 1.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
import _ from 'lodash'
import PropTypes from 'prop-types'
import React from 'react'
import { createPortal } from 'react-dom'
import { isBrowser, makeDebugger, useEventCallback } from '../../lib'
import usePortalElement from './usePortalElement'
const debug = makeDebugger('PortalInner')
/**
* An inner component that allows you to render children outside their parent.
*/
const PortalInner = React.forwardRef(function (props, ref) {
const handleMount = useEventCallback(() => props.onMount?.(null, props))
const handleUnmount = useEventCallback(() => props.onUnmount?.(null, props))
const element = usePortalElement(props.children, ref)
React.useEffect(() => {
debug('componentDidMount()')
handleMount()
return () => {
debug('componentWillUnmount()')
handleUnmount()
}
}, [])
if (!isBrowser()) {
return null
}
return createPortal(element, props.mountNode || document.body)
})
PortalInner.displayName = 'PortalInner'
PortalInner.propTypes = {
/** Primary content. */
children: PropTypes.node.isRequired,
/** The node where the portal should mount. */
mountNode: PropTypes.any,
/**
* Called when the portal is mounted on the DOM
*
* @param {null}
* @param {object} data - All props.
*/
onMount: PropTypes.func,
/**
* Called when the portal is unmounted from the DOM
*
* @param {null}
* @param {object} data - All props.
*/
onUnmount: PropTypes.func,
}
export default PortalInner