@tamagui/portal 1.0.0-beta.9 → 1.0.1-beta.102

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,261 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
9
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
10
+ var __spreadValues = (a, b) => {
11
+ for (var prop in b || (b = {}))
12
+ if (__hasOwnProp.call(b, prop))
13
+ __defNormalProp(a, prop, b[prop]);
14
+ if (__getOwnPropSymbols)
15
+ for (var prop of __getOwnPropSymbols(b)) {
16
+ if (__propIsEnum.call(b, prop))
17
+ __defNormalProp(a, prop, b[prop]);
18
+ }
19
+ return a;
20
+ };
21
+ var __export = (target, all) => {
22
+ for (var name in all)
23
+ __defProp(target, name, { get: all[name], enumerable: true });
24
+ };
25
+ var __copyProps = (to, from, except, desc) => {
26
+ if (from && typeof from === "object" || typeof from === "function") {
27
+ for (let key of __getOwnPropNames(from))
28
+ if (!__hasOwnProp.call(to, key) && key !== except)
29
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
30
+ }
31
+ return to;
32
+ };
33
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
34
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
35
+ var GorhomPortal_exports = {};
36
+ __export(GorhomPortal_exports, {
37
+ ACTIONS: () => ACTIONS,
38
+ INITIAL_STATE: () => INITIAL_STATE,
39
+ PortalHost: () => PortalHost,
40
+ PortalItem: () => PortalItem,
41
+ PortalProvider: () => PortalProvider,
42
+ usePortal: () => usePortal
43
+ });
44
+ module.exports = __toCommonJS(GorhomPortal_exports);
45
+ var import_core = require("@tamagui/core");
46
+ var import_non_secure = require("nanoid/non-secure");
47
+ var import_react = require("react");
48
+ var import_react2 = __toESM(require("react"));
49
+ var ACTIONS = /* @__PURE__ */ ((ACTIONS2) => {
50
+ ACTIONS2[ACTIONS2["REGISTER_HOST"] = 0] = "REGISTER_HOST";
51
+ ACTIONS2[ACTIONS2["DEREGISTER_HOST"] = 1] = "DEREGISTER_HOST";
52
+ ACTIONS2[ACTIONS2["ADD_UPDATE_PORTAL"] = 2] = "ADD_UPDATE_PORTAL";
53
+ ACTIONS2[ACTIONS2["REMOVE_PORTAL"] = 3] = "REMOVE_PORTAL";
54
+ return ACTIONS2;
55
+ })(ACTIONS || {});
56
+ const INITIAL_STATE = {};
57
+ const registerHost = (state, hostName) => {
58
+ if (!(hostName in state)) {
59
+ state[hostName] = [];
60
+ }
61
+ return state;
62
+ };
63
+ const deregisterHost = (state, hostName) => {
64
+ delete state[hostName];
65
+ return state;
66
+ };
67
+ const addUpdatePortal = (state, hostName, portalName, node) => {
68
+ if (!(hostName in state)) {
69
+ state = registerHost(state, hostName);
70
+ }
71
+ const index = state[hostName].findIndex((item) => item.name === portalName);
72
+ if (index !== -1) {
73
+ state[hostName][index].node = node;
74
+ } else {
75
+ state[hostName].push({
76
+ name: portalName,
77
+ node
78
+ });
79
+ }
80
+ return state;
81
+ };
82
+ const removePortal = (state, hostName, portalName) => {
83
+ if (!(hostName in state)) {
84
+ console.log(`Failed to remove portal '${portalName}', '${hostName}' was not registered!`);
85
+ return state;
86
+ }
87
+ const index = state[hostName].findIndex((item) => item.name === portalName);
88
+ if (index !== -1)
89
+ state[hostName].splice(index, 1);
90
+ return state;
91
+ };
92
+ const reducer = (state, action) => {
93
+ const { type } = action;
94
+ switch (type) {
95
+ case 0 /* REGISTER_HOST */:
96
+ return registerHost(__spreadValues({}, state), action.hostName);
97
+ case 1 /* DEREGISTER_HOST */:
98
+ return deregisterHost(__spreadValues({}, state), action.hostName);
99
+ case 2 /* ADD_UPDATE_PORTAL */:
100
+ return addUpdatePortal(__spreadValues({}, state), action.hostName, action.portalName, action.node);
101
+ case 3 /* REMOVE_PORTAL */:
102
+ return removePortal(__spreadValues({}, state), action.hostName, action.portalName);
103
+ default:
104
+ return state;
105
+ }
106
+ };
107
+ const PortalStateContext = (0, import_react2.createContext)(null);
108
+ const PortalDispatchContext = (0, import_react2.createContext)(null);
109
+ const usePortalState = (hostName) => {
110
+ const state = (0, import_react2.useContext)(PortalStateContext);
111
+ if (state === null) {
112
+ throw new Error("'PortalStateContext' cannot be null, please add 'PortalProvider' to the root component.");
113
+ }
114
+ return state[hostName] || [];
115
+ };
116
+ const usePortal = (hostName = "root") => {
117
+ const dispatch = (0, import_react2.useContext)(PortalDispatchContext);
118
+ if (dispatch === null) {
119
+ throw new Error("'PortalDispatchContext' cannot be null, please add 'PortalProvider' to the root component.");
120
+ }
121
+ const registerHost2 = (0, import_react2.useCallback)(() => {
122
+ dispatch({
123
+ type: 0 /* REGISTER_HOST */,
124
+ hostName
125
+ });
126
+ }, []);
127
+ const deregisterHost2 = (0, import_react2.useCallback)(() => {
128
+ dispatch({
129
+ type: 1 /* DEREGISTER_HOST */,
130
+ hostName
131
+ });
132
+ }, []);
133
+ const addUpdatePortal2 = (0, import_react2.useCallback)((name, node) => {
134
+ dispatch({
135
+ type: 2 /* ADD_UPDATE_PORTAL */,
136
+ hostName,
137
+ portalName: name,
138
+ node
139
+ });
140
+ }, []);
141
+ const removePortal2 = (0, import_react2.useCallback)((name) => {
142
+ dispatch({
143
+ type: 3 /* REMOVE_PORTAL */,
144
+ hostName,
145
+ portalName: name
146
+ });
147
+ }, []);
148
+ return {
149
+ registerHost: registerHost2,
150
+ deregisterHost: deregisterHost2,
151
+ addPortal: addUpdatePortal2,
152
+ updatePortal: addUpdatePortal2,
153
+ removePortal: removePortal2
154
+ };
155
+ };
156
+ const PortalProviderComponent = ({
157
+ rootHostName = "root",
158
+ shouldAddRootHost = true,
159
+ children
160
+ }) => {
161
+ const [state, dispatch] = (0, import_react2.useReducer)(reducer, INITIAL_STATE);
162
+ const transitionDispatch = (0, import_react.useMemo)(() => {
163
+ const next = (value) => {
164
+ (0, import_core.startTransition)(() => {
165
+ dispatch(value);
166
+ });
167
+ };
168
+ return next;
169
+ }, [dispatch]);
170
+ return /* @__PURE__ */ import_react2.default.createElement(PortalDispatchContext.Provider, {
171
+ value: transitionDispatch
172
+ }, /* @__PURE__ */ import_react2.default.createElement(PortalStateContext.Provider, {
173
+ value: state
174
+ }, children, shouldAddRootHost && /* @__PURE__ */ import_react2.default.createElement(PortalHost, {
175
+ name: rootHostName
176
+ })));
177
+ };
178
+ const PortalProvider = (0, import_react2.memo)(PortalProviderComponent);
179
+ PortalProvider.displayName = "PortalProvider";
180
+ const PortalHostComponent = ({ name }) => {
181
+ const isServer = (0, import_core.useIsSSR)();
182
+ const state = usePortalState(name);
183
+ const { registerHost: registerHost2, deregisterHost: deregisterHost2 } = usePortal(name);
184
+ (0, import_react2.useEffect)(() => {
185
+ if (isServer)
186
+ return;
187
+ registerHost2();
188
+ return () => {
189
+ deregisterHost2();
190
+ };
191
+ }, [isServer]);
192
+ return /* @__PURE__ */ import_react2.default.createElement(import_react2.default.Fragment, null, state.map((item) => item.node));
193
+ };
194
+ const PortalHost = (0, import_react2.memo)(PortalHostComponent);
195
+ PortalHost.displayName = "PortalHost";
196
+ const PortalComponent = ({
197
+ name: _providedName,
198
+ hostName,
199
+ handleOnMount: _providedHandleOnMount,
200
+ handleOnUnmount: _providedHandleOnUnmount,
201
+ handleOnUpdate: _providedHandleOnUpdate,
202
+ children
203
+ }) => {
204
+ const { addPortal: addUpdatePortal2, removePortal: removePortal2 } = usePortal(hostName);
205
+ const name = (0, import_react.useMemo)(() => _providedName || (0, import_non_secure.nanoid)(), [_providedName]);
206
+ const handleOnMountRef = (0, import_react.useRef)();
207
+ const handleOnUnmountRef = (0, import_react.useRef)();
208
+ const handleOnUpdateRef = (0, import_react.useRef)();
209
+ const handleOnMount = (0, import_react2.useCallback)(() => {
210
+ if (_providedHandleOnMount) {
211
+ _providedHandleOnMount(() => addUpdatePortal2(name, children));
212
+ } else {
213
+ addUpdatePortal2(name, children);
214
+ }
215
+ }, [_providedHandleOnMount, addUpdatePortal2]);
216
+ handleOnMountRef.current = handleOnMount;
217
+ const handleOnUnmount = (0, import_react2.useCallback)(() => {
218
+ if (_providedHandleOnUnmount) {
219
+ _providedHandleOnUnmount(() => removePortal2(name));
220
+ } else {
221
+ removePortal2(name);
222
+ }
223
+ }, [_providedHandleOnUnmount, removePortal2]);
224
+ handleOnUnmountRef.current = handleOnUnmount;
225
+ const handleOnUpdate = (0, import_react2.useCallback)(() => {
226
+ if (_providedHandleOnUpdate) {
227
+ _providedHandleOnUpdate(() => addUpdatePortal2(name, children));
228
+ } else {
229
+ addUpdatePortal2(name, children);
230
+ }
231
+ }, [_providedHandleOnUpdate, addUpdatePortal2, children]);
232
+ handleOnUpdateRef.current = handleOnUpdate;
233
+ (0, import_react2.useEffect)(() => {
234
+ var _a;
235
+ (_a = handleOnMountRef.current) == null ? void 0 : _a.call(handleOnMountRef);
236
+ return () => {
237
+ var _a2;
238
+ (_a2 = handleOnUnmountRef.current) == null ? void 0 : _a2.call(handleOnUnmountRef);
239
+ handleOnMountRef.current = void 0;
240
+ handleOnUnmountRef.current = void 0;
241
+ handleOnUpdateRef.current = void 0;
242
+ };
243
+ }, []);
244
+ (0, import_react2.useEffect)(() => {
245
+ var _a;
246
+ (_a = handleOnUpdateRef.current) == null ? void 0 : _a.call(handleOnUpdateRef);
247
+ }, [children]);
248
+ return null;
249
+ };
250
+ const PortalItem = (0, import_react2.memo)(PortalComponent);
251
+ PortalItem.displayName = "Portal";
252
+ // Annotate the CommonJS export names for ESM import in node:
253
+ 0 && (module.exports = {
254
+ ACTIONS,
255
+ INITIAL_STATE,
256
+ PortalHost,
257
+ PortalItem,
258
+ PortalProvider,
259
+ usePortal
260
+ });
261
+ //# sourceMappingURL=GorhomPortal.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/GorhomPortal.tsx"],
4
+ "sourcesContent": ["// from https://github.com/gorhom/react-native-portal\n// MIT License Copyright (c) 2020 Mo Gorhom\n// fixing SSR issue\n\nimport { startTransition, useIsSSR } from '@tamagui/core'\nimport { nanoid } from 'nanoid/non-secure'\nimport { ReactNode, useMemo, useRef } from 'react'\nimport React, { createContext, memo, useCallback, useContext, useEffect, useReducer } from 'react'\n\ninterface PortalType {\n name: string\n node: ReactNode\n}\n\nenum ACTIONS {\n REGISTER_HOST,\n DEREGISTER_HOST,\n ADD_UPDATE_PORTAL,\n REMOVE_PORTAL,\n}\n\nconst INITIAL_STATE = {}\n\nexport { ACTIONS, INITIAL_STATE }\n\nexport interface AddUpdatePortalAction {\n type: ACTIONS\n hostName: string\n portalName: string\n node: ReactNode\n}\n\nexport interface RemovePortalAction {\n type: ACTIONS\n hostName: string\n portalName: string\n}\n\nexport interface RegisterHostAction {\n type: ACTIONS\n hostName: string\n}\n\nexport interface UnregisterHostAction {\n type: ACTIONS\n hostName: string\n}\n\nexport type ActionTypes =\n | AddUpdatePortalAction\n | RemovePortalAction\n | RegisterHostAction\n | UnregisterHostAction\n\nconst registerHost = (state: Record<string, Array<PortalType>>, hostName: string) => {\n if (!(hostName in state)) {\n state[hostName] = []\n }\n return state\n}\n\nconst deregisterHost = (state: Record<string, Array<PortalType>>, hostName: string) => {\n delete state[hostName]\n return state\n}\n\nconst addUpdatePortal = (\n state: Record<string, Array<PortalType>>,\n hostName: string,\n portalName: string,\n node: any\n) => {\n if (!(hostName in state)) {\n state = registerHost(state, hostName)\n }\n\n /**\n * updated portal, if it was already added.\n */\n const index = state[hostName].findIndex((item) => item.name === portalName)\n if (index !== -1) {\n state[hostName][index].node = node\n } else {\n state[hostName].push({\n name: portalName,\n node,\n })\n }\n return state\n}\n\nconst removePortal = (\n state: Record<string, Array<PortalType>>,\n hostName: string,\n portalName: string\n) => {\n if (!(hostName in state)) {\n console.log(`Failed to remove portal '${portalName}', '${hostName}' was not registered!`)\n return state\n }\n\n const index = state[hostName].findIndex((item) => item.name === portalName)\n if (index !== -1) state[hostName].splice(index, 1)\n return state\n}\n\nconst reducer = (state: Record<string, Array<PortalType>>, action: ActionTypes) => {\n const { type } = action\n switch (type) {\n case ACTIONS.REGISTER_HOST:\n return registerHost({ ...state }, action.hostName)\n case ACTIONS.DEREGISTER_HOST:\n return deregisterHost({ ...state }, action.hostName)\n case ACTIONS.ADD_UPDATE_PORTAL:\n return addUpdatePortal(\n { ...state },\n action.hostName,\n (action as AddUpdatePortalAction).portalName,\n (action as AddUpdatePortalAction).node\n )\n case ACTIONS.REMOVE_PORTAL:\n return removePortal({ ...state }, action.hostName, (action as RemovePortalAction).portalName)\n default:\n return state\n }\n}\n\nconst PortalStateContext = createContext<Record<string, Array<PortalType>> | null>(null)\nconst PortalDispatchContext = createContext<React.Dispatch<ActionTypes> | null>(null)\n\nconst usePortalState = (hostName: string) => {\n const state = useContext(PortalStateContext)\n\n if (state === null) {\n throw new Error(\n \"'PortalStateContext' cannot be null, please add 'PortalProvider' to the root component.\"\n )\n }\n\n return state[hostName] || []\n}\n\nexport const usePortal = (hostName = 'root') => {\n const dispatch = useContext(PortalDispatchContext)\n\n if (dispatch === null) {\n throw new Error(\n \"'PortalDispatchContext' cannot be null, please add 'PortalProvider' to the root component.\"\n )\n }\n\n //#region methods\n const registerHost = useCallback(() => {\n dispatch({\n type: ACTIONS.REGISTER_HOST,\n hostName: hostName,\n })\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [])\n\n const deregisterHost = useCallback(() => {\n dispatch({\n type: ACTIONS.DEREGISTER_HOST,\n hostName: hostName,\n })\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [])\n\n const addUpdatePortal = useCallback((name: string, node: ReactNode) => {\n dispatch({\n type: ACTIONS.ADD_UPDATE_PORTAL,\n hostName,\n portalName: name,\n node,\n })\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [])\n\n const removePortal = useCallback((name: string) => {\n dispatch({\n type: ACTIONS.REMOVE_PORTAL,\n hostName,\n portalName: name,\n })\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [])\n //#endregion\n\n return {\n registerHost,\n deregisterHost,\n addPortal: addUpdatePortal,\n updatePortal: addUpdatePortal,\n removePortal,\n }\n}\n\nexport interface PortalProviderProps {\n /**\n * Defines whether to add a default root host or not.\n *\n * @default true\n * @type boolean\n */\n shouldAddRootHost?: boolean\n\n /**\n * Defines the root portal host name.\n *\n * @default \"root\"\n * @type string\n */\n rootHostName?: string\n\n children: ReactNode | ReactNode[]\n}\n\nconst PortalProviderComponent = ({\n rootHostName = 'root',\n shouldAddRootHost = true,\n children,\n}: PortalProviderProps) => {\n const [state, dispatch] = useReducer(reducer, INITIAL_STATE)\n const transitionDispatch = useMemo(() => {\n const next = (value: any) => {\n startTransition(() => {\n dispatch(value)\n })\n }\n return next as typeof dispatch\n }, [dispatch])\n return (\n <PortalDispatchContext.Provider value={transitionDispatch}>\n <PortalStateContext.Provider value={state}>\n {children}\n {shouldAddRootHost && <PortalHost name={rootHostName} />}\n </PortalStateContext.Provider>\n </PortalDispatchContext.Provider>\n )\n}\n\nexport const PortalProvider = memo(PortalProviderComponent)\nPortalProvider.displayName = 'PortalProvider'\n\nexport interface PortalHostProps {\n /**\n * Host's key or name to be used as an identifier.\n * @type string\n */\n name: string\n}\n\nconst PortalHostComponent = ({ name }: PortalHostProps) => {\n //#region hooks\n const isServer = useIsSSR()\n const state = usePortalState(name)\n const { registerHost, deregisterHost } = usePortal(name)\n //#endregion\n\n //#region effects\n useEffect(() => {\n if (isServer) return\n registerHost()\n return () => {\n deregisterHost()\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [isServer])\n //#endregion\n\n //#region render\n return <>{state.map((item) => item.node)}</>\n //#endregion\n}\n\nexport const PortalHost = memo(PortalHostComponent)\nPortalHost.displayName = 'PortalHost'\n\nexport interface PortalItemProps {\n /**\n * Portal's key or name to be used as an identifier.\n * @type string\n * @default nanoid generated unique key.\n */\n name?: string\n /**\n * Host's name to teleport the portal content to.\n * @type string\n * @default 'root'\n */\n hostName?: string\n /**\n * Override internal mounting functionality, this is useful\n * if you want to trigger any action before mounting the portal content.\n * @type (mount?: () => void) => void\n * @default undefined\n */\n handleOnMount?: (mount: () => void) => void\n /**\n * Override internal un-mounting functionality, this is useful\n * if you want to trigger any action before un-mounting the portal content.\n * @type (unmount?: () => void) => void\n * @default undefined\n */\n handleOnUnmount?: (unmount: () => void) => void\n /**\n * Override internal updating functionality, this is useful\n * if you want to trigger any action before updating the portal content.\n * @type (update?: () => void) => void\n * @default undefined\n */\n handleOnUpdate?: (update: () => void) => void\n /**\n * Portal's content.\n * @type ReactNode\n * @default undefined\n */\n children?: ReactNode | ReactNode[]\n}\n\nconst PortalComponent = ({\n name: _providedName,\n hostName,\n handleOnMount: _providedHandleOnMount,\n handleOnUnmount: _providedHandleOnUnmount,\n handleOnUpdate: _providedHandleOnUpdate,\n children,\n}: PortalItemProps) => {\n //#region hooks\n const { addPortal: addUpdatePortal, removePortal } = usePortal(hostName)\n //#endregion\n\n //#region variables\n const name = useMemo(() => _providedName || nanoid(), [_providedName])\n //#endregion\n\n //#region refs\n const handleOnMountRef = useRef<Function>()\n const handleOnUnmountRef = useRef<Function>()\n const handleOnUpdateRef = useRef<Function>()\n //#endregion\n\n //#region callbacks\n const handleOnMount = useCallback(() => {\n if (_providedHandleOnMount) {\n _providedHandleOnMount(() => addUpdatePortal(name, children))\n } else {\n addUpdatePortal(name, children)\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [_providedHandleOnMount, addUpdatePortal])\n handleOnMountRef.current = handleOnMount\n\n const handleOnUnmount = useCallback(() => {\n if (_providedHandleOnUnmount) {\n _providedHandleOnUnmount(() => removePortal(name))\n } else {\n removePortal(name)\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [_providedHandleOnUnmount, removePortal])\n handleOnUnmountRef.current = handleOnUnmount\n\n const handleOnUpdate = useCallback(() => {\n if (_providedHandleOnUpdate) {\n _providedHandleOnUpdate(() => addUpdatePortal(name, children))\n } else {\n addUpdatePortal(name, children)\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [_providedHandleOnUpdate, addUpdatePortal, children])\n handleOnUpdateRef.current = handleOnUpdate\n //#endregion\n\n //#region effects\n useEffect(() => {\n handleOnMountRef.current?.()\n return () => {\n handleOnUnmountRef.current?.()\n\n // remove callbacks refs\n handleOnMountRef.current = undefined\n handleOnUnmountRef.current = undefined\n handleOnUpdateRef.current = undefined\n }\n }, [])\n useEffect(() => {\n handleOnUpdateRef.current?.()\n }, [children])\n //#endregion\n\n return null\n}\n\nexport const PortalItem = memo(PortalComponent)\nPortalItem.displayName = 'Portal'\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAIA,kBAA0C;AAC1C,wBAAuB;AACvB,mBAA2C;AAC3C,oBAA2F;AAO3F,IAAK,UAAL,kBAAK,aAAL;AACE;AACA;AACA;AACA;AAJG;AAAA;AAOL,MAAM,gBAAgB,CAAC;AAiCvB,MAAM,eAAe,CAAC,OAA0C,aAAqB;AACnF,MAAI,CAAE,aAAY,QAAQ;AACxB,UAAM,YAAY,CAAC;AAAA,EACrB;AACA,SAAO;AACT;AAEA,MAAM,iBAAiB,CAAC,OAA0C,aAAqB;AACrF,SAAO,MAAM;AACb,SAAO;AACT;AAEA,MAAM,kBAAkB,CACtB,OACA,UACA,YACA,SACG;AACH,MAAI,CAAE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,UAAU;AAE7B,QAAM,iBAAiB,+BAAY,MAAM;AACvC,QAAI,yBAAyB;AAC3B,8BAAwB,MAAM,iBAAgB,MAAM,QAAQ,CAAC;AAAA,IAC/D,OAAO;AACL,uBAAgB,MAAM,QAAQ;AAAA,IAChC;AAAA,EAEF,GAAG,CAAC,yBAAyB,kBAAiB,QAAQ,CAAC;AACvD,oBAAkB,UAAU;AAI5B,+BAAU,MAAM;AAvXlB;AAwXI,2BAAiB,YAAjB;AACA,WAAO,MAAM;AAzXjB;AA0XM,gCAAmB,YAAnB;AAGA,uBAAiB,UAAU;AAC3B,yBAAmB,UAAU;AAC7B,wBAAkB,UAAU;AAAA,IAC9B;AAAA,EACF,GAAG,CAAC,CAAC;AACL,+BAAU,MAAM;AAlYlB;AAmYI,4BAAkB,YAAlB;AAAA,EACF,GAAG,CAAC,QAAQ,CAAC;AAGb,SAAO;AACT;AAEO,MAAM,aAAa,wBAAK,eAAe;AAC9C,WAAW,cAAc;",
6
+ "names": []
7
+ }
@@ -18,7 +18,6 @@ var __spreadValues = (a, b) => {
18
18
  }
19
19
  return a;
20
20
  };
21
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
22
21
  var __export = (target, all) => {
23
22
  for (var name in all)
24
23
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -44,14 +43,17 @@ var import_stacks = require("@tamagui/stacks");
44
43
  var React = __toESM(require("react"));
45
44
  var import_react_dom = require("react-dom");
46
45
  var import_react_native = require("react-native");
47
- const Portal = /* @__PURE__ */ __name((props) => {
46
+ const Portal = (props) => {
48
47
  const contents = /* @__PURE__ */ React.createElement(import_stacks.YStack, __spreadValues({
49
48
  pointerEvents: "none",
50
- fullscreen: true
49
+ fullscreen: true,
50
+ position: import_core.isWeb ? "fixed" : "absolute"
51
51
  }, props));
52
52
  if (!import_core.isWeb) {
53
53
  return /* @__PURE__ */ React.createElement(import_react_native.Modal, {
54
- visible: true
54
+ visible: true,
55
+ transparent: true,
56
+ pointerEvents: "none"
55
57
  }, contents);
56
58
  }
57
59
  const [hostElement, setHostElement] = React.useState(null);
@@ -63,7 +65,7 @@ const Portal = /* @__PURE__ */ __name((props) => {
63
65
  return (0, import_react_dom.createPortal)(contents, hostElement);
64
66
  }
65
67
  return null;
66
- }, "Portal");
68
+ };
67
69
  // Annotate the CommonJS export names for ESM import in node:
68
70
  0 && (module.exports = {
69
71
  Portal
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/Portal.tsx"],
4
- "sourcesContent": ["import '@tamagui/polyfill-dev'\n\nimport { isWeb, useIsomorphicLayoutEffect } from '@tamagui/core'\nimport { YStack, YStackProps } from '@tamagui/stacks'\nimport * as React from 'react'\nimport { createPortal } from 'react-dom'\nimport { Modal } from 'react-native'\n\n// todo extract to package\nexport const Portal = (props: YStackProps) => {\n const contents = <YStack pointerEvents=\"none\" fullscreen {...props} />\n if (!isWeb) {\n return <Modal visible>{contents}</Modal>\n }\n const [hostElement, setHostElement] = React.useState<any>(null)\n\n useIsomorphicLayoutEffect(() => {\n setHostElement(globalThis.document?.body)\n }, [])\n\n if (hostElement) {\n return createPortal(contents, hostElement)\n }\n // ssr return null\n return null\n}\n"],
5
- "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,0BAAO;AAEP,kBAAiD;AACjD,oBAAoC;AACpC,YAAuB;AACvB,uBAA6B;AAC7B,0BAAsB;AAGf,MAAM,SAAS,wBAAC,UAAuB;AAC5C,QAAM,WAAW,oCAAC;AAAA,IAAO,eAAc;AAAA,IAAO,YAAU;AAAA,KAAK,MAAO;AACpE,MAAI,CAAC,mBAAO;AACV,WAAO,oCAAC;AAAA,MAAM,SAAO;AAAA,OAAE,QAAS;AAAA,EAClC;AACA,QAAM,CAAC,aAAa,kBAAkB,MAAM,SAAc,IAAI;AAE9D,6CAA0B,MAAM;AAhBlC;AAiBI,mBAAe,iBAAW,aAAX,mBAAqB,IAAI;AAAA,EAC1C,GAAG,CAAC,CAAC;AAEL,MAAI,aAAa;AACf,WAAO,mCAAa,UAAU,WAAW;AAAA,EAC3C;AAEA,SAAO;AACT,GAhBsB;",
4
+ "sourcesContent": ["/* eslint-disable react-hooks/rules-of-hooks */\nimport '@tamagui/polyfill-dev'\n\nimport { isWeb, useIsomorphicLayoutEffect } from '@tamagui/core'\nimport { YStack, YStackProps } from '@tamagui/stacks'\nimport * as React from 'react'\nimport { createPortal } from 'react-dom'\nimport { Modal } from 'react-native'\n\nexport type PortalProps = YStackProps\n\nexport const Portal = (props: PortalProps) => {\n const contents = (\n <YStack\n pointerEvents=\"none\"\n fullscreen\n // @ts-expect-error ok on web\n position={isWeb ? 'fixed' : 'absolute'}\n {...props}\n />\n )\n\n if (!isWeb) {\n // check if theme stays in context here\n return (\n <Modal visible transparent pointerEvents=\"none\">\n {contents}\n </Modal>\n )\n }\n\n const [hostElement, setHostElement] = React.useState<any>(null)\n\n useIsomorphicLayoutEffect(() => {\n setHostElement(globalThis.document?.body)\n }, [])\n\n if (hostElement) {\n return createPortal(contents, hostElement)\n }\n\n // ssr return null\n return null\n}\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AACA,0BAAO;AAEP,kBAAiD;AACjD,oBAAoC;AACpC,YAAuB;AACvB,uBAA6B;AAC7B,0BAAsB;AAIf,MAAM,SAAS,CAAC,UAAuB;AAC5C,QAAM,WACJ,oCAAC;AAAA,IACC,eAAc;AAAA,IACd,YAAU;AAAA,IAEV,UAAU,oBAAQ,UAAU;AAAA,KACxB,MACN;AAGF,MAAI,CAAC,mBAAO;AAEV,WACE,oCAAC;AAAA,MAAM,SAAO;AAAA,MAAC,aAAW;AAAA,MAAC,eAAc;AAAA,OACtC,QACH;AAAA,EAEJ;AAEA,QAAM,CAAC,aAAa,kBAAkB,MAAM,SAAc,IAAI;AAE9D,6CAA0B,MAAM;AAjClC;AAkCI,mBAAe,iBAAW,aAAX,mBAAqB,IAAI;AAAA,EAC1C,GAAG,CAAC,CAAC;AAEL,MAAI,aAAa;AACf,WAAO,mCAAa,UAAU,WAAW;AAAA,EAC3C;AAGA,SAAO;AACT;",
6
6
  "names": []
7
7
  }
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=PortalProvider.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [],
5
+ "mappings": "",
6
+ "names": []
7
+ }
package/dist/cjs/index.js CHANGED
@@ -15,4 +15,5 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
15
15
  var src_exports = {};
16
16
  module.exports = __toCommonJS(src_exports);
17
17
  __reExport(src_exports, require("./Portal"), module.exports);
18
+ __reExport(src_exports, require("./GorhomPortal"), module.exports);
18
19
  //# sourceMappingURL=index.js.map
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/index.tsx"],
4
- "sourcesContent": ["export * from './Portal'\n"],
5
- "mappings": ";;;;;;;;;;;;;;AAAA;AAAA;AAAA,wBAAc,qBAAd;",
4
+ "sourcesContent": ["export * from './Portal'\nexport * from './GorhomPortal'\n"],
5
+ "mappings": ";;;;;;;;;;;;;;AAAA;AAAA;AAAA,wBAAc,qBAAd;AACA,wBAAc,2BADd;",
6
6
  "names": []
7
7
  }
@@ -0,0 +1,232 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
3
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
4
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
5
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
+ var __spreadValues = (a, b) => {
7
+ for (var prop in b || (b = {}))
8
+ if (__hasOwnProp.call(b, prop))
9
+ __defNormalProp(a, prop, b[prop]);
10
+ if (__getOwnPropSymbols)
11
+ for (var prop of __getOwnPropSymbols(b)) {
12
+ if (__propIsEnum.call(b, prop))
13
+ __defNormalProp(a, prop, b[prop]);
14
+ }
15
+ return a;
16
+ };
17
+ import { startTransition, useIsSSR } from "@tamagui/core";
18
+ import { nanoid } from "nanoid/non-secure";
19
+ import { useMemo, useRef } from "react";
20
+ import React, { createContext, memo, useCallback, useContext, useEffect, useReducer } from "react";
21
+ var ACTIONS = /* @__PURE__ */ ((ACTIONS2) => {
22
+ ACTIONS2[ACTIONS2["REGISTER_HOST"] = 0] = "REGISTER_HOST";
23
+ ACTIONS2[ACTIONS2["DEREGISTER_HOST"] = 1] = "DEREGISTER_HOST";
24
+ ACTIONS2[ACTIONS2["ADD_UPDATE_PORTAL"] = 2] = "ADD_UPDATE_PORTAL";
25
+ ACTIONS2[ACTIONS2["REMOVE_PORTAL"] = 3] = "REMOVE_PORTAL";
26
+ return ACTIONS2;
27
+ })(ACTIONS || {});
28
+ const INITIAL_STATE = {};
29
+ const registerHost = (state, hostName) => {
30
+ if (!(hostName in state)) {
31
+ state[hostName] = [];
32
+ }
33
+ return state;
34
+ };
35
+ const deregisterHost = (state, hostName) => {
36
+ delete state[hostName];
37
+ return state;
38
+ };
39
+ const addUpdatePortal = (state, hostName, portalName, node) => {
40
+ if (!(hostName in state)) {
41
+ state = registerHost(state, hostName);
42
+ }
43
+ const index = state[hostName].findIndex((item) => item.name === portalName);
44
+ if (index !== -1) {
45
+ state[hostName][index].node = node;
46
+ } else {
47
+ state[hostName].push({
48
+ name: portalName,
49
+ node
50
+ });
51
+ }
52
+ return state;
53
+ };
54
+ const removePortal = (state, hostName, portalName) => {
55
+ if (!(hostName in state)) {
56
+ console.log(`Failed to remove portal '${portalName}', '${hostName}' was not registered!`);
57
+ return state;
58
+ }
59
+ const index = state[hostName].findIndex((item) => item.name === portalName);
60
+ if (index !== -1)
61
+ state[hostName].splice(index, 1);
62
+ return state;
63
+ };
64
+ const reducer = (state, action) => {
65
+ const { type } = action;
66
+ switch (type) {
67
+ case 0 /* REGISTER_HOST */:
68
+ return registerHost(__spreadValues({}, state), action.hostName);
69
+ case 1 /* DEREGISTER_HOST */:
70
+ return deregisterHost(__spreadValues({}, state), action.hostName);
71
+ case 2 /* ADD_UPDATE_PORTAL */:
72
+ return addUpdatePortal(__spreadValues({}, state), action.hostName, action.portalName, action.node);
73
+ case 3 /* REMOVE_PORTAL */:
74
+ return removePortal(__spreadValues({}, state), action.hostName, action.portalName);
75
+ default:
76
+ return state;
77
+ }
78
+ };
79
+ const PortalStateContext = createContext(null);
80
+ const PortalDispatchContext = createContext(null);
81
+ const usePortalState = (hostName) => {
82
+ const state = useContext(PortalStateContext);
83
+ if (state === null) {
84
+ throw new Error("'PortalStateContext' cannot be null, please add 'PortalProvider' to the root component.");
85
+ }
86
+ return state[hostName] || [];
87
+ };
88
+ const usePortal = (hostName = "root") => {
89
+ const dispatch = useContext(PortalDispatchContext);
90
+ if (dispatch === null) {
91
+ throw new Error("'PortalDispatchContext' cannot be null, please add 'PortalProvider' to the root component.");
92
+ }
93
+ const registerHost2 = useCallback(() => {
94
+ dispatch({
95
+ type: 0 /* REGISTER_HOST */,
96
+ hostName
97
+ });
98
+ }, []);
99
+ const deregisterHost2 = useCallback(() => {
100
+ dispatch({
101
+ type: 1 /* DEREGISTER_HOST */,
102
+ hostName
103
+ });
104
+ }, []);
105
+ const addUpdatePortal2 = useCallback((name, node) => {
106
+ dispatch({
107
+ type: 2 /* ADD_UPDATE_PORTAL */,
108
+ hostName,
109
+ portalName: name,
110
+ node
111
+ });
112
+ }, []);
113
+ const removePortal2 = useCallback((name) => {
114
+ dispatch({
115
+ type: 3 /* REMOVE_PORTAL */,
116
+ hostName,
117
+ portalName: name
118
+ });
119
+ }, []);
120
+ return {
121
+ registerHost: registerHost2,
122
+ deregisterHost: deregisterHost2,
123
+ addPortal: addUpdatePortal2,
124
+ updatePortal: addUpdatePortal2,
125
+ removePortal: removePortal2
126
+ };
127
+ };
128
+ const PortalProviderComponent = ({
129
+ rootHostName = "root",
130
+ shouldAddRootHost = true,
131
+ children
132
+ }) => {
133
+ const [state, dispatch] = useReducer(reducer, INITIAL_STATE);
134
+ const transitionDispatch = useMemo(() => {
135
+ const next = (value) => {
136
+ startTransition(() => {
137
+ dispatch(value);
138
+ });
139
+ };
140
+ return next;
141
+ }, [dispatch]);
142
+ return /* @__PURE__ */ React.createElement(PortalDispatchContext.Provider, {
143
+ value: transitionDispatch
144
+ }, /* @__PURE__ */ React.createElement(PortalStateContext.Provider, {
145
+ value: state
146
+ }, children, shouldAddRootHost && /* @__PURE__ */ React.createElement(PortalHost, {
147
+ name: rootHostName
148
+ })));
149
+ };
150
+ const PortalProvider = memo(PortalProviderComponent);
151
+ PortalProvider.displayName = "PortalProvider";
152
+ const PortalHostComponent = ({ name }) => {
153
+ const isServer = useIsSSR();
154
+ const state = usePortalState(name);
155
+ const { registerHost: registerHost2, deregisterHost: deregisterHost2 } = usePortal(name);
156
+ useEffect(() => {
157
+ if (isServer)
158
+ return;
159
+ registerHost2();
160
+ return () => {
161
+ deregisterHost2();
162
+ };
163
+ }, [isServer]);
164
+ return /* @__PURE__ */ React.createElement(React.Fragment, null, state.map((item) => item.node));
165
+ };
166
+ const PortalHost = memo(PortalHostComponent);
167
+ PortalHost.displayName = "PortalHost";
168
+ const PortalComponent = ({
169
+ name: _providedName,
170
+ hostName,
171
+ handleOnMount: _providedHandleOnMount,
172
+ handleOnUnmount: _providedHandleOnUnmount,
173
+ handleOnUpdate: _providedHandleOnUpdate,
174
+ children
175
+ }) => {
176
+ const { addPortal: addUpdatePortal2, removePortal: removePortal2 } = usePortal(hostName);
177
+ const name = useMemo(() => _providedName || nanoid(), [_providedName]);
178
+ const handleOnMountRef = useRef();
179
+ const handleOnUnmountRef = useRef();
180
+ const handleOnUpdateRef = useRef();
181
+ const handleOnMount = useCallback(() => {
182
+ if (_providedHandleOnMount) {
183
+ _providedHandleOnMount(() => addUpdatePortal2(name, children));
184
+ } else {
185
+ addUpdatePortal2(name, children);
186
+ }
187
+ }, [_providedHandleOnMount, addUpdatePortal2]);
188
+ handleOnMountRef.current = handleOnMount;
189
+ const handleOnUnmount = useCallback(() => {
190
+ if (_providedHandleOnUnmount) {
191
+ _providedHandleOnUnmount(() => removePortal2(name));
192
+ } else {
193
+ removePortal2(name);
194
+ }
195
+ }, [_providedHandleOnUnmount, removePortal2]);
196
+ handleOnUnmountRef.current = handleOnUnmount;
197
+ const handleOnUpdate = useCallback(() => {
198
+ if (_providedHandleOnUpdate) {
199
+ _providedHandleOnUpdate(() => addUpdatePortal2(name, children));
200
+ } else {
201
+ addUpdatePortal2(name, children);
202
+ }
203
+ }, [_providedHandleOnUpdate, addUpdatePortal2, children]);
204
+ handleOnUpdateRef.current = handleOnUpdate;
205
+ useEffect(() => {
206
+ var _a;
207
+ (_a = handleOnMountRef.current) == null ? void 0 : _a.call(handleOnMountRef);
208
+ return () => {
209
+ var _a2;
210
+ (_a2 = handleOnUnmountRef.current) == null ? void 0 : _a2.call(handleOnUnmountRef);
211
+ handleOnMountRef.current = void 0;
212
+ handleOnUnmountRef.current = void 0;
213
+ handleOnUpdateRef.current = void 0;
214
+ };
215
+ }, []);
216
+ useEffect(() => {
217
+ var _a;
218
+ (_a = handleOnUpdateRef.current) == null ? void 0 : _a.call(handleOnUpdateRef);
219
+ }, [children]);
220
+ return null;
221
+ };
222
+ const PortalItem = memo(PortalComponent);
223
+ PortalItem.displayName = "Portal";
224
+ export {
225
+ ACTIONS,
226
+ INITIAL_STATE,
227
+ PortalHost,
228
+ PortalItem,
229
+ PortalProvider,
230
+ usePortal
231
+ };
232
+ //# sourceMappingURL=GorhomPortal.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/GorhomPortal.tsx"],
4
+ "sourcesContent": ["// from https://github.com/gorhom/react-native-portal\n// MIT License Copyright (c) 2020 Mo Gorhom\n// fixing SSR issue\n\nimport { startTransition, useIsSSR } from '@tamagui/core'\nimport { nanoid } from 'nanoid/non-secure'\nimport { ReactNode, useMemo, useRef } from 'react'\nimport React, { createContext, memo, useCallback, useContext, useEffect, useReducer } from 'react'\n\ninterface PortalType {\n name: string\n node: ReactNode\n}\n\nenum ACTIONS {\n REGISTER_HOST,\n DEREGISTER_HOST,\n ADD_UPDATE_PORTAL,\n REMOVE_PORTAL,\n}\n\nconst INITIAL_STATE = {}\n\nexport { ACTIONS, INITIAL_STATE }\n\nexport interface AddUpdatePortalAction {\n type: ACTIONS\n hostName: string\n portalName: string\n node: ReactNode\n}\n\nexport interface RemovePortalAction {\n type: ACTIONS\n hostName: string\n portalName: string\n}\n\nexport interface RegisterHostAction {\n type: ACTIONS\n hostName: string\n}\n\nexport interface UnregisterHostAction {\n type: ACTIONS\n hostName: string\n}\n\nexport type ActionTypes =\n | AddUpdatePortalAction\n | RemovePortalAction\n | RegisterHostAction\n | UnregisterHostAction\n\nconst registerHost = (state: Record<string, Array<PortalType>>, hostName: string) => {\n if (!(hostName in state)) {\n state[hostName] = []\n }\n return state\n}\n\nconst deregisterHost = (state: Record<string, Array<PortalType>>, hostName: string) => {\n delete state[hostName]\n return state\n}\n\nconst addUpdatePortal = (\n state: Record<string, Array<PortalType>>,\n hostName: string,\n portalName: string,\n node: any\n) => {\n if (!(hostName in state)) {\n state = registerHost(state, hostName)\n }\n\n /**\n * updated portal, if it was already added.\n */\n const index = state[hostName].findIndex((item) => item.name === portalName)\n if (index !== -1) {\n state[hostName][index].node = node\n } else {\n state[hostName].push({\n name: portalName,\n node,\n })\n }\n return state\n}\n\nconst removePortal = (\n state: Record<string, Array<PortalType>>,\n hostName: string,\n portalName: string\n) => {\n if (!(hostName in state)) {\n console.log(`Failed to remove portal '${portalName}', '${hostName}' was not registered!`)\n return state\n }\n\n const index = state[hostName].findIndex((item) => item.name === portalName)\n if (index !== -1) state[hostName].splice(index, 1)\n return state\n}\n\nconst reducer = (state: Record<string, Array<PortalType>>, action: ActionTypes) => {\n const { type } = action\n switch (type) {\n case ACTIONS.REGISTER_HOST:\n return registerHost({ ...state }, action.hostName)\n case ACTIONS.DEREGISTER_HOST:\n return deregisterHost({ ...state }, action.hostName)\n case ACTIONS.ADD_UPDATE_PORTAL:\n return addUpdatePortal(\n { ...state },\n action.hostName,\n (action as AddUpdatePortalAction).portalName,\n (action as AddUpdatePortalAction).node\n )\n case ACTIONS.REMOVE_PORTAL:\n return removePortal({ ...state }, action.hostName, (action as RemovePortalAction).portalName)\n default:\n return state\n }\n}\n\nconst PortalStateContext = createContext<Record<string, Array<PortalType>> | null>(null)\nconst PortalDispatchContext = createContext<React.Dispatch<ActionTypes> | null>(null)\n\nconst usePortalState = (hostName: string) => {\n const state = useContext(PortalStateContext)\n\n if (state === null) {\n throw new Error(\n \"'PortalStateContext' cannot be null, please add 'PortalProvider' to the root component.\"\n )\n }\n\n return state[hostName] || []\n}\n\nexport const usePortal = (hostName = 'root') => {\n const dispatch = useContext(PortalDispatchContext)\n\n if (dispatch === null) {\n throw new Error(\n \"'PortalDispatchContext' cannot be null, please add 'PortalProvider' to the root component.\"\n )\n }\n\n //#region methods\n const registerHost = useCallback(() => {\n dispatch({\n type: ACTIONS.REGISTER_HOST,\n hostName: hostName,\n })\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [])\n\n const deregisterHost = useCallback(() => {\n dispatch({\n type: ACTIONS.DEREGISTER_HOST,\n hostName: hostName,\n })\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [])\n\n const addUpdatePortal = useCallback((name: string, node: ReactNode) => {\n dispatch({\n type: ACTIONS.ADD_UPDATE_PORTAL,\n hostName,\n portalName: name,\n node,\n })\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [])\n\n const removePortal = useCallback((name: string) => {\n dispatch({\n type: ACTIONS.REMOVE_PORTAL,\n hostName,\n portalName: name,\n })\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [])\n //#endregion\n\n return {\n registerHost,\n deregisterHost,\n addPortal: addUpdatePortal,\n updatePortal: addUpdatePortal,\n removePortal,\n }\n}\n\nexport interface PortalProviderProps {\n /**\n * Defines whether to add a default root host or not.\n *\n * @default true\n * @type boolean\n */\n shouldAddRootHost?: boolean\n\n /**\n * Defines the root portal host name.\n *\n * @default \"root\"\n * @type string\n */\n rootHostName?: string\n\n children: ReactNode | ReactNode[]\n}\n\nconst PortalProviderComponent = ({\n rootHostName = 'root',\n shouldAddRootHost = true,\n children,\n}: PortalProviderProps) => {\n const [state, dispatch] = useReducer(reducer, INITIAL_STATE)\n const transitionDispatch = useMemo(() => {\n const next = (value: any) => {\n startTransition(() => {\n dispatch(value)\n })\n }\n return next as typeof dispatch\n }, [dispatch])\n return (\n <PortalDispatchContext.Provider value={transitionDispatch}>\n <PortalStateContext.Provider value={state}>\n {children}\n {shouldAddRootHost && <PortalHost name={rootHostName} />}\n </PortalStateContext.Provider>\n </PortalDispatchContext.Provider>\n )\n}\n\nexport const PortalProvider = memo(PortalProviderComponent)\nPortalProvider.displayName = 'PortalProvider'\n\nexport interface PortalHostProps {\n /**\n * Host's key or name to be used as an identifier.\n * @type string\n */\n name: string\n}\n\nconst PortalHostComponent = ({ name }: PortalHostProps) => {\n //#region hooks\n const isServer = useIsSSR()\n const state = usePortalState(name)\n const { registerHost, deregisterHost } = usePortal(name)\n //#endregion\n\n //#region effects\n useEffect(() => {\n if (isServer) return\n registerHost()\n return () => {\n deregisterHost()\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [isServer])\n //#endregion\n\n //#region render\n return <>{state.map((item) => item.node)}</>\n //#endregion\n}\n\nexport const PortalHost = memo(PortalHostComponent)\nPortalHost.displayName = 'PortalHost'\n\nexport interface PortalItemProps {\n /**\n * Portal's key or name to be used as an identifier.\n * @type string\n * @default nanoid generated unique key.\n */\n name?: string\n /**\n * Host's name to teleport the portal content to.\n * @type string\n * @default 'root'\n */\n hostName?: string\n /**\n * Override internal mounting functionality, this is useful\n * if you want to trigger any action before mounting the portal content.\n * @type (mount?: () => void) => void\n * @default undefined\n */\n handleOnMount?: (mount: () => void) => void\n /**\n * Override internal un-mounting functionality, this is useful\n * if you want to trigger any action before un-mounting the portal content.\n * @type (unmount?: () => void) => void\n * @default undefined\n */\n handleOnUnmount?: (unmount: () => void) => void\n /**\n * Override internal updating functionality, this is useful\n * if you want to trigger any action before updating the portal content.\n * @type (update?: () => void) => void\n * @default undefined\n */\n handleOnUpdate?: (update: () => void) => void\n /**\n * Portal's content.\n * @type ReactNode\n * @default undefined\n */\n children?: ReactNode | ReactNode[]\n}\n\nconst PortalComponent = ({\n name: _providedName,\n hostName,\n handleOnMount: _providedHandleOnMount,\n handleOnUnmount: _providedHandleOnUnmount,\n handleOnUpdate: _providedHandleOnUpdate,\n children,\n}: PortalItemProps) => {\n //#region hooks\n const { addPortal: addUpdatePortal, removePortal } = usePortal(hostName)\n //#endregion\n\n //#region variables\n const name = useMemo(() => _providedName || nanoid(), [_providedName])\n //#endregion\n\n //#region refs\n const handleOnMountRef = useRef<Function>()\n const handleOnUnmountRef = useRef<Function>()\n const handleOnUpdateRef = useRef<Function>()\n //#endregion\n\n //#region callbacks\n const handleOnMount = useCallback(() => {\n if (_providedHandleOnMount) {\n _providedHandleOnMount(() => addUpdatePortal(name, children))\n } else {\n addUpdatePortal(name, children)\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [_providedHandleOnMount, addUpdatePortal])\n handleOnMountRef.current = handleOnMount\n\n const handleOnUnmount = useCallback(() => {\n if (_providedHandleOnUnmount) {\n _providedHandleOnUnmount(() => removePortal(name))\n } else {\n removePortal(name)\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [_providedHandleOnUnmount, removePortal])\n handleOnUnmountRef.current = handleOnUnmount\n\n const handleOnUpdate = useCallback(() => {\n if (_providedHandleOnUpdate) {\n _providedHandleOnUpdate(() => addUpdatePortal(name, children))\n } else {\n addUpdatePortal(name, children)\n }\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [_providedHandleOnUpdate, addUpdatePortal, children])\n handleOnUpdateRef.current = handleOnUpdate\n //#endregion\n\n //#region effects\n useEffect(() => {\n handleOnMountRef.current?.()\n return () => {\n handleOnUnmountRef.current?.()\n\n // remove callbacks refs\n handleOnMountRef.current = undefined\n handleOnUnmountRef.current = undefined\n handleOnUpdateRef.current = undefined\n }\n }, [])\n useEffect(() => {\n handleOnUpdateRef.current?.()\n }, [children])\n //#endregion\n\n return null\n}\n\nexport const PortalItem = memo(PortalComponent)\nPortalItem.displayName = 'Portal'\n"],
5
+ "mappings": ";;;;;;;;;;;;;;;;AAIA;AACA;AACA;AACA;AAOA,IAAK,UAAL,kBAAK,aAAL;AACE;AACA;AACA;AACA;AAJG;AAAA;AAOL,MAAM,gBAAgB,CAAC;AAiCvB,MAAM,eAAe,CAAC,OAA0C,aAAqB;AACnF,MAAI,CAAE,aAAY,QAAQ;AACxB,UAAM,YAAY,CAAC;AAAA,EACrB;AACA,SAAO;AACT;AAEA,MAAM,iBAAiB,CAAC,OAA0C,aAAqB;AACrF,SAAO,MAAM;AACb,SAAO;AACT;AAEA,MAAM,kBAAkB,CACtB,OACA,UACA,YACA,SACG;AACH,MAAI,CAAE,aAAY,QAAQ;AACxB,YAAQ,aAAa,OAAO,QAAQ;AAAA,EACtC;AAKA,QAAM,QAAQ,MAAM,UAAU,UAAU,CAAC,SAAS,KAAK,SAAS,UAAU;AAC1E,MAAI,UAAU,IAAI;AAChB,UAAM,UAAU,OAAO,OAAO;AAAA,EAChC,OAAO;AACL,UAAM,UAAU,KAAK;AAAA,MACnB,MAAM;AAAA,MACN;AAAA,IACF,CAAC;AAAA,EACH;AACA,SAAO;AACT;AAEA,MAAM,eAAe,CACnB,OACA,UACA,eACG;AACH,MAAI,CAAE,aAAY,QAAQ;AACxB,YAAQ,IAAI,4BAA4B,iBAAiB,+BAA+B;AACxF,WAAO;AAAA,EACT;AAEA,QAAM,QAAQ,MAAM,UAAU,UAAU,CAAC,SAAS,KAAK,SAAS,UAAU;AAC1E,MAAI,UAAU;AAAI,UA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