@mantine/core
Version:
React components library focused on usability, accessibility and developer experience
51 lines (48 loc) • 2.08 kB
JavaScript
'use client';
import { jsx, Fragment } from 'react/jsx-runtime';
import { forwardRef, useState, useRef } from 'react';
import { createPortal } from 'react-dom';
import { useIsomorphicEffect, assignRef } from '@mantine/hooks';
import 'clsx';
import '../../core/MantineProvider/Mantine.context.mjs';
import '../../core/MantineProvider/default-theme.mjs';
import '../../core/MantineProvider/MantineProvider.mjs';
import '../../core/MantineProvider/MantineThemeProvider/MantineThemeProvider.mjs';
import { useProps } from '../../core/MantineProvider/use-props/use-props.mjs';
import '../../core/MantineProvider/MantineCssVariables/MantineCssVariables.mjs';
import '../../core/Box/Box.mjs';
import '../../core/DirectionProvider/DirectionProvider.mjs';
function createPortalNode(props) {
const node = document.createElement("div");
node.setAttribute("data-portal", "true");
typeof props.className === "string" && node.classList.add(...props.className.split(" ").filter(Boolean));
typeof props.style === "object" && Object.assign(node.style, props.style);
typeof props.id === "string" && node.setAttribute("id", props.id);
return node;
}
const defaultProps = {};
const Portal = forwardRef((props, ref) => {
const { children, target, ...others } = useProps("Portal", defaultProps, props);
const [mounted, setMounted] = useState(false);
const nodeRef = useRef(null);
useIsomorphicEffect(() => {
setMounted(true);
nodeRef.current = !target ? createPortalNode(others) : typeof target === "string" ? document.querySelector(target) : target;
assignRef(ref, nodeRef.current);
if (!target && nodeRef.current) {
document.body.appendChild(nodeRef.current);
}
return () => {
if (!target && nodeRef.current) {
document.body.removeChild(nodeRef.current);
}
};
}, [target]);
if (!mounted || !nodeRef.current) {
return null;
}
return createPortal(/* @__PURE__ */ jsx(Fragment, { children }), nodeRef.current);
});
Portal.displayName = "@mantine/core/Portal";
export { Portal };
//# sourceMappingURL=Portal.mjs.map