@tamagui/portal 2.0.0-rc.4 → 2.0.0-rc.40

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 (103) hide show
  1. package/dist/cjs/GorhomPortal.cjs +177 -178
  2. package/dist/cjs/GorhomPortal.native.js +206 -203
  3. package/dist/cjs/GorhomPortal.native.js.map +1 -1
  4. package/dist/cjs/GorhomPortalItem.cjs +49 -24
  5. package/dist/cjs/GorhomPortalItem.native.js +100 -75
  6. package/dist/cjs/GorhomPortalItem.native.js.map +1 -1
  7. package/dist/cjs/Portal.cjs +56 -46
  8. package/dist/cjs/Portal.native.js +43 -38
  9. package/dist/cjs/Portal.native.js.map +1 -1
  10. package/dist/cjs/PortalProps.cjs +7 -5
  11. package/dist/cjs/PortalProps.native.js +7 -5
  12. package/dist/cjs/PortalProps.native.js.map +1 -1
  13. package/dist/cjs/constants.cjs +24 -18
  14. package/dist/cjs/constants.native.js +25 -21
  15. package/dist/cjs/constants.native.js.map +1 -1
  16. package/dist/cjs/helpers.cjs +19 -13
  17. package/dist/cjs/helpers.native.js +21 -19
  18. package/dist/cjs/helpers.native.js.map +1 -1
  19. package/dist/cjs/index.cjs +13 -11
  20. package/dist/cjs/index.native.js +13 -11
  21. package/dist/cjs/index.native.js.map +1 -1
  22. package/dist/cjs/native-portal.cjs +31 -30
  23. package/dist/cjs/native-portal.native.js +36 -35
  24. package/dist/cjs/native-portal.native.js.map +1 -1
  25. package/dist/cjs/types.cjs +7 -5
  26. package/dist/cjs/types.native.js +7 -5
  27. package/dist/cjs/types.native.js.map +1 -1
  28. package/dist/esm/GorhomPortal.mjs +148 -151
  29. package/dist/esm/GorhomPortal.mjs.map +1 -1
  30. package/dist/esm/GorhomPortal.native.js +176 -175
  31. package/dist/esm/GorhomPortal.native.js.map +1 -1
  32. package/dist/esm/GorhomPortalItem.mjs +33 -10
  33. package/dist/esm/GorhomPortalItem.mjs.map +1 -1
  34. package/dist/esm/GorhomPortalItem.native.js +81 -58
  35. package/dist/esm/GorhomPortalItem.native.js.map +1 -1
  36. package/dist/esm/Portal.mjs +26 -19
  37. package/dist/esm/Portal.mjs.map +1 -1
  38. package/dist/esm/Portal.native.js +22 -19
  39. package/dist/esm/Portal.native.js.map +1 -1
  40. package/dist/esm/constants.mjs +10 -6
  41. package/dist/esm/constants.mjs.map +1 -1
  42. package/dist/esm/constants.native.js +10 -8
  43. package/dist/esm/constants.native.js.map +1 -1
  44. package/dist/esm/helpers.mjs +7 -3
  45. package/dist/esm/helpers.mjs.map +1 -1
  46. package/dist/esm/helpers.native.js +7 -7
  47. package/dist/esm/helpers.native.js.map +1 -1
  48. package/dist/esm/index.js +7 -10
  49. package/dist/esm/index.js.map +1 -6
  50. package/dist/esm/native-portal.mjs +19 -20
  51. package/dist/esm/native-portal.mjs.map +1 -1
  52. package/dist/esm/native-portal.native.js +24 -25
  53. package/dist/esm/native-portal.native.js.map +1 -1
  54. package/package.json +13 -16
  55. package/src/GorhomPortal.tsx +30 -15
  56. package/src/GorhomPortalItem.tsx +7 -1
  57. package/src/Portal.tsx +18 -21
  58. package/src/PortalProps.tsx +10 -2
  59. package/types/GorhomPortal.d.ts.map +1 -1
  60. package/types/GorhomPortalItem.d.ts +1 -1
  61. package/types/GorhomPortalItem.d.ts.map +1 -1
  62. package/types/Portal.d.ts +1 -1
  63. package/types/Portal.d.ts.map +1 -1
  64. package/types/PortalProps.d.ts +9 -2
  65. package/types/PortalProps.d.ts.map +1 -1
  66. package/dist/cjs/GorhomPortal.js +0 -159
  67. package/dist/cjs/GorhomPortal.js.map +0 -6
  68. package/dist/cjs/GorhomPortalItem.js +0 -41
  69. package/dist/cjs/GorhomPortalItem.js.map +0 -6
  70. package/dist/cjs/Portal.js +0 -58
  71. package/dist/cjs/Portal.js.map +0 -6
  72. package/dist/cjs/PortalProps.js +0 -14
  73. package/dist/cjs/PortalProps.js.map +0 -6
  74. package/dist/cjs/constants.js +0 -28
  75. package/dist/cjs/constants.js.map +0 -6
  76. package/dist/cjs/helpers.js +0 -26
  77. package/dist/cjs/helpers.js.map +0 -6
  78. package/dist/cjs/index.js +0 -27
  79. package/dist/cjs/index.js.map +0 -6
  80. package/dist/cjs/native-portal.js +0 -40
  81. package/dist/cjs/native-portal.js.map +0 -6
  82. package/dist/cjs/setup-native.js +0 -29
  83. package/dist/cjs/setup-native.js.map +0 -1
  84. package/dist/cjs/types.js +0 -14
  85. package/dist/cjs/types.js.map +0 -6
  86. package/dist/esm/GorhomPortal.js +0 -148
  87. package/dist/esm/GorhomPortal.js.map +0 -6
  88. package/dist/esm/GorhomPortalItem.js +0 -28
  89. package/dist/esm/GorhomPortalItem.js.map +0 -6
  90. package/dist/esm/Portal.js +0 -40
  91. package/dist/esm/Portal.js.map +0 -6
  92. package/dist/esm/PortalProps.js +0 -1
  93. package/dist/esm/PortalProps.js.map +0 -6
  94. package/dist/esm/constants.js +0 -13
  95. package/dist/esm/constants.js.map +0 -6
  96. package/dist/esm/helpers.js +0 -10
  97. package/dist/esm/helpers.js.map +0 -6
  98. package/dist/esm/native-portal.js +0 -24
  99. package/dist/esm/native-portal.js.map +0 -6
  100. package/dist/esm/setup-native.js +0 -3
  101. package/dist/esm/setup-native.js.map +0 -1
  102. package/dist/esm/types.js +0 -1
  103. package/dist/esm/types.js.map +0 -6
@@ -2,33 +2,35 @@ var __create = Object.create;
2
2
  var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf,
6
- __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
7
  var __export = (target, all) => {
8
- for (var name in all) __defProp(target, name, {
9
- get: all[name],
10
- enumerable: !0
11
- });
12
- },
13
- __copyProps = (to, from, except, desc) => {
14
- if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
8
+ for (var name in all) __defProp(target, name, {
9
+ get: all[name],
10
+ enumerable: true
11
+ });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
15
16
  get: () => from[key],
16
17
  enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
18
  });
18
- return to;
19
- };
19
+ }
20
+ return to;
21
+ };
20
22
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
26
- value: mod,
27
- enumerable: !0
28
- }) : target, mod)),
29
- __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
30
- value: !0
31
- }), mod);
23
+ // If the importer is in node compatibility mode or this is not an ESM
24
+ // file that has been converted to a CommonJS file using a Babel-
25
+ // compatible transform (i.e. "__esModule" has not been set), then set
26
+ // "default" to the CommonJS "module.exports" for node compatibility.
27
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
28
+ value: mod,
29
+ enumerable: true
30
+ }) : target, mod));
31
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
32
+ value: true
33
+ }), mod);
32
34
  var GorhomPortal_exports = {};
33
35
  __export(GorhomPortal_exports, {
34
36
  ACTIONS: () => ACTIONS,
@@ -38,172 +40,169 @@ __export(GorhomPortal_exports, {
38
40
  usePortal: () => usePortal
39
41
  });
40
42
  module.exports = __toCommonJS(GorhomPortal_exports);
41
- var import_constants = require("@tamagui/constants"),
42
- import_native = require("@tamagui/native"),
43
- import_start_transition = require("@tamagui/start-transition"),
44
- import_react = __toESM(require("react"), 1),
45
- import_constants2 = require("./constants.cjs"),
46
- import_jsx_runtime = require("react/jsx-runtime"),
47
- ACTIONS = /* @__PURE__ */(ACTIONS2 => (ACTIONS2[ACTIONS2.REGISTER_HOST = 0] = "REGISTER_HOST", ACTIONS2[ACTIONS2.DEREGISTER_HOST = 1] = "DEREGISTER_HOST", ACTIONS2[ACTIONS2.ADD_UPDATE_PORTAL = 2] = "ADD_UPDATE_PORTAL", ACTIONS2[ACTIONS2.REMOVE_PORTAL = 3] = "REMOVE_PORTAL", ACTIONS2))(ACTIONS || {});
43
+ var import_constants = require("@tamagui/constants");
44
+ var import_native = require("@tamagui/native");
45
+ var import_start_transition = require("@tamagui/start-transition");
46
+ var import_react = __toESM(require("react"), 1);
47
+ var import_constants2 = require("./constants.cjs");
48
+ var import_jsx_runtime = require("react/jsx-runtime");
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 || {});
48
56
  const INITIAL_STATE = {};
49
- const registerHost = (state, hostName) => (hostName in state || (state[hostName] = []), state),
50
- deregisterHost = (state, hostName) => (delete state[hostName], state),
51
- addUpdatePortal = (state, hostName, portalName, node) => {
52
- hostName in state || (state = registerHost(state, hostName));
53
- const index = state[hostName].findIndex(item => item.name === portalName);
54
- return index !== -1 ? state[hostName][index].node = node : state[hostName].push({
55
- name: portalName,
57
+ const registerHost = (state, hostName) => {
58
+ if (!(hostName in state)) state[hostName] = [];
59
+ return state;
60
+ };
61
+ const deregisterHost = (state, hostName) => {
62
+ delete state[hostName];
63
+ return state;
64
+ };
65
+ const addUpdatePortal = (state, hostName, portalName, node) => {
66
+ if (!(hostName in state)) state = registerHost(state, hostName);
67
+ const index = state[hostName].findIndex(item => item.name === portalName);
68
+ if (index !== -1) state[hostName][index].node = node;else state[hostName].push({
69
+ name: portalName,
70
+ node
71
+ });
72
+ return state;
73
+ };
74
+ const removePortal = (state, hostName, portalName) => {
75
+ if (!(hostName in state)) {
76
+ if (process.env.NODE_ENV === "development") console.info(`Failed to remove portal '${portalName}', '${hostName}' was not registered!`);
77
+ return state;
78
+ }
79
+ const index = state[hostName].findIndex(item => item.name === portalName);
80
+ if (index !== -1) state[hostName].splice(index, 1);
81
+ return state;
82
+ };
83
+ const reducer = (state, action) => {
84
+ const {
85
+ type
86
+ } = action;
87
+ switch (type) {
88
+ case 0:
89
+ return registerHost({
90
+ ...state
91
+ }, action.hostName);
92
+ case 1:
93
+ return deregisterHost({
94
+ ...state
95
+ }, action.hostName);
96
+ case 2:
97
+ return addUpdatePortal({
98
+ ...state
99
+ }, action.hostName, action.portalName, action.node);
100
+ case 3:
101
+ return removePortal({
102
+ ...state
103
+ }, action.hostName, action.portalName);
104
+ default:
105
+ return state;
106
+ }
107
+ };
108
+ const PortalStateContext = (0, import_react.createContext)(null);
109
+ const PortalDispatchContext = (0, import_react.createContext)(null);
110
+ const PortalProviderActiveContext = (0, import_react.createContext)(false);
111
+ const usePortal = (hostName = "root") => {
112
+ const dispatch = (0, import_react.useContext)(PortalDispatchContext);
113
+ if (dispatch === null) throw new Error("'PortalDispatchContext' cannot be null, please add 'PortalProvider' to the root component.");
114
+ const registerHost2 = (0, import_react.useCallback)(() => {
115
+ dispatch({
116
+ type: 0,
117
+ hostName
118
+ });
119
+ }, []);
120
+ const deregisterHost2 = (0, import_react.useCallback)(() => {
121
+ dispatch({
122
+ type: 1,
123
+ hostName
124
+ });
125
+ }, []);
126
+ const addUpdatePortal2 = (0, import_react.useCallback)((name, node) => {
127
+ dispatch({
128
+ type: 2,
129
+ hostName,
130
+ portalName: name,
56
131
  node
57
- }), state;
58
- },
59
- removePortal = (state, hostName, portalName) => {
60
- if (!(hostName in state)) return process.env.NODE_ENV === "development" && console.info(`Failed to remove portal '${portalName}', '${hostName}' was not registered!`), state;
61
- const index = state[hostName].findIndex(item => item.name === portalName);
62
- return index !== -1 && state[hostName].splice(index, 1), state;
63
- },
64
- reducer = (state, action) => {
65
- const {
66
- type
67
- } = action;
68
- switch (type) {
69
- case 0 /* REGISTER_HOST */:
70
- return registerHost({
71
- ...state
72
- }, action.hostName);
73
- case 1 /* DEREGISTER_HOST */:
74
- return deregisterHost({
75
- ...state
76
- }, action.hostName);
77
- case 2 /* ADD_UPDATE_PORTAL */:
78
- return addUpdatePortal({
79
- ...state
80
- }, action.hostName, action.portalName, action.node);
81
- case 3 /* REMOVE_PORTAL */:
82
- return removePortal({
83
- ...state
84
- }, action.hostName, action.portalName);
85
- default:
86
- return state;
87
- }
88
- },
89
- PortalStateContext = (0, import_react.createContext)(null),
90
- PortalDispatchContext = (0, import_react.createContext)(null),
91
- usePortalState = hostName => {
92
- const state = (0, import_react.useContext)(PortalStateContext);
93
- if (state === null) throw new Error("'PortalStateContext' cannot be null, please add 'PortalProvider' to the root component.");
94
- return state[hostName] || [];
95
- },
96
- usePortal = (hostName = "root") => {
97
- const dispatch = (0, import_react.useContext)(PortalDispatchContext);
98
- if (dispatch === null) throw new Error("'PortalDispatchContext' cannot be null, please add 'PortalProvider' to the root component.");
99
- const registerHost2 = (0, import_react.useCallback)(() => {
100
- dispatch({
101
- type: 0 /* REGISTER_HOST */,
102
- hostName
103
- });
104
- }, []),
105
- deregisterHost2 = (0, import_react.useCallback)(() => {
106
- dispatch({
107
- type: 1 /* DEREGISTER_HOST */,
108
- hostName
109
- });
110
- }, []),
111
- addUpdatePortal2 = (0, import_react.useCallback)((name, node) => {
112
- dispatch({
113
- type: 2 /* ADD_UPDATE_PORTAL */,
114
- hostName,
115
- portalName: name,
116
- node
117
- });
118
- }, []),
119
- removePortal2 = (0, import_react.useCallback)(name => {
120
- dispatch({
121
- type: 3 /* REMOVE_PORTAL */,
122
- hostName,
123
- portalName: name
124
- });
125
- }, []);
126
- return {
127
- registerHost: registerHost2,
128
- deregisterHost: deregisterHost2,
129
- addPortal: addUpdatePortal2,
130
- updatePortal: addUpdatePortal2,
131
- removePortal: removePortal2
132
- };
133
- },
134
- PortalProviderComponent = ({
135
- rootHostName = "root",
136
- shouldAddRootHost = !0,
137
- children
138
- }) => {
139
- const [state, dispatch] = (0, import_react.useReducer)(reducer, INITIAL_STATE),
140
- transitionDispatch = (0, import_react.useMemo)(() => value => {
141
- (0, import_start_transition.startTransition)(() => {
142
- dispatch(value);
143
- });
144
- }, [dispatch]),
145
- portalState = (0, import_native.getPortal)().state,
146
- content = /* @__PURE__ */(0, import_jsx_runtime.jsx)(PortalDispatchContext.Provider, {
147
- value: transitionDispatch,
148
- children: /* @__PURE__ */(0, import_jsx_runtime.jsxs)(PortalStateContext.Provider, {
149
- value: state,
150
- children: [children, shouldAddRootHost && /* @__PURE__ */(0, import_jsx_runtime.jsx)(PortalHost, {
151
- name: rootHostName
152
- })]
153
- })
132
+ });
133
+ }, []);
134
+ return {
135
+ registerHost: registerHost2,
136
+ deregisterHost: deregisterHost2,
137
+ addPortal: addUpdatePortal2,
138
+ updatePortal: addUpdatePortal2,
139
+ removePortal: (0, import_react.useCallback)(name => {
140
+ dispatch({
141
+ type: 3,
142
+ hostName,
143
+ portalName: name
154
144
  });
155
- return portalState.type === "teleport" ? /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_native.NativePortalProvider, {
156
- children: content
157
- }) : content;
158
- },
159
- PortalProvider = (0, import_react.memo)(PortalProviderComponent);
145
+ }, [])
146
+ };
147
+ };
148
+ const PortalProviderComponent = ({
149
+ rootHostName = "root",
150
+ shouldAddRootHost = true,
151
+ children
152
+ }) => {
153
+ const isAlreadyInProvider = (0, import_react.useContext)(PortalProviderActiveContext);
154
+ if (process.env.NODE_ENV === "development") {
155
+ if (isAlreadyInProvider && shouldAddRootHost) console.warn(`[tamagui] Nested PortalProvider with shouldAddRootHost detected. This causes hydration mismatches. TamaguiProvider from 'tamagui' already includes PortalProvider - remove the explicit PortalProvider wrapper or set shouldAddRootHost={false}.`);
156
+ }
157
+ const [state, dispatch] = (0, import_react.useReducer)(reducer, INITIAL_STATE);
158
+ const transitionDispatch = (0, import_react.useMemo)(() => {
159
+ const next = value => {
160
+ (0, import_start_transition.startTransition)(() => {
161
+ dispatch(value);
162
+ });
163
+ };
164
+ return next;
165
+ }, [dispatch]);
166
+ const portalState = (0, import_native.getPortal)().state;
167
+ const content = /* @__PURE__ */(0, import_jsx_runtime.jsx)(PortalProviderActiveContext.Provider, {
168
+ value: true,
169
+ children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(PortalDispatchContext.Provider, {
170
+ value: transitionDispatch,
171
+ children: /* @__PURE__ */(0, import_jsx_runtime.jsxs)(PortalStateContext.Provider, {
172
+ value: state,
173
+ children: [children, shouldAddRootHost && /* @__PURE__ */(0, import_jsx_runtime.jsx)(PortalHost, {
174
+ name: rootHostName
175
+ })]
176
+ })
177
+ })
178
+ });
179
+ if (portalState.type === "teleport") return /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_native.NativePortalProvider, {
180
+ children: content
181
+ });
182
+ return content;
183
+ };
184
+ const PortalProvider = (0, import_react.memo)(PortalProviderComponent);
160
185
  PortalProvider.displayName = "PortalProvider";
161
- const defaultRenderer = children => /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, {
162
- children
163
- }),
164
- PortalHost = (0, import_react.memo)(function (props) {
165
- return import_constants.isWeb ? /* @__PURE__ */(0, import_jsx_runtime.jsx)(PortalHostWeb, {
166
- ...props
167
- }) : (0, import_native.getPortal)().state.type === "teleport" ? /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_native.NativePortalHost, {
168
- name: props.name
169
- }) : /* @__PURE__ */(0, import_jsx_runtime.jsx)(PortalHostNonNative, {
170
- ...props
171
- });
186
+ const PortalHost = (0, import_react.memo)(function PortalHost2(props) {
187
+ return /* @__PURE__ */(0, import_jsx_runtime.jsx)(PortalHostWeb, {
188
+ ...props
172
189
  });
190
+ });
173
191
  function PortalHostWeb(props) {
174
- return (0, import_constants.useIsomorphicLayoutEffect)(() => () => {
175
- import_constants2.allPortalHosts.delete(props.name);
176
- }, [props.name]), /* @__PURE__ */(0, import_jsx_runtime.jsx)("div", {
192
+ (0, import_constants.useIsomorphicLayoutEffect)(() => {
193
+ return () => {
194
+ import_constants2.allPortalHosts.delete(props.name);
195
+ };
196
+ }, [props.name]);
197
+ return /* @__PURE__ */(0, import_jsx_runtime.jsx)("div", {
177
198
  style: {
178
199
  display: "contents"
179
200
  },
180
201
  ref: node => {
181
- node && (import_constants2.allPortalHosts.set(props.name, node), import_constants2.portalListeners[props.name]?.forEach(x => x(node)));
202
+ if (node) {
203
+ import_constants2.allPortalHosts.set(props.name, node);
204
+ import_constants2.portalListeners[props.name]?.forEach(x => x(node));
205
+ }
182
206
  }
183
207
  });
184
- }
185
- function PortalHostNonNative(props) {
186
- const {
187
- name,
188
- forwardProps,
189
- render = defaultRenderer
190
- } = props,
191
- state = usePortalState(name),
192
- {
193
- registerHost: registerHost2,
194
- deregisterHost: deregisterHost2
195
- } = usePortal(props.name);
196
- return (0, import_constants.useIsomorphicLayoutEffect)(() => (registerHost2(), () => {
197
- deregisterHost2();
198
- }), []), render(forwardProps ? state.map(item => {
199
- let next = item.node;
200
- const {
201
- children,
202
- ...restForwardProps
203
- } = forwardProps;
204
- return forwardProps ? import_react.default.Children.map(next, child => import_react.default.isValidElement(child) ? import_react.default.cloneElement(child, {
205
- key: child.key,
206
- ...restForwardProps
207
- }) : child) : next;
208
- }) : state.map(item => item.node));
209
208
  }