@tamagui/adapt 2.0.0-rc.9 → 2.0.0

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.
@@ -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 Adapt_exports = {};
33
35
  __export(Adapt_exports, {
34
36
  Adapt: () => Adapt,
@@ -41,14 +43,14 @@ __export(Adapt_exports, {
41
43
  useAdaptIsActive: () => useAdaptIsActive
42
44
  });
43
45
  module.exports = __toCommonJS(Adapt_exports);
44
- var import_constants = require("@tamagui/constants"),
45
- import_core = require("@tamagui/core"),
46
- import_helpers = require("@tamagui/helpers"),
47
- import_native = require("@tamagui/native"),
48
- import_portal = require("@tamagui/portal"),
49
- import_z_index_stack = require("@tamagui/z-index-stack"),
50
- import_react = __toESM(require("react"), 1),
51
- import_jsx_runtime = require("react/jsx-runtime");
46
+ var import_constants = require("@tamagui/constants");
47
+ var import_core = require("@tamagui/core");
48
+ var import_helpers = require("@tamagui/helpers");
49
+ var import_native = require("@tamagui/native");
50
+ var import_portal = require("@tamagui/portal");
51
+ var import_z_index_stack = require("@tamagui/z-index-stack");
52
+ var import_react = __toESM(require("react"), 1);
53
+ var import_jsx_runtime = require("react/jsx-runtime");
52
54
  function createAdaptChildrenStore() {
53
55
  let children = null;
54
56
  const listeners = /* @__PURE__ */new Set();
@@ -59,177 +61,179 @@ function createAdaptChildrenStore() {
59
61
  },
60
62
  get: () => children,
61
63
  subscribe(callback) {
62
- return listeners.add(callback), () => listeners.delete(callback);
64
+ listeners.add(callback);
65
+ return () => listeners.delete(callback);
63
66
  }
64
67
  };
65
68
  }
66
- const AdaptChildrenStoreContext = (0, import_react.createContext)(null),
67
- emptySubscribe = () => () => {},
68
- emptyGet = () => null;
69
- function TeleportAdaptContents() {
70
- const store = (0, import_react.useContext)(AdaptChildrenStoreContext),
71
- children = import_react.default.useSyncExternalStore(store?.subscribe ?? emptySubscribe, store?.get ?? emptyGet, store?.get ?? emptyGet);
72
- return /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, {
73
- children
74
- });
75
- }
69
+ const AdaptChildrenStoreContext = (0, import_react.createContext)(null);
76
70
  const AdaptContext = (0, import_core.createStyledContext)({
77
- Contents: null,
78
- scopeName: "",
79
- portalName: "",
80
- platform: null,
81
- setPlatform: x => {},
82
- when: null,
83
- setChildren: null,
84
- setWhen: () => {}
85
- }),
86
- LastAdaptContextScope = (0, import_react.createContext)(""),
87
- ProvideAdaptContext = ({
88
- children,
89
- ...context
90
- }) => {
91
- const scope = context.scopeName || "",
92
- lastScope = (0, import_react.useContext)(LastAdaptContextScope);
93
- return /* @__PURE__ */(0, import_jsx_runtime.jsx)(LastAdaptContextScope.Provider, {
94
- value: lastScope || context.lastScope || "",
95
- children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(AdaptContext.Provider, {
96
- scope,
97
- lastScope: lastScope || context.lastScope,
98
- ...context,
71
+ Contents: null,
72
+ scopeName: "",
73
+ portalName: "",
74
+ platform: null,
75
+ setPlatform: x => {},
76
+ when: null,
77
+ setWhen: () => {}
78
+ });
79
+ const LastAdaptContextScope = (0, import_react.createContext)("");
80
+ const ProvideAdaptContext = ({
81
+ children,
82
+ ...context
83
+ }) => {
84
+ const scope = context.scopeName || "";
85
+ const lastScope = (0, import_react.useContext)(LastAdaptContextScope);
86
+ return /* @__PURE__ */(0, import_jsx_runtime.jsx)(LastAdaptContextScope.Provider, {
87
+ value: lastScope || context.lastScope || "",
88
+ children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(AdaptContext.Provider, {
89
+ scope,
90
+ lastScope: lastScope || context.lastScope,
91
+ ...context,
92
+ children
93
+ })
94
+ });
95
+ };
96
+ const useAdaptContext = scope => {
97
+ const lastScope = (0, import_react.useContext)(LastAdaptContextScope);
98
+ const adaptScope = scope ?? lastScope;
99
+ return AdaptContext.useStyledContext(adaptScope);
100
+ };
101
+ const AdaptPortals = /* @__PURE__ */new Map();
102
+ const AdaptParent = ({
103
+ children,
104
+ Contents,
105
+ scope,
106
+ portal
107
+ }) => {
108
+ const id = (0, import_react.useId)();
109
+ const portalName = `AdaptPortal${scope}${id}`;
110
+ const childrenStoreRef = import_react.default.useRef(null);
111
+ if (!childrenStoreRef.current) childrenStoreRef.current = createAdaptChildrenStore();
112
+ const isTeleport = false;
113
+ const FinalContents = (0, import_react.useMemo)(() => {
114
+ if (Contents) return Contents;
115
+ if (AdaptPortals.has(portalName)) return AdaptPortals.get(portalName);
116
+ const element = () => {
117
+ return /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_portal.PortalHost, {
118
+ name: portalName,
119
+ forwardProps: typeof portal === "boolean" ? void 0 : portal?.forwardProps
120
+ }, id);
121
+ };
122
+ AdaptPortals.set(portalName, element);
123
+ return element;
124
+ }, [portalName, Contents, isTeleport]);
125
+ (0, import_constants.useIsomorphicLayoutEffect)(() => {
126
+ AdaptPortals.set(portalName, FinalContents);
127
+ return () => {
128
+ AdaptPortals.delete(portalName);
129
+ };
130
+ }, [portalName, isTeleport]);
131
+ const [when, setWhen] = import_react.default.useState(null);
132
+ const [platform, setPlatform] = import_react.default.useState(null);
133
+ return /* @__PURE__ */(0, import_jsx_runtime.jsx)(AdaptChildrenStoreContext.Provider, {
134
+ value: childrenStoreRef.current,
135
+ children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(LastAdaptContextScope.Provider, {
136
+ value: scope,
137
+ children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(ProvideAdaptContext, {
138
+ Contents: FinalContents,
139
+ when,
140
+ platform,
141
+ setPlatform,
142
+ setWhen,
143
+ portalName,
144
+ scopeName: scope,
99
145
  children
100
146
  })
101
- });
102
- },
103
- useAdaptContext = scope => {
104
- const lastScope = (0, import_react.useContext)(LastAdaptContextScope),
105
- adaptScope = scope ?? lastScope;
106
- return AdaptContext.useStyledContext(adaptScope);
107
- },
108
- AdaptPortals = /* @__PURE__ */new Map(),
109
- AdaptParent = ({
147
+ })
148
+ });
149
+ };
150
+ const AdaptContents = ({
151
+ scope,
152
+ ...rest
153
+ }) => {
154
+ const context = useAdaptContext(scope);
155
+ if (!context?.Contents) throw new Error(process.env.NODE_ENV === "production" ? `tamagui.dev/docs/intro/errors#warning-002` : `You're rendering a Tamagui <Adapt /> component without nesting it inside a parent that is able to adapt.`);
156
+ return import_react.default.createElement(context.Contents, {
157
+ ...rest,
158
+ key: `stable`
159
+ });
160
+ };
161
+ AdaptContents.shouldForwardSpace = true;
162
+ const Adapt = (0, import_helpers.withStaticProperties)(function Adapt2(props) {
163
+ const {
164
+ platform,
165
+ when,
110
166
  children,
111
- Contents,
112
- scope,
113
- portal
114
- }) => {
115
- const id = (0, import_react.useId)(),
116
- portalName = `AdaptPortal${scope}${id}`,
117
- childrenStoreRef = import_react.default.useRef(null);
118
- childrenStoreRef.current || (childrenStoreRef.current = createAdaptChildrenStore());
119
- const isTeleport = !import_constants.isWeb && (0, import_native.getPortal)().state.type === "teleport",
120
- FinalContents = (0, import_react.useMemo)(() => {
121
- if (Contents) return Contents;
122
- if (isTeleport) return TeleportAdaptContents;
123
- if (AdaptPortals.has(portalName)) return AdaptPortals.get(portalName);
124
- const element = () => /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_portal.PortalHost, {
125
- name: portalName,
126
- forwardProps: typeof portal == "boolean" ? void 0 : portal?.forwardProps
127
- }, id);
128
- return AdaptPortals.set(portalName, element), element;
129
- }, [portalName, Contents, isTeleport]);
130
- (0, import_constants.useIsomorphicLayoutEffect)(() => {
131
- if (!isTeleport) return AdaptPortals.set(portalName, FinalContents), () => {
132
- AdaptPortals.delete(portalName);
133
- };
134
- }, [portalName, isTeleport]);
135
- const [when, setWhen] = import_react.default.useState(null),
136
- [platform, setPlatform] = import_react.default.useState(null),
137
- [children2, setChildren] = import_react.default.useState(null);
138
- return /* @__PURE__ */(0, import_jsx_runtime.jsx)(AdaptChildrenStoreContext.Provider, {
139
- value: childrenStoreRef.current,
140
- children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(LastAdaptContextScope.Provider, {
141
- value: scope,
142
- children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(ProvideAdaptContext, {
143
- Contents: FinalContents,
144
- when,
145
- platform,
146
- setPlatform,
147
- setWhen,
148
- setChildren,
149
- portalName,
150
- scopeName: scope,
151
- children
152
- })
153
- })
154
- });
155
- },
156
- AdaptContents = ({
157
- scope,
158
- ...rest
159
- }) => {
160
- const context = useAdaptContext(scope);
161
- if (!context?.Contents) throw new Error(process.env.NODE_ENV === "production" ? "tamagui.dev/docs/intro/errors#warning-002" : "You're rendering a Tamagui <Adapt /> component without nesting it inside a parent that is able to adapt.");
162
- return import_react.default.createElement(context.Contents, {
163
- ...rest,
164
- key: "stable"
165
- });
166
- };
167
- AdaptContents.shouldForwardSpace = !0;
168
- const Adapt = (0, import_helpers.withStaticProperties)(function (props) {
169
- const {
170
- platform,
171
- when,
172
- children,
173
- scope
174
- } = props,
175
- context = useAdaptContext(scope),
176
- enabled = useAdaptIsActiveGiven(props);
177
- (0, import_constants.useIsomorphicLayoutEffect)(() => {
178
- context?.setWhen?.(when || enabled), context?.setPlatform?.(platform || null);
179
- }, [when, platform, enabled, context.setWhen, context.setPlatform]), (0, import_constants.useIsomorphicLayoutEffect)(() => () => {
180
- context?.setWhen?.(null), context?.setPlatform?.(null);
181
- }, []);
182
- let output;
183
- if (typeof children == "function") {
184
- const Component = context?.Contents;
185
- output = children(Component ? /* @__PURE__ */(0, import_jsx_runtime.jsx)(Component, {}) : null);
186
- } else output = children;
187
- return (0, import_constants.useIsomorphicLayoutEffect)(() => {
188
- typeof children == "function" && output !== void 0 && context?.setChildren(output);
189
- }, [output]), /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_z_index_stack.StackZIndexContext, {
190
- children: enabled ? output : null
191
- });
192
- }, {
193
- Contents: AdaptContents
194
- }),
195
- AdaptPortalContents = props => {
196
- const isActive = useAdaptIsActive(props.scope),
197
- {
198
- portalName
199
- } = useAdaptContext(props.scope),
200
- childrenStore = (0, import_react.useContext)(AdaptChildrenStoreContext);
201
- return !import_constants.isWeb && (0, import_native.getPortal)().state.type === "teleport" && childrenStore ? /* @__PURE__ */(0, import_jsx_runtime.jsx)(AdaptPortalTeleport, {
202
- isActive,
203
- store: childrenStore,
204
- children: props.children
205
- }) : /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_portal.PortalItem, {
206
- passThrough: !isActive,
207
- hostName: portalName,
208
- children: props.children
209
- });
210
- };
167
+ scope
168
+ } = props;
169
+ const context = useAdaptContext(scope);
170
+ const enabled = useAdaptIsActiveGiven(props);
171
+ (0, import_constants.useIsomorphicLayoutEffect)(() => {
172
+ context?.setWhen?.(when || enabled);
173
+ context?.setPlatform?.(platform || null);
174
+ }, [when, platform, enabled, context.setWhen, context.setPlatform]);
175
+ (0, import_constants.useIsomorphicLayoutEffect)(() => {
176
+ return () => {
177
+ context?.setWhen?.(null);
178
+ context?.setPlatform?.(null);
179
+ };
180
+ }, []);
181
+ let output;
182
+ if (typeof children === "function") {
183
+ const Component = context?.Contents;
184
+ output = children(Component ? /* @__PURE__ */(0, import_jsx_runtime.jsx)(Component, {}) : null);
185
+ } else output = children;
186
+ return /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_z_index_stack.StackZIndexContext, {
187
+ children: !enabled ? null : output
188
+ });
189
+ }, {
190
+ Contents: AdaptContents
191
+ });
192
+ const AdaptPortalContents = props => {
193
+ const isActive = useAdaptIsActive(props.scope);
194
+ const {
195
+ portalName
196
+ } = useAdaptContext(props.scope);
197
+ const childrenStore = (0, import_react.useContext)(AdaptChildrenStoreContext);
198
+ if (!import_constants.isWeb && (0, import_native.getPortal)().state.type === "teleport" && childrenStore) return /* @__PURE__ */(0, import_jsx_runtime.jsx)(AdaptPortalTeleport, {
199
+ isActive,
200
+ store: childrenStore,
201
+ children: props.children
202
+ });
203
+ return /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_portal.PortalItem, {
204
+ passThrough: !isActive,
205
+ hostName: portalName,
206
+ children: props.children
207
+ });
208
+ };
211
209
  function AdaptPortalTeleport({
212
210
  isActive,
213
211
  store,
214
212
  children
215
213
  }) {
216
- return (0, import_constants.useIsomorphicLayoutEffect)(() => {
217
- if (isActive) return store.set(children), () => store.set(null);
218
- }), isActive ? null : /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, {
214
+ (0, import_constants.useIsomorphicLayoutEffect)(() => {
215
+ if (isActive) store.set(children);else store.set(null);
216
+ });
217
+ (0, import_constants.useIsomorphicLayoutEffect)(() => {
218
+ return () => store.set(null);
219
+ }, [store]);
220
+ return isActive ? null : /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, {
219
221
  children
220
222
  });
221
223
  }
222
224
  const useAdaptIsActiveGiven = ({
223
- when,
224
- platform
225
- }) => {
226
- const media = (0, import_core.useMedia)();
227
- if (when == null && platform == null) return !1;
228
- if (when === !0) return !0;
229
- let enabled = !1;
230
- return platform === "touch" ? enabled = import_constants.isTouchable : platform === "native" ? enabled = !import_constants.isWeb : platform === "web" ? enabled = import_constants.isWeb : platform === "ios" ? enabled = import_constants.isIos : platform === "android" && (enabled = import_constants.isAndroid), platform && enabled == !1 ? !1 : (when && typeof when == "string" && (enabled = media[when]), enabled);
231
- },
232
- useAdaptIsActive = scope => {
233
- const props = useAdaptContext(scope);
234
- return useAdaptIsActiveGiven(props);
235
- };
225
+ when,
226
+ platform
227
+ }) => {
228
+ const media = (0, import_core.useMedia)();
229
+ if (when == null && platform == null) return false;
230
+ if (when === true) return true;
231
+ let enabled = false;
232
+ if (platform === "touch") enabled = import_constants.isTouchable;else if (platform === "native") enabled = !import_constants.isWeb;else if (platform === "web") enabled = import_constants.isWeb;else if (platform === "ios") enabled = import_constants.isIos;else if (platform === "android") enabled = import_constants.isAndroid;
233
+ if (platform && enabled == false) return false;
234
+ if (when && typeof when === "string") enabled = media[when];
235
+ return enabled;
236
+ };
237
+ const useAdaptIsActive = scope => {
238
+ return useAdaptIsActiveGiven(useAdaptContext(scope));
239
+ };