@tamagui/portal 1.0.1-beta.21 → 1.0.1-beta.210

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.
Files changed (43) hide show
  1. package/dist/cjs/GorhomPortal.js +266 -0
  2. package/dist/cjs/GorhomPortal.js.map +7 -0
  3. package/dist/cjs/Portal.js +19 -31
  4. package/dist/cjs/Portal.js.map +2 -2
  5. package/dist/cjs/Portal.native.js +66 -0
  6. package/dist/cjs/Portal.native.js.map +7 -0
  7. package/dist/cjs/PortalProps.js +17 -0
  8. package/dist/cjs/PortalProps.js.map +7 -0
  9. package/dist/cjs/index.js +2 -0
  10. package/dist/cjs/index.js.map +2 -2
  11. package/dist/esm/GorhomPortal.js +237 -0
  12. package/dist/esm/GorhomPortal.js.map +7 -0
  13. package/dist/esm/Portal.js +11 -15
  14. package/dist/esm/Portal.js.map +2 -2
  15. package/dist/esm/Portal.native.js +36 -0
  16. package/dist/esm/Portal.native.js.map +7 -0
  17. package/dist/esm/PortalProps.js +1 -0
  18. package/dist/esm/PortalProps.js.map +7 -0
  19. package/dist/esm/index.js +1 -0
  20. package/dist/esm/index.js.map +2 -2
  21. package/dist/jsx/GorhomPortal.js +223 -0
  22. package/dist/jsx/GorhomPortal.js.map +7 -0
  23. package/dist/jsx/Portal.js +6 -12
  24. package/dist/jsx/Portal.js.map +7 -0
  25. package/dist/jsx/Portal.native.js +21 -0
  26. package/dist/jsx/Portal.native.js.map +7 -0
  27. package/dist/jsx/PortalProps.js +1 -0
  28. package/dist/jsx/PortalProps.js.map +7 -0
  29. package/dist/jsx/index.js +2 -0
  30. package/dist/jsx/index.js.map +7 -0
  31. package/package.json +9 -7
  32. package/src/GorhomPortal.tsx +402 -0
  33. package/src/Portal.native.tsx +40 -0
  34. package/src/Portal.tsx +37 -0
  35. package/src/PortalProps.tsx +6 -0
  36. package/src/index.tsx +2 -0
  37. package/types/GorhomPortal.d.ts +106 -0
  38. package/types/Portal.d.ts +3 -3
  39. package/types/Portal.native.d.ts +3 -0
  40. package/types/PortalProps.d.ts +6 -0
  41. package/types/index.d.ts +1 -0
  42. package/types/Portal.d.ts.map +0 -1
  43. package/types/index.d.ts.map +0 -1
@@ -0,0 +1,237 @@
1
+ import { Fragment, jsx, jsxs } from "react/jsx-runtime";
2
+ import { useDidFinishSSR } from "@tamagui/core";
3
+ import { startTransition, useMemo, useRef } from "react";
4
+ import { createContext, memo, useCallback, useContext, useEffect, useReducer } from "react";
5
+ var ACTIONS = /* @__PURE__ */ ((ACTIONS2) => {
6
+ ACTIONS2[ACTIONS2["REGISTER_HOST"] = 0] = "REGISTER_HOST";
7
+ ACTIONS2[ACTIONS2["DEREGISTER_HOST"] = 1] = "DEREGISTER_HOST";
8
+ ACTIONS2[ACTIONS2["ADD_UPDATE_PORTAL"] = 2] = "ADD_UPDATE_PORTAL";
9
+ ACTIONS2[ACTIONS2["REMOVE_PORTAL"] = 3] = "REMOVE_PORTAL";
10
+ return ACTIONS2;
11
+ })(ACTIONS || {});
12
+ const INITIAL_STATE = {};
13
+ const registerHost = (state, hostName) => {
14
+ if (!(hostName in state)) {
15
+ state[hostName] = [];
16
+ }
17
+ return state;
18
+ };
19
+ const deregisterHost = (state, hostName) => {
20
+ delete state[hostName];
21
+ return state;
22
+ };
23
+ const addUpdatePortal = (state, hostName, portalName, node) => {
24
+ if (!(hostName in state)) {
25
+ state = registerHost(state, hostName);
26
+ }
27
+ const index = state[hostName].findIndex((item) => item.name === portalName);
28
+ if (index !== -1) {
29
+ state[hostName][index].node = node;
30
+ } else {
31
+ state[hostName].push({
32
+ name: portalName,
33
+ node
34
+ });
35
+ }
36
+ return state;
37
+ };
38
+ const removePortal = (state, hostName, portalName) => {
39
+ if (!(hostName in state)) {
40
+ console.log(`Failed to remove portal '${portalName}', '${hostName}' was not registered!`);
41
+ return state;
42
+ }
43
+ const index = state[hostName].findIndex((item) => item.name === portalName);
44
+ if (index !== -1)
45
+ state[hostName].splice(index, 1);
46
+ return state;
47
+ };
48
+ const reducer = (state, action) => {
49
+ const { type } = action;
50
+ switch (type) {
51
+ case 0 /* REGISTER_HOST */:
52
+ return registerHost({ ...state }, action.hostName);
53
+ case 1 /* DEREGISTER_HOST */:
54
+ return deregisterHost({ ...state }, action.hostName);
55
+ case 2 /* ADD_UPDATE_PORTAL */:
56
+ return addUpdatePortal(
57
+ { ...state },
58
+ action.hostName,
59
+ action.portalName,
60
+ action.node
61
+ );
62
+ case 3 /* REMOVE_PORTAL */:
63
+ return removePortal({ ...state }, action.hostName, action.portalName);
64
+ default:
65
+ return state;
66
+ }
67
+ };
68
+ const PortalStateContext = createContext(null);
69
+ const PortalDispatchContext = createContext(null);
70
+ const usePortalState = (hostName) => {
71
+ const state = useContext(PortalStateContext);
72
+ if (state === null) {
73
+ throw new Error(
74
+ "'PortalStateContext' cannot be null, please add 'PortalProvider' to the root component."
75
+ );
76
+ }
77
+ return state[hostName] || [];
78
+ };
79
+ const usePortal = (hostName = "root") => {
80
+ const dispatch = useContext(PortalDispatchContext);
81
+ if (dispatch === null) {
82
+ throw new Error(
83
+ "'PortalDispatchContext' cannot be null, please add 'PortalProvider' to the root component."
84
+ );
85
+ }
86
+ const registerHost2 = useCallback(() => {
87
+ dispatch({
88
+ type: 0 /* REGISTER_HOST */,
89
+ hostName
90
+ });
91
+ }, []);
92
+ const deregisterHost2 = useCallback(() => {
93
+ dispatch({
94
+ type: 1 /* DEREGISTER_HOST */,
95
+ hostName
96
+ });
97
+ }, []);
98
+ const addUpdatePortal2 = useCallback((name, node) => {
99
+ dispatch({
100
+ type: 2 /* ADD_UPDATE_PORTAL */,
101
+ hostName,
102
+ portalName: name,
103
+ node
104
+ });
105
+ }, []);
106
+ const removePortal2 = useCallback((name) => {
107
+ dispatch({
108
+ type: 3 /* REMOVE_PORTAL */,
109
+ hostName,
110
+ portalName: name
111
+ });
112
+ }, []);
113
+ return {
114
+ registerHost: registerHost2,
115
+ deregisterHost: deregisterHost2,
116
+ addPortal: addUpdatePortal2,
117
+ updatePortal: addUpdatePortal2,
118
+ removePortal: removePortal2
119
+ };
120
+ };
121
+ const PortalProviderComponent = ({
122
+ rootHostName = "root",
123
+ shouldAddRootHost = true,
124
+ children
125
+ }) => {
126
+ const [state, dispatch] = useReducer(reducer, INITIAL_STATE);
127
+ const transitionDispatch = useMemo(() => {
128
+ const next = (value) => {
129
+ startTransition(() => {
130
+ dispatch(value);
131
+ });
132
+ };
133
+ return next;
134
+ }, [dispatch]);
135
+ return /* @__PURE__ */ jsx(PortalDispatchContext.Provider, {
136
+ value: transitionDispatch,
137
+ children: /* @__PURE__ */ jsxs(PortalStateContext.Provider, {
138
+ value: state,
139
+ children: [
140
+ children,
141
+ shouldAddRootHost && /* @__PURE__ */ jsx(PortalHost, {
142
+ name: rootHostName
143
+ })
144
+ ]
145
+ })
146
+ });
147
+ };
148
+ const PortalProvider = memo(PortalProviderComponent);
149
+ PortalProvider.displayName = "PortalProvider";
150
+ const PortalHostComponent = ({ name }) => {
151
+ const isServer = !useDidFinishSSR();
152
+ const state = usePortalState(name);
153
+ const { registerHost: registerHost2, deregisterHost: deregisterHost2 } = usePortal(name);
154
+ useEffect(() => {
155
+ if (isServer)
156
+ return;
157
+ registerHost2();
158
+ return () => {
159
+ deregisterHost2();
160
+ };
161
+ }, [isServer]);
162
+ return /* @__PURE__ */ jsx(Fragment, {
163
+ children: state.map((item) => item.node)
164
+ });
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
+ let id = 0;
178
+ const nanoid = () => {
179
+ id = (id + 1) % Number.MAX_SAFE_INTEGER;
180
+ return `${id}`;
181
+ };
182
+ const name = useMemo(() => _providedName || nanoid(), [_providedName]);
183
+ const handleOnMountRef = useRef();
184
+ const handleOnUnmountRef = useRef();
185
+ const handleOnUpdateRef = useRef();
186
+ const handleOnMount = useCallback(() => {
187
+ if (_providedHandleOnMount) {
188
+ _providedHandleOnMount(() => addUpdatePortal2(name, children));
189
+ } else {
190
+ addUpdatePortal2(name, children);
191
+ }
192
+ }, [_providedHandleOnMount, addUpdatePortal2]);
193
+ handleOnMountRef.current = handleOnMount;
194
+ const handleOnUnmount = useCallback(() => {
195
+ if (_providedHandleOnUnmount) {
196
+ _providedHandleOnUnmount(() => removePortal2(name));
197
+ } else {
198
+ removePortal2(name);
199
+ }
200
+ }, [_providedHandleOnUnmount, removePortal2]);
201
+ handleOnUnmountRef.current = handleOnUnmount;
202
+ const handleOnUpdate = useCallback(() => {
203
+ if (_providedHandleOnUpdate) {
204
+ _providedHandleOnUpdate(() => addUpdatePortal2(name, children));
205
+ } else {
206
+ addUpdatePortal2(name, children);
207
+ }
208
+ }, [_providedHandleOnUpdate, addUpdatePortal2, children]);
209
+ handleOnUpdateRef.current = handleOnUpdate;
210
+ useEffect(() => {
211
+ var _a;
212
+ (_a = handleOnMountRef.current) == null ? void 0 : _a.call(handleOnMountRef);
213
+ return () => {
214
+ var _a2;
215
+ (_a2 = handleOnUnmountRef.current) == null ? void 0 : _a2.call(handleOnUnmountRef);
216
+ handleOnMountRef.current = void 0;
217
+ handleOnUnmountRef.current = void 0;
218
+ handleOnUpdateRef.current = void 0;
219
+ };
220
+ }, []);
221
+ useEffect(() => {
222
+ var _a;
223
+ (_a = handleOnUpdateRef.current) == null ? void 0 : _a.call(handleOnUpdateRef);
224
+ }, [children]);
225
+ return null;
226
+ };
227
+ const PortalItem = memo(PortalComponent);
228
+ PortalItem.displayName = "Portal";
229
+ export {
230
+ ACTIONS,
231
+ INITIAL_STATE,
232
+ PortalHost,
233
+ PortalItem,
234
+ PortalProvider,
235
+ usePortal
236
+ };
237
+ //# 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 { useDidFinishSSR } from '@tamagui/core'\nimport { ReactNode, startTransition, 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 // eslint-disable-next-line no-console\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 = !useDidFinishSSR()\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 let id = 0\n const nanoid = () => {\n id = (id + 1) % Number.MAX_SAFE_INTEGER\n return `${id}`\n }\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": "AAyOM,SAsCG,UApCqB,KAFxB;AArON,SAAS,uBAAuB;AAChC,SAAoB,iBAAiB,SAAS,cAAc;AAC5D,SAAgB,eAAe,MAAM,aAAa,YAAY,WAAW,kBAAkB;AAO3F,IAAK,UAAL,kBAAKA,aAAL;AACE,EAAAA,kBAAA;AACA,EAAAA,kBAAA;AACA,EAAAA,kBAAA;AACA,EAAAA,kBAAA;AAJG,SAAAA;AAAA,GAAA;AAOL,MAAM,gBAAgB,CAAC;AAiCvB,MAAM,eAAe,CAAC,OAA0C,aAAqB;AACnF,MAAI,EAAE,YAAY,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,EAAE,YAAY,QAAQ;AACxB,YAAQ,aAAa,OAAO,QAAQ;AAAA,EACtC;AAKA,QAAM,QAAQ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wBAAwBA,gBAAe,CAAC;AAC5C,mBAAiB,UAAU;AAE3B,QAAM,kBAAkB,YAAY,MAAM;AACxC,QAAI,0BAA0B;AAC5B,+BAAyB,MAAMC,cAAa,IAAI,CAAC;AAAA,IACnD,OAAO;AACL,MAAAA,cAAa,IAAI;AAAA,IACnB;AAAA,EAEF,GAAG,CAAC,0BAA0BA,aAAY,CAAC;AAC3C,qBAAmB,UAAU;AAE7B,QAAM,iBAAiB,YAAY,MAAM;AACvC,QAAI,yBAAyB;AAC3B,8BAAwB,MAAMD,iBAAgB,MAAM,QAAQ,CAAC;AAAA,IAC/D,OAAO;AACL,MAAAA,iBAAgB,MAAM,QAAQ;AAAA,IAChC;AAAA,EAEF,GAAG,CAAC,yBAAyBA,kBAAiB,QAAQ,CAAC;AACvD,oBAAkB,UAAU;AAI5B,YAAU,MAAM;AA7XlB;AA8XI,2BAAiB,YAAjB;AACA,WAAO,MAAM;AA/XjB,UAAAE;AAgYM,OAAAA,MAAA,mBAAmB,YAAnB,gBAAAA,IAAA;AAGA,uBAAiB,UAAU;AAC3B,yBAAmB,UAAU;AAC7B,wBAAkB,UAAU;AAAA,IAC9B;AAAA,EACF,GAAG,CAAC,CAAC;AACL,YAAU,MAAM;AAxYlB;AAyYI,4BAAkB,YAAlB;AAAA,EACF,GAAG,CAAC,QAAQ,CAAC;AAGb,SAAO;AACT;AAEO,MAAM,aAAa,KAAK,eAAe;AAC9C,WAAW,cAAc;",
6
+ "names": ["ACTIONS", "registerHost", "deregisterHost", "addUpdatePortal", "removePortal", "_a"]
7
+ }
@@ -1,32 +1,28 @@
1
- var __defProp = Object.defineProperty;
2
- var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
1
+ import { jsx } from "react/jsx-runtime";
3
2
  import "@tamagui/polyfill-dev";
4
3
  import { isWeb, useIsomorphicLayoutEffect } from "@tamagui/core";
5
4
  import { YStack } from "@tamagui/stacks";
6
5
  import * as React from "react";
7
6
  import { createPortal } from "react-dom";
8
- import { Modal } from "react-native";
9
- const Portal = /* @__PURE__ */ __name((props) => {
10
- const contents = /* @__PURE__ */ React.createElement(YStack, {
11
- pointerEvents: "none",
7
+ const Portal = ({ host = ((_a) => (_a = globalThis.document) == null ? void 0 : _a.body)(), ...props }) => {
8
+ const contents = /* @__PURE__ */ jsx(YStack, {
9
+ pointerEvents: "box-none",
10
+ contain: "strict",
12
11
  fullscreen: true,
12
+ position: isWeb ? "fixed" : "absolute",
13
+ maxWidth: isWeb ? "100vw" : "100%",
14
+ maxHeight: isWeb ? "100vh" : "100%",
13
15
  ...props
14
16
  });
15
- if (!isWeb) {
16
- return /* @__PURE__ */ React.createElement(Modal, {
17
- visible: true
18
- }, contents);
19
- }
20
17
  const [hostElement, setHostElement] = React.useState(null);
21
18
  useIsomorphicLayoutEffect(() => {
22
- var _a;
23
- setHostElement((_a = globalThis.document) == null ? void 0 : _a.body);
24
- }, []);
19
+ setHostElement(host);
20
+ }, [host]);
25
21
  if (hostElement) {
26
22
  return createPortal(contents, hostElement);
27
23
  }
28
24
  return null;
29
- }, "Portal");
25
+ };
30
26
  export {
31
27
  Portal
32
28
  };
@@ -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;AAEA;AACA;AACA;AACA;AACA;AAGO,MAAM,SAAS,wBAAC,UAAuB;AAC5C,QAAM,WAAW,oCAAC;AAAA,IAAO,eAAc;AAAA,IAAO,YAAU;AAAA,OAAK;AAAA,GAAO;AACpE,MAAI,CAAC,OAAO;AACV,WAAO,oCAAC;AAAA,MAAM,SAAO;AAAA,OAAE,QAAS;AAAA,EAClC;AACA,QAAM,CAAC,aAAa,kBAAkB,MAAM,SAAc,IAAI;AAE9D,4BAA0B,MAAM;AAhBlC;AAiBI,mBAAe,iBAAW,aAAX,mBAAqB,IAAI;AAAA,EAC1C,GAAG,CAAC,CAAC;AAEL,MAAI,aAAa;AACf,WAAO,aAAa,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 } from '@tamagui/stacks'\nimport * as React from 'react'\nimport { createPortal } from 'react-dom'\n\nimport { PortalProps } from './PortalProps'\n\nexport const Portal = ({ host = globalThis.document?.body, ...props }: PortalProps) => {\n const contents = (\n <YStack\n pointerEvents=\"box-none\"\n contain=\"strict\"\n fullscreen\n // @ts-expect-error ok on web\n position={isWeb ? 'fixed' : 'absolute'}\n maxWidth={isWeb ? '100vw' : '100%'}\n maxHeight={isWeb ? '100vh' : '100%'}\n {...props}\n />\n )\n\n const [hostElement, setHostElement] = React.useState<any>(null)\n\n useIsomorphicLayoutEffect(() => {\n setHostElement(host)\n }, [host])\n\n if (hostElement) {\n return createPortal(contents, hostElement)\n }\n\n // ssr return null\n return null\n}\n"],
5
+ "mappings": "AAYI;AAXJ,OAAO;AAEP,SAAS,OAAO,iCAAiC;AACjD,SAAS,cAAc;AACvB,YAAY,WAAW;AACvB,SAAS,oBAAoB;AAItB,MAAM,SAAS,CAAC,EAAE,QAAO,yBAAW,aAAX,mBAAqB,YAAS,MAAM,MAAmB;AACrF,QAAM,WACJ,oBAAC;AAAA,IACC,eAAc;AAAA,IACd,SAAQ;AAAA,IACR,YAAU;AAAA,IAEV,UAAU,QAAQ,UAAU;AAAA,IAC5B,UAAU,QAAQ,UAAU;AAAA,IAC5B,WAAW,QAAQ,UAAU;AAAA,IAC5B,GAAG;AAAA,GACN;AAGF,QAAM,CAAC,aAAa,cAAc,IAAI,MAAM,SAAc,IAAI;AAE9D,4BAA0B,MAAM;AAC9B,mBAAe,IAAI;AAAA,EACrB,GAAG,CAAC,IAAI,CAAC;AAET,MAAI,aAAa;AACf,WAAO,aAAa,UAAU,WAAW;AAAA,EAC3C;AAGA,SAAO;AACT;",
6
6
  "names": []
7
7
  }
@@ -0,0 +1,36 @@
1
+ import { jsx } from "react/jsx-runtime";
2
+ import { YStack } from "@tamagui/stacks";
3
+ import * as React from "react";
4
+ import { Platform, RootTagContext } from "react-native";
5
+ import { createPortal } from "react-native/Libraries/Renderer/shims/ReactNative";
6
+ import { PortalItem } from "./GorhomPortal";
7
+ const Portal = (props) => {
8
+ const rootTag = React.useContext(RootTagContext);
9
+ const contents = /* @__PURE__ */ jsx(YStack, {
10
+ pointerEvents: "box-none",
11
+ fullscreen: true,
12
+ position: "absolute",
13
+ maxWidth: "100%",
14
+ zIndex: 1e5,
15
+ ...props
16
+ });
17
+ if (Platform.OS === "android") {
18
+ return /* @__PURE__ */ jsx(PortalItem, {
19
+ name: "_tamagui_root_portal",
20
+ children: contents
21
+ });
22
+ }
23
+ if (rootTag) {
24
+ return createPortal(contents, rootTag);
25
+ }
26
+ if (process.env.NODE_ENV === "development") {
27
+ console.warn(
28
+ `Missing rootTag, this is a bug - you may need a different React Native version, or to avoid using "modal" on native.`
29
+ );
30
+ }
31
+ return null;
32
+ };
33
+ export {
34
+ Portal
35
+ };
36
+ //# sourceMappingURL=Portal.native.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../src/Portal.native.tsx"],
4
+ "sourcesContent": ["import { YStack } from '@tamagui/stacks'\nimport * as React from 'react'\n// @ts-ignore\nimport { Platform, RootTagContext } from 'react-native'\nimport { createPortal } from 'react-native/Libraries/Renderer/shims/ReactNative'\n\nimport { PortalItem } from './GorhomPortal'\nimport { PortalProps } from './PortalProps'\n\nexport const Portal = (props: PortalProps) => {\n const rootTag = React.useContext(RootTagContext)\n\n const contents = (\n <YStack\n pointerEvents=\"box-none\"\n fullscreen\n position=\"absolute\"\n maxWidth=\"100%\"\n zIndex={100000}\n {...props}\n />\n )\n\n if (Platform.OS === 'android') {\n return <PortalItem name=\"_tamagui_root_portal\">{contents}</PortalItem>\n }\n\n if (rootTag) {\n return createPortal(contents, rootTag)\n }\n\n if (process.env.NODE_ENV === 'development') {\n // eslint-disable-next-line no-console\n console.warn(\n `Missing rootTag, this is a bug - you may need a different React Native version, or to avoid using \"modal\" on native.`\n )\n }\n\n return null\n}\n"],
5
+ "mappings": "AAaI;AAbJ,SAAS,cAAc;AACvB,YAAY,WAAW;AAEvB,SAAS,UAAU,sBAAsB;AACzC,SAAS,oBAAoB;AAE7B,SAAS,kBAAkB;AAGpB,MAAM,SAAS,CAAC,UAAuB;AAC5C,QAAM,UAAU,MAAM,WAAW,cAAc;AAE/C,QAAM,WACJ,oBAAC;AAAA,IACC,eAAc;AAAA,IACd,YAAU;AAAA,IACV,UAAS;AAAA,IACT,UAAS;AAAA,IACT,QAAQ;AAAA,IACP,GAAG;AAAA,GACN;AAGF,MAAI,SAAS,OAAO,WAAW;AAC7B,WAAO,oBAAC;AAAA,MAAW,MAAK;AAAA,MAAwB;AAAA,KAAS;AAAA,EAC3D;AAEA,MAAI,SAAS;AACX,WAAO,aAAa,UAAU,OAAO;AAAA,EACvC;AAEA,MAAI,QAAQ,IAAI,aAAa,eAAe;AAE1C,YAAQ;AAAA,MACN;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;",
6
+ "names": []
7
+ }
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=PortalProps.js.map
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": [],
4
+ "sourcesContent": [],
5
+ "mappings": "",
6
+ "names": []
7
+ }
package/dist/esm/index.js CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from "./Portal";
2
+ export * from "./GorhomPortal";
2
3
  //# 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;",
4
+ "sourcesContent": ["export * from './Portal'\nexport * from './GorhomPortal'\n"],
5
+ "mappings": "AAAA,cAAc;AACd,cAAc;",
6
6
  "names": []
7
7
  }
@@ -0,0 +1,223 @@
1
+ import { startTransition, useDidFinishSSR } from "@tamagui/core";
2
+ import { useMemo, useRef } from "react";
3
+ import { createContext, memo, useCallback, useContext, useEffect, useReducer } from "react";
4
+ var ACTIONS = /* @__PURE__ */ ((ACTIONS2) => {
5
+ ACTIONS2[ACTIONS2["REGISTER_HOST"] = 0] = "REGISTER_HOST";
6
+ ACTIONS2[ACTIONS2["DEREGISTER_HOST"] = 1] = "DEREGISTER_HOST";
7
+ ACTIONS2[ACTIONS2["ADD_UPDATE_PORTAL"] = 2] = "ADD_UPDATE_PORTAL";
8
+ ACTIONS2[ACTIONS2["REMOVE_PORTAL"] = 3] = "REMOVE_PORTAL";
9
+ return ACTIONS2;
10
+ })(ACTIONS || {});
11
+ const INITIAL_STATE = {};
12
+ const registerHost = (state, hostName) => {
13
+ if (!(hostName in state)) {
14
+ state[hostName] = [];
15
+ }
16
+ return state;
17
+ };
18
+ const deregisterHost = (state, hostName) => {
19
+ delete state[hostName];
20
+ return state;
21
+ };
22
+ const addUpdatePortal = (state, hostName, portalName, node) => {
23
+ if (!(hostName in state)) {
24
+ state = registerHost(state, hostName);
25
+ }
26
+ const index = state[hostName].findIndex((item) => item.name === portalName);
27
+ if (index !== -1) {
28
+ state[hostName][index].node = node;
29
+ } else {
30
+ state[hostName].push({
31
+ name: portalName,
32
+ node
33
+ });
34
+ }
35
+ return state;
36
+ };
37
+ const removePortal = (state, hostName, portalName) => {
38
+ if (!(hostName in state)) {
39
+ console.log(`Failed to remove portal '${portalName}', '${hostName}' was not registered!`);
40
+ return state;
41
+ }
42
+ const index = state[hostName].findIndex((item) => item.name === portalName);
43
+ if (index !== -1)
44
+ state[hostName].splice(index, 1);
45
+ return state;
46
+ };
47
+ const reducer = (state, action) => {
48
+ const { type } = action;
49
+ switch (type) {
50
+ case 0 /* REGISTER_HOST */:
51
+ return registerHost({ ...state }, action.hostName);
52
+ case 1 /* DEREGISTER_HOST */:
53
+ return deregisterHost({ ...state }, action.hostName);
54
+ case 2 /* ADD_UPDATE_PORTAL */:
55
+ return addUpdatePortal(
56
+ { ...state },
57
+ action.hostName,
58
+ action.portalName,
59
+ action.node
60
+ );
61
+ case 3 /* REMOVE_PORTAL */:
62
+ return removePortal({ ...state }, action.hostName, action.portalName);
63
+ default:
64
+ return state;
65
+ }
66
+ };
67
+ const PortalStateContext = createContext(null);
68
+ const PortalDispatchContext = createContext(null);
69
+ const usePortalState = (hostName) => {
70
+ const state = useContext(PortalStateContext);
71
+ if (state === null) {
72
+ throw new Error(
73
+ "'PortalStateContext' cannot be null, please add 'PortalProvider' to the root component."
74
+ );
75
+ }
76
+ return state[hostName] || [];
77
+ };
78
+ const usePortal = (hostName = "root") => {
79
+ const dispatch = useContext(PortalDispatchContext);
80
+ if (dispatch === null) {
81
+ throw new Error(
82
+ "'PortalDispatchContext' cannot be null, please add 'PortalProvider' to the root component."
83
+ );
84
+ }
85
+ const registerHost2 = useCallback(() => {
86
+ dispatch({
87
+ type: 0 /* REGISTER_HOST */,
88
+ hostName
89
+ });
90
+ }, []);
91
+ const deregisterHost2 = useCallback(() => {
92
+ dispatch({
93
+ type: 1 /* DEREGISTER_HOST */,
94
+ hostName
95
+ });
96
+ }, []);
97
+ const addUpdatePortal2 = useCallback((name, node) => {
98
+ dispatch({
99
+ type: 2 /* ADD_UPDATE_PORTAL */,
100
+ hostName,
101
+ portalName: name,
102
+ node
103
+ });
104
+ }, []);
105
+ const removePortal2 = useCallback((name) => {
106
+ dispatch({
107
+ type: 3 /* REMOVE_PORTAL */,
108
+ hostName,
109
+ portalName: name
110
+ });
111
+ }, []);
112
+ return {
113
+ registerHost: registerHost2,
114
+ deregisterHost: deregisterHost2,
115
+ addPortal: addUpdatePortal2,
116
+ updatePortal: addUpdatePortal2,
117
+ removePortal: removePortal2
118
+ };
119
+ };
120
+ const PortalProviderComponent = ({
121
+ rootHostName = "root",
122
+ shouldAddRootHost = true,
123
+ children
124
+ }) => {
125
+ const [state, dispatch] = useReducer(reducer, INITIAL_STATE);
126
+ const transitionDispatch = useMemo(() => {
127
+ const next = (value) => {
128
+ startTransition(() => {
129
+ dispatch(value);
130
+ });
131
+ };
132
+ return next;
133
+ }, [dispatch]);
134
+ return <PortalDispatchContext.Provider value={transitionDispatch}><PortalStateContext.Provider value={state}>
135
+ {children}
136
+ {shouldAddRootHost && <PortalHost name={rootHostName} />}
137
+ </PortalStateContext.Provider></PortalDispatchContext.Provider>;
138
+ };
139
+ const PortalProvider = memo(PortalProviderComponent);
140
+ PortalProvider.displayName = "PortalProvider";
141
+ const PortalHostComponent = ({ name }) => {
142
+ const isServer = !useDidFinishSSR();
143
+ const state = usePortalState(name);
144
+ const { registerHost: registerHost2, deregisterHost: deregisterHost2 } = usePortal(name);
145
+ useEffect(() => {
146
+ if (isServer)
147
+ return;
148
+ registerHost2();
149
+ return () => {
150
+ deregisterHost2();
151
+ };
152
+ }, [isServer]);
153
+ return <>{state.map((item) => item.node)}</>;
154
+ };
155
+ const PortalHost = memo(PortalHostComponent);
156
+ PortalHost.displayName = "PortalHost";
157
+ const PortalComponent = ({
158
+ name: _providedName,
159
+ hostName,
160
+ handleOnMount: _providedHandleOnMount,
161
+ handleOnUnmount: _providedHandleOnUnmount,
162
+ handleOnUpdate: _providedHandleOnUpdate,
163
+ children
164
+ }) => {
165
+ const { addPortal: addUpdatePortal2, removePortal: removePortal2 } = usePortal(hostName);
166
+ let id = 0;
167
+ const nanoid = () => {
168
+ id = (id + 1) % Number.MAX_SAFE_INTEGER;
169
+ return `${id}`;
170
+ };
171
+ const name = useMemo(() => _providedName || nanoid(), [_providedName]);
172
+ const handleOnMountRef = useRef();
173
+ const handleOnUnmountRef = useRef();
174
+ const handleOnUpdateRef = useRef();
175
+ const handleOnMount = useCallback(() => {
176
+ if (_providedHandleOnMount) {
177
+ _providedHandleOnMount(() => addUpdatePortal2(name, children));
178
+ } else {
179
+ addUpdatePortal2(name, children);
180
+ }
181
+ }, [_providedHandleOnMount, addUpdatePortal2]);
182
+ handleOnMountRef.current = handleOnMount;
183
+ const handleOnUnmount = useCallback(() => {
184
+ if (_providedHandleOnUnmount) {
185
+ _providedHandleOnUnmount(() => removePortal2(name));
186
+ } else {
187
+ removePortal2(name);
188
+ }
189
+ }, [_providedHandleOnUnmount, removePortal2]);
190
+ handleOnUnmountRef.current = handleOnUnmount;
191
+ const handleOnUpdate = useCallback(() => {
192
+ if (_providedHandleOnUpdate) {
193
+ _providedHandleOnUpdate(() => addUpdatePortal2(name, children));
194
+ } else {
195
+ addUpdatePortal2(name, children);
196
+ }
197
+ }, [_providedHandleOnUpdate, addUpdatePortal2, children]);
198
+ handleOnUpdateRef.current = handleOnUpdate;
199
+ useEffect(() => {
200
+ handleOnMountRef.current?.();
201
+ return () => {
202
+ handleOnUnmountRef.current?.();
203
+ handleOnMountRef.current = void 0;
204
+ handleOnUnmountRef.current = void 0;
205
+ handleOnUpdateRef.current = void 0;
206
+ };
207
+ }, []);
208
+ useEffect(() => {
209
+ handleOnUpdateRef.current?.();
210
+ }, [children]);
211
+ return null;
212
+ };
213
+ const PortalItem = memo(PortalComponent);
214
+ PortalItem.displayName = "Portal";
215
+ export {
216
+ ACTIONS,
217
+ INITIAL_STATE,
218
+ PortalHost,
219
+ PortalItem,
220
+ PortalProvider,
221
+ usePortal
222
+ };
223
+ //# sourceMappingURL=GorhomPortal.js.map