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