-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathkeyframes.ts
More file actions
86 lines (76 loc) · 2.21 KB
/
keyframes.ts
File metadata and controls
86 lines (76 loc) · 2.21 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
import type {
AnimationIterationCount,
EasingFunction as CSSEasingFunction,
KeyframesRule,
} from "lightningcss";
import type { StyleDescriptor } from "./compiler.types";
import { parseDeclaration } from "./declarations";
import type { StylesheetBuilder } from "./stylesheet";
export function parseIterationCount(
value: AnimationIterationCount[],
): number[] {
return value.map((value) => {
return value.type === "infinite" ? -1 : value.value;
});
}
export function parseEasingFunction(
value: CSSEasingFunction[],
): StyleDescriptor {
const easingFn = value.map((value) => {
switch (value.type) {
case "linear":
case "ease":
case "ease-in":
case "ease-out":
case "ease-in-out":
return value.type;
case "cubic-bezier":
return [
{},
"cubic-bezier",
[value.x1, value.y1, value.x2, value.y2],
] as const;
case "steps":
return [{}, "steps", [value.count, value.position?.type]] as const;
}
}) as StyleDescriptor[];
if (easingFn.length === 1) {
return easingFn[0];
}
return easingFn;
}
export function extractKeyFrames(
keyframes: KeyframesRule,
builder: StylesheetBuilder,
) {
builder = builder.fork("keyframes");
builder.newAnimationFrames(keyframes.name.value);
for (const frame of keyframes.keyframes) {
if (!frame.declarations.declarations) continue;
const selectors = frame.selectors.map((selector) => {
switch (selector.type) {
case "percentage":
return frame.selectors.length > 1
? `${selector.value * 100}%`
: selector.value;
case "from":
case "to":
return selector.type;
case "timeline-range-percentage":
// TODO
return frame.selectors.length > 1
? `${selector.value.percentage}%`
: selector.value.percentage;
}
});
const firstSelector = selectors[0];
const progress =
firstSelector && selectors.length === 1
? firstSelector.toString()
: selectors.join(", ");
builder.newAnimationFrame(progress);
for (const declaration of frame.declarations.declarations) {
parseDeclaration(declaration, builder);
}
}
}