1 | 'use client';
|
2 |
|
3 | import * as React from 'react';
|
4 | import * as ReactDOM from 'react-dom';
|
5 | import PropTypes from 'prop-types';
|
6 | import { exactProp, HTMLElementType, unstable_useEnhancedEffect as useEnhancedEffect, unstable_useForkRef as useForkRef, unstable_setRef as setRef, unstable_getReactElementRef as getReactElementRef } from '@mui/utils';
|
7 | function getContainer(container) {
|
8 | return typeof container === 'function' ? container() : container;
|
9 | }
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | const Portal = React.forwardRef(function Portal(props, forwardedRef) {
|
24 | const {
|
25 | children,
|
26 | container,
|
27 | disablePortal = false
|
28 | } = props;
|
29 | const [mountNode, setMountNode] = React.useState(null);
|
30 | const handleRef = useForkRef(React.isValidElement(children) ? getReactElementRef(children) : 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 children;
|
53 | }
|
54 | return mountNode ? ReactDOM.createPortal(children, mountNode) : mountNode;
|
55 | });
|
56 | process.env.NODE_ENV !== "production" ? Portal.propTypes = {
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | |
62 |
|
63 |
|
64 | children: PropTypes.node,
|
65 | |
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 | container: PropTypes .oneOfType([HTMLElementType, PropTypes.func]),
|
76 | |
77 |
|
78 |
|
79 |
|
80 | disablePortal: PropTypes.bool
|
81 | } : void 0;
|
82 | if (process.env.NODE_ENV !== 'production') {
|
83 |
|
84 | Portal['propTypes' + ''] = exactProp(Portal.propTypes);
|
85 | }
|
86 | export default Portal; |
\ | No newline at end of file |