-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathanimation.ts
More file actions
181 lines (153 loc) · 3.91 KB
/
animation.ts
File metadata and controls
181 lines (153 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* eslint-disable */
import type { ComponentType } from "react";
import { applyShorthand } from "../../utils";
import { StyleCollection } from "../injection";
import { weakFamily } from "../reactivity";
import type { StyleFunctionResolver } from "./resolve";
import { shorthandHandler } from "./shorthand";
const name = ["animationName", "string", "none"] as const;
const delay = ["animationDelay", "number", 0] as const;
const duration = ["animationDuration", "number", 0] as const;
const iteration = [
"animationIterationCount",
["number", "infinite"],
1,
] as const;
const fill = [
"animationFillMode",
["none", "forwards", "backwards", "both"],
"none",
] as const;
const playState = [
"animationPlayState",
["running", "paused"],
"running",
] as const;
const direction = [
"animationDirection",
["normal", "reverse", "alternate", "alternate-reverse"],
"normal",
] as const;
const timingFunction = [
"animationTimingFunction",
["linear", "ease", "ease-in", "ease-out", "ease-in-out", "object"],
"ease",
] as const;
export const animationShorthand = shorthandHandler(
[
[name],
[duration, name],
[name, duration],
[name, duration, iteration],
[name, duration, timingFunction, iteration],
[duration, delay, name],
[duration, delay, iteration, name],
[duration, delay, iteration, timingFunction, name],
[name, duration, timingFunction, delay, iteration, fill],
],
[
name,
delay,
direction,
duration,
fill,
iteration,
playState,
timingFunction,
],
"tuples",
);
export const animatedComponentFamily = weakFamily(
(component: ComponentType) => {
if (
"displayName" in component &&
component.displayName?.startsWith("Animated.")
) {
return component;
}
const createAnimatedComponent =
require("react-native-reanimated").createAnimatedComponent;
return createAnimatedComponent(component);
},
);
export const animation: StyleFunctionResolver = (
resolveValue,
value,
get,
options,
) => {
const animationShortHandTuples = animationShorthand(
resolveValue,
value,
get,
options,
);
if (!Array.isArray(animationShortHandTuples)) {
return;
}
animationShortHandTuples.pop();
const nameTuple = animationShortHandTuples.find(
(tuple) => tuple[1] === "animationName",
);
const name = nameTuple?.[0];
if (!nameTuple || typeof name !== "string") {
return;
}
const keyframes = get(StyleCollection.keyframes(name));
const animation: Record<string, any> = {};
for (const [progress, declarations] of keyframes) {
animation[progress] ??= {};
const props = options.calculateProps?.(
get,
// Cast this into a StyleRule[]
[{ s: [0], d: declarations }],
options.renderGuards,
options.inheritedVariables,
options.inlineVariables,
);
if (!props) {
continue;
}
if (props.normal) {
Object.assign(animation[progress], props.normal);
}
if (props.important) {
Object.assign(animation[progress], props.important);
}
animation[progress] = animation[progress].style;
}
nameTuple[0] = animation;
return applyShorthand(animationShortHandTuples);
};
const advancedTimingFunctions: Record<
string,
() => (...args: any[]) => unknown
> = {
"cubic-bezier": () => {
return (
require("react-native-reanimated") as typeof import("react-native-reanimated")
).cubicBezier;
},
"steps": () => {
return (
require("react-native-reanimated") as typeof import("react-native-reanimated")
).steps;
},
};
export const timingFunctionResolver: StyleFunctionResolver = (
resolveValue,
value,
) => {
const name = value[1];
const resolver = advancedTimingFunctions[name];
if (!resolver) {
return;
}
const args = resolveValue(value[2]);
if (!Array.isArray(args)) {
return;
}
const fn = resolver();
const result = fn(...args);
return result;
};