motion-dom 11.14.3 → 11.16.1
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 +665 -31
- 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/gestures/press/index.mjs +4 -3
- 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 +5 -2
|
@@ -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 };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
let pendingRules = {};
|
|
2
|
+
let style = null;
|
|
3
|
+
const css = {
|
|
4
|
+
set: (selector, values) => {
|
|
5
|
+
pendingRules[selector] = values;
|
|
6
|
+
},
|
|
7
|
+
commit: () => {
|
|
8
|
+
if (!style) {
|
|
9
|
+
style = document.createElement("style");
|
|
10
|
+
style.id = "motion-view";
|
|
11
|
+
}
|
|
12
|
+
let cssText = "";
|
|
13
|
+
for (const selector in pendingRules) {
|
|
14
|
+
const rule = pendingRules[selector];
|
|
15
|
+
cssText += `${selector} {\n`;
|
|
16
|
+
for (const [property, value] of Object.entries(rule)) {
|
|
17
|
+
cssText += ` ${property}: ${value};\n`;
|
|
18
|
+
}
|
|
19
|
+
cssText += "}\n";
|
|
20
|
+
}
|
|
21
|
+
style.textContent = cssText;
|
|
22
|
+
document.head.appendChild(style);
|
|
23
|
+
pendingRules = {};
|
|
24
|
+
},
|
|
25
|
+
remove: () => {
|
|
26
|
+
if (style && style.parentElement) {
|
|
27
|
+
style.parentElement.removeChild(style);
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export { css };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
function filterViewAnimations(animation) {
|
|
2
|
+
var _a;
|
|
3
|
+
const { effect } = animation;
|
|
4
|
+
if (!effect)
|
|
5
|
+
return false;
|
|
6
|
+
return (effect.target === document.documentElement &&
|
|
7
|
+
((_a = effect.pseudoElement) === null || _a === void 0 ? void 0 : _a.startsWith("::view-transition")));
|
|
8
|
+
}
|
|
9
|
+
function getViewAnimations() {
|
|
10
|
+
return document.getAnimations().filter(filterViewAnimations);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export { getViewAnimations };
|