@tamagui/adapt 2.0.0-rc.4 → 2.0.0-rc.40

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.
@@ -2,33 +2,35 @@ var __create = Object.create;
2
2
  var __defProp = Object.defineProperty;
3
3
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
4
  var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __getProtoOf = Object.getPrototypeOf,
6
- __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __getProtoOf = Object.getPrototypeOf;
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: !0
11
- });
12
- },
13
- __copyProps = (to, from, except, desc) => {
14
- if (from && typeof from == "object" || typeof from == "function") for (let key of __getOwnPropNames(from)) !__hasOwnProp.call(to, key) && key !== except && __defProp(to, key, {
8
+ for (var name in all) __defProp(target, name, {
9
+ get: all[name],
10
+ enumerable: true
11
+ });
12
+ };
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, {
15
16
  get: () => from[key],
16
17
  enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
17
18
  });
18
- return to;
19
- };
19
+ }
20
+ return to;
21
+ };
20
22
  var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
- // If the importer is in node compatibility mode or this is not an ESM
22
- // file that has been converted to a CommonJS file using a Babel-
23
- // compatible transform (i.e. "__esModule" has not been set), then set
24
- // "default" to the CommonJS "module.exports" for node compatibility.
25
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
26
- value: mod,
27
- enumerable: !0
28
- }) : target, mod)),
29
- __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
30
- value: !0
31
- }), mod);
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
30
+ }) : target, mod));
31
+ var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
32
+ value: true
33
+ }), mod);
32
34
  var Adapt_exports = {};
33
35
  __export(Adapt_exports, {
34
36
  Adapt: () => Adapt,
@@ -41,70 +43,96 @@ __export(Adapt_exports, {
41
43
  useAdaptIsActive: () => useAdaptIsActive
42
44
  });
43
45
  module.exports = __toCommonJS(Adapt_exports);
44
- var import_constants = require("@tamagui/constants"),
45
- import_core = require("@tamagui/core"),
46
- import_helpers = require("@tamagui/helpers"),
47
- import_portal = require("@tamagui/portal"),
48
- import_z_index_stack = require("@tamagui/z-index-stack"),
49
- import_react = __toESM(require("react"), 1),
50
- 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_native = require("@tamagui/native");
50
+ var import_portal = require("@tamagui/portal");
51
+ var import_z_index_stack = require("@tamagui/z-index-stack");
52
+ var import_react = __toESM(require("react"), 1);
53
+ 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
+ };
68
+ }
69
+ const AdaptChildrenStoreContext = (0, import_react.createContext)(null);
51
70
  const AdaptContext = (0, import_core.createStyledContext)({
52
- Contents: null,
53
- scopeName: "",
54
- portalName: "",
55
- platform: null,
56
- setPlatform: x => {},
57
- when: null,
58
- setChildren: null,
59
- setWhen: () => {}
60
- }),
61
- LastAdaptContextScope = (0, import_react.createContext)(""),
62
- ProvideAdaptContext = ({
63
- children,
64
- ...context
65
- }) => {
66
- const scope = context.scopeName || "",
67
- lastScope = (0, import_react.useContext)(LastAdaptContextScope);
68
- return /* @__PURE__ */(0, import_jsx_runtime.jsx)(LastAdaptContextScope.Provider, {
69
- value: lastScope || context.lastScope || "",
70
- children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(AdaptContext.Provider, {
71
- scope,
72
- lastScope: lastScope || context.lastScope,
73
- ...context,
74
- children
75
- })
76
- });
77
- },
78
- useAdaptContext = scope => {
79
- const lastScope = (0, import_react.useContext)(LastAdaptContextScope),
80
- adaptScope = scope ?? lastScope;
81
- return AdaptContext.useStyledContext(adaptScope);
82
- },
83
- AdaptPortals = /* @__PURE__ */new Map(),
84
- AdaptParent = ({
85
- children,
86
- Contents,
87
- scope,
88
- portal
89
- }) => {
90
- const id = (0, import_react.useId)(),
91
- portalName = `AdaptPortal${scope}${id}`,
92
- FinalContents = (0, import_react.useMemo)(() => {
93
- if (Contents) return Contents;
94
- if (AdaptPortals.has(portalName)) return AdaptPortals.get(portalName);
95
- const element = () => /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_portal.PortalHost, {
96
- name: portalName,
97
- forwardProps: typeof portal == "boolean" ? void 0 : portal?.forwardProps
98
- }, id);
99
- return AdaptPortals.set(portalName, element), element;
100
- }, [portalName, Contents]);
101
- (0, import_constants.useIsomorphicLayoutEffect)(() => (AdaptPortals.set(portalName, FinalContents), () => {
71
+ Contents: null,
72
+ scopeName: "",
73
+ portalName: "",
74
+ platform: null,
75
+ setPlatform: x => {},
76
+ when: null,
77
+ setWhen: () => {}
78
+ });
79
+ 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
+ });
95
+ };
96
+ const useAdaptContext = scope => {
97
+ const lastScope = (0, import_react.useContext)(LastAdaptContextScope);
98
+ const adaptScope = scope ?? lastScope;
99
+ return AdaptContext.useStyledContext(adaptScope);
100
+ };
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 () => {
102
128
  AdaptPortals.delete(portalName);
103
- }), [portalName]);
104
- const [when, setWhen] = import_react.default.useState(null),
105
- [platform, setPlatform] = import_react.default.useState(null),
106
- [children2, setChildren] = import_react.default.useState(null);
107
- return /* @__PURE__ */(0, import_jsx_runtime.jsx)(LastAdaptContextScope.Provider, {
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, {
108
136
  value: scope,
109
137
  children: /* @__PURE__ */(0, import_jsx_runtime.jsx)(ProvideAdaptContext, {
110
138
  Contents: FinalContents,
@@ -112,74 +140,99 @@ const AdaptContext = (0, import_core.createStyledContext)({
112
140
  platform,
113
141
  setPlatform,
114
142
  setWhen,
115
- setChildren,
116
143
  portalName,
117
144
  scopeName: scope,
118
145
  children
119
146
  })
120
- });
121
- },
122
- AdaptContents = ({
123
- scope,
124
- ...rest
125
- }) => {
126
- const context = useAdaptContext(scope);
127
- 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.");
128
- return import_react.default.createElement(context.Contents, {
129
- ...rest,
130
- key: "stable"
131
- });
132
- };
133
- AdaptContents.shouldForwardSpace = !0;
134
- const Adapt = (0, import_helpers.withStaticProperties)(function (props) {
135
- const {
136
- platform,
137
- when,
138
- children,
139
- scope
140
- } = props,
141
- context = useAdaptContext(scope),
142
- enabled = useAdaptIsActiveGiven(props);
143
- (0, import_constants.useIsomorphicLayoutEffect)(() => {
144
- context?.setWhen?.(when || enabled), context?.setPlatform?.(platform || null);
145
- }, [when, platform, enabled, context.setWhen, context.setPlatform]), (0, import_constants.useIsomorphicLayoutEffect)(() => () => {
146
- context?.setWhen?.(null), context?.setPlatform?.(null);
147
- }, []);
148
- let output;
149
- if (typeof children == "function") {
150
- const Component = context?.Contents;
151
- output = children(Component ? /* @__PURE__ */(0, import_jsx_runtime.jsx)(Component, {}) : null);
152
- } else output = children;
153
- return (0, import_constants.useIsomorphicLayoutEffect)(() => {
154
- typeof children == "function" && output !== void 0 && context?.setChildren(output);
155
- }, [output]), /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_z_index_stack.StackZIndexContext, {
156
- children: enabled ? output : null
157
- });
158
- }, {
159
- Contents: AdaptContents
160
- }),
161
- AdaptPortalContents = props => {
162
- const isActive = useAdaptIsActive(props.scope),
163
- {
164
- portalName
165
- } = useAdaptContext(props.scope);
166
- return /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_portal.PortalItem, {
167
- passThrough: !isActive,
168
- hostName: portalName,
169
- children: props.children
170
- });
171
- },
172
- useAdaptIsActiveGiven = ({
147
+ })
148
+ });
149
+ };
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
+ });
160
+ };
161
+ AdaptContents.shouldForwardSpace = true;
162
+ const Adapt = (0, import_helpers.withStaticProperties)(function Adapt2(props) {
163
+ const {
164
+ platform,
173
165
  when,
174
- platform
175
- }) => {
176
- const media = (0, import_core.useMedia)();
177
- if (when == null && platform == null) return !1;
178
- if (when === !0) return !0;
179
- let enabled = !1;
180
- return platform === "touch" ? enabled = import_constants.isTouchable : platform === "native" ? enabled = !import_constants.isWeb : platform === "web" ? enabled = import_constants.isWeb : platform === "ios" ? enabled = import_constants.isIos : platform === "android" && (enabled = import_constants.isAndroid), platform && enabled == !1 ? !1 : (when && typeof when == "string" && (enabled = media[when]), enabled);
181
- },
182
- useAdaptIsActive = scope => {
183
- const props = useAdaptContext(scope);
184
- return useAdaptIsActiveGiven(props);
185
- };
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
+ });
208
+ };
209
+ function AdaptPortalTeleport({
210
+ isActive,
211
+ store,
212
+ children
213
+ }) {
214
+ (0, import_constants.useIsomorphicLayoutEffect)(() => {
215
+ if (!isActive) return;
216
+ store.set(children);
217
+ return () => store.set(null);
218
+ });
219
+ return isActive ? null : /* @__PURE__ */(0, import_jsx_runtime.jsx)(import_jsx_runtime.Fragment, {
220
+ children
221
+ });
222
+ }
223
+ const useAdaptIsActiveGiven = ({
224
+ when,
225
+ platform
226
+ }) => {
227
+ const media = (0, import_core.useMedia)();
228
+ if (when == null && platform == null) return false;
229
+ if (when === true) return true;
230
+ let enabled = false;
231
+ 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;
232
+ if (platform && enabled == false) return false;
233
+ if (when && typeof when === "string") enabled = media[when];
234
+ return enabled;
235
+ };
236
+ const useAdaptIsActive = scope => {
237
+ return useAdaptIsActiveGiven(useAdaptContext(scope));
238
+ };