1 | import * as React from 'react';
|
2 | import * as ReactDOM from 'react-dom';
|
3 | import PropTypes from 'prop-types';
|
4 | import { exactProp, HTMLElementType, unstable_useEnhancedEffect as useEnhancedEffect, unstable_useForkRef as useForkRef, unstable_setRef as setRef } from '@mui/utils';
|
5 | import { jsx as _jsx } from "react/jsx-runtime";
|
6 | function getContainer(container) {
|
7 | return typeof container === 'function' ? container() : container;
|
8 | }
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | const Portal = React.forwardRef(function Portal(props, forwardedRef) {
|
23 | const {
|
24 | children,
|
25 | container,
|
26 | disablePortal = false
|
27 | } = props;
|
28 | const [mountNode, setMountNode] = React.useState(null);
|
29 |
|
30 | const handleRef = useForkRef( React.isValidElement(children) ? children.ref : null, forwardedRef);
|
31 | useEnhancedEffect(() => {
|
32 | if (!disablePortal) {
|
33 | setMountNode(getContainer(container) || document.body);
|
34 | }
|
35 | }, [container, disablePortal]);
|
36 | useEnhancedEffect(() => {
|
37 | if (mountNode && !disablePortal) {
|
38 | setRef(forwardedRef, mountNode);
|
39 | return () => {
|
40 | setRef(forwardedRef, null);
|
41 | };
|
42 | }
|
43 | return undefined;
|
44 | }, [forwardedRef, mountNode, disablePortal]);
|
45 | if (disablePortal) {
|
46 | if ( React.isValidElement(children)) {
|
47 | const newProps = {
|
48 | ref: handleRef
|
49 | };
|
50 | return React.cloneElement(children, newProps);
|
51 | }
|
52 | return _jsx(React.Fragment, {
|
53 | children: children
|
54 | });
|
55 | }
|
56 | return _jsx(React.Fragment, {
|
57 | children: mountNode ? ReactDOM.createPortal(children, mountNode) : mountNode
|
58 | });
|
59 | });
|
60 | process.env.NODE_ENV !== "production" ? Portal.propTypes = {
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | |
66 |
|
67 |
|
68 | children: PropTypes.node,
|
69 | |
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | container: PropTypes .oneOfType([HTMLElementType, PropTypes.func]),
|
77 | |
78 |
|
79 |
|
80 |
|
81 | disablePortal: PropTypes.bool
|
82 | } : void 0;
|
83 | if (process.env.NODE_ENV !== 'production') {
|
84 |
|
85 | Portal['propTypes' + ''] = exactProp(Portal.propTypes);
|
86 | }
|
87 | export default Portal; |
\ | No newline at end of file |