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