motion-dom 11.14.1 → 11.16.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/dist/cjs/index.js +633 -0
- package/dist/es/animation/controls/BaseGroup.mjs +82 -0
- package/dist/es/animation/controls/Group.mjs +13 -0
- package/dist/es/animation/generators/utils/calc-duration.mjs +17 -0
- package/dist/es/animation/generators/utils/create-generator-easing.mjs +19 -0
- package/dist/es/animation/generators/utils/is-generator.mjs +5 -0
- package/dist/es/animation/utils/get-value-transition.mjs +9 -0
- package/dist/es/animation/waapi/NativeAnimationControls.mjs +83 -0
- package/dist/es/animation/waapi/PseudoAnimation.mjs +15 -0
- package/dist/es/animation/waapi/utils/attach-timeline.mjs +6 -0
- package/dist/es/animation/waapi/utils/convert-options.mjs +55 -0
- package/dist/es/animation/waapi/utils/easing.mjs +44 -0
- package/dist/es/animation/waapi/utils/linear.mjs +14 -0
- package/dist/es/index.mjs +15 -1
- package/dist/es/utils/is-bezier-definition.mjs +3 -0
- package/dist/es/utils/supports/flags.mjs +9 -0
- package/dist/es/utils/supports/linear-easing.mjs +15 -0
- package/dist/es/utils/supports/memo.mjs +9 -0
- package/dist/es/utils/supports/scroll-timeline.mjs +5 -0
- package/dist/es/view/index.mjs +65 -0
- package/dist/es/view/start.mjs +146 -0
- package/dist/es/view/utils/choose-layer-type.mjs +11 -0
- package/dist/es/view/utils/css.mjs +32 -0
- package/dist/es/view/utils/get-layer-name.mjs +8 -0
- package/dist/es/view/utils/get-view-animations.mjs +13 -0
- package/dist/es/view/utils/has-target.mjs +5 -0
- package/dist/index.d.ts +648 -3
- package/package.json +10 -13
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { supportsScrollTimeline } from '../../utils/supports/scroll-timeline.mjs';
|
|
2
|
+
|
|
3
|
+
class BaseGroupPlaybackControls {
|
|
4
|
+
constructor(animations) {
|
|
5
|
+
// Bound to accomodate common `return animation.stop` pattern
|
|
6
|
+
this.stop = () => this.runAll("stop");
|
|
7
|
+
this.animations = animations.filter(Boolean);
|
|
8
|
+
}
|
|
9
|
+
get finished() {
|
|
10
|
+
// Support for new finished Promise and legacy thennable API
|
|
11
|
+
return Promise.all(this.animations.map((animation) => "finished" in animation ? animation.finished : animation));
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* TODO: Filter out cancelled or stopped animations before returning
|
|
15
|
+
*/
|
|
16
|
+
getAll(propName) {
|
|
17
|
+
return this.animations[0][propName];
|
|
18
|
+
}
|
|
19
|
+
setAll(propName, newValue) {
|
|
20
|
+
for (let i = 0; i < this.animations.length; i++) {
|
|
21
|
+
this.animations[i][propName] = newValue;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
attachTimeline(timeline, fallback) {
|
|
25
|
+
const subscriptions = this.animations.map((animation) => {
|
|
26
|
+
if (supportsScrollTimeline() && animation.attachTimeline) {
|
|
27
|
+
return animation.attachTimeline(timeline);
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
return fallback(animation);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
return () => {
|
|
34
|
+
subscriptions.forEach((cancel, i) => {
|
|
35
|
+
cancel && cancel();
|
|
36
|
+
this.animations[i].stop();
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
get time() {
|
|
41
|
+
return this.getAll("time");
|
|
42
|
+
}
|
|
43
|
+
set time(time) {
|
|
44
|
+
this.setAll("time", time);
|
|
45
|
+
}
|
|
46
|
+
get speed() {
|
|
47
|
+
return this.getAll("speed");
|
|
48
|
+
}
|
|
49
|
+
set speed(speed) {
|
|
50
|
+
this.setAll("speed", speed);
|
|
51
|
+
}
|
|
52
|
+
get startTime() {
|
|
53
|
+
return this.getAll("startTime");
|
|
54
|
+
}
|
|
55
|
+
get duration() {
|
|
56
|
+
let max = 0;
|
|
57
|
+
for (let i = 0; i < this.animations.length; i++) {
|
|
58
|
+
max = Math.max(max, this.animations[i].duration);
|
|
59
|
+
}
|
|
60
|
+
return max;
|
|
61
|
+
}
|
|
62
|
+
runAll(methodName) {
|
|
63
|
+
this.animations.forEach((controls) => controls[methodName]());
|
|
64
|
+
}
|
|
65
|
+
flatten() {
|
|
66
|
+
this.runAll("flatten");
|
|
67
|
+
}
|
|
68
|
+
play() {
|
|
69
|
+
this.runAll("play");
|
|
70
|
+
}
|
|
71
|
+
pause() {
|
|
72
|
+
this.runAll("pause");
|
|
73
|
+
}
|
|
74
|
+
cancel() {
|
|
75
|
+
this.runAll("cancel");
|
|
76
|
+
}
|
|
77
|
+
complete() {
|
|
78
|
+
this.runAll("complete");
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export { BaseGroupPlaybackControls };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { BaseGroupPlaybackControls } from './BaseGroup.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* TODO: This is a temporary class to support the legacy
|
|
5
|
+
* thennable API
|
|
6
|
+
*/
|
|
7
|
+
class GroupPlaybackControls extends BaseGroupPlaybackControls {
|
|
8
|
+
then(onResolve, onReject) {
|
|
9
|
+
return Promise.all(this.animations).then(onResolve).catch(onReject);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export { GroupPlaybackControls };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Implement a practical max duration for keyframe generation
|
|
3
|
+
* to prevent infinite loops
|
|
4
|
+
*/
|
|
5
|
+
const maxGeneratorDuration = 20000;
|
|
6
|
+
function calcGeneratorDuration(generator) {
|
|
7
|
+
let duration = 0;
|
|
8
|
+
const timeStep = 50;
|
|
9
|
+
let state = generator.next(duration);
|
|
10
|
+
while (!state.done && duration < maxGeneratorDuration) {
|
|
11
|
+
duration += timeStep;
|
|
12
|
+
state = generator.next(duration);
|
|
13
|
+
}
|
|
14
|
+
return duration >= maxGeneratorDuration ? Infinity : duration;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { calcGeneratorDuration, maxGeneratorDuration };
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { millisecondsToSeconds } from 'motion-utils';
|
|
2
|
+
import { calcGeneratorDuration, maxGeneratorDuration } from './calc-duration.mjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Create a progress => progress easing function from a generator.
|
|
6
|
+
*/
|
|
7
|
+
function createGeneratorEasing(options, scale = 100, createGenerator) {
|
|
8
|
+
const generator = createGenerator({ ...options, keyframes: [0, scale] });
|
|
9
|
+
const duration = Math.min(calcGeneratorDuration(generator), maxGeneratorDuration);
|
|
10
|
+
return {
|
|
11
|
+
type: "keyframes",
|
|
12
|
+
ease: (progress) => {
|
|
13
|
+
return generator.next(duration * progress).value / scale;
|
|
14
|
+
},
|
|
15
|
+
duration: millisecondsToSeconds(duration),
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { createGeneratorEasing };
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { millisecondsToSeconds, secondsToMilliseconds, noop } from 'motion-utils';
|
|
2
|
+
import { attachTimeline } from './utils/attach-timeline.mjs';
|
|
3
|
+
|
|
4
|
+
class NativeAnimationControls {
|
|
5
|
+
constructor(animation) {
|
|
6
|
+
this.animation = animation;
|
|
7
|
+
}
|
|
8
|
+
get duration() {
|
|
9
|
+
var _a, _b, _c;
|
|
10
|
+
const durationInMs = ((_b = (_a = this.animation) === null || _a === void 0 ? void 0 : _a.effect) === null || _b === void 0 ? void 0 : _b.getComputedTiming().duration) ||
|
|
11
|
+
((_c = this.options) === null || _c === void 0 ? void 0 : _c.duration) ||
|
|
12
|
+
300;
|
|
13
|
+
return millisecondsToSeconds(Number(durationInMs));
|
|
14
|
+
}
|
|
15
|
+
get time() {
|
|
16
|
+
var _a;
|
|
17
|
+
if (this.animation) {
|
|
18
|
+
return millisecondsToSeconds(((_a = this.animation) === null || _a === void 0 ? void 0 : _a.currentTime) || 0);
|
|
19
|
+
}
|
|
20
|
+
return 0;
|
|
21
|
+
}
|
|
22
|
+
set time(newTime) {
|
|
23
|
+
if (this.animation) {
|
|
24
|
+
this.animation.currentTime = secondsToMilliseconds(newTime);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
get speed() {
|
|
28
|
+
return this.animation ? this.animation.playbackRate : 1;
|
|
29
|
+
}
|
|
30
|
+
set speed(newSpeed) {
|
|
31
|
+
if (this.animation) {
|
|
32
|
+
this.animation.playbackRate = newSpeed;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
get state() {
|
|
36
|
+
return this.animation ? this.animation.playState : "finished";
|
|
37
|
+
}
|
|
38
|
+
get startTime() {
|
|
39
|
+
return this.animation ? this.animation.startTime : null;
|
|
40
|
+
}
|
|
41
|
+
get finished() {
|
|
42
|
+
return this.animation ? this.animation.finished : Promise.resolve();
|
|
43
|
+
}
|
|
44
|
+
play() {
|
|
45
|
+
this.animation && this.animation.play();
|
|
46
|
+
}
|
|
47
|
+
pause() {
|
|
48
|
+
this.animation && this.animation.pause();
|
|
49
|
+
}
|
|
50
|
+
stop() {
|
|
51
|
+
if (!this.animation ||
|
|
52
|
+
this.state === "idle" ||
|
|
53
|
+
this.state === "finished") {
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
if (this.animation.commitStyles) {
|
|
57
|
+
this.animation.commitStyles();
|
|
58
|
+
}
|
|
59
|
+
this.cancel();
|
|
60
|
+
}
|
|
61
|
+
flatten() {
|
|
62
|
+
var _a;
|
|
63
|
+
if (!this.animation)
|
|
64
|
+
return;
|
|
65
|
+
(_a = this.animation.effect) === null || _a === void 0 ? void 0 : _a.updateTiming({ easing: "linear" });
|
|
66
|
+
}
|
|
67
|
+
attachTimeline(timeline) {
|
|
68
|
+
if (this.animation)
|
|
69
|
+
attachTimeline(this.animation, timeline);
|
|
70
|
+
return noop;
|
|
71
|
+
}
|
|
72
|
+
complete() {
|
|
73
|
+
this.animation && this.animation.finish();
|
|
74
|
+
}
|
|
75
|
+
cancel() {
|
|
76
|
+
try {
|
|
77
|
+
this.animation && this.animation.cancel();
|
|
78
|
+
}
|
|
79
|
+
catch (e) { }
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export { NativeAnimationControls };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { NativeAnimationControls } from './NativeAnimationControls.mjs';
|
|
2
|
+
import { convertMotionOptionsToNative } from './utils/convert-options.mjs';
|
|
3
|
+
|
|
4
|
+
class PseudoAnimation extends NativeAnimationControls {
|
|
5
|
+
constructor(target, pseudoElement, valueName, keyframes, options) {
|
|
6
|
+
const animationOptions = convertMotionOptionsToNative(valueName, keyframes, options);
|
|
7
|
+
const animation = target.animate(animationOptions.keyframes, {
|
|
8
|
+
pseudoElement,
|
|
9
|
+
...animationOptions.options,
|
|
10
|
+
});
|
|
11
|
+
super(animation);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { PseudoAnimation };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { secondsToMilliseconds } from 'motion-utils';
|
|
2
|
+
import { supportsLinearEasing } from '../../../utils/supports/linear-easing.mjs';
|
|
3
|
+
import { createGeneratorEasing } from '../../generators/utils/create-generator-easing.mjs';
|
|
4
|
+
import { isGenerator } from '../../generators/utils/is-generator.mjs';
|
|
5
|
+
import { mapEasingToNativeEasing } from './easing.mjs';
|
|
6
|
+
|
|
7
|
+
const defaultEasing = "easeOut";
|
|
8
|
+
function applyGeneratorOptions(options) {
|
|
9
|
+
var _a;
|
|
10
|
+
if (isGenerator(options.type)) {
|
|
11
|
+
const generatorOptions = createGeneratorEasing(options, 100, options.type);
|
|
12
|
+
options.ease = supportsLinearEasing()
|
|
13
|
+
? generatorOptions.ease
|
|
14
|
+
: defaultEasing;
|
|
15
|
+
options.duration = secondsToMilliseconds(generatorOptions.duration);
|
|
16
|
+
options.type = "keyframes";
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
options.duration = secondsToMilliseconds((_a = options.duration) !== null && _a !== void 0 ? _a : 0.3);
|
|
20
|
+
options.ease = options.ease || defaultEasing;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
// TODO: Reuse for NativeAnimation
|
|
24
|
+
function convertMotionOptionsToNative(valueName, keyframes, options) {
|
|
25
|
+
var _a;
|
|
26
|
+
const nativeKeyframes = {};
|
|
27
|
+
const nativeOptions = {
|
|
28
|
+
fill: "both",
|
|
29
|
+
easing: "linear",
|
|
30
|
+
composite: "replace",
|
|
31
|
+
};
|
|
32
|
+
nativeOptions.delay = secondsToMilliseconds((_a = options.delay) !== null && _a !== void 0 ? _a : 0);
|
|
33
|
+
applyGeneratorOptions(options);
|
|
34
|
+
nativeOptions.duration = options.duration;
|
|
35
|
+
const { ease, times } = options;
|
|
36
|
+
if (times)
|
|
37
|
+
nativeKeyframes.offset = times;
|
|
38
|
+
nativeKeyframes[valueName] = keyframes;
|
|
39
|
+
const easing = mapEasingToNativeEasing(ease, options.duration);
|
|
40
|
+
/**
|
|
41
|
+
* If this is an easing array, apply to keyframes, not animation as a whole
|
|
42
|
+
*/
|
|
43
|
+
if (Array.isArray(easing)) {
|
|
44
|
+
nativeKeyframes.easing = easing;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
nativeOptions.easing = easing;
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
keyframes: nativeKeyframes,
|
|
51
|
+
options: nativeOptions,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export { applyGeneratorOptions, convertMotionOptionsToNative };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { isBezierDefinition } from '../../../utils/is-bezier-definition.mjs';
|
|
2
|
+
import { supportsLinearEasing } from '../../../utils/supports/linear-easing.mjs';
|
|
3
|
+
import { generateLinearEasing } from './linear.mjs';
|
|
4
|
+
|
|
5
|
+
function isWaapiSupportedEasing(easing) {
|
|
6
|
+
return Boolean((typeof easing === "function" && supportsLinearEasing()) ||
|
|
7
|
+
!easing ||
|
|
8
|
+
(typeof easing === "string" &&
|
|
9
|
+
(easing in supportedWaapiEasing || supportsLinearEasing())) ||
|
|
10
|
+
isBezierDefinition(easing) ||
|
|
11
|
+
(Array.isArray(easing) && easing.every(isWaapiSupportedEasing)));
|
|
12
|
+
}
|
|
13
|
+
const cubicBezierAsString = ([a, b, c, d]) => `cubic-bezier(${a}, ${b}, ${c}, ${d})`;
|
|
14
|
+
const supportedWaapiEasing = {
|
|
15
|
+
linear: "linear",
|
|
16
|
+
ease: "ease",
|
|
17
|
+
easeIn: "ease-in",
|
|
18
|
+
easeOut: "ease-out",
|
|
19
|
+
easeInOut: "ease-in-out",
|
|
20
|
+
circIn: /*@__PURE__*/ cubicBezierAsString([0, 0.65, 0.55, 1]),
|
|
21
|
+
circOut: /*@__PURE__*/ cubicBezierAsString([0.55, 0, 1, 0.45]),
|
|
22
|
+
backIn: /*@__PURE__*/ cubicBezierAsString([0.31, 0.01, 0.66, -0.59]),
|
|
23
|
+
backOut: /*@__PURE__*/ cubicBezierAsString([0.33, 1.53, 0.69, 0.99]),
|
|
24
|
+
};
|
|
25
|
+
function mapEasingToNativeEasing(easing, duration) {
|
|
26
|
+
if (!easing) {
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
else if (typeof easing === "function" && supportsLinearEasing()) {
|
|
30
|
+
return generateLinearEasing(easing, duration);
|
|
31
|
+
}
|
|
32
|
+
else if (isBezierDefinition(easing)) {
|
|
33
|
+
return cubicBezierAsString(easing);
|
|
34
|
+
}
|
|
35
|
+
else if (Array.isArray(easing)) {
|
|
36
|
+
return easing.map((segmentEasing) => mapEasingToNativeEasing(segmentEasing, duration) ||
|
|
37
|
+
supportedWaapiEasing.easeOut);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
return supportedWaapiEasing[easing];
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export { cubicBezierAsString, isWaapiSupportedEasing, mapEasingToNativeEasing, supportedWaapiEasing };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { progress } from 'motion-utils';
|
|
2
|
+
|
|
3
|
+
const generateLinearEasing = (easing, duration, // as milliseconds
|
|
4
|
+
resolution = 10 // as milliseconds
|
|
5
|
+
) => {
|
|
6
|
+
let points = "";
|
|
7
|
+
const numPoints = Math.max(Math.round(duration / resolution), 2);
|
|
8
|
+
for (let i = 0; i < numPoints; i++) {
|
|
9
|
+
points += easing(progress(0, numPoints - 1, i)) + ", ";
|
|
10
|
+
}
|
|
11
|
+
return `linear(${points.substring(0, points.length - 2)})`;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export { generateLinearEasing };
|
package/dist/es/index.mjs
CHANGED
|
@@ -1,7 +1,21 @@
|
|
|
1
|
+
export { GroupPlaybackControls } from './animation/controls/Group.mjs';
|
|
2
|
+
export { getValueTransition } from './animation/utils/get-value-transition.mjs';
|
|
3
|
+
export { calcGeneratorDuration, maxGeneratorDuration } from './animation/generators/utils/calc-duration.mjs';
|
|
4
|
+
export { createGeneratorEasing } from './animation/generators/utils/create-generator-easing.mjs';
|
|
5
|
+
export { isGenerator } from './animation/generators/utils/is-generator.mjs';
|
|
6
|
+
export { NativeAnimationControls } from './animation/waapi/NativeAnimationControls.mjs';
|
|
7
|
+
export { attachTimeline } from './animation/waapi/utils/attach-timeline.mjs';
|
|
8
|
+
export { cubicBezierAsString, isWaapiSupportedEasing, mapEasingToNativeEasing, supportedWaapiEasing } from './animation/waapi/utils/easing.mjs';
|
|
9
|
+
export { generateLinearEasing } from './animation/waapi/utils/linear.mjs';
|
|
1
10
|
export { hover } from './gestures/hover.mjs';
|
|
2
11
|
export { press } from './gestures/press/index.mjs';
|
|
12
|
+
export { isBezierDefinition } from './utils/is-bezier-definition.mjs';
|
|
3
13
|
export { resolveElements } from './utils/resolve-elements.mjs';
|
|
14
|
+
export { supportsFlags } from './utils/supports/flags.mjs';
|
|
15
|
+
export { supportsLinearEasing } from './utils/supports/linear-easing.mjs';
|
|
16
|
+
export { supportsScrollTimeline } from './utils/supports/scroll-timeline.mjs';
|
|
17
|
+
export { ViewTransitionBuilder, view } from './view/index.mjs';
|
|
4
18
|
export { isDragActive, isDragging } from './gestures/drag/state/is-active.mjs';
|
|
5
19
|
export { setDragLock } from './gestures/drag/state/set-active.mjs';
|
|
6
|
-
export { isPrimaryPointer } from './gestures/utils/is-primary-pointer.mjs';
|
|
7
20
|
export { isNodeOrChild } from './gestures/utils/is-node-or-child.mjs';
|
|
21
|
+
export { isPrimaryPointer } from './gestures/utils/is-primary-pointer.mjs';
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { memoSupports } from './memo.mjs';
|
|
2
|
+
|
|
3
|
+
const supportsLinearEasing = /*@__PURE__*/ memoSupports(() => {
|
|
4
|
+
try {
|
|
5
|
+
document
|
|
6
|
+
.createElement("div")
|
|
7
|
+
.animate({ opacity: 0 }, { easing: "linear(0, 1)" });
|
|
8
|
+
}
|
|
9
|
+
catch (e) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
return true;
|
|
13
|
+
}, "linearEasing");
|
|
14
|
+
|
|
15
|
+
export { supportsLinearEasing };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { memo } from 'motion-utils';
|
|
2
|
+
import { supportsFlags } from './flags.mjs';
|
|
3
|
+
|
|
4
|
+
function memoSupports(callback, supportsFlag) {
|
|
5
|
+
const memoized = memo(callback);
|
|
6
|
+
return () => { var _a; return (_a = supportsFlags[supportsFlag]) !== null && _a !== void 0 ? _a : memoized(); };
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export { memoSupports };
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { startViewAnimation } from './start.mjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* TODO:
|
|
5
|
+
* - Create view transition on next tick
|
|
6
|
+
* - Replace animations with Motion animations
|
|
7
|
+
* - Return GroupAnimation on next tick
|
|
8
|
+
*/
|
|
9
|
+
class ViewTransitionBuilder {
|
|
10
|
+
constructor(update, options = {}) {
|
|
11
|
+
this.currentTarget = "root";
|
|
12
|
+
this.targets = new Map();
|
|
13
|
+
this.readyPromise = new Promise((resolve) => {
|
|
14
|
+
this.notifyReady = resolve;
|
|
15
|
+
});
|
|
16
|
+
queueMicrotask(() => {
|
|
17
|
+
startViewAnimation(update, options, this.targets).then((animation) => this.notifyReady(animation));
|
|
18
|
+
});
|
|
19
|
+
}
|
|
20
|
+
get(selector) {
|
|
21
|
+
this.currentTarget = selector;
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
layout(keyframes, options) {
|
|
25
|
+
this.updateTarget("layout", keyframes, options);
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
new(keyframes, options) {
|
|
29
|
+
this.updateTarget("new", keyframes, options);
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
old(keyframes, options) {
|
|
33
|
+
this.updateTarget("old", keyframes, options);
|
|
34
|
+
return this;
|
|
35
|
+
}
|
|
36
|
+
enter(keyframes, options) {
|
|
37
|
+
this.updateTarget("enter", keyframes, options);
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
exit(keyframes, options) {
|
|
41
|
+
this.updateTarget("exit", keyframes, options);
|
|
42
|
+
return this;
|
|
43
|
+
}
|
|
44
|
+
crossfade(options) {
|
|
45
|
+
this.updateTarget("enter", { opacity: 1 }, options);
|
|
46
|
+
this.updateTarget("exit", { opacity: 0 }, options);
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
updateTarget(target, keyframes, options = {}) {
|
|
50
|
+
const { currentTarget, targets } = this;
|
|
51
|
+
if (!targets.has(currentTarget)) {
|
|
52
|
+
targets.set(currentTarget, {});
|
|
53
|
+
}
|
|
54
|
+
const targetData = targets.get(currentTarget);
|
|
55
|
+
targetData[target] = { keyframes, options };
|
|
56
|
+
}
|
|
57
|
+
then(resolve, reject) {
|
|
58
|
+
return this.readyPromise.then(resolve, reject);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function view(update, defaultOptions = {}) {
|
|
62
|
+
return new ViewTransitionBuilder(update, defaultOptions);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export { ViewTransitionBuilder, view };
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { secondsToMilliseconds } from 'motion-utils';
|
|
2
|
+
import { BaseGroupPlaybackControls } from '../animation/controls/BaseGroup.mjs';
|
|
3
|
+
import { getValueTransition } from '../animation/utils/get-value-transition.mjs';
|
|
4
|
+
import { NativeAnimationControls } from '../animation/waapi/NativeAnimationControls.mjs';
|
|
5
|
+
import { PseudoAnimation } from '../animation/waapi/PseudoAnimation.mjs';
|
|
6
|
+
import { applyGeneratorOptions } from '../animation/waapi/utils/convert-options.mjs';
|
|
7
|
+
import { mapEasingToNativeEasing } from '../animation/waapi/utils/easing.mjs';
|
|
8
|
+
import { chooseLayerType } from './utils/choose-layer-type.mjs';
|
|
9
|
+
import { css } from './utils/css.mjs';
|
|
10
|
+
import { getLayerName } from './utils/get-layer-name.mjs';
|
|
11
|
+
import { getViewAnimations } from './utils/get-view-animations.mjs';
|
|
12
|
+
import { hasTarget } from './utils/has-target.mjs';
|
|
13
|
+
|
|
14
|
+
const definitionNames = ["layout", "enter", "exit", "new", "old"];
|
|
15
|
+
function startViewAnimation(update, defaultOptions, targets) {
|
|
16
|
+
if (!document.startViewTransition) {
|
|
17
|
+
return new Promise(async (resolve) => {
|
|
18
|
+
await update();
|
|
19
|
+
resolve(new BaseGroupPlaybackControls([]));
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
// TODO: Go over existing targets and ensure they all have ids
|
|
23
|
+
/**
|
|
24
|
+
* If we don't have any animations defined for the root target,
|
|
25
|
+
* remove it from being captured.
|
|
26
|
+
*/
|
|
27
|
+
if (!hasTarget("root", targets)) {
|
|
28
|
+
css.set(":root", {
|
|
29
|
+
"view-transition-name": "none",
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Set the timing curve to linear for all view transition layers.
|
|
34
|
+
* This gets baked into the keyframes, which can't be changed
|
|
35
|
+
* without breaking the generated animation.
|
|
36
|
+
*
|
|
37
|
+
* This allows us to set easing via updateTiming - which can be changed.
|
|
38
|
+
*/
|
|
39
|
+
css.set("::view-transition-group(*), ::view-transition-old(*), ::view-transition-new(*)", { "animation-timing-function": "linear !important" });
|
|
40
|
+
css.commit(); // Write
|
|
41
|
+
const transition = document.startViewTransition(async () => {
|
|
42
|
+
await update();
|
|
43
|
+
// TODO: Go over new targets and ensure they all have ids
|
|
44
|
+
});
|
|
45
|
+
transition.finished.finally(() => {
|
|
46
|
+
css.remove(); // Write
|
|
47
|
+
});
|
|
48
|
+
return new Promise((resolve) => {
|
|
49
|
+
transition.ready.then(() => {
|
|
50
|
+
var _a;
|
|
51
|
+
const generatedViewAnimations = getViewAnimations();
|
|
52
|
+
const animations = [];
|
|
53
|
+
/**
|
|
54
|
+
* Create animations for our definitions
|
|
55
|
+
*/
|
|
56
|
+
targets.forEach((definition, target) => {
|
|
57
|
+
// TODO: If target is not "root", resolve elements
|
|
58
|
+
// and iterate over each
|
|
59
|
+
for (const key of definitionNames) {
|
|
60
|
+
if (!definition[key])
|
|
61
|
+
continue;
|
|
62
|
+
const { keyframes, options } = definition[key];
|
|
63
|
+
for (let [valueName, valueKeyframes] of Object.entries(keyframes)) {
|
|
64
|
+
if (!valueKeyframes)
|
|
65
|
+
continue;
|
|
66
|
+
const valueOptions = {
|
|
67
|
+
...getValueTransition(defaultOptions, valueName),
|
|
68
|
+
...getValueTransition(options, valueName),
|
|
69
|
+
};
|
|
70
|
+
const type = chooseLayerType(key);
|
|
71
|
+
/**
|
|
72
|
+
* If this is an opacity animation, and keyframes are not an array,
|
|
73
|
+
* we need to convert them into an array and set an initial value.
|
|
74
|
+
*/
|
|
75
|
+
if (valueName === "opacity" &&
|
|
76
|
+
!Array.isArray(valueKeyframes)) {
|
|
77
|
+
const initialValue = type === "new" ? 0 : 1;
|
|
78
|
+
valueKeyframes = [initialValue, valueKeyframes];
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Resolve stagger function if provided.
|
|
82
|
+
*/
|
|
83
|
+
if (typeof valueOptions.delay === "function") {
|
|
84
|
+
valueOptions.delay = valueOptions.delay(0, 1);
|
|
85
|
+
}
|
|
86
|
+
const animation = new PseudoAnimation(document.documentElement, `::view-transition-${type}(${target})`, valueName, valueKeyframes, valueOptions);
|
|
87
|
+
animations.push(animation);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
/**
|
|
92
|
+
* Handle browser generated animations
|
|
93
|
+
*/
|
|
94
|
+
for (const animation of generatedViewAnimations) {
|
|
95
|
+
if (animation.playState === "finished")
|
|
96
|
+
continue;
|
|
97
|
+
const { effect } = animation;
|
|
98
|
+
if (!effect || !(effect instanceof KeyframeEffect))
|
|
99
|
+
continue;
|
|
100
|
+
const { pseudoElement } = effect;
|
|
101
|
+
if (!pseudoElement)
|
|
102
|
+
continue;
|
|
103
|
+
const name = getLayerName(pseudoElement);
|
|
104
|
+
if (!name)
|
|
105
|
+
continue;
|
|
106
|
+
const targetDefinition = targets.get(name.layer);
|
|
107
|
+
if (!targetDefinition) {
|
|
108
|
+
/**
|
|
109
|
+
* If transition name is group then update the timing of the animation
|
|
110
|
+
* whereas if it's old or new then we could possibly replace it using
|
|
111
|
+
* the above method.
|
|
112
|
+
*/
|
|
113
|
+
const transitionName = name.type === "group" ? "layout" : "";
|
|
114
|
+
const animationTransition = {
|
|
115
|
+
...getValueTransition(defaultOptions, transitionName),
|
|
116
|
+
};
|
|
117
|
+
applyGeneratorOptions(animationTransition);
|
|
118
|
+
const easing = mapEasingToNativeEasing(animationTransition.ease, animationTransition.duration);
|
|
119
|
+
effect.updateTiming({
|
|
120
|
+
delay: secondsToMilliseconds((_a = animationTransition.delay) !== null && _a !== void 0 ? _a : 0),
|
|
121
|
+
duration: animationTransition.duration,
|
|
122
|
+
easing,
|
|
123
|
+
});
|
|
124
|
+
animations.push(new NativeAnimationControls(animation));
|
|
125
|
+
}
|
|
126
|
+
else if (hasOpacity(targetDefinition, "enter") &&
|
|
127
|
+
hasOpacity(targetDefinition, "exit") &&
|
|
128
|
+
effect
|
|
129
|
+
.getKeyframes()
|
|
130
|
+
.some((keyframe) => keyframe.mixBlendMode)) {
|
|
131
|
+
animations.push(new NativeAnimationControls(animation));
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
animation.cancel();
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
resolve(new BaseGroupPlaybackControls(animations));
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
function hasOpacity(target, key) {
|
|
142
|
+
var _a;
|
|
143
|
+
return (_a = target === null || target === void 0 ? void 0 : target[key]) === null || _a === void 0 ? void 0 : _a.keyframes.opacity;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export { startViewAnimation };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
function chooseLayerType(valueName) {
|
|
2
|
+
if (valueName === "layout")
|
|
3
|
+
return "group";
|
|
4
|
+
if (valueName === "enter" || valueName === "new")
|
|
5
|
+
return "new";
|
|
6
|
+
if (valueName === "exit" || valueName === "old")
|
|
7
|
+
return "old";
|
|
8
|
+
return "group";
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export { chooseLayerType };
|