@tamagui/animations-react-native 1.88.13 → 1.89.0-1706308641099
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.
- package/LICENSE +21 -0
- package/dist/esm/createAnimations.mjs +277 -0
- package/dist/esm/index.mjs +2 -0
- package/dist/esm/polyfill.mjs +1 -0
- package/package.json +5 -5
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Nate Wienert
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { ResetPresence, usePresence } from "@tamagui/use-presence";
|
|
2
|
+
import { isWeb, useIsomorphicLayoutEffect } from "@tamagui/constants";
|
|
3
|
+
import { useEvent } from "@tamagui/web";
|
|
4
|
+
import { useEffect, useMemo, useRef } from "react";
|
|
5
|
+
import { Animated } from "react-native-web";
|
|
6
|
+
const animatedStyleKey = {
|
|
7
|
+
transform: !0,
|
|
8
|
+
opacity: !0
|
|
9
|
+
},
|
|
10
|
+
colorStyleKey = {
|
|
11
|
+
backgroundColor: !0,
|
|
12
|
+
color: !0,
|
|
13
|
+
borderColor: !0,
|
|
14
|
+
borderLeftColor: !0,
|
|
15
|
+
borderRightColor: !0,
|
|
16
|
+
borderTopColor: !0,
|
|
17
|
+
borderBottomColor: !0
|
|
18
|
+
},
|
|
19
|
+
costlyToAnimateStyleKey = {
|
|
20
|
+
borderRadius: !0,
|
|
21
|
+
borderTopLeftRadius: !0,
|
|
22
|
+
borderTopRightRadius: !0,
|
|
23
|
+
borderBottomLeftRadius: !0,
|
|
24
|
+
borderBottomRightRadius: !0,
|
|
25
|
+
borderWidth: !0,
|
|
26
|
+
borderLeftWidth: !0,
|
|
27
|
+
borderRightWidth: !0,
|
|
28
|
+
borderTopWidth: !0,
|
|
29
|
+
borderBottomWidth: !0,
|
|
30
|
+
...colorStyleKey
|
|
31
|
+
// TODO for other keys like height or width, it's better to not add them here till layout animations are ready
|
|
32
|
+
},
|
|
33
|
+
AnimatedView = Animated.View,
|
|
34
|
+
AnimatedText = Animated.Text;
|
|
35
|
+
function useAnimatedNumber(initial) {
|
|
36
|
+
const state = useRef(null);
|
|
37
|
+
return state.current || (state.current = {
|
|
38
|
+
composite: null,
|
|
39
|
+
val: new Animated.Value(initial),
|
|
40
|
+
strategy: {
|
|
41
|
+
type: "spring"
|
|
42
|
+
}
|
|
43
|
+
}), {
|
|
44
|
+
getInstance() {
|
|
45
|
+
return state.current.val;
|
|
46
|
+
},
|
|
47
|
+
getValue() {
|
|
48
|
+
return state.current.val._value;
|
|
49
|
+
},
|
|
50
|
+
stop() {
|
|
51
|
+
state.current.composite?.stop(), state.current.composite = null;
|
|
52
|
+
},
|
|
53
|
+
setValue(next, {
|
|
54
|
+
type,
|
|
55
|
+
...config
|
|
56
|
+
} = {
|
|
57
|
+
type: "spring"
|
|
58
|
+
}, onFinish) {
|
|
59
|
+
const val = state.current.val,
|
|
60
|
+
handleFinish = onFinish ? ({
|
|
61
|
+
finished
|
|
62
|
+
}) => finished ? onFinish() : null : void 0;
|
|
63
|
+
if (type === "direct") val.setValue(next);else if (type === "spring") {
|
|
64
|
+
state.current.composite?.stop();
|
|
65
|
+
const composite = Animated.spring(val, {
|
|
66
|
+
...config,
|
|
67
|
+
toValue: next,
|
|
68
|
+
useNativeDriver: !isWeb
|
|
69
|
+
});
|
|
70
|
+
composite.start(handleFinish), state.current.composite = composite;
|
|
71
|
+
} else {
|
|
72
|
+
state.current.composite?.stop();
|
|
73
|
+
const composite = Animated.timing(val, {
|
|
74
|
+
...config,
|
|
75
|
+
toValue: next,
|
|
76
|
+
useNativeDriver: !isWeb
|
|
77
|
+
});
|
|
78
|
+
composite.start(handleFinish), state.current.composite = composite;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
function useAnimatedNumberReaction({
|
|
84
|
+
value
|
|
85
|
+
}, onValue) {
|
|
86
|
+
const onChange = useEvent(current => {
|
|
87
|
+
onValue(current.value);
|
|
88
|
+
});
|
|
89
|
+
useEffect(() => {
|
|
90
|
+
const id = value.getInstance().addListener(onChange);
|
|
91
|
+
return () => {
|
|
92
|
+
value.getInstance().removeListener(id);
|
|
93
|
+
};
|
|
94
|
+
}, [value, onChange]);
|
|
95
|
+
}
|
|
96
|
+
function useAnimatedNumberStyle(value, getStyle) {
|
|
97
|
+
return getStyle(value.getInstance());
|
|
98
|
+
}
|
|
99
|
+
function createAnimations(animations) {
|
|
100
|
+
return {
|
|
101
|
+
isReactNative: !0,
|
|
102
|
+
animations,
|
|
103
|
+
View: AnimatedView,
|
|
104
|
+
Text: AnimatedText,
|
|
105
|
+
useAnimatedNumber,
|
|
106
|
+
useAnimatedNumberReaction,
|
|
107
|
+
useAnimatedNumberStyle,
|
|
108
|
+
usePresence,
|
|
109
|
+
ResetPresence,
|
|
110
|
+
useAnimations: ({
|
|
111
|
+
props,
|
|
112
|
+
onDidAnimate,
|
|
113
|
+
style,
|
|
114
|
+
componentState,
|
|
115
|
+
presence
|
|
116
|
+
}) => {
|
|
117
|
+
const isExiting = presence?.[0] === !1,
|
|
118
|
+
sendExitComplete = presence?.[1],
|
|
119
|
+
animateStyles = useRef({}),
|
|
120
|
+
animatedTranforms = useRef([]),
|
|
121
|
+
animationsState = useRef( /* @__PURE__ */new WeakMap()),
|
|
122
|
+
animateOnly = props.animateOnly || [],
|
|
123
|
+
hasAnimateOnly = !!props.animateOnly,
|
|
124
|
+
args = [JSON.stringify(style), componentState, isExiting, !!onDidAnimate],
|
|
125
|
+
isThereNoNativeStyleKeys = useMemo(() => isWeb ? !0 : Object.keys(style).some(key => animateOnly.length ? !animatedStyleKey[key] && animateOnly.indexOf(key) === -1 : !animatedStyleKey[key]), args),
|
|
126
|
+
res = useMemo(() => {
|
|
127
|
+
const runners = [],
|
|
128
|
+
completions = [],
|
|
129
|
+
nonAnimatedStyle = {};
|
|
130
|
+
for (const key in style) {
|
|
131
|
+
const val = style[key];
|
|
132
|
+
if (animatedStyleKey[key] == null && !costlyToAnimateStyleKey[key]) {
|
|
133
|
+
nonAnimatedStyle[key] = val;
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (hasAnimateOnly && !animateOnly.includes(key)) {
|
|
137
|
+
nonAnimatedStyle[key] = val;
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
if (key !== "transform") {
|
|
141
|
+
animateStyles.current[key] = update(key, animateStyles.current[key], val);
|
|
142
|
+
continue;
|
|
143
|
+
}
|
|
144
|
+
if (val) {
|
|
145
|
+
if (typeof val == "string") {
|
|
146
|
+
console.warn("Warning: Tamagui can't animate string transforms yet!");
|
|
147
|
+
continue;
|
|
148
|
+
}
|
|
149
|
+
for (const [index, transform] of val.entries()) {
|
|
150
|
+
if (!transform) continue;
|
|
151
|
+
const tkey = Object.keys(transform)[0],
|
|
152
|
+
currentTransform = animatedTranforms.current[index]?.[tkey];
|
|
153
|
+
animatedTranforms.current[index] = {
|
|
154
|
+
[tkey]: update(tkey, currentTransform, transform[tkey])
|
|
155
|
+
}, animatedTranforms.current = [...animatedTranforms.current];
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
const animatedStyle = {
|
|
160
|
+
...Object.fromEntries(Object.entries(animateStyles.current).map(([k, v]) => [k, animationsState.current.get(v)?.interpolation || v])),
|
|
161
|
+
transform: animatedTranforms.current.map(r => {
|
|
162
|
+
const key = Object.keys(r)[0],
|
|
163
|
+
val = animationsState.current.get(r[key])?.interpolation || r[key];
|
|
164
|
+
return {
|
|
165
|
+
[key]: val
|
|
166
|
+
};
|
|
167
|
+
})
|
|
168
|
+
};
|
|
169
|
+
return {
|
|
170
|
+
runners,
|
|
171
|
+
completions,
|
|
172
|
+
style: [nonAnimatedStyle, animatedStyle]
|
|
173
|
+
};
|
|
174
|
+
function update(key, animated, valIn) {
|
|
175
|
+
const isColorStyleKey = colorStyleKey[key],
|
|
176
|
+
[val, type] = isColorStyleKey ? [0, void 0] : getValue(valIn);
|
|
177
|
+
let animateToValue = val;
|
|
178
|
+
const value = animated || new Animated.Value(val),
|
|
179
|
+
curInterpolation = animationsState.current.get(value);
|
|
180
|
+
let interpolateArgs;
|
|
181
|
+
if (type && (interpolateArgs = getInterpolated(curInterpolation?.current ?? value._value, val, type), animationsState.current.set(value, {
|
|
182
|
+
interpolation: value.interpolate(interpolateArgs),
|
|
183
|
+
current: val
|
|
184
|
+
})), isColorStyleKey && (animateToValue = curInterpolation?.animateToValue ? 0 : 1, interpolateArgs = getColorInterpolated(curInterpolation?.current,
|
|
185
|
+
// valIn is the next color
|
|
186
|
+
valIn, animateToValue), animationsState.current.set(value, {
|
|
187
|
+
current: valIn,
|
|
188
|
+
interpolation: value.interpolate(interpolateArgs),
|
|
189
|
+
animateToValue: curInterpolation?.animateToValue ? 0 : 1
|
|
190
|
+
})), value) {
|
|
191
|
+
const animationConfig = getAnimationConfig(key, animations, props.animation);
|
|
192
|
+
let resolve;
|
|
193
|
+
const promise = new Promise(res2 => {
|
|
194
|
+
resolve = res2;
|
|
195
|
+
});
|
|
196
|
+
completions.push(promise), runners.push(() => {
|
|
197
|
+
value.stopAnimation();
|
|
198
|
+
function getAnimation() {
|
|
199
|
+
return Animated[animationConfig.type || "spring"](value, {
|
|
200
|
+
toValue: animateToValue,
|
|
201
|
+
useNativeDriver: !isWeb && !isThereNoNativeStyleKeys,
|
|
202
|
+
...animationConfig
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
(animationConfig.delay ? Animated.sequence([Animated.delay(animationConfig.delay), getAnimation()]) : getAnimation()).start(({
|
|
206
|
+
finished
|
|
207
|
+
}) => {
|
|
208
|
+
finished && resolve();
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
return process.env.NODE_ENV === "development" && props.debug === "verbose" && console.info(" \u{1F4A0} animate", key, `from (${value._value}) to`, valIn, `(${val})`, "type", type, "interpolate", interpolateArgs), value;
|
|
213
|
+
}
|
|
214
|
+
}, args);
|
|
215
|
+
return useIsomorphicLayoutEffect(() => {
|
|
216
|
+
res.runners.forEach(r => r());
|
|
217
|
+
let cancel = !1;
|
|
218
|
+
return Promise.all(res.completions).then(() => {
|
|
219
|
+
cancel || (onDidAnimate?.(), isExiting && sendExitComplete?.());
|
|
220
|
+
}), () => {
|
|
221
|
+
cancel = !0;
|
|
222
|
+
};
|
|
223
|
+
}, args), process.env.NODE_ENV === "development" && props.debug === "verbose" && console.info("Animated", {
|
|
224
|
+
response: res,
|
|
225
|
+
inputStyle: style,
|
|
226
|
+
isExiting
|
|
227
|
+
}), res;
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
function getColorInterpolated(currentColor, nextColor, animateToValue) {
|
|
232
|
+
const inputRange = [0, 1],
|
|
233
|
+
outputRange = [currentColor || nextColor, nextColor];
|
|
234
|
+
return animateToValue === 0 && outputRange.reverse(), {
|
|
235
|
+
inputRange,
|
|
236
|
+
outputRange
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
function getInterpolated(current, next, postfix = "deg") {
|
|
240
|
+
next === current && (current = next - 1e-9);
|
|
241
|
+
const inputRange = [current, next],
|
|
242
|
+
outputRange = [`${current}${postfix}`, `${next}${postfix}`];
|
|
243
|
+
return next < current && (inputRange.reverse(), outputRange.reverse()), {
|
|
244
|
+
inputRange,
|
|
245
|
+
outputRange
|
|
246
|
+
};
|
|
247
|
+
}
|
|
248
|
+
function getAnimationConfig(key, animations, animation) {
|
|
249
|
+
if (typeof animation == "string") return animations[animation];
|
|
250
|
+
let type = "",
|
|
251
|
+
extraConf;
|
|
252
|
+
const shortKey = transformShorthands[key];
|
|
253
|
+
if (Array.isArray(animation)) {
|
|
254
|
+
type = animation[0];
|
|
255
|
+
const conf = animation[1]?.[key] ?? animation[1]?.[shortKey];
|
|
256
|
+
conf && (typeof conf == "string" ? type = conf : (type = conf.type || type, extraConf = conf));
|
|
257
|
+
} else {
|
|
258
|
+
const val = animation?.[key] ?? animation?.[shortKey];
|
|
259
|
+
type = val?.type, extraConf = val;
|
|
260
|
+
}
|
|
261
|
+
return {
|
|
262
|
+
...animations[type],
|
|
263
|
+
...extraConf
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
const transformShorthands = {
|
|
267
|
+
x: "translateX",
|
|
268
|
+
y: "translateY",
|
|
269
|
+
translateX: "x",
|
|
270
|
+
translateY: "y"
|
|
271
|
+
};
|
|
272
|
+
function getValue(input, isColor = !1) {
|
|
273
|
+
if (typeof input != "string") return [input];
|
|
274
|
+
const [_, number, after] = input.match(/([-0-9]+)(deg|%|px)/) ?? [];
|
|
275
|
+
return [+number, after];
|
|
276
|
+
}
|
|
277
|
+
export { AnimatedText, AnimatedView, createAnimations, useAnimatedNumber, useAnimatedNumberReaction, useAnimatedNumberStyle };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
typeof requestAnimationFrame > "u" && (globalThis.requestAnimationFrame = setImmediate);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamagui/animations-react-native",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.89.0-1706308641099",
|
|
4
4
|
"source": "src/index.ts",
|
|
5
5
|
"sideEffects": [
|
|
6
6
|
"polyfill.js"
|
|
@@ -15,12 +15,12 @@
|
|
|
15
15
|
"dist"
|
|
16
16
|
],
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@tamagui/constants": "1.
|
|
19
|
-
"@tamagui/use-presence": "1.
|
|
20
|
-
"@tamagui/web": "1.
|
|
18
|
+
"@tamagui/constants": "1.89.0-1706308641099",
|
|
19
|
+
"@tamagui/use-presence": "1.89.0-1706308641099",
|
|
20
|
+
"@tamagui/web": "1.89.0-1706308641099"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"@tamagui/build": "1.
|
|
23
|
+
"@tamagui/build": "1.89.0-1706308641099",
|
|
24
24
|
"react": "^18.2.0",
|
|
25
25
|
"react-native": "^0.72.6"
|
|
26
26
|
},
|