@mantine/core
Version:
React components library focused on usability, accessibility and developer experience
58 lines (57 loc) • 2.47 kB
JavaScript
"use client";
require("../../_virtual/_rolldown/runtime.cjs");
const require_use_props = require("../../core/MantineProvider/use-props/use-props.cjs");
const require_factory = require("../../core/factory/factory.cjs");
let react = require("react");
let _mantine_hooks = require("@mantine/hooks");
let react_jsx_runtime = require("react/jsx-runtime");
let react_dom = require("react-dom");
//#region packages/@mantine/core/src/components/Portal/Portal.tsx
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;
}
function getTargetNode({ target, reuseTargetNode, ...others }) {
if (target) {
if (typeof target === "string") return document.querySelector(target) || createPortalNode(others);
return target;
}
if (reuseTargetNode) {
const existingNode = document.querySelector("[data-mantine-shared-portal-node]");
if (existingNode) return existingNode;
const node = createPortalNode(others);
node.setAttribute("data-mantine-shared-portal-node", "true");
document.body.appendChild(node);
return node;
}
return createPortalNode(others);
}
const defaultProps = { reuseTargetNode: true };
const Portal = require_factory.factory((props) => {
const { children, target, reuseTargetNode, ref, ...others } = require_use_props.useProps("Portal", defaultProps, props);
const [mounted, setMounted] = (0, react.useState)(false);
const nodeRef = (0, react.useRef)(null);
(0, _mantine_hooks.useIsomorphicEffect)(() => {
setMounted(true);
nodeRef.current = getTargetNode({
target,
reuseTargetNode,
...others
});
(0, _mantine_hooks.assignRef)(ref, nodeRef.current);
if (!target && !reuseTargetNode && nodeRef.current) document.body.appendChild(nodeRef.current);
return () => {
if (!target && !reuseTargetNode && nodeRef.current) document.body.removeChild(nodeRef.current);
};
}, [target]);
if (!mounted || !nodeRef.current) return null;
return (0, react_dom.createPortal)(/* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children }), nodeRef.current);
});
Portal.displayName = "@mantine/core/Portal";
//#endregion
exports.Portal = Portal;
//# sourceMappingURL=Portal.cjs.map