@yamada-ui/react
Version:
React UI components of the Yamada, by the Yamada, for the Yamada built with React and Emotion
36 lines (32 loc) • 1.27 kB
JavaScript
"use client";
import { useSsr } from "../../utils/ssr.js";
import { utils_exports } from "../../utils/index.js";
import { useEnvironment } from "../../core/system/environment-provider.js";
import { Children, useEffect, useState } from "react";
import { Fragment as Fragment$1, jsx } from "react/jsx-runtime";
import { createPortal } from "react-dom";
//#region src/components/portal/portal.tsx
const getPortalNode = (node) => {
const rootNode = node?.getRootNode();
if ((0, utils_exports.isShadowRoot)(rootNode)) return rootNode;
return (0, utils_exports.getDocument)(node).body;
};
/**
* `Portal` is a component that renders elements outside of the current `DOM` hierarchy.
*
* @see https://yamada-ui.com/docs/components/portal
*/
const Portal = ({ children, containerRef: ref, disabled }) => {
const [target, setTarget] = useState(ref?.current);
const ssr = useSsr();
const { getRootNode } = useEnvironment();
useEffect(() => {
setTarget(() => ref?.current);
}, [ref]);
if (ssr || disabled) return children;
const container = target ?? getPortalNode(getRootNode());
return /* @__PURE__ */ jsx(Fragment$1, { children: Children.map(children, (child) => createPortal(child, container)) });
};
//#endregion
export { Portal };
//# sourceMappingURL=portal.js.map