@tamagui/adapt 2.7.7 → 3.0.0-beta.1093.1

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.
@@ -1,196 +1,456 @@
1
1
  import { isAndroid, isIos, isTouchable, isWeb, useIsomorphicLayoutEffect } from "@tamagui/constants";
2
2
  import { createStyledContext, useMedia } from "@tamagui/core";
3
3
  import { withStaticProperties } from "@tamagui/helpers";
4
- import { getPortal } from "@tamagui/native";
5
- import { PortalHost, PortalItem } from "@tamagui/portal";
6
4
  import { StackZIndexContext } from "@tamagui/z-index-stack";
7
- import React, { createContext, useContext, useId, useMemo } from "react";
5
+ import React, { createContext, useContext, useId } from "react";
8
6
  import { Fragment, jsx } from "react/jsx-runtime";
9
- function createAdaptChildrenStore() {
10
- let children = null;
11
- const listeners = /* @__PURE__ */new Set();
12
- return {
13
- set(c) {
14
- children = c;
15
- for (const l of listeners) l();
16
- },
17
- get: () => children,
18
- subscribe(callback) {
19
- listeners.add(callback);
20
- return () => listeners.delete(callback);
21
- }
22
- };
7
+
8
+ function createAdaptSlotStore() {
9
+ const listeners = /* @__PURE__ */ new Set();
10
+ const store = {
11
+ element: null,
12
+ version: 0,
13
+ publish(element) {
14
+ store.element = element;
15
+ },
16
+ clear() {
17
+ store.element = null;
18
+ },
19
+ notify() {
20
+ store.version += 1;
21
+ for (const listener of listeners) {
22
+ listener();
23
+ }
24
+ },
25
+ getSnapshot() {
26
+ return store.version;
27
+ },
28
+ subscribe(callback) {
29
+ listeners.add(callback);
30
+ return () => {
31
+ listeners.delete(callback);
32
+ };
33
+ }
34
+ };
35
+ return store;
23
36
  }
24
- const AdaptChildrenStoreContext = createContext(null);
37
+ const adaptContextKeys = [
38
+ "Contents",
39
+ "scopeName",
40
+ "portalName",
41
+ "active",
42
+ "setAdaptConfig",
43
+ "slot",
44
+ "handoff",
45
+ "targetFullyHidden",
46
+ "registerTarget",
47
+ "unregisterTarget",
48
+ "registerContents",
49
+ "unregisterContents",
50
+ "registerRenderCallback",
51
+ "unregisterRenderCallback"
52
+ ];
25
53
  const AdaptContext = createStyledContext({
26
- Contents: null,
27
- scopeName: "",
28
- portalName: "",
29
- platform: null,
30
- setPlatform: x => {},
31
- when: null,
32
- setWhen: () => {}
33
- });
54
+ Contents: null,
55
+ scopeName: "",
56
+ portalName: "",
57
+ active: false,
58
+ setAdaptConfig: () => {},
59
+ slot: null,
60
+ handoff: {
61
+ hidden: true,
62
+ onTransition: () => {}
63
+ },
64
+ targetFullyHidden: true,
65
+ registerTarget: () => {},
66
+ unregisterTarget: () => {},
67
+ registerContents: () => {},
68
+ unregisterContents: () => {},
69
+ registerRenderCallback: () => {},
70
+ unregisterRenderCallback: () => {}
71
+ }, { keys: adaptContextKeys });
72
+ const AdaptCapabilitiesContext = createContext({});
34
73
  const LastAdaptContextScope = createContext("");
35
- const ProvideAdaptContext = ({
36
- children,
37
- ...context
38
- }) => {
39
- const scope = context.scopeName || "";
40
- const lastScope = useContext(LastAdaptContextScope);
41
- return /* @__PURE__ */jsx(LastAdaptContextScope.Provider, {
42
- value: lastScope || context.lastScope || "",
43
- children: /* @__PURE__ */jsx(AdaptContext.Provider, {
44
- scope,
45
- lastScope: lastScope || context.lastScope,
46
- ...context,
47
- children
48
- })
49
- });
74
+ const ProvideAdaptContext = ({ children, ...context }) => {
75
+ const scope = context.scopeName || "";
76
+ const lastScope = useContext(LastAdaptContextScope);
77
+ return /* @__PURE__ */ jsx(LastAdaptContextScope.Provider, {
78
+ value: lastScope || context.lastScope || "",
79
+ children: /* @__PURE__ */ jsx(AdaptContext.Provider, {
80
+ scope,
81
+ lastScope: lastScope || context.lastScope,
82
+ ...context,
83
+ children
84
+ })
85
+ });
50
86
  };
51
- const useAdaptContext = scope => {
52
- const lastScope = useContext(LastAdaptContextScope);
53
- const adaptScope = scope ?? lastScope;
54
- return AdaptContext.useStyledContext(adaptScope);
87
+ const useAdaptContext = (scope) => {
88
+ const lastScope = useContext(LastAdaptContextScope);
89
+ const adaptScope = scope ?? lastScope;
90
+ return AdaptContext.useStyledContext(adaptScope);
55
91
  };
56
- const AdaptPortals = /* @__PURE__ */new Map();
57
- const AdaptParent = ({
58
- children,
59
- Contents,
60
- scope,
61
- portal
62
- }) => {
63
- const id = useId();
64
- const portalName = `AdaptPortal${scope}${id}`;
65
- const childrenStoreRef = React.useRef(null);
66
- if (!childrenStoreRef.current) childrenStoreRef.current = createAdaptChildrenStore();
67
- const isTeleport = false;
68
- const FinalContents = useMemo(() => {
69
- if (Contents) return Contents;
70
- if (AdaptPortals.has(portalName)) return AdaptPortals.get(portalName);
71
- const element = () => {
72
- return /* @__PURE__ */jsx(PortalHost, {
73
- name: portalName,
74
- forwardProps: typeof portal === "boolean" ? void 0 : portal?.forwardProps
75
- }, id);
76
- };
77
- AdaptPortals.set(portalName, element);
78
- return element;
79
- }, [portalName, Contents, isTeleport]);
80
- useIsomorphicLayoutEffect(() => {
81
- AdaptPortals.set(portalName, FinalContents);
82
- return () => {
83
- AdaptPortals.delete(portalName);
84
- };
85
- }, [portalName, isTeleport]);
86
- const [when, setWhen] = React.useState(null);
87
- const [platform, setPlatform] = React.useState(null);
88
- return /* @__PURE__ */jsx(AdaptChildrenStoreContext.Provider, {
89
- value: childrenStoreRef.current,
90
- children: /* @__PURE__ */jsx(LastAdaptContextScope.Provider, {
91
- value: scope,
92
- children: /* @__PURE__ */jsx(ProvideAdaptContext, {
93
- Contents: FinalContents,
94
- when,
95
- platform,
96
- setPlatform,
97
- setWhen,
98
- portalName,
99
- scopeName: scope,
100
- children
101
- })
102
- })
103
- });
92
+ function findAdaptConfig(children, scope) {
93
+ for (const child of React.Children.toArray(children)) {
94
+ if (!React.isValidElement(child)) continue;
95
+ if (child.type === React.Fragment) {
96
+ const found = findAdaptConfig(child.props.children, scope);
97
+ if (found) return found;
98
+ continue;
99
+ }
100
+ if (child.type === Adapt) {
101
+ const childProps = child.props;
102
+ if (childProps.scope == null || childProps.scope === scope) {
103
+ return childProps;
104
+ }
105
+ }
106
+ }
107
+ return null;
108
+ }
109
+ const AdaptParent = ({ children, adaptChildren, Contents, scope, open, onOpenChange, state }) => {
110
+ const id = useId();
111
+ const portalName = `AdaptPortal${scope}${id}`;
112
+ const slotRef = React.useRef(null);
113
+ if (!slotRef.current) {
114
+ slotRef.current = createAdaptSlotStore();
115
+ }
116
+ const staticConfig = React.useMemo(() => findAdaptConfig(adaptChildren, scope) ?? findAdaptConfig(children, scope), [
117
+ adaptChildren,
118
+ children,
119
+ scope
120
+ ]);
121
+ const [publishedConfig, setPublishedConfig] = React.useState(null);
122
+ const rawActive = useAdaptIsActiveGiven(staticConfig ?? publishedConfig);
123
+ const staticConfigRef = React.useRef(staticConfig);
124
+ staticConfigRef.current = staticConfig;
125
+ const setAdaptConfig = React.useCallback((config) => {
126
+ if (staticConfigRef.current) return;
127
+ setPublishedConfig(config);
128
+ }, []);
129
+ const [exiting, setExiting] = React.useState(false);
130
+ const [present, setPresent] = React.useState(false);
131
+ const [targetFullyHidden, setTargetFullyHidden] = React.useState(!open);
132
+ const targetCountRef = React.useRef(0);
133
+ const contentsCountRef = React.useRef(0);
134
+ const renderCallbackCountRef = React.useRef(0);
135
+ const rawActiveRef = React.useRef(false);
136
+ const openRef = React.useRef(open);
137
+ const wasTargetHiddenRef = React.useRef(!rawActive);
138
+ const hasHadActiveTargetRef = React.useRef(false);
139
+ const shouldStartExit = !rawActive && present && Boolean(open);
140
+ const active = rawActive || exiting || shouldStartExit;
141
+ const targetHidden = !rawActive;
142
+ const skipNextAnimation = Boolean(rawActive && wasTargetHiddenRef.current && hasHadActiveTargetRef.current && open);
143
+ const releasePresenceLatch = React.useCallback(() => {
144
+ setExiting(false);
145
+ if (!rawActiveRef.current) {
146
+ setPresent(false);
147
+ }
148
+ }, []);
149
+ useIsomorphicLayoutEffect(() => {
150
+ if (shouldStartExit) {
151
+ setExiting(true);
152
+ }
153
+ }, [shouldStartExit]);
154
+ useIsomorphicLayoutEffect(() => {
155
+ rawActiveRef.current = rawActive;
156
+ openRef.current = open;
157
+ if (rawActive) {
158
+ hasHadActiveTargetRef.current = true;
159
+ }
160
+ wasTargetHiddenRef.current = targetHidden;
161
+ if (open && rawActive) {
162
+ setTargetFullyHidden(false);
163
+ return;
164
+ }
165
+ if (!open && !active) {
166
+ setTargetFullyHidden(true);
167
+ }
168
+ }, [
169
+ active,
170
+ open,
171
+ rawActive,
172
+ targetHidden
173
+ ]);
174
+ useIsomorphicLayoutEffect(() => {
175
+ if (rawActive) {
176
+ setPresent(true);
177
+ } else if (!active) {
178
+ setPresent(false);
179
+ }
180
+ if (rawActive && exiting) {
181
+ setExiting(false);
182
+ }
183
+ }, [
184
+ active,
185
+ exiting,
186
+ rawActive
187
+ ]);
188
+ const handoff = React.useMemo(() => ({
189
+ hidden: targetHidden,
190
+ skipNextAnimation,
191
+ onTransition(e) {
192
+ if (e.phase !== "end" || e.finished === false) return;
193
+ if (e.cause !== "close") {
194
+ setTargetFullyHidden(false);
195
+ return;
196
+ }
197
+ if (openRef.current && rawActiveRef.current) {
198
+ setTargetFullyHidden(false);
199
+ return;
200
+ }
201
+ setTargetFullyHidden(true);
202
+ releasePresenceLatch();
203
+ }
204
+ }), [
205
+ releasePresenceLatch,
206
+ skipNextAnimation,
207
+ targetHidden
208
+ ]);
209
+ const registerTarget = React.useCallback(() => {
210
+ targetCountRef.current += 1;
211
+ if (process.env.NODE_ENV === "development" && targetCountRef.current > 1) {
212
+ console.error(`Adapt expected exactly one target in scope "${scope}", but ${targetCountRef.current} targets registered.`);
213
+ }
214
+ }, [scope]);
215
+ const unregisterTarget = React.useCallback(() => {
216
+ targetCountRef.current = Math.max(0, targetCountRef.current - 1);
217
+ }, []);
218
+ const registerContents = React.useCallback(() => {
219
+ contentsCountRef.current += 1;
220
+ }, []);
221
+ const unregisterContents = React.useCallback(() => {
222
+ contentsCountRef.current = Math.max(0, contentsCountRef.current - 1);
223
+ }, []);
224
+ const registerRenderCallback = React.useCallback(() => {
225
+ renderCallbackCountRef.current += 1;
226
+ }, []);
227
+ const unregisterRenderCallback = React.useCallback(() => {
228
+ renderCallbackCountRef.current = Math.max(0, renderCallbackCountRef.current - 1);
229
+ }, []);
230
+ React.useEffect(() => {
231
+ if (process.env.NODE_ENV !== "development") return;
232
+ if (!active) return;
233
+ const timer = setTimeout(() => {
234
+ if (rawActiveRef.current && targetCountRef.current === 0 && contentsCountRef.current === 0 && renderCallbackCountRef.current === 0) {
235
+ console.error(`Adapt is active in scope "${scope}" but no target registered with useAdaptTarget(), Adapt.Contents marker, or render callback consumed it.`);
236
+ }
237
+ });
238
+ return () => clearTimeout(timer);
239
+ }, [active, scope]);
240
+ const FinalContents = Contents || AdaptSlotContents;
241
+ return /* @__PURE__ */ jsx(LastAdaptContextScope.Provider, {
242
+ value: scope,
243
+ children: /* @__PURE__ */ jsx(ProvideAdaptContext, {
244
+ Contents: FinalContents,
245
+ active,
246
+ setAdaptConfig,
247
+ portalName,
248
+ scopeName: scope,
249
+ slot: slotRef.current,
250
+ open,
251
+ onOpenChange,
252
+ state,
253
+ handoff,
254
+ targetFullyHidden,
255
+ registerTarget,
256
+ unregisterTarget,
257
+ registerContents,
258
+ unregisterContents,
259
+ registerRenderCallback,
260
+ unregisterRenderCallback,
261
+ children
262
+ })
263
+ });
104
264
  };
105
- const AdaptContents = ({
106
- scope,
107
- ...rest
108
- }) => {
109
- const context = useAdaptContext(scope);
110
- 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.`);
111
- return React.createElement(context.Contents, {
112
- ...rest,
113
- key: `stable`
114
- });
265
+ const AdaptContents = ({ scope, ...rest }) => {
266
+ const context = useAdaptContext(scope);
267
+ if (!context?.Contents) {
268
+ 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.`);
269
+ }
270
+ useIsomorphicLayoutEffect(() => {
271
+ if (!context.active) return;
272
+ context.registerContents();
273
+ return () => {
274
+ context.unregisterContents();
275
+ };
276
+ }, [
277
+ context.active,
278
+ context.registerContents,
279
+ context.unregisterContents
280
+ ]);
281
+ return React.createElement(context.Contents, {
282
+ ...rest,
283
+ scope,
284
+ key: `stable`
285
+ });
115
286
  };
116
287
  AdaptContents.shouldForwardSpace = true;
117
288
  const Adapt = withStaticProperties(function Adapt2(props) {
118
- const {
119
- platform,
120
- when,
121
- children,
122
- scope
123
- } = props;
124
- const context = useAdaptContext(scope);
125
- const enabled = useAdaptIsActiveGiven(props);
126
- useIsomorphicLayoutEffect(() => {
127
- context?.setWhen?.(when || enabled);
128
- context?.setPlatform?.(platform || null);
129
- }, [when, platform, enabled, context.setWhen, context.setPlatform]);
130
- useIsomorphicLayoutEffect(() => {
131
- return () => {
132
- context?.setWhen?.(null);
133
- context?.setPlatform?.(null);
134
- };
135
- }, []);
136
- let output;
137
- if (typeof children === "function") {
138
- const Component = context?.Contents;
139
- output = children(Component ? /* @__PURE__ */jsx(Component, {}) : null);
140
- } else output = children;
141
- return /* @__PURE__ */jsx(StackZIndexContext, {
142
- children: !enabled ? null : output
143
- });
144
- }, {
145
- Contents: AdaptContents
146
- });
147
- const AdaptPortalContents = props => {
148
- const isActive = useAdaptIsActive(props.scope);
149
- const {
150
- portalName
151
- } = useAdaptContext(props.scope);
152
- const childrenStore = useContext(AdaptChildrenStoreContext);
153
- if (!isWeb && getPortal().state.type === "teleport" && childrenStore) return /* @__PURE__ */jsx(AdaptPortalTeleport, {
154
- isActive,
155
- store: childrenStore,
156
- children: props.children
157
- });
158
- return /* @__PURE__ */jsx(PortalItem, {
159
- passThrough: !isActive,
160
- hostName: portalName,
161
- children: props.children
162
- });
289
+ const { children, platform, scope, when } = props;
290
+ const context = useAdaptContext(scope);
291
+ const enabled = context.active;
292
+ const isRenderCallback = typeof children === "function";
293
+ useIsomorphicLayoutEffect(() => {
294
+ context.setAdaptConfig({
295
+ when,
296
+ platform
297
+ });
298
+ return () => {
299
+ context.setAdaptConfig(null);
300
+ };
301
+ }, [
302
+ when,
303
+ platform,
304
+ context.setAdaptConfig
305
+ ]);
306
+ useIsomorphicLayoutEffect(() => {
307
+ if (!enabled || !isRenderCallback) return;
308
+ context.registerRenderCallback();
309
+ return () => {
310
+ context.unregisterRenderCallback();
311
+ };
312
+ }, [
313
+ enabled,
314
+ isRenderCallback,
315
+ context.registerRenderCallback,
316
+ context.unregisterRenderCallback
317
+ ]);
318
+ let output;
319
+ if (isRenderCallback) {
320
+ const Component = context?.Contents;
321
+ output = children(Component ? /* @__PURE__ */ jsx(Component, {}) : null, getAdaptRenderState(context));
322
+ } else {
323
+ output = children;
324
+ }
325
+ return /* @__PURE__ */ jsx(StackZIndexContext, { children: !enabled ? null : output });
326
+ }, { Contents: AdaptContents });
327
+ const AdaptPortalContents = (props) => {
328
+ const isActive = useAdaptIsActive(props.scope);
329
+ const { slot } = useAdaptContext(props.scope);
330
+ return /* @__PURE__ */ jsx(AdaptSlotPublisher, {
331
+ isActive,
332
+ slot,
333
+ children: props.children
334
+ });
335
+ };
336
+ function AdaptSlotContents({ scope }) {
337
+ const { slot } = useAdaptContext(scope);
338
+ React.useSyncExternalStore(slot?.subscribe ?? emptySubscribe, slot?.getSnapshot ?? emptySnapshot, slot?.getSnapshot ?? emptySnapshot);
339
+ return /* @__PURE__ */ jsx(Fragment, { children: slot?.element ?? null });
340
+ }
341
+ function AdaptSlotPublisher({ isActive, slot, children }) {
342
+ const publishedRef = React.useRef(null);
343
+ useIsomorphicLayoutEffect(() => {
344
+ if (!slot) return;
345
+ const published = publishedRef.current;
346
+ if (published && published.isActive === isActive && published.element === children) {
347
+ return;
348
+ }
349
+ publishedRef.current = {
350
+ isActive,
351
+ element: children
352
+ };
353
+ if (isActive) {
354
+ slot.publish(children);
355
+ } else {
356
+ slot.clear();
357
+ }
358
+ slot.notify();
359
+ });
360
+ useIsomorphicLayoutEffect(() => {
361
+ return () => {
362
+ if (publishedRef.current?.isActive) {
363
+ slot?.clear();
364
+ slot?.notify();
365
+ }
366
+ publishedRef.current = null;
367
+ };
368
+ }, [slot]);
369
+ return isActive ? null : /* @__PURE__ */ jsx(Fragment, { children });
370
+ }
371
+ const emptySubscribe = () => () => {};
372
+ const emptySnapshot = () => 0;
373
+ const useAdaptIsActiveGiven = (config) => {
374
+ const media = useMedia();
375
+ const when = config?.when;
376
+ const platform = config?.platform;
377
+ if (when == null && platform == null) {
378
+ return false;
379
+ }
380
+ if (when === true) {
381
+ return true;
382
+ }
383
+ let enabled = false;
384
+ if (platform === "touch") enabled = isTouchable;
385
+ else if (platform === "native") enabled = !isWeb;
386
+ else if (platform === "web") enabled = isWeb;
387
+ else if (platform === "ios") enabled = isIos;
388
+ else if (platform === "android") enabled = isAndroid;
389
+ if (platform && enabled == false) {
390
+ return false;
391
+ }
392
+ if (when && typeof when === "string") {
393
+ enabled = media[when];
394
+ }
395
+ return enabled;
396
+ };
397
+ const useAdaptIsActive = (scope) => {
398
+ return useAdaptContext(scope).active;
163
399
  };
164
- function AdaptPortalTeleport({
165
- isActive,
166
- store,
167
- children
168
- }) {
169
- useIsomorphicLayoutEffect(() => {
170
- if (isActive) store.set(children);else store.set(null);
171
- });
172
- useIsomorphicLayoutEffect(() => {
173
- return () => store.set(null);
174
- }, [store]);
175
- return isActive ? null : /* @__PURE__ */jsx(Fragment, {
176
- children
177
- });
400
+ function useAdaptTarget(scope) {
401
+ const context = useAdaptContext(scope);
402
+ useIsomorphicLayoutEffect(() => {
403
+ if (!context.active) return;
404
+ context.registerTarget();
405
+ return () => {
406
+ context.unregisterTarget();
407
+ };
408
+ }, [
409
+ context.active,
410
+ context.registerTarget,
411
+ context.unregisterTarget
412
+ ]);
413
+ if (!context.active) {
414
+ return null;
415
+ }
416
+ return {
417
+ open: context.open,
418
+ onOpenChange: context.onOpenChange,
419
+ handoff: context.handoff,
420
+ state: context.state
421
+ };
178
422
  }
179
- const useAdaptIsActiveGiven = ({
180
- when,
181
- platform
182
- }) => {
183
- const media = useMedia();
184
- if (when == null && platform == null) return false;
185
- if (when === true) return true;
186
- let enabled = false;
187
- if (platform === "touch") enabled = isTouchable;else if (platform === "native") enabled = !isWeb;else if (platform === "web") enabled = isWeb;else if (platform === "ios") enabled = isIos;else if (platform === "android") enabled = isAndroid;
188
- if (platform && enabled == false) return false;
189
- if (when && typeof when === "string") enabled = media[when];
190
- return enabled;
423
+ const AdaptCapabilities = ({ children, scroll, overlay, dismiss }) => {
424
+ const parent = useContext(AdaptCapabilitiesContext);
425
+ const value = React.useMemo(() => ({
426
+ scroll: scroll ?? parent.scroll,
427
+ overlay: overlay ?? parent.overlay,
428
+ dismiss: dismiss ?? parent.dismiss
429
+ }), [
430
+ dismiss,
431
+ overlay,
432
+ parent.dismiss,
433
+ parent.overlay,
434
+ parent.scroll,
435
+ scroll
436
+ ]);
437
+ return /* @__PURE__ */ jsx(AdaptCapabilitiesContext.Provider, {
438
+ value,
439
+ children
440
+ });
191
441
  };
192
- const useAdaptIsActive = scope => {
193
- return useAdaptIsActiveGiven(useAdaptContext(scope));
442
+ const useAdaptedCapabilities = () => {
443
+ return useContext(AdaptCapabilitiesContext);
194
444
  };
195
- export { Adapt, AdaptContents, AdaptContext, AdaptParent, AdaptPortalContents, ProvideAdaptContext, useAdaptContext, useAdaptIsActive };
445
+ function getAdaptRenderState(context) {
446
+ return {
447
+ active: context.active,
448
+ open: context.open,
449
+ onOpenChange: context.onOpenChange,
450
+ state: context.state,
451
+ handoff: context.handoff
452
+ };
453
+ }
454
+
455
+ export { Adapt, AdaptCapabilities, AdaptContents, AdaptContext, AdaptParent, AdaptPortalContents, ProvideAdaptContext, useAdaptContext, useAdaptIsActive, useAdaptTarget, useAdaptedCapabilities };
196
456
  //# sourceMappingURL=Adapt.mjs.map