UNPKG

@clayui/shared

Version:
87 lines (86 loc) 3.42 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; 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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var Portal_exports = {}; __export(Portal_exports, { ClayPortal: () => ClayPortal }); module.exports = __toCommonJS(Portal_exports); var import_provider = require("@clayui/provider"); var import_classnames = __toESM(require("classnames")); var import_react = __toESM(require("react")); var import_react_dom = require("react-dom"); const ClayPortalContext = import_react.default.createContext(null); ClayPortalContext.displayName = "ClayPortalContext"; function createDivElement(className, id) { const element = document.createElement("div"); if (className) { element.setAttribute("class", className); } if (id) { element.setAttribute("id", id); } return element; } function ClayPortal({ children, className, containerRef, id, subPortalRef }) { const { theme } = (0, import_provider.useProvider)(); const parentPortalRef = import_react.default.useContext(ClayPortalContext); const portalRef = import_react.default.useRef( typeof document !== "undefined" ? createDivElement((0, import_classnames.default)(theme, className), id) : null ); import_react.default.useEffect(() => { const closestParent = parentPortalRef && parentPortalRef.current ? parentPortalRef.current : document.body; const elToMountTo = containerRef && containerRef.current ? containerRef.current : closestParent; if (elToMountTo && portalRef.current) { elToMountTo.appendChild(portalRef.current); } return () => { if (portalRef.current) { if (typeof portalRef.current.remove === "function") { portalRef.current.remove(); } else if (elToMountTo) { elToMountTo.removeChild(portalRef.current); } } }; }, [containerRef, parentPortalRef]); const content = /* @__PURE__ */ import_react.default.createElement( ClayPortalContext.Provider, { value: subPortalRef ? subPortalRef : portalRef }, children ); return portalRef.current ? (0, import_react_dom.createPortal)(content, portalRef.current) : content; }