UNPKG

3.58 kBJavaScriptView Raw
1'use client';
2
3import * as React from 'react';
4import * as ReactDOM from 'react-dom';
5import PropTypes from 'prop-types';
6import { exactProp, HTMLElementType, unstable_useEnhancedEffect as useEnhancedEffect, unstable_useForkRef as useForkRef, unstable_setRef as setRef } from '@mui/utils';
7import { jsx as _jsx } from "react/jsx-runtime";
8function getContainer(container) {
9 return typeof container === 'function' ? container() : container;
10}
11
12/**
13 * Portals provide a first-class way to render children into a DOM node
14 * that exists outside the DOM hierarchy of the parent component.
15 *
16 * Demos:
17 *
18 * - [Portal](https://mui.com/base-ui/react-portal/)
19 *
20 * API:
21 *
22 * - [Portal API](https://mui.com/base-ui/react-portal/components-api/#portal)
23 */
24const Portal = /*#__PURE__*/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 // @ts-expect-error TODO upstream fix
32 const handleRef = useForkRef( /*#__PURE__*/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 ( /*#__PURE__*/React.isValidElement(children)) {
49 const newProps = {
50 ref: handleRef
51 };
52 return /*#__PURE__*/React.cloneElement(children, newProps);
53 }
54 return /*#__PURE__*/_jsx(React.Fragment, {
55 children: children
56 });
57 }
58 return /*#__PURE__*/_jsx(React.Fragment, {
59 children: mountNode ? /*#__PURE__*/ReactDOM.createPortal(children, mountNode) : mountNode
60 });
61});
62process.env.NODE_ENV !== "production" ? Portal.propTypes /* remove-proptypes */ = {
63 // ┌────────────────────────────── Warning ──────────────────────────────┐
64 // │ These PropTypes are generated from the TypeScript type definitions. │
65 // │ To update them, edit the TypeScript types and run `pnpm proptypes`. │
66 // └─────────────────────────────────────────────────────────────────────┘
67 /**
68 * The children to render into the `container`.
69 */
70 children: PropTypes.node,
71 /**
72 * An HTML element or function that returns one.
73 * The `container` will have the portal children appended to it.
74 *
75 * You can also provide a callback, which is called in a React layout effect.
76 * This lets you set the container from a ref, and also makes server-side rendering possible.
77 *
78 * By default, it uses the body of the top-level document object,
79 * so it's simply `document.body` most of the time.
80 */
81 container: PropTypes /* @typescript-to-proptypes-ignore */.oneOfType([HTMLElementType, PropTypes.func]),
82 /**
83 * The `children` will be under the DOM hierarchy of the parent component.
84 * @default false
85 */
86 disablePortal: PropTypes.bool
87} : void 0;
88if (process.env.NODE_ENV !== 'production') {
89 // eslint-disable-next-line
90 Portal['propTypes' + ''] = exactProp(Portal.propTypes);
91}
92export { Portal };
\No newline at end of file