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