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 | 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 | const handleRef = useForkRef(React.isValidElement(children) ? getReactElementRef(children) : null, forwardedRef);
|
32 | useEnhancedEffect(() => {
|
33 | if (!disablePortal) {
|
34 | setMountNode(getContainer(container) || document.body);
|
35 | }
|
36 | }, [container, disablePortal]);
|
37 | useEnhancedEffect(() => {
|
38 | if (mountNode && !disablePortal) {
|
39 | setRef(forwardedRef, mountNode);
|
40 | return () => {
|
41 | setRef(forwardedRef, null);
|
42 | };
|
43 | }
|
44 | return undefined;
|
45 | }, [forwardedRef, mountNode, disablePortal]);
|
46 | if (disablePortal) {
|
47 | if (React.isValidElement(children)) {
|
48 | const newProps = {
|
49 | ref: handleRef
|
50 | };
|
51 | return React.cloneElement(children, newProps);
|
52 | }
|
53 | return _jsx(React.Fragment, {
|
54 | children: children
|
55 | });
|
56 | }
|
57 | return _jsx(React.Fragment, {
|
58 | children: mountNode ? ReactDOM.createPortal(children, mountNode) : mountNode
|
59 | });
|
60 | });
|
61 | process.env.NODE_ENV !== "production" ? Portal.propTypes = {
|
62 |
|
63 |
|
64 |
|
65 |
|
66 | |
67 |
|
68 |
|
69 | children: PropTypes.node,
|
70 | |
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 | container: PropTypes .oneOfType([HTMLElementType, PropTypes.func]),
|
81 | |
82 |
|
83 |
|
84 |
|
85 | disablePortal: PropTypes.bool
|
86 | } : void 0;
|
87 | if (process.env.NODE_ENV !== 'production') {
|
88 |
|
89 | Portal['propTypes' + ''] = exactProp(Portal.propTypes);
|
90 | }
|
91 | export default Portal; |
\ | No newline at end of file |