@tamagui/animations-react-native 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,3 +1,4 @@
1
+
1
2
  var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
@@ -5,41 +6,34 @@ var __getOwnPropNames = Object.getOwnPropertyNames;
5
6
  var __getProtoOf = Object.getPrototypeOf;
6
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
7
8
  var __export = (target, all) => {
8
- for (var name in all) __defProp(target, name, {
9
- get: all[name],
10
- enumerable: true
11
- });
9
+ for (var name in all) __defProp(target, name, {
10
+ get: all[name],
11
+ enumerable: true
12
+ });
12
13
  };
13
14
  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;
15
+ if (from && typeof from === "object" || typeof from === "function") {
16
+ for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
17
+ get: () => from[key],
18
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
19
+ });
20
+ }
21
+ return to;
21
22
  };
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
23
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
24
+ value: mod,
25
+ enumerable: true
30
26
  }) : target, mod));
31
- var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", {
32
- value: true
33
- }), mod);
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
34
28
  var createAnimations_exports = {};
35
29
  __export(createAnimations_exports, {
36
- AnimatedText: () => AnimatedText,
37
- AnimatedView: () => AnimatedView,
38
- createAnimations: () => createAnimations,
39
- useAnimatedNumber: () => useAnimatedNumber,
40
- useAnimatedNumberReaction: () => useAnimatedNumberReaction,
41
- useAnimatedNumberStyle: () => useAnimatedNumberStyle,
42
- useAnimatedNumbersStyle: () => useAnimatedNumbersStyle
30
+ AnimatedText: () => AnimatedText,
31
+ AnimatedView: () => AnimatedView,
32
+ createAnimations: () => createAnimations,
33
+ useAnimatedNumber: () => useAnimatedNumber,
34
+ useAnimatedNumberReaction: () => useAnimatedNumberReaction,
35
+ useAnimatedNumberStyle: () => useAnimatedNumberStyle,
36
+ useAnimatedNumbersStyle: () => useAnimatedNumbersStyle
43
37
  });
44
38
  module.exports = __toCommonJS(createAnimations_exports);
45
39
  var import_animation_helpers = require("@tamagui/animation-helpers");
@@ -50,517 +44,575 @@ var import_react = __toESM(require("react"), 1);
50
44
  var import_react_native = require("react-native-web");
51
45
  const isFabric = !import_constants.isWeb && typeof global !== "undefined" && !!global.__nativeFabricUIManager;
52
46
  const resolveDynamicValue = (value, isDark) => {
53
- if (value && typeof value === "object" && "dynamic" in value) {
54
- const dynamicValue = isDark ? value.dynamic.dark : value.dynamic.light;
55
- return dynamicValue;
56
- }
57
- return value;
47
+ if (value && typeof value === "object" && "dynamic" in value) {
48
+ const dynamicValue = isDark ? value.dynamic.dark : value.dynamic.light;
49
+ return dynamicValue;
50
+ }
51
+ return value;
58
52
  };
59
53
  const animatedStyleKey = {
60
- transform: true,
61
- opacity: true
54
+ transform: true,
55
+ opacity: true
62
56
  };
63
57
  const colorStyleKey = {
64
- backgroundColor: true,
65
- color: true,
66
- borderColor: true,
67
- borderLeftColor: true,
68
- borderRightColor: true,
69
- borderTopColor: true,
70
- borderBottomColor: true
58
+ backgroundColor: true,
59
+ color: true,
60
+ borderColor: true,
61
+ borderLeftColor: true,
62
+ borderRightColor: true,
63
+ borderTopColor: true,
64
+ borderBottomColor: true
71
65
  };
72
66
  const layoutStyleKey = {
73
- height: true,
74
- width: true,
75
- minHeight: true,
76
- maxHeight: true,
77
- minWidth: true,
78
- maxWidth: true
67
+ height: true,
68
+ width: true,
69
+ minHeight: true,
70
+ maxHeight: true,
71
+ minWidth: true,
72
+ maxWidth: true
79
73
  };
80
74
  function hasAnimatedLayoutKey(style, isDark, animateOnly) {
81
- for (const key in layoutStyleKey) {
82
- if (animateOnly && !animateOnly.includes(key)) continue;
83
- if (typeof resolveDynamicValue(style[key], isDark) === "number") return true;
84
- }
85
- return false;
75
+ for (const key in layoutStyleKey) {
76
+ if (animateOnly && !animateOnly.includes(key)) continue;
77
+ if (typeof resolveDynamicValue(style[key], isDark) === "number") return true;
78
+ }
79
+ return false;
86
80
  }
87
81
  function isAnimatableColor(value) {
88
- if (typeof value !== "string") return false;
89
- if (value === "") return false;
90
- if (value.includes("var(") || value.includes("calc(")) return false;
91
- return true;
82
+ return typeof value === "string" && (0, import_react_native.processColor)(value) != null;
92
83
  }
93
84
  const costlyToAnimateStyleKey = {
94
- borderRadius: true,
95
- borderTopLeftRadius: true,
96
- borderTopRightRadius: true,
97
- borderBottomLeftRadius: true,
98
- borderBottomRightRadius: true,
99
- borderWidth: true,
100
- borderLeftWidth: true,
101
- borderRightWidth: true,
102
- borderTopWidth: true,
103
- borderBottomWidth: true,
104
- ...colorStyleKey
85
+ borderRadius: true,
86
+ borderTopLeftRadius: true,
87
+ borderTopRightRadius: true,
88
+ borderBottomLeftRadius: true,
89
+ borderBottomRightRadius: true,
90
+ borderWidth: true,
91
+ borderLeftWidth: true,
92
+ borderRightWidth: true,
93
+ borderTopWidth: true,
94
+ borderBottomWidth: true,
95
+ ...colorStyleKey
105
96
  };
106
97
  const AnimatedView = import_react_native.Animated.View;
107
98
  const AnimatedText = import_react_native.Animated.Text;
108
99
  function useAnimatedNumber(initial) {
109
- const state = import_react.default.useRef(null);
110
- if (!state.current) {
111
- state.current = {
112
- composite: null,
113
- val: new import_react_native.Animated.Value(initial),
114
- strategy: {
115
- type: "spring"
116
- }
117
- };
118
- }
119
- return {
120
- getInstance() {
121
- return state.current.val;
122
- },
123
- getValue() {
124
- return state.current.val["_value"];
125
- },
126
- stop() {
127
- state.current.composite?.stop();
128
- state.current.composite = null;
129
- },
130
- setValue(next, {
131
- type,
132
- ...config
133
- } = {
134
- type: "spring"
135
- }, onFinish) {
136
- const val = state.current.val;
137
- const handleFinish = onFinish ? ({
138
- finished
139
- }) => finished ? onFinish() : null : void 0;
140
- if (type === "direct") {
141
- val.setValue(next);
142
- } else if (type === "spring") {
143
- state.current.composite?.stop();
144
- const composite = import_react_native.Animated.spring(val, {
145
- ...config,
146
- toValue: next,
147
- useNativeDriver: isFabric
148
- });
149
- composite.start(handleFinish);
150
- state.current.composite = composite;
151
- } else {
152
- state.current.composite?.stop();
153
- const composite = import_react_native.Animated.timing(val, {
154
- ...config,
155
- toValue: next,
156
- useNativeDriver: isFabric
157
- });
158
- composite.start(handleFinish);
159
- state.current.composite = composite;
160
- }
161
- }
162
- };
100
+ const state = import_react.default.useRef(null);
101
+ if (!state.current) {
102
+ state.current = {
103
+ composite: null,
104
+ val: new import_react_native.Animated.Value(initial),
105
+ strategy: { type: "spring" }
106
+ };
107
+ }
108
+ return {
109
+ getInstance() {
110
+ return state.current.val;
111
+ },
112
+ getValue() {
113
+ return state.current.val["_value"];
114
+ },
115
+ stop() {
116
+ state.current.composite?.stop();
117
+ state.current.composite = null;
118
+ },
119
+ setValue(next, { type, ...config } = { type: "spring" }, onFinish) {
120
+ const val = state.current.val;
121
+ const handleFinish = onFinish ? ({ finished }) => finished ? onFinish() : null : void 0;
122
+ if (type === "direct") {
123
+ val.setValue(next);
124
+ } else if (type === "spring") {
125
+ state.current.composite?.stop();
126
+ const composite = import_react_native.Animated.spring(val, {
127
+ ...config,
128
+ toValue: next,
129
+ useNativeDriver: isFabric
130
+ });
131
+ composite.start(handleFinish);
132
+ state.current.composite = composite;
133
+ } else {
134
+ state.current.composite?.stop();
135
+ const composite = import_react_native.Animated.timing(val, {
136
+ ...config,
137
+ toValue: next,
138
+ useNativeDriver: isFabric
139
+ });
140
+ composite.start(handleFinish);
141
+ state.current.composite = composite;
142
+ }
143
+ }
144
+ };
163
145
  }
164
- const useAnimatedNumberReaction = ({
165
- value
166
- }, onValue) => {
167
- const onChange = (0, import_web.useEvent)(current => {
168
- onValue(current.value);
169
- });
170
- import_react.default.useEffect(() => {
171
- const id = value.getInstance().addListener(onChange);
172
- return () => {
173
- value.getInstance().removeListener(id);
174
- };
175
- }, [value, onChange]);
146
+ const useAnimatedNumberReaction = ({ value }, onValue) => {
147
+ const onChange = (0, import_web.useEvent)((current) => {
148
+ onValue(current.value);
149
+ });
150
+ import_react.default.useEffect(() => {
151
+ const id = value.getInstance().addListener(onChange);
152
+ return () => {
153
+ value.getInstance().removeListener(id);
154
+ };
155
+ }, [value, onChange]);
176
156
  };
177
157
  const useAnimatedNumberStyle = (value, getStyle) => {
178
- return getStyle(value.getInstance());
158
+ const instance = value.getInstance();
159
+ const animatedStyle = getStyle(instance);
160
+ const usesAnimatedNode = hasAnimatedNode(animatedStyle);
161
+ const [current, setCurrent] = import_react.default.useState(value.getValue());
162
+ import_react.default.useEffect(() => {
163
+ if (usesAnimatedNode) return;
164
+ const id = instance.addListener(({ value: next }) => {
165
+ setCurrent(next);
166
+ });
167
+ return () => {
168
+ instance.removeListener(id);
169
+ };
170
+ }, [instance, usesAnimatedNode]);
171
+ return usesAnimatedNode ? animatedStyle : getStyle(current);
179
172
  };
173
+ function hasAnimatedNode(value) {
174
+ if (!value || typeof value !== "object") return false;
175
+ if (typeof value.__getValue === "function") return true;
176
+ if (Array.isArray(value)) return value.some(hasAnimatedNode);
177
+ return Object.values(value).some(hasAnimatedNode);
178
+ }
180
179
  const useAnimatedNumbersStyle = (vals, getStyle) => {
181
- return getStyle(...vals.map(v => v.getInstance()));
180
+ return getStyle(...vals.map((v) => v.getInstance()));
182
181
  };
183
182
  function createAnimations(animations, options) {
184
- const nativeDriver = options?.useNativeDriver ?? isFabric;
185
- return {
186
- isReactNative: true,
187
- inputStyle: "value",
188
- outputStyle: "inline",
189
- avoidReRenders: true,
190
- animations,
191
- needsCustomComponent: true,
192
- View: AnimatedView,
193
- Text: AnimatedText,
194
- useAnimatedNumber,
195
- useAnimatedNumberReaction,
196
- useAnimatedNumberStyle,
197
- useAnimatedNumbersStyle,
198
- usePresence: import_use_presence.usePresence,
199
- ResetPresence: import_use_presence.ResetPresence,
200
- useAnimations: ({
201
- props,
202
- onDidAnimate,
203
- style,
204
- componentState,
205
- presence,
206
- stateRef,
207
- useStyleEmitter
208
- }) => {
209
- const isDisabled = import_constants.isWeb && componentState.unmounted === true;
210
- const isExiting = presence?.[0] === false;
211
- const sendExitComplete = presence?.[1];
212
- const [, themeState] = (0, import_web.useThemeWithState)({});
213
- const isDark = themeState?.scheme === "dark" || themeState?.name?.startsWith("dark");
214
- const animateStyles = import_react.default.useRef({});
215
- const animatedTranforms = import_react.default.useRef([]);
216
- const animationsState = import_react.default.useRef(/* @__PURE__ */new WeakMap());
217
- const pseudoActiveRef = import_react.default.useRef(false);
218
- const exitCycleIdRef = import_react.default.useRef(0);
219
- const exitCompletedRef = import_react.default.useRef(false);
220
- const wasExitingRef = import_react.default.useRef(false);
221
- const justStartedExiting = isExiting && !wasExitingRef.current;
222
- const justStoppedExiting = !isExiting && wasExitingRef.current;
223
- if (justStartedExiting) {
224
- exitCycleIdRef.current++;
225
- exitCompletedRef.current = false;
226
- }
227
- if (justStoppedExiting) {
228
- exitCycleIdRef.current++;
229
- }
230
- const animateOnly = props.animateOnly || [];
231
- const hasTransitionOnly = !!props.animateOnly;
232
- const isEntering = !!componentState.unmounted;
233
- const wasEnteringRef = import_react.default.useRef(isEntering);
234
- const justFinishedEntering = wasEnteringRef.current && !isEntering;
235
- import_react.default.useEffect(() => {
236
- wasEnteringRef.current = isEntering;
237
- });
238
- const args = [JSON.stringify(style), componentState, isExiting, !!onDidAnimate, isDark, justFinishedEntering, hasTransitionOnly];
239
- const res = import_react.default.useMemo(() => {
240
- const runners = [];
241
- const completions = [];
242
- const animationState = isExiting ? "exit" : isEntering || justFinishedEntering ? "enter" : "default";
243
- const nonAnimatedStyle = {};
244
- const useNativeDriverForNode = nativeDriver && !hasAnimatedLayoutKey(style, isDark, hasTransitionOnly ? animateOnly : void 0);
245
- const seenAnimateKeys = /* @__PURE__ */new Set();
246
- let sawTransform = false;
247
- let transformCount = 0;
248
- for (const key in style) {
249
- const rawVal = style[key];
250
- const val = resolveDynamicValue(rawVal, isDark);
251
- if (val === void 0) continue;
252
- if (isDisabled) {
253
- continue;
254
- }
255
- if (animatedStyleKey[key] == null && !costlyToAnimateStyleKey[key] && !layoutStyleKey[key]) {
256
- nonAnimatedStyle[key] = val;
257
- continue;
258
- }
259
- if (hasTransitionOnly && !animateOnly.includes(key)) {
260
- nonAnimatedStyle[key] = val;
261
- continue;
262
- }
263
- if (layoutStyleKey[key] && typeof val !== "number") {
264
- nonAnimatedStyle[key] = val;
265
- continue;
266
- }
267
- if (colorStyleKey[key] && !isAnimatableColor(val)) {
268
- nonAnimatedStyle[key] = val;
269
- continue;
270
- }
271
- if (key !== "transform") {
272
- animateStyles.current[key] = update(key, animateStyles.current[key], val);
273
- seenAnimateKeys.add(key);
274
- continue;
275
- }
276
- if (!val) continue;
277
- if (typeof val === "string") {
278
- console.warn(`Warning: Tamagui can't animate string transforms yet!`);
279
- continue;
280
- }
281
- sawTransform = true;
282
- for (const transform of val) {
283
- if (!transform) continue;
284
- const index = transformCount++;
285
- const tkey = Object.keys(transform)[0];
286
- const currentTransform = animatedTranforms.current[index]?.[tkey];
287
- animatedTranforms.current[index] = {
288
- [tkey]: update(tkey, currentTransform, transform[tkey])
289
- };
290
- animatedTranforms.current = [...animatedTranforms.current];
291
- }
292
- }
293
- if (!isExiting && !isDisabled && !pseudoActiveRef.current) {
294
- for (const k in animateStyles.current) {
295
- if (!seenAnimateKeys.has(k)) delete animateStyles.current[k];
296
- }
297
- if (!sawTransform) {
298
- if (animatedTranforms.current.length) animatedTranforms.current = [];
299
- } else if (animatedTranforms.current.length > transformCount) {
300
- animatedTranforms.current = animatedTranforms.current.slice(0, transformCount);
301
- }
302
- }
303
- const animatedTransformStyle = animatedTranforms.current.length > 0 ? {
304
- transform: animatedTranforms.current.map(r => {
305
- const key = Object.keys(r)[0];
306
- const val = animationsState.current.get(r[key])?.interpolation || r[key];
307
- return {
308
- [key]: val
309
- };
310
- })
311
- } : {};
312
- const animatedStyle = {
313
- ...Object.fromEntries(Object.entries(animateStyles.current).map(([k, v]) => [k, animationsState.current.get(v)?.interpolation || v])),
314
- ...animatedTransformStyle
315
- };
316
- return {
317
- runners,
318
- completions,
319
- style: [nonAnimatedStyle, animatedStyle]
320
- };
321
- function update(key, animated, valIn) {
322
- const isColorStyleKey = colorStyleKey[key];
323
- const [val, type] = isColorStyleKey ? [0, void 0] : getValue(valIn);
324
- let animateToValue = val;
325
- const value = animated || new import_react_native.Animated.Value(val);
326
- const curInterpolation = animationsState.current.get(value);
327
- let interpolateArgs;
328
- if (type) {
329
- interpolateArgs = getInterpolated(curInterpolation?.current ?? value["_value"], val, type);
330
- animationsState.current.set(value, {
331
- interpolation: value.interpolate(interpolateArgs),
332
- current: val
333
- });
334
- }
335
- if (isColorStyleKey) {
336
- animateToValue = curInterpolation?.animateToValue ? 0 : 1;
337
- interpolateArgs = getColorInterpolated(curInterpolation?.current,
338
- // valIn is the next color
339
- valIn, animateToValue);
340
- animationsState.current.set(value, {
341
- current: valIn,
342
- interpolation: value.interpolate(interpolateArgs),
343
- animateToValue: curInterpolation?.animateToValue ? 0 : 1
344
- });
345
- }
346
- if (value) {
347
- const animationConfig = getAnimationConfig(key, animations, props.transition, animationState);
348
- let resolve;
349
- const promise = new Promise(res2 => {
350
- resolve = res2;
351
- });
352
- completions.push(promise);
353
- runners.push(() => {
354
- value.stopAnimation();
355
- function getAnimation() {
356
- return import_react_native.Animated[animationConfig.type || "spring"](value, {
357
- toValue: animateToValue,
358
- ...animationConfig,
359
- useNativeDriver: useNativeDriverForNode
360
- });
361
- }
362
- const animation = animationConfig.delay ? import_react_native.Animated.sequence([import_react_native.Animated.delay(animationConfig.delay), getAnimation()]) : getAnimation();
363
- animation.start(({
364
- finished
365
- }) => {
366
- if (finished || isExiting) {
367
- resolve();
368
- }
369
- });
370
- });
371
- }
372
- if (process.env.NODE_ENV === "development") {
373
- if (props["debug"] === "verbose") {
374
- console.info(" \u{1F4A0} animate", key, `from (${value["_value"]}) to`, valIn, `(${val})`, "type", type, "interpolate", interpolateArgs);
375
- }
376
- }
377
- return value;
378
- }
379
- }, args);
380
- import_react.default.useEffect(() => {
381
- wasExitingRef.current = isExiting;
382
- });
383
- (0, import_constants.useIsomorphicLayoutEffect)(() => {
384
- res.runners.forEach(r => r());
385
- const cycleId = exitCycleIdRef.current;
386
- if (res.completions.length === 0) {
387
- onDidAnimate?.();
388
- if (isExiting && !exitCompletedRef.current) {
389
- exitCompletedRef.current = true;
390
- sendExitComplete?.();
391
- }
392
- return;
393
- }
394
- let cancel = false;
395
- Promise.all(res.completions).then(() => {
396
- if (cancel) return;
397
- if (isExiting && cycleId !== exitCycleIdRef.current) return;
398
- if (isExiting && exitCompletedRef.current) return;
399
- onDidAnimate?.();
400
- if (isExiting) {
401
- exitCompletedRef.current = true;
402
- sendExitComplete?.();
403
- }
404
- });
405
- return () => {
406
- cancel = true;
407
- };
408
- }, args);
409
- useStyleEmitter?.((nextStyle, _effectiveTransition, pseudoActive) => {
410
- pseudoActiveRef.current = pseudoActive === true;
411
- const runners = [];
412
- const seenAnimateKeys = /* @__PURE__ */new Set();
413
- let transformCount = 0;
414
- let animatedShapeChanged = false;
415
- const useNativeDriverForNode = nativeDriver && !hasAnimatedLayoutKey(nextStyle, isDark) && !Object.keys(animateStyles.current).some(key => layoutStyleKey[key]);
416
- for (const key in nextStyle) {
417
- const rawVal = nextStyle[key];
418
- const val = resolveDynamicValue(rawVal, isDark);
419
- if (val === void 0) continue;
420
- if (key === "transform" && Array.isArray(val)) {
421
- for (const transform of val) {
422
- if (!transform) continue;
423
- const index = transformCount++;
424
- const tkey = Object.keys(transform)[0];
425
- const currentTransform = animatedTranforms.current[index]?.[tkey];
426
- if (!currentTransform) animatedShapeChanged = true;
427
- animatedTranforms.current[index] = {
428
- [tkey]: update(tkey, currentTransform, transform[tkey])
429
- };
430
- }
431
- } else if (animatedStyleKey[key] != null || costlyToAnimateStyleKey[key] || layoutStyleKey[key]) {
432
- if (layoutStyleKey[key] && typeof val !== "number") continue;
433
- if (colorStyleKey[key] && !isAnimatableColor(val)) continue;
434
- if (!animateStyles.current[key]) animatedShapeChanged = true;
435
- animateStyles.current[key] = update(key, animateStyles.current[key], val);
436
- seenAnimateKeys.add(key);
437
- }
438
- }
439
- for (const key in animateStyles.current) {
440
- if (!seenAnimateKeys.has(key)) {
441
- delete animateStyles.current[key];
442
- animatedShapeChanged = true;
443
- }
444
- }
445
- if (animatedTranforms.current.length > transformCount) {
446
- animatedTranforms.current = animatedTranforms.current.slice(0, transformCount);
447
- animatedShapeChanged = true;
448
- }
449
- runners.forEach(r => r());
450
- if (animatedShapeChanged && stateRef.current.nextState) {
451
- stateRef.current.baseSetStateShallow?.(stateRef.current.nextState);
452
- }
453
- function update(key, animated, valIn) {
454
- const isColor = colorStyleKey[key];
455
- const [numVal, type] = isColor ? [0, void 0] : getValue(valIn);
456
- let animateToValue = numVal;
457
- const value = animated || new import_react_native.Animated.Value(numVal);
458
- const curInterpolation = animationsState.current.get(value);
459
- if (type) {
460
- animationsState.current.set(value, {
461
- interpolation: value.interpolate(getInterpolated(curInterpolation?.current ?? value["_value"], numVal, type)),
462
- current: numVal
463
- });
464
- }
465
- if (isColor) {
466
- animateToValue = curInterpolation?.animateToValue ? 0 : 1;
467
- animationsState.current.set(value, {
468
- current: valIn,
469
- interpolation: value.interpolate(getColorInterpolated(curInterpolation?.current, valIn, animateToValue)),
470
- animateToValue: curInterpolation?.animateToValue ? 0 : 1
471
- });
472
- }
473
- const animationConfig = getAnimationConfig(key, animations, props.transition, "default");
474
- runners.push(() => {
475
- value.stopAnimation();
476
- const anim = import_react_native.Animated[animationConfig.type || "spring"](value, {
477
- toValue: animateToValue,
478
- ...animationConfig,
479
- useNativeDriver: useNativeDriverForNode
480
- });
481
- (animationConfig.delay ? import_react_native.Animated.sequence([import_react_native.Animated.delay(animationConfig.delay), anim]) : anim).start();
482
- });
483
- return value;
484
- }
485
- });
486
- if (process.env.NODE_ENV === "development") {
487
- if (props["debug"] === "verbose") {
488
- console.info(`Animated`, {
489
- response: res,
490
- inputStyle: style,
491
- isExiting
492
- });
493
- }
494
- }
495
- return res;
496
- }
497
- };
183
+ const nativeDriver = options?.useNativeDriver ?? isFabric;
184
+ return {
185
+ isReactNative: true,
186
+ inputStyle: "value",
187
+ outputStyle: "inline",
188
+ avoidReRenders: true,
189
+ animations,
190
+ needsCustomComponent: true,
191
+ View: AnimatedView,
192
+ Text: AnimatedText,
193
+ useAnimatedNumber,
194
+ useAnimatedNumberReaction,
195
+ useAnimatedNumberStyle,
196
+ useAnimatedNumbersStyle,
197
+ usePresence: import_use_presence.usePresence,
198
+ ResetPresence: import_use_presence.ResetPresence,
199
+ useAnimations: ({ props, onTransition, style, componentState, presence, stateRef, useStyleEmitter }) => {
200
+ const isDisabled = import_constants.isWeb && componentState.unmounted === true;
201
+ const isExiting = presence?.[0] === false;
202
+ const sendExitComplete = presence?.[1];
203
+ const onTransitionRef = import_react.default.useRef(onTransition);
204
+ onTransitionRef.current = onTransition;
205
+ const emit = (phase, cause, finished) => {
206
+ onTransitionRef.current?.(phase === "end" ? {
207
+ phase,
208
+ cause,
209
+ finished
210
+ } : {
211
+ phase,
212
+ cause
213
+ });
214
+ };
215
+ const [, themeState] = (0, import_web.useThemeWithState)({});
216
+ const isDark = themeState?.scheme === "dark" || themeState?.name?.startsWith("dark");
217
+ const animateStyles = import_react.default.useRef({});
218
+ const animatedTranforms = import_react.default.useRef([]);
219
+ const animationsState = import_react.default.useRef(/* @__PURE__ */ new WeakMap());
220
+ const pseudoActiveRef = import_react.default.useRef(false);
221
+ const exitCycleIdRef = import_react.default.useRef(0);
222
+ const exitCompletedRef = import_react.default.useRef(false);
223
+ const wasExitingRef = import_react.default.useRef(false);
224
+ const enterStartedRef = import_react.default.useRef(false);
225
+ const exitStartedRef = import_react.default.useRef(false);
226
+ const updateInFlightRef = import_react.default.useRef(false);
227
+ const updateCycleIdRef = import_react.default.useRef(0);
228
+ const prevStyleSigRef = import_react.default.useRef(null);
229
+ const justStartedExiting = isExiting && !wasExitingRef.current;
230
+ const justStoppedExiting = !isExiting && wasExitingRef.current;
231
+ if (justStartedExiting) {
232
+ exitCycleIdRef.current++;
233
+ exitCompletedRef.current = false;
234
+ }
235
+ if (justStoppedExiting) {
236
+ exitCycleIdRef.current++;
237
+ }
238
+ const animateOnly = props.animateOnly || [];
239
+ const hasTransitionOnly = !!props.animateOnly;
240
+ const isEntering = !!componentState.unmounted;
241
+ const wasEnteringRef = import_react.default.useRef(isEntering);
242
+ const justFinishedEntering = wasEnteringRef.current && !isEntering;
243
+ import_react.default.useEffect(() => {
244
+ wasEnteringRef.current = isEntering;
245
+ });
246
+ const args = [
247
+ JSON.stringify(style),
248
+ componentState,
249
+ isExiting,
250
+ !!onTransition,
251
+ isDark,
252
+ justFinishedEntering,
253
+ hasTransitionOnly
254
+ ];
255
+ const res = import_react.default.useMemo(() => {
256
+ const runners = [];
257
+ const completions = [];
258
+ const animationState = isExiting ? "exit" : isEntering || justFinishedEntering ? "enter" : "default";
259
+ const nonAnimatedStyle = {};
260
+ const useNativeDriverForNode = nativeDriver && !hasAnimatedLayoutKey(style, isDark, hasTransitionOnly ? animateOnly : void 0);
261
+ const seenAnimateKeys = /* @__PURE__ */ new Set();
262
+ let sawTransform = false;
263
+ let transformCount = 0;
264
+ for (const key in style) {
265
+ const rawVal = style[key];
266
+ const val = resolveDynamicValue(rawVal, isDark);
267
+ if (val === void 0) continue;
268
+ if (isDisabled) {
269
+ continue;
270
+ }
271
+ if (animatedStyleKey[key] == null && !costlyToAnimateStyleKey[key] && !layoutStyleKey[key]) {
272
+ nonAnimatedStyle[key] = val;
273
+ continue;
274
+ }
275
+ if (hasTransitionOnly && !animateOnly.includes(key)) {
276
+ nonAnimatedStyle[key] = val;
277
+ continue;
278
+ }
279
+ if (layoutStyleKey[key] && typeof val !== "number") {
280
+ nonAnimatedStyle[key] = val;
281
+ continue;
282
+ }
283
+ if (colorStyleKey[key] && !isAnimatableColor(val)) {
284
+ nonAnimatedStyle[key] = val;
285
+ continue;
286
+ }
287
+ if (key !== "transform") {
288
+ animateStyles.current[key] = update(key, animateStyles.current[key], val);
289
+ seenAnimateKeys.add(key);
290
+ continue;
291
+ }
292
+ if (!val) continue;
293
+ if (typeof val === "string") {
294
+ console.warn(`Warning: Tamagui can't animate string transforms yet!`);
295
+ continue;
296
+ }
297
+ sawTransform = true;
298
+ for (const transform of val) {
299
+ if (!transform) continue;
300
+ const index = transformCount++;
301
+ const tkey = Object.keys(transform)[0];
302
+ const currentTransform = animatedTranforms.current[index]?.[tkey];
303
+ animatedTranforms.current[index] = { [tkey]: update(tkey, currentTransform, transform[tkey]) };
304
+ animatedTranforms.current = [...animatedTranforms.current];
305
+ }
306
+ }
307
+ if (!isExiting && !isDisabled && !pseudoActiveRef.current) {
308
+ for (const k in animateStyles.current) {
309
+ if (!seenAnimateKeys.has(k)) delete animateStyles.current[k];
310
+ }
311
+ if (!sawTransform) {
312
+ if (animatedTranforms.current.length) animatedTranforms.current = [];
313
+ } else if (animatedTranforms.current.length > transformCount) {
314
+ animatedTranforms.current = animatedTranforms.current.slice(0, transformCount);
315
+ }
316
+ }
317
+ const animatedTransformStyle = animatedTranforms.current.length > 0 ? { transform: animatedTranforms.current.map((r) => {
318
+ const key = Object.keys(r)[0];
319
+ const val = animationsState.current.get(r[key])?.interpolation || r[key];
320
+ return { [key]: val };
321
+ }) } : {};
322
+ const animatedStyle = {
323
+ ...Object.fromEntries(Object.entries(animateStyles.current).map(([k, v]) => [k, animationsState.current.get(v)?.interpolation || v])),
324
+ ...animatedTransformStyle
325
+ };
326
+ return {
327
+ runners,
328
+ completions,
329
+ style: [nonAnimatedStyle, animatedStyle]
330
+ };
331
+ function update(key, animated, valIn) {
332
+ const isColorStyleKey = colorStyleKey[key];
333
+ const [val, type] = isColorStyleKey ? [0, void 0] : getValue(valIn);
334
+ let animateToValue = val;
335
+ const value = animated || new import_react_native.Animated.Value(val);
336
+ const curInterpolation = animationsState.current.get(value);
337
+ let interpolateArgs;
338
+ if (type) {
339
+ interpolateArgs = getInterpolated(curInterpolation?.current ?? value["_value"], val, type);
340
+ animationsState.current.set(value, {
341
+ interpolation: value.interpolate(interpolateArgs),
342
+ current: val
343
+ });
344
+ }
345
+ if (isColorStyleKey) {
346
+ animateToValue = curInterpolation?.animateToValue ? 0 : 1;
347
+ interpolateArgs = getColorInterpolated(curInterpolation?.current, valIn, animateToValue);
348
+ animationsState.current.set(value, {
349
+ current: valIn,
350
+ interpolation: value.interpolate(interpolateArgs),
351
+ animateToValue: curInterpolation?.animateToValue ? 0 : 1
352
+ });
353
+ }
354
+ if (value) {
355
+ const animationConfig = getAnimationConfig(key, animations, props.transition, animationState);
356
+ let resolve;
357
+ const promise = new Promise((res2) => {
358
+ resolve = res2;
359
+ });
360
+ completions.push(promise);
361
+ runners.push(() => {
362
+ value.stopAnimation();
363
+ function getAnimation() {
364
+ return import_react_native.Animated[animationConfig.type || "spring"](value, {
365
+ toValue: animateToValue,
366
+ ...animationConfig,
367
+ useNativeDriver: useNativeDriverForNode
368
+ });
369
+ }
370
+ const animation = animationConfig.delay ? import_react_native.Animated.sequence([import_react_native.Animated.delay(animationConfig.delay), getAnimation()]) : getAnimation();
371
+ animation.start(({ finished }) => {
372
+ if (finished || isExiting) {
373
+ resolve();
374
+ }
375
+ });
376
+ });
377
+ }
378
+ if (process.env.NODE_ENV === "development") {
379
+ if (props["debug"] === "verbose") {
380
+ console.info(" 💠 animate", key, `from (${value["_value"]}) to`, valIn, `(${val})`, "type", type, "interpolate", interpolateArgs);
381
+ }
382
+ }
383
+ return value;
384
+ }
385
+ }, args);
386
+ import_react.default.useEffect(() => {
387
+ wasExitingRef.current = isExiting;
388
+ });
389
+ (0, import_constants.useIsomorphicLayoutEffect)(() => {
390
+ if (justStoppedExiting && exitStartedRef.current && !exitCompletedRef.current) {
391
+ exitStartedRef.current = false;
392
+ emit("end", "exit", false);
393
+ }
394
+ }, [justStoppedExiting]);
395
+ (0, import_constants.useIsomorphicLayoutEffect)(() => {
396
+ res.runners.forEach((r) => r());
397
+ const cycleId = exitCycleIdRef.current;
398
+ const cause = isExiting ? "exit" : isEntering || justFinishedEntering ? "enter" : "update";
399
+ if (cause === "exit") {
400
+ if (enterStartedRef.current) {
401
+ enterStartedRef.current = false;
402
+ emit("end", "enter", false);
403
+ }
404
+ if (updateInFlightRef.current) {
405
+ updateInFlightRef.current = false;
406
+ updateCycleIdRef.current++;
407
+ emit("end", "update", false);
408
+ }
409
+ }
410
+ if (cause === "update") {
411
+ const sig = args[0];
412
+ if (prevStyleSigRef.current === null || prevStyleSigRef.current === sig) {
413
+ prevStyleSigRef.current = sig;
414
+ return;
415
+ }
416
+ prevStyleSigRef.current = sig;
417
+ if (res.completions.length === 0) return;
418
+ if (updateInFlightRef.current) {
419
+ emit("end", "update", false);
420
+ }
421
+ updateInFlightRef.current = true;
422
+ const uid = ++updateCycleIdRef.current;
423
+ emit("start", "update");
424
+ Promise.all(res.completions).then(() => {
425
+ if (uid !== updateCycleIdRef.current) return;
426
+ updateInFlightRef.current = false;
427
+ emit("end", "update", true);
428
+ });
429
+ return;
430
+ }
431
+ prevStyleSigRef.current = args[0];
432
+ if (res.completions.length === 0) {
433
+ emit("start", cause);
434
+ emit("end", cause, true);
435
+ if (isExiting && !exitCompletedRef.current) {
436
+ exitCompletedRef.current = true;
437
+ sendExitComplete?.();
438
+ }
439
+ return;
440
+ }
441
+ if (cause === "enter" && !enterStartedRef.current) {
442
+ enterStartedRef.current = true;
443
+ emit("start", "enter");
444
+ }
445
+ if (cause === "exit" && !exitStartedRef.current) {
446
+ exitStartedRef.current = true;
447
+ emit("start", "exit");
448
+ }
449
+ Promise.all(res.completions).then(() => {
450
+ if (isExiting && cycleId !== exitCycleIdRef.current) return;
451
+ if (isExiting && exitCompletedRef.current) return;
452
+ if (isExiting) {
453
+ if (exitStartedRef.current) {
454
+ exitStartedRef.current = false;
455
+ emit("end", "exit", true);
456
+ }
457
+ exitCompletedRef.current = true;
458
+ sendExitComplete?.();
459
+ } else if (enterStartedRef.current) {
460
+ enterStartedRef.current = false;
461
+ emit("end", "enter", true);
462
+ }
463
+ });
464
+ }, args);
465
+ useStyleEmitter?.((nextStyle, _effectiveTransition, pseudoActive) => {
466
+ pseudoActiveRef.current = pseudoActive === true;
467
+ const runners = [];
468
+ const seenAnimateKeys = /* @__PURE__ */ new Set();
469
+ let transformCount = 0;
470
+ let animatedShapeChanged = false;
471
+ const useNativeDriverForNode = nativeDriver && !hasAnimatedLayoutKey(nextStyle, isDark) && !Object.keys(animateStyles.current).some((key) => layoutStyleKey[key]);
472
+ for (const key in nextStyle) {
473
+ const rawVal = nextStyle[key];
474
+ const val = resolveDynamicValue(rawVal, isDark);
475
+ if (val === void 0) continue;
476
+ if (key === "transform" && Array.isArray(val)) {
477
+ for (const transform of val) {
478
+ if (!transform) continue;
479
+ const index = transformCount++;
480
+ const tkey = Object.keys(transform)[0];
481
+ const currentTransform = animatedTranforms.current[index]?.[tkey];
482
+ if (!currentTransform) animatedShapeChanged = true;
483
+ animatedTranforms.current[index] = { [tkey]: update(tkey, currentTransform, transform[tkey]) };
484
+ }
485
+ } else if (animatedStyleKey[key] != null || costlyToAnimateStyleKey[key] || layoutStyleKey[key]) {
486
+ if (layoutStyleKey[key] && typeof val !== "number") continue;
487
+ if (colorStyleKey[key] && !isAnimatableColor(val)) continue;
488
+ if (!animateStyles.current[key]) animatedShapeChanged = true;
489
+ animateStyles.current[key] = update(key, animateStyles.current[key], val);
490
+ seenAnimateKeys.add(key);
491
+ }
492
+ }
493
+ for (const key in animateStyles.current) {
494
+ if (!seenAnimateKeys.has(key)) {
495
+ delete animateStyles.current[key];
496
+ animatedShapeChanged = true;
497
+ }
498
+ }
499
+ if (animatedTranforms.current.length > transformCount) {
500
+ animatedTranforms.current = animatedTranforms.current.slice(0, transformCount);
501
+ animatedShapeChanged = true;
502
+ }
503
+ runners.forEach((r) => r());
504
+ if (animatedShapeChanged && stateRef.current.nextState) {
505
+ stateRef.current.baseSetStateShallow?.(stateRef.current.nextState);
506
+ }
507
+ function update(key, animated, valIn) {
508
+ const isColor = colorStyleKey[key];
509
+ const [numVal, type] = isColor ? [0, void 0] : getValue(valIn);
510
+ let animateToValue = numVal;
511
+ const value = animated || new import_react_native.Animated.Value(numVal);
512
+ const curInterpolation = animationsState.current.get(value);
513
+ if (type) {
514
+ animationsState.current.set(value, {
515
+ interpolation: value.interpolate(getInterpolated(curInterpolation?.current ?? value["_value"], numVal, type)),
516
+ current: numVal
517
+ });
518
+ }
519
+ if (isColor) {
520
+ animateToValue = curInterpolation?.animateToValue ? 0 : 1;
521
+ animationsState.current.set(value, {
522
+ current: valIn,
523
+ interpolation: value.interpolate(getColorInterpolated(curInterpolation?.current, valIn, animateToValue)),
524
+ animateToValue: curInterpolation?.animateToValue ? 0 : 1
525
+ });
526
+ }
527
+ const animationConfig = getAnimationConfig(key, animations, props.transition, "default");
528
+ runners.push(() => {
529
+ value.stopAnimation();
530
+ const anim = import_react_native.Animated[animationConfig.type || "spring"](value, {
531
+ toValue: animateToValue,
532
+ ...animationConfig,
533
+ useNativeDriver: useNativeDriverForNode
534
+ });
535
+ (animationConfig.delay ? import_react_native.Animated.sequence([import_react_native.Animated.delay(animationConfig.delay), anim]) : anim).start();
536
+ });
537
+ return value;
538
+ }
539
+ });
540
+ if (process.env.NODE_ENV === "development") {
541
+ if (props["debug"] === "verbose") {
542
+ console.info(`Animated`, {
543
+ response: res,
544
+ inputStyle: style,
545
+ isExiting
546
+ });
547
+ }
548
+ }
549
+ return res;
550
+ }
551
+ };
498
552
  }
499
553
  function getColorInterpolated(currentColor, nextColor, animateToValue) {
500
- const inputRange = [0, 1];
501
- const outputRange = [currentColor ? currentColor : nextColor, nextColor];
502
- if (animateToValue === 0) {
503
- outputRange.reverse();
504
- }
505
- return {
506
- inputRange,
507
- outputRange
508
- };
554
+ const inputRange = [0, 1];
555
+ const outputRange = [currentColor ? currentColor : nextColor, nextColor];
556
+ if (animateToValue === 0) {
557
+ outputRange.reverse();
558
+ }
559
+ return {
560
+ inputRange,
561
+ outputRange
562
+ };
509
563
  }
510
564
  function getInterpolated(current, next, postfix = "deg") {
511
- if (next === current) {
512
- current = next - 1e-9;
513
- }
514
- const inputRange = [current, next];
515
- const outputRange = [`${current}${postfix}`, `${next}${postfix}`];
516
- if (next < current) {
517
- inputRange.reverse();
518
- outputRange.reverse();
519
- }
520
- return {
521
- inputRange,
522
- outputRange
523
- };
565
+ if (next === current) {
566
+ current = next - 1e-9;
567
+ }
568
+ const inputRange = [current, next];
569
+ const outputRange = [`${current}${postfix}`, `${next}${postfix}`];
570
+ if (next < current) {
571
+ inputRange.reverse();
572
+ outputRange.reverse();
573
+ }
574
+ return {
575
+ inputRange,
576
+ outputRange
577
+ };
524
578
  }
525
579
  function getAnimationConfig(key, animations, transition, animationState = "default") {
526
- const normalized = (0, import_animation_helpers.normalizeTransition)(transition);
527
- const shortKey = transformShorthands[key];
528
- const propAnimation = normalized.properties[key] ?? normalized.properties[shortKey];
529
- let animationType = null;
530
- let extraConf = {};
531
- if (typeof propAnimation === "string") {
532
- animationType = propAnimation;
533
- } else if (propAnimation && typeof propAnimation === "object") {
534
- animationType = propAnimation.type || (0, import_animation_helpers.getEffectiveAnimation)(normalized, animationState);
535
- extraConf = propAnimation;
536
- } else {
537
- animationType = (0, import_animation_helpers.getEffectiveAnimation)(normalized, animationState);
538
- }
539
- if (normalized.delay && !extraConf.delay) {
540
- extraConf = {
541
- ...extraConf,
542
- delay: normalized.delay
543
- };
544
- }
545
- const found = animationType ? animations[animationType] : {};
546
- return {
547
- ...found,
548
- // Apply global spring config overrides (from transition={['bouncy', { stiffness: 1000 }]})
549
- ...normalized.config,
550
- // Property-specific config takes highest precedence
551
- ...extraConf
552
- };
580
+ const normalized = (0, import_animation_helpers.normalizeTransition)(transition);
581
+ const shortKey = transformShorthands[key];
582
+ const propAnimation = normalized.properties[key] ?? normalized.properties[shortKey];
583
+ let animationType = null;
584
+ let extraConf = {};
585
+ if (typeof propAnimation === "string") {
586
+ animationType = propAnimation;
587
+ } else if (propAnimation && typeof propAnimation === "object") {
588
+ animationType = propAnimation.type || (0, import_animation_helpers.getEffectiveAnimation)(normalized, animationState);
589
+ extraConf = propAnimation;
590
+ } else {
591
+ animationType = (0, import_animation_helpers.getEffectiveAnimation)(normalized, animationState);
592
+ }
593
+ if (normalized.delay && !extraConf.delay) {
594
+ extraConf = {
595
+ ...extraConf,
596
+ delay: normalized.delay
597
+ };
598
+ }
599
+ const found = animationType ? animations[animationType] : {};
600
+ return {
601
+ ...found,
602
+ ...normalized.config,
603
+ ...extraConf
604
+ };
553
605
  }
554
606
  const transformShorthands = {
555
- x: "translateX",
556
- y: "translateY",
557
- translateX: "x",
558
- translateY: "y"
607
+ x: "translateX",
608
+ y: "translateY",
609
+ translateX: "x",
610
+ translateY: "y"
559
611
  };
560
612
  function getValue(input, isColor = false) {
561
- if (typeof input !== "string") {
562
- return [input];
563
- }
564
- const [_, number, after] = input.match(/([-0-9]+)(deg|%|px)/) ?? [];
565
- return [+number, after];
566
- }
613
+ if (typeof input !== "string") {
614
+ return [input];
615
+ }
616
+ const [_, number, after] = input.match(/(-?(?:\d+\.?\d*|\.\d+))(deg|%|px)?/) ?? [];
617
+ return [+number, after];
618
+ }