UNPKG

@mantine/core

Version:

React components library focused on usability, accessibility and developer experience

53 lines (49 loc) 2.13 kB
'use client'; 'use strict'; var jsxRuntime = require('react/jsx-runtime'); var React = require('react'); var ReactDOM = require('react-dom'); var hooks = require('@mantine/hooks'); require('clsx'); require('../../core/MantineProvider/Mantine.context.cjs'); require('../../core/MantineProvider/default-theme.cjs'); require('../../core/MantineProvider/MantineProvider.cjs'); require('../../core/MantineProvider/MantineThemeProvider/MantineThemeProvider.cjs'); var useProps = require('../../core/MantineProvider/use-props/use-props.cjs'); require('../../core/MantineProvider/MantineCssVariables/MantineCssVariables.cjs'); require('../../core/Box/Box.cjs'); require('../../core/DirectionProvider/DirectionProvider.cjs'); 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 = React.forwardRef((props, ref) => { const { children, target, ...others } = useProps.useProps("Portal", defaultProps, props); const [mounted, setMounted] = React.useState(false); const nodeRef = React.useRef(null); hooks.useIsomorphicEffect(() => { setMounted(true); nodeRef.current = !target ? createPortalNode(others) : typeof target === "string" ? document.querySelector(target) : target; hooks.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 ReactDOM.createPortal(/* @__PURE__ */ jsxRuntime.jsx(jsxRuntime.Fragment, { children }), nodeRef.current); }); Portal.displayName = "@mantine/core/Portal"; exports.Portal = Portal; //# sourceMappingURL=Portal.cjs.map