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
package/dist/cjs/index.js
CHANGED
|
@@ -2,6 +2,303 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
+
var motionUtils = require('motion-utils');
|
|
6
|
+
|
|
7
|
+
const supportsScrollTimeline = motionUtils.memo(() => window.ScrollTimeline !== undefined);
|
|
8
|
+
|
|
9
|
+
class BaseGroupPlaybackControls {
|
|
10
|
+
constructor(animations) {
|
|
11
|
+
// Bound to accomodate common `return animation.stop` pattern
|
|
12
|
+
this.stop = () => this.runAll("stop");
|
|
13
|
+
this.animations = animations.filter(Boolean);
|
|
14
|
+
}
|
|
15
|
+
get finished() {
|
|
16
|
+
// Support for new finished Promise and legacy thennable API
|
|
17
|
+
return Promise.all(this.animations.map((animation) => "finished" in animation ? animation.finished : animation));
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* TODO: Filter out cancelled or stopped animations before returning
|
|
21
|
+
*/
|
|
22
|
+
getAll(propName) {
|
|
23
|
+
return this.animations[0][propName];
|
|
24
|
+
}
|
|
25
|
+
setAll(propName, newValue) {
|
|
26
|
+
for (let i = 0; i < this.animations.length; i++) {
|
|
27
|
+
this.animations[i][propName] = newValue;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
attachTimeline(timeline, fallback) {
|
|
31
|
+
const subscriptions = this.animations.map((animation) => {
|
|
32
|
+
if (supportsScrollTimeline() && animation.attachTimeline) {
|
|
33
|
+
return animation.attachTimeline(timeline);
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
return fallback(animation);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
return () => {
|
|
40
|
+
subscriptions.forEach((cancel, i) => {
|
|
41
|
+
cancel && cancel();
|
|
42
|
+
this.animations[i].stop();
|
|
43
|
+
});
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
get time() {
|
|
47
|
+
return this.getAll("time");
|
|
48
|
+
}
|
|
49
|
+
set time(time) {
|
|
50
|
+
this.setAll("time", time);
|
|
51
|
+
}
|
|
52
|
+
get speed() {
|
|
53
|
+
return this.getAll("speed");
|
|
54
|
+
}
|
|
55
|
+
set speed(speed) {
|
|
56
|
+
this.setAll("speed", speed);
|
|
57
|
+
}
|
|
58
|
+
get startTime() {
|
|
59
|
+
return this.getAll("startTime");
|
|
60
|
+
}
|
|
61
|
+
get duration() {
|
|
62
|
+
let max = 0;
|
|
63
|
+
for (let i = 0; i < this.animations.length; i++) {
|
|
64
|
+
max = Math.max(max, this.animations[i].duration);
|
|
65
|
+
}
|
|
66
|
+
return max;
|
|
67
|
+
}
|
|
68
|
+
runAll(methodName) {
|
|
69
|
+
this.animations.forEach((controls) => controls[methodName]());
|
|
70
|
+
}
|
|
71
|
+
flatten() {
|
|
72
|
+
this.runAll("flatten");
|
|
73
|
+
}
|
|
74
|
+
play() {
|
|
75
|
+
this.runAll("play");
|
|
76
|
+
}
|
|
77
|
+
pause() {
|
|
78
|
+
this.runAll("pause");
|
|
79
|
+
}
|
|
80
|
+
cancel() {
|
|
81
|
+
this.runAll("cancel");
|
|
82
|
+
}
|
|
83
|
+
complete() {
|
|
84
|
+
this.runAll("complete");
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* TODO: This is a temporary class to support the legacy
|
|
90
|
+
* thennable API
|
|
91
|
+
*/
|
|
92
|
+
class GroupPlaybackControls extends BaseGroupPlaybackControls {
|
|
93
|
+
then(onResolve, onReject) {
|
|
94
|
+
return Promise.all(this.animations).then(onResolve).catch(onReject);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function getValueTransition(transition, key) {
|
|
99
|
+
return transition
|
|
100
|
+
? transition[key] ||
|
|
101
|
+
transition["default"] ||
|
|
102
|
+
transition
|
|
103
|
+
: undefined;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Implement a practical max duration for keyframe generation
|
|
108
|
+
* to prevent infinite loops
|
|
109
|
+
*/
|
|
110
|
+
const maxGeneratorDuration = 20000;
|
|
111
|
+
function calcGeneratorDuration(generator) {
|
|
112
|
+
let duration = 0;
|
|
113
|
+
const timeStep = 50;
|
|
114
|
+
let state = generator.next(duration);
|
|
115
|
+
while (!state.done && duration < maxGeneratorDuration) {
|
|
116
|
+
duration += timeStep;
|
|
117
|
+
state = generator.next(duration);
|
|
118
|
+
}
|
|
119
|
+
return duration >= maxGeneratorDuration ? Infinity : duration;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Create a progress => progress easing function from a generator.
|
|
124
|
+
*/
|
|
125
|
+
function createGeneratorEasing(options, scale = 100, createGenerator) {
|
|
126
|
+
const generator = createGenerator({ ...options, keyframes: [0, scale] });
|
|
127
|
+
const duration = Math.min(calcGeneratorDuration(generator), maxGeneratorDuration);
|
|
128
|
+
return {
|
|
129
|
+
type: "keyframes",
|
|
130
|
+
ease: (progress) => {
|
|
131
|
+
return generator.next(duration * progress).value / scale;
|
|
132
|
+
},
|
|
133
|
+
duration: motionUtils.millisecondsToSeconds(duration),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function isGenerator(type) {
|
|
138
|
+
return typeof type === "function";
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function attachTimeline(animation, timeline) {
|
|
142
|
+
animation.timeline = timeline;
|
|
143
|
+
animation.onfinish = null;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
class NativeAnimationControls {
|
|
147
|
+
constructor(animation) {
|
|
148
|
+
this.animation = animation;
|
|
149
|
+
}
|
|
150
|
+
get duration() {
|
|
151
|
+
var _a, _b, _c;
|
|
152
|
+
const durationInMs = ((_b = (_a = this.animation) === null || _a === void 0 ? void 0 : _a.effect) === null || _b === void 0 ? void 0 : _b.getComputedTiming().duration) ||
|
|
153
|
+
((_c = this.options) === null || _c === void 0 ? void 0 : _c.duration) ||
|
|
154
|
+
300;
|
|
155
|
+
return motionUtils.millisecondsToSeconds(Number(durationInMs));
|
|
156
|
+
}
|
|
157
|
+
get time() {
|
|
158
|
+
var _a;
|
|
159
|
+
if (this.animation) {
|
|
160
|
+
return motionUtils.millisecondsToSeconds(((_a = this.animation) === null || _a === void 0 ? void 0 : _a.currentTime) || 0);
|
|
161
|
+
}
|
|
162
|
+
return 0;
|
|
163
|
+
}
|
|
164
|
+
set time(newTime) {
|
|
165
|
+
if (this.animation) {
|
|
166
|
+
this.animation.currentTime = motionUtils.secondsToMilliseconds(newTime);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
get speed() {
|
|
170
|
+
return this.animation ? this.animation.playbackRate : 1;
|
|
171
|
+
}
|
|
172
|
+
set speed(newSpeed) {
|
|
173
|
+
if (this.animation) {
|
|
174
|
+
this.animation.playbackRate = newSpeed;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
get state() {
|
|
178
|
+
return this.animation ? this.animation.playState : "finished";
|
|
179
|
+
}
|
|
180
|
+
get startTime() {
|
|
181
|
+
return this.animation ? this.animation.startTime : null;
|
|
182
|
+
}
|
|
183
|
+
get finished() {
|
|
184
|
+
return this.animation ? this.animation.finished : Promise.resolve();
|
|
185
|
+
}
|
|
186
|
+
play() {
|
|
187
|
+
this.animation && this.animation.play();
|
|
188
|
+
}
|
|
189
|
+
pause() {
|
|
190
|
+
this.animation && this.animation.pause();
|
|
191
|
+
}
|
|
192
|
+
stop() {
|
|
193
|
+
if (!this.animation ||
|
|
194
|
+
this.state === "idle" ||
|
|
195
|
+
this.state === "finished") {
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
if (this.animation.commitStyles) {
|
|
199
|
+
this.animation.commitStyles();
|
|
200
|
+
}
|
|
201
|
+
this.cancel();
|
|
202
|
+
}
|
|
203
|
+
flatten() {
|
|
204
|
+
var _a;
|
|
205
|
+
if (!this.animation)
|
|
206
|
+
return;
|
|
207
|
+
(_a = this.animation.effect) === null || _a === void 0 ? void 0 : _a.updateTiming({ easing: "linear" });
|
|
208
|
+
}
|
|
209
|
+
attachTimeline(timeline) {
|
|
210
|
+
if (this.animation)
|
|
211
|
+
attachTimeline(this.animation, timeline);
|
|
212
|
+
return motionUtils.noop;
|
|
213
|
+
}
|
|
214
|
+
complete() {
|
|
215
|
+
this.animation && this.animation.finish();
|
|
216
|
+
}
|
|
217
|
+
cancel() {
|
|
218
|
+
try {
|
|
219
|
+
this.animation && this.animation.cancel();
|
|
220
|
+
}
|
|
221
|
+
catch (e) { }
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const isBezierDefinition = (easing) => Array.isArray(easing) && typeof easing[0] === "number";
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Add the ability for test suites to manually set support flags
|
|
229
|
+
* to better test more environments.
|
|
230
|
+
*/
|
|
231
|
+
const supportsFlags = {
|
|
232
|
+
linearEasing: undefined,
|
|
233
|
+
};
|
|
234
|
+
|
|
235
|
+
function memoSupports(callback, supportsFlag) {
|
|
236
|
+
const memoized = motionUtils.memo(callback);
|
|
237
|
+
return () => { var _a; return (_a = supportsFlags[supportsFlag]) !== null && _a !== void 0 ? _a : memoized(); };
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const supportsLinearEasing = /*@__PURE__*/ memoSupports(() => {
|
|
241
|
+
try {
|
|
242
|
+
document
|
|
243
|
+
.createElement("div")
|
|
244
|
+
.animate({ opacity: 0 }, { easing: "linear(0, 1)" });
|
|
245
|
+
}
|
|
246
|
+
catch (e) {
|
|
247
|
+
return false;
|
|
248
|
+
}
|
|
249
|
+
return true;
|
|
250
|
+
}, "linearEasing");
|
|
251
|
+
|
|
252
|
+
const generateLinearEasing = (easing, duration, // as milliseconds
|
|
253
|
+
resolution = 10 // as milliseconds
|
|
254
|
+
) => {
|
|
255
|
+
let points = "";
|
|
256
|
+
const numPoints = Math.max(Math.round(duration / resolution), 2);
|
|
257
|
+
for (let i = 0; i < numPoints; i++) {
|
|
258
|
+
points += easing(motionUtils.progress(0, numPoints - 1, i)) + ", ";
|
|
259
|
+
}
|
|
260
|
+
return `linear(${points.substring(0, points.length - 2)})`;
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
function isWaapiSupportedEasing(easing) {
|
|
264
|
+
return Boolean((typeof easing === "function" && supportsLinearEasing()) ||
|
|
265
|
+
!easing ||
|
|
266
|
+
(typeof easing === "string" &&
|
|
267
|
+
(easing in supportedWaapiEasing || supportsLinearEasing())) ||
|
|
268
|
+
isBezierDefinition(easing) ||
|
|
269
|
+
(Array.isArray(easing) && easing.every(isWaapiSupportedEasing)));
|
|
270
|
+
}
|
|
271
|
+
const cubicBezierAsString = ([a, b, c, d]) => `cubic-bezier(${a}, ${b}, ${c}, ${d})`;
|
|
272
|
+
const supportedWaapiEasing = {
|
|
273
|
+
linear: "linear",
|
|
274
|
+
ease: "ease",
|
|
275
|
+
easeIn: "ease-in",
|
|
276
|
+
easeOut: "ease-out",
|
|
277
|
+
easeInOut: "ease-in-out",
|
|
278
|
+
circIn: /*@__PURE__*/ cubicBezierAsString([0, 0.65, 0.55, 1]),
|
|
279
|
+
circOut: /*@__PURE__*/ cubicBezierAsString([0.55, 0, 1, 0.45]),
|
|
280
|
+
backIn: /*@__PURE__*/ cubicBezierAsString([0.31, 0.01, 0.66, -0.59]),
|
|
281
|
+
backOut: /*@__PURE__*/ cubicBezierAsString([0.33, 1.53, 0.69, 0.99]),
|
|
282
|
+
};
|
|
283
|
+
function mapEasingToNativeEasing(easing, duration) {
|
|
284
|
+
if (!easing) {
|
|
285
|
+
return undefined;
|
|
286
|
+
}
|
|
287
|
+
else if (typeof easing === "function" && supportsLinearEasing()) {
|
|
288
|
+
return generateLinearEasing(easing, duration);
|
|
289
|
+
}
|
|
290
|
+
else if (isBezierDefinition(easing)) {
|
|
291
|
+
return cubicBezierAsString(easing);
|
|
292
|
+
}
|
|
293
|
+
else if (Array.isArray(easing)) {
|
|
294
|
+
return easing.map((segmentEasing) => mapEasingToNativeEasing(segmentEasing, duration) ||
|
|
295
|
+
supportedWaapiEasing.easeOut);
|
|
296
|
+
}
|
|
297
|
+
else {
|
|
298
|
+
return supportedWaapiEasing[easing];
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
5
302
|
const isDragging = {
|
|
6
303
|
x: false,
|
|
7
304
|
y: false,
|
|
@@ -230,6 +527,323 @@ function press(elementOrSelector, onPressStart, options = {}) {
|
|
|
230
527
|
return cancelEvents;
|
|
231
528
|
}
|
|
232
529
|
|
|
530
|
+
const defaultEasing = "easeOut";
|
|
531
|
+
function applyGeneratorOptions(options) {
|
|
532
|
+
var _a;
|
|
533
|
+
if (isGenerator(options.type)) {
|
|
534
|
+
const generatorOptions = createGeneratorEasing(options, 100, options.type);
|
|
535
|
+
options.ease = supportsLinearEasing()
|
|
536
|
+
? generatorOptions.ease
|
|
537
|
+
: defaultEasing;
|
|
538
|
+
options.duration = motionUtils.secondsToMilliseconds(generatorOptions.duration);
|
|
539
|
+
options.type = "keyframes";
|
|
540
|
+
}
|
|
541
|
+
else {
|
|
542
|
+
options.duration = motionUtils.secondsToMilliseconds((_a = options.duration) !== null && _a !== void 0 ? _a : 0.3);
|
|
543
|
+
options.ease = options.ease || defaultEasing;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
// TODO: Reuse for NativeAnimation
|
|
547
|
+
function convertMotionOptionsToNative(valueName, keyframes, options) {
|
|
548
|
+
var _a;
|
|
549
|
+
const nativeKeyframes = {};
|
|
550
|
+
const nativeOptions = {
|
|
551
|
+
fill: "both",
|
|
552
|
+
easing: "linear",
|
|
553
|
+
composite: "replace",
|
|
554
|
+
};
|
|
555
|
+
nativeOptions.delay = motionUtils.secondsToMilliseconds((_a = options.delay) !== null && _a !== void 0 ? _a : 0);
|
|
556
|
+
applyGeneratorOptions(options);
|
|
557
|
+
nativeOptions.duration = options.duration;
|
|
558
|
+
const { ease, times } = options;
|
|
559
|
+
if (times)
|
|
560
|
+
nativeKeyframes.offset = times;
|
|
561
|
+
nativeKeyframes[valueName] = keyframes;
|
|
562
|
+
const easing = mapEasingToNativeEasing(ease, options.duration);
|
|
563
|
+
/**
|
|
564
|
+
* If this is an easing array, apply to keyframes, not animation as a whole
|
|
565
|
+
*/
|
|
566
|
+
if (Array.isArray(easing)) {
|
|
567
|
+
nativeKeyframes.easing = easing;
|
|
568
|
+
}
|
|
569
|
+
else {
|
|
570
|
+
nativeOptions.easing = easing;
|
|
571
|
+
}
|
|
572
|
+
return {
|
|
573
|
+
keyframes: nativeKeyframes,
|
|
574
|
+
options: nativeOptions,
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
class PseudoAnimation extends NativeAnimationControls {
|
|
579
|
+
constructor(target, pseudoElement, valueName, keyframes, options) {
|
|
580
|
+
const animationOptions = convertMotionOptionsToNative(valueName, keyframes, options);
|
|
581
|
+
const animation = target.animate(animationOptions.keyframes, {
|
|
582
|
+
pseudoElement,
|
|
583
|
+
...animationOptions.options,
|
|
584
|
+
});
|
|
585
|
+
super(animation);
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
function chooseLayerType(valueName) {
|
|
590
|
+
if (valueName === "layout")
|
|
591
|
+
return "group";
|
|
592
|
+
if (valueName === "enter" || valueName === "new")
|
|
593
|
+
return "new";
|
|
594
|
+
if (valueName === "exit" || valueName === "old")
|
|
595
|
+
return "old";
|
|
596
|
+
return "group";
|
|
597
|
+
}
|
|
598
|
+
|
|
599
|
+
let pendingRules = {};
|
|
600
|
+
let style = null;
|
|
601
|
+
const css = {
|
|
602
|
+
set: (selector, values) => {
|
|
603
|
+
pendingRules[selector] = values;
|
|
604
|
+
},
|
|
605
|
+
commit: () => {
|
|
606
|
+
if (!style) {
|
|
607
|
+
style = document.createElement("style");
|
|
608
|
+
style.id = "motion-view";
|
|
609
|
+
}
|
|
610
|
+
let cssText = "";
|
|
611
|
+
for (const selector in pendingRules) {
|
|
612
|
+
const rule = pendingRules[selector];
|
|
613
|
+
cssText += `${selector} {\n`;
|
|
614
|
+
for (const [property, value] of Object.entries(rule)) {
|
|
615
|
+
cssText += ` ${property}: ${value};\n`;
|
|
616
|
+
}
|
|
617
|
+
cssText += "}\n";
|
|
618
|
+
}
|
|
619
|
+
style.textContent = cssText;
|
|
620
|
+
document.head.appendChild(style);
|
|
621
|
+
pendingRules = {};
|
|
622
|
+
},
|
|
623
|
+
remove: () => {
|
|
624
|
+
if (style && style.parentElement) {
|
|
625
|
+
style.parentElement.removeChild(style);
|
|
626
|
+
}
|
|
627
|
+
},
|
|
628
|
+
};
|
|
629
|
+
|
|
630
|
+
function getLayerName(pseudoElement) {
|
|
631
|
+
const match = pseudoElement.match(/::view-transition-(old|new|group|image-pair)\((.*?)\)/);
|
|
632
|
+
if (!match)
|
|
633
|
+
return null;
|
|
634
|
+
return { layer: match[2], type: match[1] };
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
function filterViewAnimations(animation) {
|
|
638
|
+
var _a;
|
|
639
|
+
const { effect } = animation;
|
|
640
|
+
if (!effect)
|
|
641
|
+
return false;
|
|
642
|
+
return (effect.target === document.documentElement &&
|
|
643
|
+
((_a = effect.pseudoElement) === null || _a === void 0 ? void 0 : _a.startsWith("::view-transition")));
|
|
644
|
+
}
|
|
645
|
+
function getViewAnimations() {
|
|
646
|
+
return document.getAnimations().filter(filterViewAnimations);
|
|
647
|
+
}
|
|
648
|
+
|
|
649
|
+
function hasTarget(target, targets) {
|
|
650
|
+
return targets.has(target) && Object.keys(targets.get(target)).length > 0;
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
const definitionNames = ["layout", "enter", "exit", "new", "old"];
|
|
654
|
+
function startViewAnimation(update, defaultOptions, targets) {
|
|
655
|
+
if (!document.startViewTransition) {
|
|
656
|
+
return new Promise(async (resolve) => {
|
|
657
|
+
await update();
|
|
658
|
+
resolve(new BaseGroupPlaybackControls([]));
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
// TODO: Go over existing targets and ensure they all have ids
|
|
662
|
+
/**
|
|
663
|
+
* If we don't have any animations defined for the root target,
|
|
664
|
+
* remove it from being captured.
|
|
665
|
+
*/
|
|
666
|
+
if (!hasTarget("root", targets)) {
|
|
667
|
+
css.set(":root", {
|
|
668
|
+
"view-transition-name": "none",
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
/**
|
|
672
|
+
* Set the timing curve to linear for all view transition layers.
|
|
673
|
+
* This gets baked into the keyframes, which can't be changed
|
|
674
|
+
* without breaking the generated animation.
|
|
675
|
+
*
|
|
676
|
+
* This allows us to set easing via updateTiming - which can be changed.
|
|
677
|
+
*/
|
|
678
|
+
css.set("::view-transition-group(*), ::view-transition-old(*), ::view-transition-new(*)", { "animation-timing-function": "linear !important" });
|
|
679
|
+
css.commit(); // Write
|
|
680
|
+
const transition = document.startViewTransition(async () => {
|
|
681
|
+
await update();
|
|
682
|
+
// TODO: Go over new targets and ensure they all have ids
|
|
683
|
+
});
|
|
684
|
+
transition.finished.finally(() => {
|
|
685
|
+
css.remove(); // Write
|
|
686
|
+
});
|
|
687
|
+
return new Promise((resolve) => {
|
|
688
|
+
transition.ready.then(() => {
|
|
689
|
+
var _a;
|
|
690
|
+
const generatedViewAnimations = getViewAnimations();
|
|
691
|
+
const animations = [];
|
|
692
|
+
/**
|
|
693
|
+
* Create animations for our definitions
|
|
694
|
+
*/
|
|
695
|
+
targets.forEach((definition, target) => {
|
|
696
|
+
// TODO: If target is not "root", resolve elements
|
|
697
|
+
// and iterate over each
|
|
698
|
+
for (const key of definitionNames) {
|
|
699
|
+
if (!definition[key])
|
|
700
|
+
continue;
|
|
701
|
+
const { keyframes, options } = definition[key];
|
|
702
|
+
for (let [valueName, valueKeyframes] of Object.entries(keyframes)) {
|
|
703
|
+
if (!valueKeyframes)
|
|
704
|
+
continue;
|
|
705
|
+
const valueOptions = {
|
|
706
|
+
...getValueTransition(defaultOptions, valueName),
|
|
707
|
+
...getValueTransition(options, valueName),
|
|
708
|
+
};
|
|
709
|
+
const type = chooseLayerType(key);
|
|
710
|
+
/**
|
|
711
|
+
* If this is an opacity animation, and keyframes are not an array,
|
|
712
|
+
* we need to convert them into an array and set an initial value.
|
|
713
|
+
*/
|
|
714
|
+
if (valueName === "opacity" &&
|
|
715
|
+
!Array.isArray(valueKeyframes)) {
|
|
716
|
+
const initialValue = type === "new" ? 0 : 1;
|
|
717
|
+
valueKeyframes = [initialValue, valueKeyframes];
|
|
718
|
+
}
|
|
719
|
+
/**
|
|
720
|
+
* Resolve stagger function if provided.
|
|
721
|
+
*/
|
|
722
|
+
if (typeof valueOptions.delay === "function") {
|
|
723
|
+
valueOptions.delay = valueOptions.delay(0, 1);
|
|
724
|
+
}
|
|
725
|
+
const animation = new PseudoAnimation(document.documentElement, `::view-transition-${type}(${target})`, valueName, valueKeyframes, valueOptions);
|
|
726
|
+
animations.push(animation);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
});
|
|
730
|
+
/**
|
|
731
|
+
* Handle browser generated animations
|
|
732
|
+
*/
|
|
733
|
+
for (const animation of generatedViewAnimations) {
|
|
734
|
+
if (animation.playState === "finished")
|
|
735
|
+
continue;
|
|
736
|
+
const { effect } = animation;
|
|
737
|
+
if (!effect || !(effect instanceof KeyframeEffect))
|
|
738
|
+
continue;
|
|
739
|
+
const { pseudoElement } = effect;
|
|
740
|
+
if (!pseudoElement)
|
|
741
|
+
continue;
|
|
742
|
+
const name = getLayerName(pseudoElement);
|
|
743
|
+
if (!name)
|
|
744
|
+
continue;
|
|
745
|
+
const targetDefinition = targets.get(name.layer);
|
|
746
|
+
if (!targetDefinition) {
|
|
747
|
+
/**
|
|
748
|
+
* If transition name is group then update the timing of the animation
|
|
749
|
+
* whereas if it's old or new then we could possibly replace it using
|
|
750
|
+
* the above method.
|
|
751
|
+
*/
|
|
752
|
+
const transitionName = name.type === "group" ? "layout" : "";
|
|
753
|
+
const animationTransition = {
|
|
754
|
+
...getValueTransition(defaultOptions, transitionName),
|
|
755
|
+
};
|
|
756
|
+
applyGeneratorOptions(animationTransition);
|
|
757
|
+
const easing = mapEasingToNativeEasing(animationTransition.ease, animationTransition.duration);
|
|
758
|
+
effect.updateTiming({
|
|
759
|
+
delay: motionUtils.secondsToMilliseconds((_a = animationTransition.delay) !== null && _a !== void 0 ? _a : 0),
|
|
760
|
+
duration: animationTransition.duration,
|
|
761
|
+
easing,
|
|
762
|
+
});
|
|
763
|
+
animations.push(new NativeAnimationControls(animation));
|
|
764
|
+
}
|
|
765
|
+
else if (hasOpacity(targetDefinition, "enter") &&
|
|
766
|
+
hasOpacity(targetDefinition, "exit") &&
|
|
767
|
+
effect
|
|
768
|
+
.getKeyframes()
|
|
769
|
+
.some((keyframe) => keyframe.mixBlendMode)) {
|
|
770
|
+
animations.push(new NativeAnimationControls(animation));
|
|
771
|
+
}
|
|
772
|
+
else {
|
|
773
|
+
animation.cancel();
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
resolve(new BaseGroupPlaybackControls(animations));
|
|
777
|
+
});
|
|
778
|
+
});
|
|
779
|
+
}
|
|
780
|
+
function hasOpacity(target, key) {
|
|
781
|
+
var _a;
|
|
782
|
+
return (_a = target === null || target === void 0 ? void 0 : target[key]) === null || _a === void 0 ? void 0 : _a.keyframes.opacity;
|
|
783
|
+
}
|
|
784
|
+
|
|
785
|
+
/**
|
|
786
|
+
* TODO:
|
|
787
|
+
* - Create view transition on next tick
|
|
788
|
+
* - Replace animations with Motion animations
|
|
789
|
+
* - Return GroupAnimation on next tick
|
|
790
|
+
*/
|
|
791
|
+
class ViewTransitionBuilder {
|
|
792
|
+
constructor(update, options = {}) {
|
|
793
|
+
this.currentTarget = "root";
|
|
794
|
+
this.targets = new Map();
|
|
795
|
+
this.readyPromise = new Promise((resolve) => {
|
|
796
|
+
this.notifyReady = resolve;
|
|
797
|
+
});
|
|
798
|
+
queueMicrotask(() => {
|
|
799
|
+
startViewAnimation(update, options, this.targets).then((animation) => this.notifyReady(animation));
|
|
800
|
+
});
|
|
801
|
+
}
|
|
802
|
+
get(selector) {
|
|
803
|
+
this.currentTarget = selector;
|
|
804
|
+
return this;
|
|
805
|
+
}
|
|
806
|
+
layout(keyframes, options) {
|
|
807
|
+
this.updateTarget("layout", keyframes, options);
|
|
808
|
+
return this;
|
|
809
|
+
}
|
|
810
|
+
new(keyframes, options) {
|
|
811
|
+
this.updateTarget("new", keyframes, options);
|
|
812
|
+
return this;
|
|
813
|
+
}
|
|
814
|
+
old(keyframes, options) {
|
|
815
|
+
this.updateTarget("old", keyframes, options);
|
|
816
|
+
return this;
|
|
817
|
+
}
|
|
818
|
+
enter(keyframes, options) {
|
|
819
|
+
this.updateTarget("enter", keyframes, options);
|
|
820
|
+
return this;
|
|
821
|
+
}
|
|
822
|
+
exit(keyframes, options) {
|
|
823
|
+
this.updateTarget("exit", keyframes, options);
|
|
824
|
+
return this;
|
|
825
|
+
}
|
|
826
|
+
crossfade(options) {
|
|
827
|
+
this.updateTarget("enter", { opacity: 1 }, options);
|
|
828
|
+
this.updateTarget("exit", { opacity: 0 }, options);
|
|
829
|
+
return this;
|
|
830
|
+
}
|
|
831
|
+
updateTarget(target, keyframes, options = {}) {
|
|
832
|
+
const { currentTarget, targets } = this;
|
|
833
|
+
if (!targets.has(currentTarget)) {
|
|
834
|
+
targets.set(currentTarget, {});
|
|
835
|
+
}
|
|
836
|
+
const targetData = targets.get(currentTarget);
|
|
837
|
+
targetData[target] = { keyframes, options };
|
|
838
|
+
}
|
|
839
|
+
then(resolve, reject) {
|
|
840
|
+
return this.readyPromise.then(resolve, reject);
|
|
841
|
+
}
|
|
842
|
+
}
|
|
843
|
+
function view(update, defaultOptions = {}) {
|
|
844
|
+
return new ViewTransitionBuilder(update, defaultOptions);
|
|
845
|
+
}
|
|
846
|
+
|
|
233
847
|
function setDragLock(axis) {
|
|
234
848
|
if (axis === "x" || axis === "y") {
|
|
235
849
|
if (isDragging[axis]) {
|
|
@@ -255,11 +869,30 @@ function setDragLock(axis) {
|
|
|
255
869
|
}
|
|
256
870
|
}
|
|
257
871
|
|
|
872
|
+
exports.GroupPlaybackControls = GroupPlaybackControls;
|
|
873
|
+
exports.NativeAnimationControls = NativeAnimationControls;
|
|
874
|
+
exports.ViewTransitionBuilder = ViewTransitionBuilder;
|
|
875
|
+
exports.attachTimeline = attachTimeline;
|
|
876
|
+
exports.calcGeneratorDuration = calcGeneratorDuration;
|
|
877
|
+
exports.createGeneratorEasing = createGeneratorEasing;
|
|
878
|
+
exports.cubicBezierAsString = cubicBezierAsString;
|
|
879
|
+
exports.generateLinearEasing = generateLinearEasing;
|
|
880
|
+
exports.getValueTransition = getValueTransition;
|
|
258
881
|
exports.hover = hover;
|
|
882
|
+
exports.isBezierDefinition = isBezierDefinition;
|
|
259
883
|
exports.isDragActive = isDragActive;
|
|
260
884
|
exports.isDragging = isDragging;
|
|
885
|
+
exports.isGenerator = isGenerator;
|
|
261
886
|
exports.isNodeOrChild = isNodeOrChild;
|
|
262
887
|
exports.isPrimaryPointer = isPrimaryPointer;
|
|
888
|
+
exports.isWaapiSupportedEasing = isWaapiSupportedEasing;
|
|
889
|
+
exports.mapEasingToNativeEasing = mapEasingToNativeEasing;
|
|
890
|
+
exports.maxGeneratorDuration = maxGeneratorDuration;
|
|
263
891
|
exports.press = press;
|
|
264
892
|
exports.resolveElements = resolveElements;
|
|
265
893
|
exports.setDragLock = setDragLock;
|
|
894
|
+
exports.supportedWaapiEasing = supportedWaapiEasing;
|
|
895
|
+
exports.supportsFlags = supportsFlags;
|
|
896
|
+
exports.supportsLinearEasing = supportsLinearEasing;
|
|
897
|
+
exports.supportsScrollTimeline = supportsScrollTimeline;
|
|
898
|
+
exports.view = view;
|