@hozo/engine 0.1.0 → 0.2.0
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/CHANGELOG.md +8 -0
- package/dist/generated/animation.d.ts +1 -0
- package/dist/generated/animation.d.ts.map +1 -1
- package/dist/generated/animation.js +1 -0
- package/dist/generated/animation.js.map +1 -1
- package/dist/index.native.d.ts +1 -0
- package/dist/index.native.d.ts.map +1 -1
- package/dist/index.native.js +1 -0
- package/dist/index.native.js.map +1 -1
- package/dist/keyframes.native.d.ts +27 -0
- package/dist/keyframes.native.d.ts.map +1 -0
- package/dist/keyframes.native.js +167 -0
- package/dist/keyframes.native.js.map +1 -0
- package/package.json +2 -1
- package/src/generated/animation.ts +1 -0
- package/src/index.native.ts +5 -0
- package/src/keyframes.native.ts +210 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# @hozo/engine
|
|
2
|
+
|
|
3
|
+
## 0.2.0
|
|
4
|
+
|
|
5
|
+
- Add the Native `useHozoKeyframes` hook and generated animation ABI for the supported project-keyframe subset.
|
|
6
|
+
- Keep the compiled-output engine aligned with 0.2.0 motion/theme lowering. Rebuild output when upgrading rather than mixing compiler and engine versions.
|
|
7
|
+
|
|
8
|
+
See the [Hozo release notes and migration steps](https://github.com/iray-tno/hozo/blob/main/CHANGELOG.md#020) for cross-package changes and measured boundaries.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"animation.d.ts","sourceRoot":"","sources":["../../src/generated/animation.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,aAAa,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA"}
|
|
1
|
+
{"version":3,"file":"animation.d.ts","sourceRoot":"","sources":["../../src/generated/animation.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,aAAa,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AACzE,OAAO,EAAE,KAAK,YAAY,EAAE,KAAK,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"animation.js","sourceRoot":"","sources":["../../src/generated/animation.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,OAAO,EAAsB,gBAAgB,EAAE,MAAM,oBAAoB,CAAA"}
|
|
1
|
+
{"version":3,"file":"animation.js","sourceRoot":"","sources":["../../src/generated/animation.ts"],"names":[],"mappings":"AAAA,uCAAuC;AACvC,OAAO,EAAsB,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AACzE,OAAO,EAA6C,gBAAgB,EAAE,MAAM,wBAAwB,CAAA"}
|
package/dist/index.native.d.ts
CHANGED
|
@@ -4,5 +4,6 @@ export { type HozoDomStyle, hozoDomProps, hozoDomStyle } from './dom-style.nativ
|
|
|
4
4
|
export { type HozoAnimation, useHozoAnimation, useHozoBreakpoint, useHozoDark, useHozoEnvironment, useHozoViewport, useHozoWidthAtLeast, } from './hooks.native.ts';
|
|
5
5
|
export { hozoImageSource } from './image-source.native.ts';
|
|
6
6
|
export * from './index.ts';
|
|
7
|
+
export { type HozoKeyframe, type HozoKeyframesSpec, useHozoKeyframes, } from './keyframes.native.ts';
|
|
7
8
|
export { type HozoSafeAreaInsets, useHozoSafeArea } from './safe-area.native.ts';
|
|
8
9
|
//# sourceMappingURL=index.native.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.native.d.ts","sourceRoot":"","sources":["../src/index.native.ts"],"names":[],"mappings":"AAmBA,OAAO,wBAAwB,CAAA;AAE/B,YAAY,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AAI5D,OAAO,EAAE,KAAK,YAAY,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAA;AACrF,OAAO,EACL,KAAK,aAAa,EAClB,gBAAgB,EAChB,iBAAiB,EACjB,WAAW,EACX,kBAAkB,EAClB,eAAe,EACf,mBAAmB,GACpB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAA;AAC1D,cAAc,YAAY,CAAA;AAC1B,OAAO,EAAE,KAAK,kBAAkB,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.native.d.ts","sourceRoot":"","sources":["../src/index.native.ts"],"names":[],"mappings":"AAmBA,OAAO,wBAAwB,CAAA;AAE/B,YAAY,EAAE,cAAc,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AAI5D,OAAO,EAAE,KAAK,YAAY,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAA;AACrF,OAAO,EACL,KAAK,aAAa,EAClB,gBAAgB,EAChB,iBAAiB,EACjB,WAAW,EACX,kBAAkB,EAClB,eAAe,EACf,mBAAmB,GACpB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAA;AAC1D,cAAc,YAAY,CAAA;AAC1B,OAAO,EACL,KAAK,YAAY,EACjB,KAAK,iBAAiB,EACtB,gBAAgB,GACjB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,EAAE,KAAK,kBAAkB,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAA"}
|
package/dist/index.native.js
CHANGED
|
@@ -24,5 +24,6 @@ export { hozoDomProps, hozoDomStyle } from './dom-style.native.js';
|
|
|
24
24
|
export { useHozoAnimation, useHozoBreakpoint, useHozoDark, useHozoEnvironment, useHozoViewport, useHozoWidthAtLeast, } from './hooks.native.js';
|
|
25
25
|
export { hozoImageSource } from './image-source.native.js';
|
|
26
26
|
export * from './index.js';
|
|
27
|
+
export { useHozoKeyframes, } from './keyframes.native.js';
|
|
27
28
|
export { useHozoSafeArea } from './safe-area.native.js';
|
|
28
29
|
//# sourceMappingURL=index.native.js.map
|
package/dist/index.native.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.native.js","sourceRoot":"","sources":["../src/index.native.ts"],"names":[],"mappings":"AAAA,mDAAmD;AACnD,EAAE;AACF,yEAAyE;AACzE,wEAAwE;AACxE,sEAAsE;AACtE,wEAAwE;AACxE,6BAA6B;AAC7B,EAAE;AACF,yEAAyE;AACzE,4EAA4E;AAC5E,2EAA2E;AAC3E,0EAA0E;AAC1E,qBAAqB;AAErB,mEAAmE;AACnE,kEAAkE;AAClE,0EAA0E;AAC1E,yEAAyE;AACzE,mBAAmB;AACnB,OAAO,wBAAwB,CAAA;AAG/B,sEAAsE;AACtE,0EAA0E;AAC1E,gCAAgC;AAChC,OAAO,EAAqB,YAAY,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAA;AACrF,OAAO,EAEL,gBAAgB,EAChB,iBAAiB,EACjB,WAAW,EACX,kBAAkB,EAClB,eAAe,EACf,mBAAmB,GACpB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAA;AAC1D,cAAc,YAAY,CAAA;AAC1B,OAAO,EAA2B,eAAe,EAAE,MAAM,uBAAuB,CAAA"}
|
|
1
|
+
{"version":3,"file":"index.native.js","sourceRoot":"","sources":["../src/index.native.ts"],"names":[],"mappings":"AAAA,mDAAmD;AACnD,EAAE;AACF,yEAAyE;AACzE,wEAAwE;AACxE,sEAAsE;AACtE,wEAAwE;AACxE,6BAA6B;AAC7B,EAAE;AACF,yEAAyE;AACzE,4EAA4E;AAC5E,2EAA2E;AAC3E,0EAA0E;AAC1E,qBAAqB;AAErB,mEAAmE;AACnE,kEAAkE;AAClE,0EAA0E;AAC1E,yEAAyE;AACzE,mBAAmB;AACnB,OAAO,wBAAwB,CAAA;AAG/B,sEAAsE;AACtE,0EAA0E;AAC1E,gCAAgC;AAChC,OAAO,EAAqB,YAAY,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAA;AACrF,OAAO,EAEL,gBAAgB,EAChB,iBAAiB,EACjB,WAAW,EACX,kBAAkB,EAClB,eAAe,EACf,mBAAmB,GACpB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAA;AAC1D,cAAc,YAAY,CAAA;AAC1B,OAAO,EAGL,gBAAgB,GACjB,MAAM,uBAAuB,CAAA;AAC9B,OAAO,EAA2B,eAAe,EAAE,MAAM,uBAAuB,CAAA"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export interface HozoKeyframe {
|
|
2
|
+
/** Where the frame sits, 0 to 1: `from`, `50%`, `to`. */
|
|
3
|
+
at: number;
|
|
4
|
+
/** The frame as a React Native style. */
|
|
5
|
+
style: {
|
|
6
|
+
opacity?: number;
|
|
7
|
+
backgroundColor?: string;
|
|
8
|
+
borderColor?: string;
|
|
9
|
+
color?: string;
|
|
10
|
+
transform?: Record<string, number | string>[];
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export interface HozoKeyframesSpec {
|
|
14
|
+
frames: HozoKeyframe[];
|
|
15
|
+
/** `animation-duration`, in milliseconds. */
|
|
16
|
+
duration: number;
|
|
17
|
+
/** `animation-delay`, in milliseconds. */
|
|
18
|
+
delay?: number;
|
|
19
|
+
/** `animation-timing-function`: a keyword, or a cubic-bezier's four numbers. */
|
|
20
|
+
easing?: 'linear' | 'ease' | 'ease-in' | 'ease-out' | 'ease-in-out' | [number, number, number, number];
|
|
21
|
+
/** `animation-iteration-count`; `-1` is `infinite`. */
|
|
22
|
+
iterations?: number;
|
|
23
|
+
direction?: 'normal' | 'reverse' | 'alternate' | 'alternate-reverse';
|
|
24
|
+
fillMode?: 'none' | 'forwards' | 'backwards' | 'both';
|
|
25
|
+
}
|
|
26
|
+
export declare function useHozoKeyframes(spec: HozoKeyframesSpec): Record<string, unknown> | null;
|
|
27
|
+
//# sourceMappingURL=keyframes.native.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keyframes.native.d.ts","sourceRoot":"","sources":["../src/keyframes.native.ts"],"names":[],"mappings":"AAiBA,MAAM,WAAW,YAAY;IAC3B,yDAAyD;IACzD,EAAE,EAAE,MAAM,CAAA;IACV,yCAAyC;IACzC,KAAK,EAAE;QACL,OAAO,CAAC,EAAE,MAAM,CAAA;QAChB,eAAe,CAAC,EAAE,MAAM,CAAA;QACxB,WAAW,CAAC,EAAE,MAAM,CAAA;QACpB,KAAK,CAAC,EAAE,MAAM,CAAA;QACd,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,EAAE,CAAA;KAC9C,CAAA;CACF;AAED,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,YAAY,EAAE,CAAA;IACtB,6CAA6C;IAC7C,QAAQ,EAAE,MAAM,CAAA;IAChB,0CAA0C;IAC1C,KAAK,CAAC,EAAE,MAAM,CAAA;IACd,gFAAgF;IAChF,MAAM,CAAC,EACH,QAAQ,GACR,MAAM,GACN,SAAS,GACT,UAAU,GACV,aAAa,GACb,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,CAAA;IACpC,uDAAuD;IACvD,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,SAAS,CAAC,EAAE,QAAQ,GAAG,SAAS,GAAG,WAAW,GAAG,mBAAmB,CAAA;IACpE,QAAQ,CAAC,EAAE,MAAM,GAAG,UAAU,GAAG,WAAW,GAAG,MAAM,CAAA;CACtD;AAoDD,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,iBAAiB,kCA6GvD"}
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
// A project's own `@keyframes`, on React Native (decision 007, slice 3).
|
|
2
|
+
//
|
|
3
|
+
// `useHozoAnimation` runs Tailwind's four loops, whose frames are known in
|
|
4
|
+
// advance and written out by hand. This runs frames the compiler read from
|
|
5
|
+
// the project: a StyleX `stylex.keyframes(...)`, or a Tailwind theme's
|
|
6
|
+
// `--animate-*` and its `@keyframes`. The compiler hands over the frames as
|
|
7
|
+
// React Native style objects and the timing as CSS said it; this turns
|
|
8
|
+
// them into one progress value, interpolated per property.
|
|
9
|
+
//
|
|
10
|
+
// What it can move is what `HozoAnimated` can: opacity, the colours, and
|
|
11
|
+
// transforms. The compiler leaves out anything else and says so, because a
|
|
12
|
+
// width that jumped from frame to frame would be a different animation
|
|
13
|
+
// from the one on the Web.
|
|
14
|
+
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
15
|
+
import { Animated, Easing } from 'react-native';
|
|
16
|
+
const COLOR_KEYS = ['backgroundColor', 'borderColor', 'color'];
|
|
17
|
+
/** What a transform key is when no frame sets it. */
|
|
18
|
+
function identity(key) {
|
|
19
|
+
if (key.startsWith('scale'))
|
|
20
|
+
return 1;
|
|
21
|
+
if (key.startsWith('rotate') || key.startsWith('skew'))
|
|
22
|
+
return '0deg';
|
|
23
|
+
return 0;
|
|
24
|
+
}
|
|
25
|
+
function easingFor(easing) {
|
|
26
|
+
if (Array.isArray(easing))
|
|
27
|
+
return Easing.bezier(...easing);
|
|
28
|
+
switch (easing) {
|
|
29
|
+
case 'linear':
|
|
30
|
+
return Easing.linear;
|
|
31
|
+
case 'ease-in':
|
|
32
|
+
return Easing.bezier(0.42, 0, 1, 1);
|
|
33
|
+
case 'ease-out':
|
|
34
|
+
return Easing.bezier(0, 0, 0.58, 1);
|
|
35
|
+
case 'ease-in-out':
|
|
36
|
+
return Easing.bezier(0.42, 0, 0.58, 1);
|
|
37
|
+
default:
|
|
38
|
+
// CSS's initial value, and what `animation` means with none written.
|
|
39
|
+
return Easing.bezier(0.25, 0.1, 0.25, 1);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* One property's track: the frames that set it, in order, with the ends
|
|
44
|
+
* filled in where no frame does. CSS fills them from the element's own
|
|
45
|
+
* value; this fills them with the identity, which is the element's own
|
|
46
|
+
* value unless it has an opacity or a transform of its own.
|
|
47
|
+
*/
|
|
48
|
+
function track(points, fill) {
|
|
49
|
+
const sorted = [...points].sort((a, b) => a.at - b.at);
|
|
50
|
+
if (fill !== undefined) {
|
|
51
|
+
if (sorted[0]?.at !== 0)
|
|
52
|
+
sorted.unshift({ at: 0, value: fill });
|
|
53
|
+
if (sorted[sorted.length - 1]?.at !== 1)
|
|
54
|
+
sorted.push({ at: 1, value: fill });
|
|
55
|
+
}
|
|
56
|
+
// `interpolate` needs a strictly increasing input range. Two frames at the
|
|
57
|
+
// same point are the later one winning, as in CSS.
|
|
58
|
+
const deduped = [];
|
|
59
|
+
for (const point of sorted) {
|
|
60
|
+
if (deduped.length > 0 && deduped[deduped.length - 1]?.at === point.at)
|
|
61
|
+
deduped.pop();
|
|
62
|
+
deduped.push(point);
|
|
63
|
+
}
|
|
64
|
+
return deduped;
|
|
65
|
+
}
|
|
66
|
+
export function useHozoKeyframes(spec) {
|
|
67
|
+
const progress = useRef(new Animated.Value(0)).current;
|
|
68
|
+
const [phase, setPhase] = useState(spec.delay ? 'waiting' : 'running');
|
|
69
|
+
const signature = JSON.stringify(spec);
|
|
70
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: `signature` is the spec, compared by value
|
|
71
|
+
useEffect(() => {
|
|
72
|
+
const iterations = spec.iterations ?? 1;
|
|
73
|
+
const direction = spec.direction ?? 'normal';
|
|
74
|
+
const reversed = direction === 'reverse' || direction === 'alternate-reverse';
|
|
75
|
+
const alternates = direction === 'alternate' || direction === 'alternate-reverse';
|
|
76
|
+
const colours = spec.frames.some((frame) => COLOR_KEYS.some((key) => frame.style[key] !== undefined));
|
|
77
|
+
const leg = (toValue) => Animated.timing(progress, {
|
|
78
|
+
toValue,
|
|
79
|
+
duration: spec.duration,
|
|
80
|
+
easing: easingFor(spec.easing),
|
|
81
|
+
// React Native cannot interpolate a colour on the native driver.
|
|
82
|
+
useNativeDriver: !colours,
|
|
83
|
+
});
|
|
84
|
+
let animation;
|
|
85
|
+
if (alternates) {
|
|
86
|
+
const forth = leg(reversed ? 0 : 1);
|
|
87
|
+
const back = leg(reversed ? 1 : 0);
|
|
88
|
+
animation =
|
|
89
|
+
iterations < 0
|
|
90
|
+
? Animated.loop(Animated.sequence([forth, back]))
|
|
91
|
+
: Animated.sequence(Array.from({ length: iterations }, (_, i) => leg((i % 2 === 0) !== reversed ? 1 : 0)));
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
animation = Animated.loop(leg(reversed ? 0 : 1), {
|
|
95
|
+
iterations: iterations < 0 ? -1 : iterations,
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
progress.setValue(reversed ? 1 : 0);
|
|
99
|
+
let timer;
|
|
100
|
+
const start = () => {
|
|
101
|
+
setPhase('running');
|
|
102
|
+
animation.start(({ finished }) => {
|
|
103
|
+
if (finished)
|
|
104
|
+
setPhase('done');
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
if (spec.delay) {
|
|
108
|
+
setPhase('waiting');
|
|
109
|
+
timer = setTimeout(start, spec.delay);
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
start();
|
|
113
|
+
}
|
|
114
|
+
return () => {
|
|
115
|
+
clearTimeout(timer);
|
|
116
|
+
animation.stop();
|
|
117
|
+
};
|
|
118
|
+
}, [progress, signature]);
|
|
119
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: as above
|
|
120
|
+
const animated = useMemo(() => {
|
|
121
|
+
const style = {};
|
|
122
|
+
const range = (points) => ({
|
|
123
|
+
inputRange: points.map((point) => point.at),
|
|
124
|
+
// Numbers or unit strings, never mixed: the compiler writes one track in
|
|
125
|
+
// one unit. The cast is to the overload; the runtime takes either.
|
|
126
|
+
outputRange: points.map((point) => point.value),
|
|
127
|
+
});
|
|
128
|
+
const opacity = spec.frames.flatMap((frame) => frame.style.opacity === undefined ? [] : [{ at: frame.at, value: frame.style.opacity }]);
|
|
129
|
+
if (opacity.length > 0) {
|
|
130
|
+
style.opacity = progress.interpolate(range(track(opacity, 1)));
|
|
131
|
+
}
|
|
132
|
+
for (const key of COLOR_KEYS) {
|
|
133
|
+
const points = spec.frames.flatMap((frame) => frame.style[key] === undefined ? [] : [{ at: frame.at, value: frame.style[key] }]);
|
|
134
|
+
// No identity for a colour: the ends hold the nearest frame.
|
|
135
|
+
if (points.length > 0)
|
|
136
|
+
style[key] = progress.interpolate(range(track(points, undefined)));
|
|
137
|
+
}
|
|
138
|
+
const keys = [];
|
|
139
|
+
for (const frame of spec.frames) {
|
|
140
|
+
for (const entry of frame.style.transform ?? []) {
|
|
141
|
+
for (const key of Object.keys(entry))
|
|
142
|
+
if (!keys.includes(key))
|
|
143
|
+
keys.push(key);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
if (keys.length > 0) {
|
|
147
|
+
style.transform = keys.map((key) => {
|
|
148
|
+
const points = spec.frames.flatMap((frame) => {
|
|
149
|
+
const entry = frame.style.transform?.find((candidate) => key in candidate);
|
|
150
|
+
return entry ? [{ at: frame.at, value: entry[key] }] : [];
|
|
151
|
+
});
|
|
152
|
+
return { [key]: progress.interpolate(range(track(points, identity(key)))) };
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
return style;
|
|
156
|
+
}, [progress, signature]);
|
|
157
|
+
// Outside the animation's run, CSS shows the element's own style unless
|
|
158
|
+
// the fill mode says otherwise: `backwards` holds the first frame through
|
|
159
|
+
// the delay, `forwards` the last after the end.
|
|
160
|
+
const fill = spec.fillMode ?? 'none';
|
|
161
|
+
if (phase === 'waiting' && fill !== 'backwards' && fill !== 'both')
|
|
162
|
+
return null;
|
|
163
|
+
if (phase === 'done' && fill !== 'forwards' && fill !== 'both')
|
|
164
|
+
return null;
|
|
165
|
+
return animated;
|
|
166
|
+
}
|
|
167
|
+
//# sourceMappingURL=keyframes.native.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"keyframes.native.js","sourceRoot":"","sources":["../src/keyframes.native.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,EAAE;AACF,2EAA2E;AAC3E,2EAA2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CAAC,EAAE,EAAE,KAAK,EAAE,KAAK,CAAC,GAAG,CAAoB,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAA;gBAC9E,CAAC,CAAC,CAAA;gBACF,OAAO,EAAE,CAAC,GAAG,CAAC,EAAE,QAAQ,CAAC,WAAW,CAAC,KAAK,CAAC,KAAK,CAAC,MAAM,EAAE,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAA;YAC7E,CAAC,CAAC,CAAA;QACJ,CAAC;QACD,OAAO,KAAK,CAAA;IACd,CAAC,EAAE,CAAC,QAAQ,EAAE,SAAS,CAAC,CAAC,CAAA;IAEzB,wEAAwE;IACxE,0EAA0E;IAC1E,gDAAgD;IAChD,MAAM,IAAI,GAAG,IAAI,CAAC,QAAQ,IAAI,MAAM,CAAA;IACpC,IAAI,KAAK,KAAK,SAAS,IAAI,IAAI,KAAK,WAAW,IAAI,IAAI,KAAK,MAAM;QAAE,OAAO,IAAI,CAAA;IAC/E,IAAI,KAAK,KAAK,MAAM,IAAI,IAAI,KAAK,UAAU,IAAI,IAAI,KAAK,MAAM;QAAE,OAAO,IAAI,CAAA;IAC3E,OAAO,QAAQ,CAAA;AACjB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@hozo/engine",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"description": "Runtime fallback for genuinely dynamic styles, interactive behavior, and accessibility behavior.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"react-native",
|
|
@@ -88,6 +88,7 @@
|
|
|
88
88
|
"./package.json": "./package.json"
|
|
89
89
|
},
|
|
90
90
|
"files": [
|
|
91
|
+
"CHANGELOG.md",
|
|
91
92
|
"dist",
|
|
92
93
|
"src",
|
|
93
94
|
"!src/**/*.test.ts",
|
package/src/index.native.ts
CHANGED
|
@@ -35,4 +35,9 @@ export {
|
|
|
35
35
|
} from './hooks.native.ts'
|
|
36
36
|
export { hozoImageSource } from './image-source.native.ts'
|
|
37
37
|
export * from './index.ts'
|
|
38
|
+
export {
|
|
39
|
+
type HozoKeyframe,
|
|
40
|
+
type HozoKeyframesSpec,
|
|
41
|
+
useHozoKeyframes,
|
|
42
|
+
} from './keyframes.native.ts'
|
|
38
43
|
export { type HozoSafeAreaInsets, useHozoSafeArea } from './safe-area.native.ts'
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
// A project's own `@keyframes`, on React Native (decision 007, slice 3).
|
|
2
|
+
//
|
|
3
|
+
// `useHozoAnimation` runs Tailwind's four loops, whose frames are known in
|
|
4
|
+
// advance and written out by hand. This runs frames the compiler read from
|
|
5
|
+
// the project: a StyleX `stylex.keyframes(...)`, or a Tailwind theme's
|
|
6
|
+
// `--animate-*` and its `@keyframes`. The compiler hands over the frames as
|
|
7
|
+
// React Native style objects and the timing as CSS said it; this turns
|
|
8
|
+
// them into one progress value, interpolated per property.
|
|
9
|
+
//
|
|
10
|
+
// What it can move is what `HozoAnimated` can: opacity, the colours, and
|
|
11
|
+
// transforms. The compiler leaves out anything else and says so, because a
|
|
12
|
+
// width that jumped from frame to frame would be a different animation
|
|
13
|
+
// from the one on the Web.
|
|
14
|
+
|
|
15
|
+
import { useEffect, useMemo, useRef, useState } from 'react'
|
|
16
|
+
import { Animated, Easing, type EasingFunction } from 'react-native'
|
|
17
|
+
|
|
18
|
+
export interface HozoKeyframe {
|
|
19
|
+
/** Where the frame sits, 0 to 1: `from`, `50%`, `to`. */
|
|
20
|
+
at: number
|
|
21
|
+
/** The frame as a React Native style. */
|
|
22
|
+
style: {
|
|
23
|
+
opacity?: number
|
|
24
|
+
backgroundColor?: string
|
|
25
|
+
borderColor?: string
|
|
26
|
+
color?: string
|
|
27
|
+
transform?: Record<string, number | string>[]
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface HozoKeyframesSpec {
|
|
32
|
+
frames: HozoKeyframe[]
|
|
33
|
+
/** `animation-duration`, in milliseconds. */
|
|
34
|
+
duration: number
|
|
35
|
+
/** `animation-delay`, in milliseconds. */
|
|
36
|
+
delay?: number
|
|
37
|
+
/** `animation-timing-function`: a keyword, or a cubic-bezier's four numbers. */
|
|
38
|
+
easing?:
|
|
39
|
+
| 'linear'
|
|
40
|
+
| 'ease'
|
|
41
|
+
| 'ease-in'
|
|
42
|
+
| 'ease-out'
|
|
43
|
+
| 'ease-in-out'
|
|
44
|
+
| [number, number, number, number]
|
|
45
|
+
/** `animation-iteration-count`; `-1` is `infinite`. */
|
|
46
|
+
iterations?: number
|
|
47
|
+
direction?: 'normal' | 'reverse' | 'alternate' | 'alternate-reverse'
|
|
48
|
+
fillMode?: 'none' | 'forwards' | 'backwards' | 'both'
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const COLOR_KEYS = ['backgroundColor', 'borderColor', 'color'] as const
|
|
52
|
+
|
|
53
|
+
/** What a transform key is when no frame sets it. */
|
|
54
|
+
function identity(key: string): number | string {
|
|
55
|
+
if (key.startsWith('scale')) return 1
|
|
56
|
+
if (key.startsWith('rotate') || key.startsWith('skew')) return '0deg'
|
|
57
|
+
return 0
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function easingFor(easing: HozoKeyframesSpec['easing']): EasingFunction {
|
|
61
|
+
if (Array.isArray(easing)) return Easing.bezier(...easing)
|
|
62
|
+
switch (easing) {
|
|
63
|
+
case 'linear':
|
|
64
|
+
return Easing.linear
|
|
65
|
+
case 'ease-in':
|
|
66
|
+
return Easing.bezier(0.42, 0, 1, 1)
|
|
67
|
+
case 'ease-out':
|
|
68
|
+
return Easing.bezier(0, 0, 0.58, 1)
|
|
69
|
+
case 'ease-in-out':
|
|
70
|
+
return Easing.bezier(0.42, 0, 0.58, 1)
|
|
71
|
+
default:
|
|
72
|
+
// CSS's initial value, and what `animation` means with none written.
|
|
73
|
+
return Easing.bezier(0.25, 0.1, 0.25, 1)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* One property's track: the frames that set it, in order, with the ends
|
|
79
|
+
* filled in where no frame does. CSS fills them from the element's own
|
|
80
|
+
* value; this fills them with the identity, which is the element's own
|
|
81
|
+
* value unless it has an opacity or a transform of its own.
|
|
82
|
+
*/
|
|
83
|
+
function track<T>(points: { at: number; value: T }[], fill: T | undefined) {
|
|
84
|
+
const sorted = [...points].sort((a, b) => a.at - b.at)
|
|
85
|
+
if (fill !== undefined) {
|
|
86
|
+
if (sorted[0]?.at !== 0) sorted.unshift({ at: 0, value: fill })
|
|
87
|
+
if (sorted[sorted.length - 1]?.at !== 1) sorted.push({ at: 1, value: fill })
|
|
88
|
+
}
|
|
89
|
+
// `interpolate` needs a strictly increasing input range. Two frames at the
|
|
90
|
+
// same point are the later one winning, as in CSS.
|
|
91
|
+
const deduped: typeof sorted = []
|
|
92
|
+
for (const point of sorted) {
|
|
93
|
+
if (deduped.length > 0 && deduped[deduped.length - 1]?.at === point.at) deduped.pop()
|
|
94
|
+
deduped.push(point)
|
|
95
|
+
}
|
|
96
|
+
return deduped
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
type Phase = 'waiting' | 'running' | 'done'
|
|
100
|
+
|
|
101
|
+
export function useHozoKeyframes(spec: HozoKeyframesSpec) {
|
|
102
|
+
const progress = useRef(new Animated.Value(0)).current
|
|
103
|
+
const [phase, setPhase] = useState<Phase>(spec.delay ? 'waiting' : 'running')
|
|
104
|
+
const signature = JSON.stringify(spec)
|
|
105
|
+
|
|
106
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: `signature` is the spec, compared by value
|
|
107
|
+
useEffect(() => {
|
|
108
|
+
const iterations = spec.iterations ?? 1
|
|
109
|
+
const direction = spec.direction ?? 'normal'
|
|
110
|
+
const reversed = direction === 'reverse' || direction === 'alternate-reverse'
|
|
111
|
+
const alternates = direction === 'alternate' || direction === 'alternate-reverse'
|
|
112
|
+
const colours = spec.frames.some((frame) =>
|
|
113
|
+
COLOR_KEYS.some((key) => frame.style[key] !== undefined),
|
|
114
|
+
)
|
|
115
|
+
const leg = (toValue: number) =>
|
|
116
|
+
Animated.timing(progress, {
|
|
117
|
+
toValue,
|
|
118
|
+
duration: spec.duration,
|
|
119
|
+
easing: easingFor(spec.easing),
|
|
120
|
+
// React Native cannot interpolate a colour on the native driver.
|
|
121
|
+
useNativeDriver: !colours,
|
|
122
|
+
})
|
|
123
|
+
|
|
124
|
+
let animation: Animated.CompositeAnimation
|
|
125
|
+
if (alternates) {
|
|
126
|
+
const forth = leg(reversed ? 0 : 1)
|
|
127
|
+
const back = leg(reversed ? 1 : 0)
|
|
128
|
+
animation =
|
|
129
|
+
iterations < 0
|
|
130
|
+
? Animated.loop(Animated.sequence([forth, back]))
|
|
131
|
+
: Animated.sequence(
|
|
132
|
+
Array.from({ length: iterations }, (_, i) => leg((i % 2 === 0) !== reversed ? 1 : 0)),
|
|
133
|
+
)
|
|
134
|
+
} else {
|
|
135
|
+
animation = Animated.loop(leg(reversed ? 0 : 1), {
|
|
136
|
+
iterations: iterations < 0 ? -1 : iterations,
|
|
137
|
+
})
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
progress.setValue(reversed ? 1 : 0)
|
|
141
|
+
let timer: ReturnType<typeof setTimeout> | undefined
|
|
142
|
+
const start = () => {
|
|
143
|
+
setPhase('running')
|
|
144
|
+
animation.start(({ finished }) => {
|
|
145
|
+
if (finished) setPhase('done')
|
|
146
|
+
})
|
|
147
|
+
}
|
|
148
|
+
if (spec.delay) {
|
|
149
|
+
setPhase('waiting')
|
|
150
|
+
timer = setTimeout(start, spec.delay)
|
|
151
|
+
} else {
|
|
152
|
+
start()
|
|
153
|
+
}
|
|
154
|
+
return () => {
|
|
155
|
+
clearTimeout(timer)
|
|
156
|
+
animation.stop()
|
|
157
|
+
}
|
|
158
|
+
}, [progress, signature])
|
|
159
|
+
|
|
160
|
+
// biome-ignore lint/correctness/useExhaustiveDependencies: as above
|
|
161
|
+
const animated = useMemo(() => {
|
|
162
|
+
const style: Record<string, unknown> = {}
|
|
163
|
+
const range = (points: { at: number; value: number | string }[]) => ({
|
|
164
|
+
inputRange: points.map((point) => point.at),
|
|
165
|
+
// Numbers or unit strings, never mixed: the compiler writes one track in
|
|
166
|
+
// one unit. The cast is to the overload; the runtime takes either.
|
|
167
|
+
outputRange: points.map((point) => point.value) as unknown as string[],
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
const opacity = spec.frames.flatMap((frame) =>
|
|
171
|
+
frame.style.opacity === undefined ? [] : [{ at: frame.at, value: frame.style.opacity }],
|
|
172
|
+
)
|
|
173
|
+
if (opacity.length > 0) {
|
|
174
|
+
style.opacity = progress.interpolate(range(track(opacity, 1)))
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
for (const key of COLOR_KEYS) {
|
|
178
|
+
const points = spec.frames.flatMap((frame) =>
|
|
179
|
+
frame.style[key] === undefined ? [] : [{ at: frame.at, value: frame.style[key] as string }],
|
|
180
|
+
)
|
|
181
|
+
// No identity for a colour: the ends hold the nearest frame.
|
|
182
|
+
if (points.length > 0) style[key] = progress.interpolate(range(track(points, undefined)))
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const keys: string[] = []
|
|
186
|
+
for (const frame of spec.frames) {
|
|
187
|
+
for (const entry of frame.style.transform ?? []) {
|
|
188
|
+
for (const key of Object.keys(entry)) if (!keys.includes(key)) keys.push(key)
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
if (keys.length > 0) {
|
|
192
|
+
style.transform = keys.map((key) => {
|
|
193
|
+
const points = spec.frames.flatMap((frame) => {
|
|
194
|
+
const entry = frame.style.transform?.find((candidate) => key in candidate)
|
|
195
|
+
return entry ? [{ at: frame.at, value: entry[key] as number | string }] : []
|
|
196
|
+
})
|
|
197
|
+
return { [key]: progress.interpolate(range(track(points, identity(key)))) }
|
|
198
|
+
})
|
|
199
|
+
}
|
|
200
|
+
return style
|
|
201
|
+
}, [progress, signature])
|
|
202
|
+
|
|
203
|
+
// Outside the animation's run, CSS shows the element's own style unless
|
|
204
|
+
// the fill mode says otherwise: `backwards` holds the first frame through
|
|
205
|
+
// the delay, `forwards` the last after the end.
|
|
206
|
+
const fill = spec.fillMode ?? 'none'
|
|
207
|
+
if (phase === 'waiting' && fill !== 'backwards' && fill !== 'both') return null
|
|
208
|
+
if (phase === 'done' && fill !== 'forwards' && fill !== 'both') return null
|
|
209
|
+
return animated
|
|
210
|
+
}
|