UNPKG

@yamada-ui/portal

Version:

Yamada UI portal component

68 lines (65 loc) 2.65 kB
"use client" "use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/default-portal.tsx var default_portal_exports = {}; __export(default_portal_exports, { DefaultPortal: () => DefaultPortal }); module.exports = __toCommonJS(default_portal_exports); var import_utils2 = require("@yamada-ui/utils"); var import_react = require("react"); var import_react_dom = require("react-dom"); // src/portal-provider.tsx var import_utils = require("@yamada-ui/utils"); var [PortalProvider, usePortal] = (0, import_utils.createContext)({ name: "PortalContext", strict: false }); // src/default-portal.tsx var import_jsx_runtime = require("react/jsx-runtime"); var DefaultPortal = ({ appendToParentPortal, children }) => { const [node, setNode] = (0, import_react.useState)(null); const el = (0, import_react.useRef)(null); const [, forceUpdate] = (0, import_react.useState)({}); (0, import_react.useEffect)(() => forceUpdate({}), []); const parent = usePortal(); (0, import_utils2.useSafeLayoutEffect)(() => { if (!node) return; const { ownerDocument } = node; const host = appendToParentPortal ? parent != null ? parent : ownerDocument.body : ownerDocument.body; el.current = ownerDocument.createElement("div"); el.current.className = "ui-portal"; host.appendChild(el.current); forceUpdate({}); const portalNode = el.current; return () => { if (host.contains(portalNode)) host.removeChild(portalNode); }; }, [node]); return el.current ? (0, import_react_dom.createPortal)( /* @__PURE__ */ (0, import_jsx_runtime.jsx)(PortalProvider, { value: el.current, children }), el.current ) : /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { ref: (el2) => el2 ? setNode(el2) : void 0 }); }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { DefaultPortal }); //# sourceMappingURL=default-portal.js.map