one
Version:
One is a React framework for web, iOS, and Android apps.
1 lines • 5.54 kB
Source Map (JSON)
{"version":3,"file":"Portal.js","names":[],"sources":["esm/platform/ui/Portal.js"],"sourcesContent":["import { useEffect, useRef, useState } from \"react\";\nimport { createPortal } from \"react-dom\";\nimport { domStyle, VIEW_BASE } from \"../web/DomView\";\nimport { jsx } from \"react/jsx-runtime\";\nconst hosts = /* @__PURE__ */ new Map();\nconst portals = [];\nfunction refresh() {\n for (let i = 0; i < portals.length; i++) {\n const portal = portals[i];\n const replaced = portal.name != null && portals.slice(i + 1).some((other) => other.name === portal.name);\n if (replaced) {\n portal.content.remove();\n continue;\n }\n const host = portal.hostName == null ? null : hosts.get(portal.hostName);\n const target = host ?? portal.source;\n portal.content.removeAttribute(\"style\");\n Object.assign(\n portal.content.style,\n host ? {\n ...VIEW_BASE,\n ...portal.style,\n position: \"absolute\",\n inset: \"0\",\n width: \"100%\",\n height: \"100%\",\n pointerEvents: \"none\"\n } : { display: \"contents\" }\n );\n portal.source.style.position = host ? \"absolute\" : portal.style.position ?? \"relative\";\n target.appendChild(portal.content);\n }\n}\nfunction Portal({\n hostName,\n name,\n children,\n style,\n testID,\n nativeID,\n accessibilityLabel\n}) {\n const source = useRef(null);\n const entry = useRef(null);\n const [content, setContent] = useState(null);\n useEffect(() => {\n if (!source.current) return;\n const node = document.createElement(\"div\");\n const value = {\n hostName: hostName || void 0,\n name: name || void 0,\n source: source.current,\n content: node,\n style: domStyle(style)\n };\n entry.current = value;\n portals.push(value);\n setContent(node);\n refresh();\n return () => {\n portals.splice(portals.indexOf(value), 1);\n node.remove();\n entry.current = null;\n refresh();\n };\n }, []);\n useEffect(() => {\n const value = entry.current;\n if (!value) return;\n value.hostName = hostName || void 0;\n value.name = name || void 0;\n value.style = domStyle(style);\n refresh();\n }, [hostName, name, style]);\n return /* @__PURE__ */ jsx(\n \"div\",\n {\n ref: source,\n \"data-testid\": testID,\n id: nativeID,\n \"aria-label\": accessibilityLabel,\n style: {\n ...VIEW_BASE,\n ...domStyle(style)\n },\n children: content ? createPortal(\n /* @__PURE__ */ jsx(\"div\", { style: { display: \"contents\", pointerEvents: \"auto\" }, children }),\n content\n ) : null\n }\n );\n}\nfunction PortalHost({\n name,\n children,\n style,\n testID,\n nativeID,\n accessibilityLabel\n}) {\n const ref = useRef(null);\n useEffect(() => {\n const node = ref.current;\n if (!node) return;\n hosts.set(name, node);\n refresh();\n return () => {\n if (hosts.get(name) === node) hosts.delete(name);\n refresh();\n };\n }, [name]);\n return /* @__PURE__ */ jsx(\n \"div\",\n {\n ref,\n \"data-testid\": testID,\n id: nativeID,\n \"aria-label\": accessibilityLabel,\n style: { ...VIEW_BASE, ...domStyle(style), pointerEvents: \"none\" },\n children: /* @__PURE__ */ jsx(\"div\", { style: { display: \"contents\", pointerEvents: \"auto\" }, children })\n }\n );\n}\nexport {\n Portal,\n PortalHost\n};\n//# sourceMappingURL=Portal.js.map\n"],"mappings":";;;;;;AAIA,MAAM,wBAAwB,IAAI,IAAI;AACtC,MAAM,UAAU,CAAC;AACjB,SAAS,UAAU;CACjB,KAAK,IAAI,IAAI,GAAG,IAAI,QAAQ,QAAQ,KAAK;EACvC,MAAM,SAAS,QAAQ;EACvB,MAAM,WAAW,OAAO,QAAQ,QAAQ,QAAQ,MAAM,IAAI,CAAC,CAAC,CAAC,MAAM,UAAU,MAAM,SAAS,OAAO,IAAI;EACvG,IAAI,UAAU;GACZ,OAAO,QAAQ,OAAO;GACtB;EACF;EACA,MAAM,OAAO,OAAO,YAAY,OAAO,OAAO,MAAM,IAAI,OAAO,QAAQ;EACvE,MAAM,SAAS,QAAQ,OAAO;EAC9B,OAAO,QAAQ,gBAAgB,OAAO;EACtC,OAAO,OACL,OAAO,QAAQ,OACf,OAAO;GACL,GAAG;GACH,GAAG,OAAO;GACV,UAAU;GACV,OAAO;GACP,OAAO;GACP,QAAQ;GACR,eAAe;EACjB,IAAI,EAAE,SAAS,WAAW,CAC5B;EACA,OAAO,OAAO,MAAM,WAAW,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