@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.
@@ -0,0 +1,551 @@
1
+ "use strict";
2
+
3
+ var __create = Object.create;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getProtoOf = Object.getPrototypeOf;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __export = (target, all) => {
10
+ for (var name in all) __defProp(target, name, {
11
+ get: all[name],
12
+ enumerable: true
13
+ });
14
+ };
15
+ var __copyProps = (to, from, except, desc) => {
16
+ if (from && typeof from === "object" || typeof from === "function") {
17
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
18
+ get: () => from[key],
19
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
20
+ });
21
+ }
22
+ return to;
23
+ };
24
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
25
+ value: mod,
26
+ enumerable: true
27
+ }) : target, mod));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+ var Adapt_exports = {};
30
+ __export(Adapt_exports, {
31
+ Adapt: () => Adapt,
32
+ AdaptCapabilities: () => AdaptCapabilities,
33
+ AdaptContents: () => AdaptContents,
34
+ AdaptContext: () => AdaptContext,
35
+ AdaptParent: () => AdaptParent,
36
+ AdaptPortalContents: () => AdaptPortalContents,
37
+ ProvideAdaptContext: () => ProvideAdaptContext,
38
+ useAdaptContext: () => useAdaptContext,
39
+ useAdaptIsActive: () => useAdaptIsActive,
40
+ useAdaptTarget: () => useAdaptTarget,
41
+ useAdaptedCapabilities: () => useAdaptedCapabilities
42
+ });
43
+ module.exports = __toCommonJS(Adapt_exports);
44
+ var import_jsx_runtime = require("react/jsx-runtime");
45
+ var import_constants = require("@tamagui/constants");
46
+ var import_core = require("@tamagui/core");
47
+ var import_helpers = require("@tamagui/helpers");
48
+ var import_z_index_stack = require("@tamagui/z-index-stack");
49
+ var import_react = __toESM(require("react"), 1);
50
+ function createAdaptSlotStore() {
51
+ var listeners = /* @__PURE__ */ new Set();
52
+ var store = {
53
+ element: null,
54
+ version: 0,
55
+ publish(element) {
56
+ store.element = element;
57
+ },
58
+ clear() {
59
+ store.element = null;
60
+ },
61
+ notify() {
62
+ store.version += 1;
63
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
64
+ try {
65
+ for (var _iterator = listeners[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
66
+ var listener = _step.value;
67
+ listener();
68
+ }
69
+ } catch (err) {
70
+ _didIteratorError = true;
71
+ _iteratorError = err;
72
+ } finally {
73
+ try {
74
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
75
+ _iterator.return();
76
+ }
77
+ } finally {
78
+ if (_didIteratorError) {
79
+ throw _iteratorError;
80
+ }
81
+ }
82
+ }
83
+ },
84
+ getSnapshot() {
85
+ return store.version;
86
+ },
87
+ subscribe(callback) {
88
+ listeners.add(callback);
89
+ return function() {
90
+ listeners.delete(callback);
91
+ };
92
+ }
93
+ };
94
+ return store;
95
+ }
96
+ var adaptContextKeys = [
97
+ "Contents",
98
+ "scopeName",
99
+ "portalName",
100
+ "active",
101
+ "setAdaptConfig",
102
+ "slot",
103
+ "handoff",
104
+ "targetFullyHidden",
105
+ "registerTarget",
106
+ "unregisterTarget",
107
+ "registerContents",
108
+ "unregisterContents",
109
+ "registerRenderCallback",
110
+ "unregisterRenderCallback"
111
+ ];
112
+ var AdaptContext = (0, import_core.createStyledContext)({
113
+ Contents: null,
114
+ scopeName: "",
115
+ portalName: "",
116
+ active: false,
117
+ setAdaptConfig: function() {},
118
+ slot: null,
119
+ handoff: {
120
+ hidden: true,
121
+ onTransition: function() {}
122
+ },
123
+ targetFullyHidden: true,
124
+ registerTarget: function() {},
125
+ unregisterTarget: function() {},
126
+ registerContents: function() {},
127
+ unregisterContents: function() {},
128
+ registerRenderCallback: function() {},
129
+ unregisterRenderCallback: function() {}
130
+ }, { keys: adaptContextKeys });
131
+ var AdaptCapabilitiesContext = /* @__PURE__ */ (0, import_react.createContext)({});
132
+ var LastAdaptContextScope = /* @__PURE__ */ (0, import_react.createContext)("");
133
+ var ProvideAdaptContext = function(param) {
134
+ var { children, ...context } = param;
135
+ var scope = context.scopeName || "";
136
+ var lastScope = (0, import_react.useContext)(LastAdaptContextScope);
137
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(LastAdaptContextScope.Provider, {
138
+ value: lastScope || context.lastScope || "",
139
+ children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AdaptContext.Provider, {
140
+ scope,
141
+ lastScope: lastScope || context.lastScope,
142
+ ...context,
143
+ children
144
+ })
145
+ });
146
+ };
147
+ var useAdaptContext = function(scope) {
148
+ var lastScope = (0, import_react.useContext)(LastAdaptContextScope);
149
+ var adaptScope = scope !== null && scope !== void 0 ? scope : lastScope;
150
+ return AdaptContext.useStyledContext(adaptScope);
151
+ };
152
+ function findAdaptConfig(children, scope) {
153
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = void 0;
154
+ try {
155
+ for (var _iterator = import_react.default.Children.toArray(children)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
156
+ var child = _step.value;
157
+ if (!/* @__PURE__ */ import_react.default.isValidElement(child)) continue;
158
+ if (child.type === import_react.default.Fragment) {
159
+ var found = findAdaptConfig(child.props.children, scope);
160
+ if (found) return found;
161
+ continue;
162
+ }
163
+ if (child.type === Adapt) {
164
+ var childProps = child.props;
165
+ if (childProps.scope == null || childProps.scope === scope) {
166
+ return childProps;
167
+ }
168
+ }
169
+ }
170
+ } catch (err) {
171
+ _didIteratorError = true;
172
+ _iteratorError = err;
173
+ } finally {
174
+ try {
175
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
176
+ _iterator.return();
177
+ }
178
+ } finally {
179
+ if (_didIteratorError) {
180
+ throw _iteratorError;
181
+ }
182
+ }
183
+ }
184
+ return null;
185
+ }
186
+ var AdaptParent = function(param) {
187
+ var { children, adaptChildren, Contents, scope, open, onOpenChange, state } = param;
188
+ var id = (0, import_react.useId)();
189
+ var portalName = `AdaptPortal${scope}${id}`;
190
+ var slotRef = import_react.default.useRef(null);
191
+ if (!slotRef.current) {
192
+ slotRef.current = createAdaptSlotStore();
193
+ }
194
+ var staticConfig = import_react.default.useMemo(function() {
195
+ var _findAdaptConfig;
196
+ return (_findAdaptConfig = findAdaptConfig(adaptChildren, scope)) !== null && _findAdaptConfig !== void 0 ? _findAdaptConfig : findAdaptConfig(children, scope);
197
+ }, [
198
+ adaptChildren,
199
+ children,
200
+ scope
201
+ ]);
202
+ var [publishedConfig, setPublishedConfig] = import_react.default.useState(null);
203
+ var rawActive = useAdaptIsActiveGiven(staticConfig !== null && staticConfig !== void 0 ? staticConfig : publishedConfig);
204
+ var staticConfigRef = import_react.default.useRef(staticConfig);
205
+ staticConfigRef.current = staticConfig;
206
+ var setAdaptConfig = import_react.default.useCallback(function(config) {
207
+ if (staticConfigRef.current) return;
208
+ setPublishedConfig(config);
209
+ }, []);
210
+ var [exiting, setExiting] = import_react.default.useState(false);
211
+ var [present, setPresent] = import_react.default.useState(false);
212
+ var [targetFullyHidden, setTargetFullyHidden] = import_react.default.useState(!open);
213
+ var targetCountRef = import_react.default.useRef(0);
214
+ var contentsCountRef = import_react.default.useRef(0);
215
+ var renderCallbackCountRef = import_react.default.useRef(0);
216
+ var rawActiveRef = import_react.default.useRef(false);
217
+ var openRef = import_react.default.useRef(open);
218
+ var wasTargetHiddenRef = import_react.default.useRef(!rawActive);
219
+ var hasHadActiveTargetRef = import_react.default.useRef(false);
220
+ var shouldStartExit = !rawActive && present && Boolean(open);
221
+ var active = rawActive || exiting || shouldStartExit;
222
+ var targetHidden = !rawActive;
223
+ var skipNextAnimation = Boolean(rawActive && wasTargetHiddenRef.current && hasHadActiveTargetRef.current && open);
224
+ var releasePresenceLatch = import_react.default.useCallback(function() {
225
+ setExiting(false);
226
+ if (!rawActiveRef.current) {
227
+ setPresent(false);
228
+ }
229
+ }, []);
230
+ (0, import_constants.useIsomorphicLayoutEffect)(function() {
231
+ if (shouldStartExit) {
232
+ setExiting(true);
233
+ }
234
+ }, [shouldStartExit]);
235
+ (0, import_constants.useIsomorphicLayoutEffect)(function() {
236
+ rawActiveRef.current = rawActive;
237
+ openRef.current = open;
238
+ if (rawActive) {
239
+ hasHadActiveTargetRef.current = true;
240
+ }
241
+ wasTargetHiddenRef.current = targetHidden;
242
+ if (open && rawActive) {
243
+ setTargetFullyHidden(false);
244
+ return;
245
+ }
246
+ if (!open && !active) {
247
+ setTargetFullyHidden(true);
248
+ }
249
+ }, [
250
+ active,
251
+ open,
252
+ rawActive,
253
+ targetHidden
254
+ ]);
255
+ (0, import_constants.useIsomorphicLayoutEffect)(function() {
256
+ if (rawActive) {
257
+ setPresent(true);
258
+ } else if (!active) {
259
+ setPresent(false);
260
+ }
261
+ if (rawActive && exiting) {
262
+ setExiting(false);
263
+ }
264
+ }, [
265
+ active,
266
+ exiting,
267
+ rawActive
268
+ ]);
269
+ var handoff = import_react.default.useMemo(function() {
270
+ return {
271
+ hidden: targetHidden,
272
+ skipNextAnimation,
273
+ onTransition(e) {
274
+ if (e.phase !== "end" || e.finished === false) return;
275
+ if (e.cause !== "close") {
276
+ setTargetFullyHidden(false);
277
+ return;
278
+ }
279
+ if (openRef.current && rawActiveRef.current) {
280
+ setTargetFullyHidden(false);
281
+ return;
282
+ }
283
+ setTargetFullyHidden(true);
284
+ releasePresenceLatch();
285
+ }
286
+ };
287
+ }, [
288
+ releasePresenceLatch,
289
+ skipNextAnimation,
290
+ targetHidden
291
+ ]);
292
+ var registerTarget = import_react.default.useCallback(function() {
293
+ targetCountRef.current += 1;
294
+ if (process.env.NODE_ENV === "development" && targetCountRef.current > 1) {
295
+ console.error(`Adapt expected exactly one target in scope "${scope}", but ${targetCountRef.current} targets registered.`);
296
+ }
297
+ }, [scope]);
298
+ var unregisterTarget = import_react.default.useCallback(function() {
299
+ targetCountRef.current = Math.max(0, targetCountRef.current - 1);
300
+ }, []);
301
+ var registerContents = import_react.default.useCallback(function() {
302
+ contentsCountRef.current += 1;
303
+ }, []);
304
+ var unregisterContents = import_react.default.useCallback(function() {
305
+ contentsCountRef.current = Math.max(0, contentsCountRef.current - 1);
306
+ }, []);
307
+ var registerRenderCallback = import_react.default.useCallback(function() {
308
+ renderCallbackCountRef.current += 1;
309
+ }, []);
310
+ var unregisterRenderCallback = import_react.default.useCallback(function() {
311
+ renderCallbackCountRef.current = Math.max(0, renderCallbackCountRef.current - 1);
312
+ }, []);
313
+ import_react.default.useEffect(function() {
314
+ if (process.env.NODE_ENV !== "development") return;
315
+ if (!active) return;
316
+ var timer = setTimeout(function() {
317
+ if (rawActiveRef.current && targetCountRef.current === 0 && contentsCountRef.current === 0 && renderCallbackCountRef.current === 0) {
318
+ console.error(`Adapt is active in scope "${scope}" but no target registered with useAdaptTarget(), Adapt.Contents marker, or render callback consumed it.`);
319
+ }
320
+ });
321
+ return function() {
322
+ return clearTimeout(timer);
323
+ };
324
+ }, [active, scope]);
325
+ var FinalContents = Contents || AdaptSlotContents;
326
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(LastAdaptContextScope.Provider, {
327
+ value: scope,
328
+ children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ProvideAdaptContext, {
329
+ Contents: FinalContents,
330
+ active,
331
+ setAdaptConfig,
332
+ portalName,
333
+ scopeName: scope,
334
+ slot: slotRef.current,
335
+ open,
336
+ onOpenChange,
337
+ state,
338
+ handoff,
339
+ targetFullyHidden,
340
+ registerTarget,
341
+ unregisterTarget,
342
+ registerContents,
343
+ unregisterContents,
344
+ registerRenderCallback,
345
+ unregisterRenderCallback,
346
+ children
347
+ })
348
+ });
349
+ };
350
+ var AdaptContents = function(param) {
351
+ var { scope, ...rest } = param;
352
+ var context = useAdaptContext(scope);
353
+ if (!(context === null || context === void 0 ? void 0 : context.Contents)) {
354
+ 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.`);
355
+ }
356
+ (0, import_constants.useIsomorphicLayoutEffect)(function() {
357
+ if (!context.active) return;
358
+ context.registerContents();
359
+ return function() {
360
+ context.unregisterContents();
361
+ };
362
+ }, [
363
+ context.active,
364
+ context.registerContents,
365
+ context.unregisterContents
366
+ ]);
367
+ return /* @__PURE__ */ import_react.default.createElement(context.Contents, {
368
+ ...rest,
369
+ scope,
370
+ key: `stable`
371
+ });
372
+ };
373
+ AdaptContents.shouldForwardSpace = true;
374
+ var Adapt = (0, import_helpers.withStaticProperties)(function Adapt2(props) {
375
+ var { children, platform, scope, when } = props;
376
+ var context = useAdaptContext(scope);
377
+ var enabled = context.active;
378
+ var isRenderCallback = typeof children === "function";
379
+ (0, import_constants.useIsomorphicLayoutEffect)(function() {
380
+ context.setAdaptConfig({
381
+ when,
382
+ platform
383
+ });
384
+ return function() {
385
+ context.setAdaptConfig(null);
386
+ };
387
+ }, [
388
+ when,
389
+ platform,
390
+ context.setAdaptConfig
391
+ ]);
392
+ (0, import_constants.useIsomorphicLayoutEffect)(function() {
393
+ if (!enabled || !isRenderCallback) return;
394
+ context.registerRenderCallback();
395
+ return function() {
396
+ context.unregisterRenderCallback();
397
+ };
398
+ }, [
399
+ enabled,
400
+ isRenderCallback,
401
+ context.registerRenderCallback,
402
+ context.unregisterRenderCallback
403
+ ]);
404
+ var output;
405
+ if (isRenderCallback) {
406
+ var Component = context === null || context === void 0 ? void 0 : context.Contents;
407
+ output = children(Component ? /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Component, {}) : null, getAdaptRenderState(context));
408
+ } else {
409
+ output = children;
410
+ }
411
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_z_index_stack.StackZIndexContext, { children: !enabled ? null : output });
412
+ }, { Contents: AdaptContents });
413
+ var AdaptPortalContents = function(props) {
414
+ var isActive = useAdaptIsActive(props.scope);
415
+ var { slot } = useAdaptContext(props.scope);
416
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AdaptSlotPublisher, {
417
+ isActive,
418
+ slot,
419
+ children: props.children
420
+ });
421
+ };
422
+ function AdaptSlotContents(param) {
423
+ var { scope } = param;
424
+ var { slot } = useAdaptContext(scope);
425
+ var _slot_subscribe, _slot_getSnapshot, _slot_getSnapshot1;
426
+ import_react.default.useSyncExternalStore((_slot_subscribe = slot === null || slot === void 0 ? void 0 : slot.subscribe) !== null && _slot_subscribe !== void 0 ? _slot_subscribe : emptySubscribe, (_slot_getSnapshot = slot === null || slot === void 0 ? void 0 : slot.getSnapshot) !== null && _slot_getSnapshot !== void 0 ? _slot_getSnapshot : emptySnapshot, (_slot_getSnapshot1 = slot === null || slot === void 0 ? void 0 : slot.getSnapshot) !== null && _slot_getSnapshot1 !== void 0 ? _slot_getSnapshot1 : emptySnapshot);
427
+ var _slot_element;
428
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children: (_slot_element = slot === null || slot === void 0 ? void 0 : slot.element) !== null && _slot_element !== void 0 ? _slot_element : null });
429
+ }
430
+ function AdaptSlotPublisher(param) {
431
+ var { isActive, slot, children } = param;
432
+ var publishedRef = import_react.default.useRef(null);
433
+ (0, import_constants.useIsomorphicLayoutEffect)(function() {
434
+ if (!slot) return;
435
+ var published = publishedRef.current;
436
+ if (published && published.isActive === isActive && published.element === children) {
437
+ return;
438
+ }
439
+ publishedRef.current = {
440
+ isActive,
441
+ element: children
442
+ };
443
+ if (isActive) {
444
+ slot.publish(children);
445
+ } else {
446
+ slot.clear();
447
+ }
448
+ slot.notify();
449
+ });
450
+ (0, import_constants.useIsomorphicLayoutEffect)(function() {
451
+ return function() {
452
+ var _publishedRef_current;
453
+ if ((_publishedRef_current = publishedRef.current) === null || _publishedRef_current === void 0 ? void 0 : _publishedRef_current.isActive) {
454
+ slot === null || slot === void 0 ? void 0 : slot.clear();
455
+ slot === null || slot === void 0 ? void 0 : slot.notify();
456
+ }
457
+ publishedRef.current = null;
458
+ };
459
+ }, [slot]);
460
+ return isActive ? null : /* @__PURE__ */ (0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, { children });
461
+ }
462
+ var emptySubscribe = function() {
463
+ return function() {};
464
+ };
465
+ var emptySnapshot = function() {
466
+ return 0;
467
+ };
468
+ var useAdaptIsActiveGiven = function(config) {
469
+ var media = (0, import_core.useMedia)();
470
+ var when = config === null || config === void 0 ? void 0 : config.when;
471
+ var platform = config === null || config === void 0 ? void 0 : config.platform;
472
+ if (when == null && platform == null) {
473
+ return false;
474
+ }
475
+ if (when === true) {
476
+ return true;
477
+ }
478
+ var enabled = false;
479
+ if (platform === "touch") enabled = import_constants.isTouchable;
480
+ else if (platform === "native") enabled = !import_constants.isWeb;
481
+ else if (platform === "web") enabled = import_constants.isWeb;
482
+ else if (platform === "ios") enabled = import_constants.isIos;
483
+ else if (platform === "android") enabled = import_constants.isAndroid;
484
+ if (platform && enabled == false) {
485
+ return false;
486
+ }
487
+ if (when && typeof when === "string") {
488
+ enabled = media[when];
489
+ }
490
+ return enabled;
491
+ };
492
+ var useAdaptIsActive = function(scope) {
493
+ return useAdaptContext(scope).active;
494
+ };
495
+ function useAdaptTarget(scope) {
496
+ var context = useAdaptContext(scope);
497
+ (0, import_constants.useIsomorphicLayoutEffect)(function() {
498
+ if (!context.active) return;
499
+ context.registerTarget();
500
+ return function() {
501
+ context.unregisterTarget();
502
+ };
503
+ }, [
504
+ context.active,
505
+ context.registerTarget,
506
+ context.unregisterTarget
507
+ ]);
508
+ if (!context.active) {
509
+ return null;
510
+ }
511
+ return {
512
+ open: context.open,
513
+ onOpenChange: context.onOpenChange,
514
+ handoff: context.handoff,
515
+ state: context.state
516
+ };
517
+ }
518
+ var AdaptCapabilities = function(param) {
519
+ var { children, scroll, overlay, dismiss } = param;
520
+ var parent = (0, import_react.useContext)(AdaptCapabilitiesContext);
521
+ var value = import_react.default.useMemo(function() {
522
+ return {
523
+ scroll: scroll !== null && scroll !== void 0 ? scroll : parent.scroll,
524
+ overlay: overlay !== null && overlay !== void 0 ? overlay : parent.overlay,
525
+ dismiss: dismiss !== null && dismiss !== void 0 ? dismiss : parent.dismiss
526
+ };
527
+ }, [
528
+ dismiss,
529
+ overlay,
530
+ parent.dismiss,
531
+ parent.overlay,
532
+ parent.scroll,
533
+ scroll
534
+ ]);
535
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AdaptCapabilitiesContext.Provider, {
536
+ value,
537
+ children
538
+ });
539
+ };
540
+ var useAdaptedCapabilities = function() {
541
+ return (0, import_react.useContext)(AdaptCapabilitiesContext);
542
+ };
543
+ function getAdaptRenderState(context) {
544
+ return {
545
+ active: context.active,
546
+ open: context.open,
547
+ onOpenChange: context.onOpenChange,
548
+ state: context.state,
549
+ handoff: context.handoff
550
+ };
551
+ }