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