motion-dom 12.4.5 → 12.4.10
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/motion-dom.dev.js +942 -0
- package/dist/motion-dom.js +1 -0
- package/package.json +3 -3
|
@@ -0,0 +1,942 @@
|
|
|
1
|
+
(function (global, factory) {
|
|
2
|
+
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('motion-utils')) :
|
|
3
|
+
typeof define === 'function' && define.amd ? define(['exports', 'motion-utils'], factory) :
|
|
4
|
+
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.MotionDom = {}, global.MotionUtils));
|
|
5
|
+
})(this, (function (exports, motionUtils) { 'use strict';
|
|
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 if (typeof fallback === "function") {
|
|
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
|
+
|
|
302
|
+
const isDragging = {
|
|
303
|
+
x: false,
|
|
304
|
+
y: false,
|
|
305
|
+
};
|
|
306
|
+
function isDragActive() {
|
|
307
|
+
return isDragging.x || isDragging.y;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function resolveElements(elementOrSelector, scope, selectorCache) {
|
|
311
|
+
var _a;
|
|
312
|
+
if (elementOrSelector instanceof EventTarget) {
|
|
313
|
+
return [elementOrSelector];
|
|
314
|
+
}
|
|
315
|
+
else if (typeof elementOrSelector === "string") {
|
|
316
|
+
let root = document;
|
|
317
|
+
if (scope) {
|
|
318
|
+
// TODO: Refactor to utils package
|
|
319
|
+
// invariant(
|
|
320
|
+
// Boolean(scope.current),
|
|
321
|
+
// "Scope provided, but no element detected."
|
|
322
|
+
// )
|
|
323
|
+
root = scope.current;
|
|
324
|
+
}
|
|
325
|
+
const elements = (_a = selectorCache === null || selectorCache === void 0 ? void 0 : selectorCache[elementOrSelector]) !== null && _a !== void 0 ? _a : root.querySelectorAll(elementOrSelector);
|
|
326
|
+
return elements ? Array.from(elements) : [];
|
|
327
|
+
}
|
|
328
|
+
return Array.from(elementOrSelector);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function setupGesture(elementOrSelector, options) {
|
|
332
|
+
const elements = resolveElements(elementOrSelector);
|
|
333
|
+
const gestureAbortController = new AbortController();
|
|
334
|
+
const eventOptions = {
|
|
335
|
+
passive: true,
|
|
336
|
+
...options,
|
|
337
|
+
signal: gestureAbortController.signal,
|
|
338
|
+
};
|
|
339
|
+
const cancel = () => gestureAbortController.abort();
|
|
340
|
+
return [elements, eventOptions, cancel];
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
function isValidHover(event) {
|
|
344
|
+
return !(event.pointerType === "touch" || isDragActive());
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Create a hover gesture. hover() is different to .addEventListener("pointerenter")
|
|
348
|
+
* in that it has an easier syntax, filters out polyfilled touch events, interoperates
|
|
349
|
+
* with drag gestures, and automatically removes the "pointerennd" event listener when the hover ends.
|
|
350
|
+
*
|
|
351
|
+
* @public
|
|
352
|
+
*/
|
|
353
|
+
function hover(elementOrSelector, onHoverStart, options = {}) {
|
|
354
|
+
const [elements, eventOptions, cancel] = setupGesture(elementOrSelector, options);
|
|
355
|
+
const onPointerEnter = (enterEvent) => {
|
|
356
|
+
if (!isValidHover(enterEvent))
|
|
357
|
+
return;
|
|
358
|
+
const { target } = enterEvent;
|
|
359
|
+
const onHoverEnd = onHoverStart(target, enterEvent);
|
|
360
|
+
if (typeof onHoverEnd !== "function" || !target)
|
|
361
|
+
return;
|
|
362
|
+
const onPointerLeave = (leaveEvent) => {
|
|
363
|
+
if (!isValidHover(leaveEvent))
|
|
364
|
+
return;
|
|
365
|
+
onHoverEnd(leaveEvent);
|
|
366
|
+
target.removeEventListener("pointerleave", onPointerLeave);
|
|
367
|
+
};
|
|
368
|
+
target.addEventListener("pointerleave", onPointerLeave, eventOptions);
|
|
369
|
+
};
|
|
370
|
+
elements.forEach((element) => {
|
|
371
|
+
element.addEventListener("pointerenter", onPointerEnter, eventOptions);
|
|
372
|
+
});
|
|
373
|
+
return cancel;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
function capturePointer(event, action) {
|
|
377
|
+
const actionName = `${action}PointerCapture`;
|
|
378
|
+
if (event.target instanceof Element &&
|
|
379
|
+
actionName in event.target &&
|
|
380
|
+
event.pointerId !== undefined) {
|
|
381
|
+
try {
|
|
382
|
+
event.target[actionName](event.pointerId);
|
|
383
|
+
}
|
|
384
|
+
catch (e) { }
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
/**
|
|
389
|
+
* Recursively traverse up the tree to check whether the provided child node
|
|
390
|
+
* is the parent or a descendant of it.
|
|
391
|
+
*
|
|
392
|
+
* @param parent - Element to find
|
|
393
|
+
* @param child - Element to test against parent
|
|
394
|
+
*/
|
|
395
|
+
const isNodeOrChild = (parent, child) => {
|
|
396
|
+
if (!child) {
|
|
397
|
+
return false;
|
|
398
|
+
}
|
|
399
|
+
else if (parent === child) {
|
|
400
|
+
return true;
|
|
401
|
+
}
|
|
402
|
+
else {
|
|
403
|
+
return isNodeOrChild(parent, child.parentElement);
|
|
404
|
+
}
|
|
405
|
+
};
|
|
406
|
+
|
|
407
|
+
const isPrimaryPointer = (event) => {
|
|
408
|
+
if (event.pointerType === "mouse") {
|
|
409
|
+
return typeof event.button !== "number" || event.button <= 0;
|
|
410
|
+
}
|
|
411
|
+
else {
|
|
412
|
+
/**
|
|
413
|
+
* isPrimary is true for all mice buttons, whereas every touch point
|
|
414
|
+
* is regarded as its own input. So subsequent concurrent touch points
|
|
415
|
+
* will be false.
|
|
416
|
+
*
|
|
417
|
+
* Specifically match against false here as incomplete versions of
|
|
418
|
+
* PointerEvents in very old browser might have it set as undefined.
|
|
419
|
+
*/
|
|
420
|
+
return event.isPrimary !== false;
|
|
421
|
+
}
|
|
422
|
+
};
|
|
423
|
+
|
|
424
|
+
const focusableElements = new Set([
|
|
425
|
+
"BUTTON",
|
|
426
|
+
"INPUT",
|
|
427
|
+
"SELECT",
|
|
428
|
+
"TEXTAREA",
|
|
429
|
+
"A",
|
|
430
|
+
]);
|
|
431
|
+
function isElementKeyboardAccessible(element) {
|
|
432
|
+
return (focusableElements.has(element.tagName) ||
|
|
433
|
+
element.tabIndex !== -1);
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
const isPressing = new WeakSet();
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Filter out events that are not "Enter" keys.
|
|
440
|
+
*/
|
|
441
|
+
function filterEvents(callback) {
|
|
442
|
+
return (event) => {
|
|
443
|
+
if (event.key !== "Enter")
|
|
444
|
+
return;
|
|
445
|
+
callback(event);
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
function firePointerEvent(target, type) {
|
|
449
|
+
target.dispatchEvent(new PointerEvent("pointer" + type, { isPrimary: true, bubbles: true }));
|
|
450
|
+
}
|
|
451
|
+
const enableKeyboardPress = (focusEvent, eventOptions) => {
|
|
452
|
+
const element = focusEvent.currentTarget;
|
|
453
|
+
if (!element)
|
|
454
|
+
return;
|
|
455
|
+
const handleKeydown = filterEvents(() => {
|
|
456
|
+
if (isPressing.has(element))
|
|
457
|
+
return;
|
|
458
|
+
firePointerEvent(element, "down");
|
|
459
|
+
const handleKeyup = filterEvents(() => {
|
|
460
|
+
firePointerEvent(element, "up");
|
|
461
|
+
});
|
|
462
|
+
const handleBlur = () => firePointerEvent(element, "cancel");
|
|
463
|
+
element.addEventListener("keyup", handleKeyup, eventOptions);
|
|
464
|
+
element.addEventListener("blur", handleBlur, eventOptions);
|
|
465
|
+
});
|
|
466
|
+
element.addEventListener("keydown", handleKeydown, eventOptions);
|
|
467
|
+
/**
|
|
468
|
+
* Add an event listener that fires on blur to remove the keydown events.
|
|
469
|
+
*/
|
|
470
|
+
element.addEventListener("blur", () => element.removeEventListener("keydown", handleKeydown), eventOptions);
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* Filter out events that are not primary pointer events, or are triggering
|
|
475
|
+
* while a Motion gesture is active.
|
|
476
|
+
*/
|
|
477
|
+
function isValidPressEvent(event) {
|
|
478
|
+
return isPrimaryPointer(event) && !isDragActive();
|
|
479
|
+
}
|
|
480
|
+
/**
|
|
481
|
+
* Create a press gesture.
|
|
482
|
+
*
|
|
483
|
+
* Press is different to `"pointerdown"`, `"pointerup"` in that it
|
|
484
|
+
* automatically filters out secondary pointer events like right
|
|
485
|
+
* click and multitouch.
|
|
486
|
+
*
|
|
487
|
+
* It also adds accessibility support for keyboards, where
|
|
488
|
+
* an element with a press gesture will receive focus and
|
|
489
|
+
* trigger on Enter `"keydown"` and `"keyup"` events.
|
|
490
|
+
*
|
|
491
|
+
* This is different to a browser's `"click"` event, which does
|
|
492
|
+
* respond to keyboards but only for the `"click"` itself, rather
|
|
493
|
+
* than the press start and end/cancel. The element also needs
|
|
494
|
+
* to be focusable for this to work, whereas a press gesture will
|
|
495
|
+
* make an element focusable by default.
|
|
496
|
+
*
|
|
497
|
+
* @public
|
|
498
|
+
*/
|
|
499
|
+
function press(targetOrSelector, onPressStart, options = {}) {
|
|
500
|
+
const [targets, eventOptions, cancelEvents] = setupGesture(targetOrSelector, options);
|
|
501
|
+
const startPress = (startEvent) => {
|
|
502
|
+
const target = startEvent.currentTarget;
|
|
503
|
+
if (!target || !isValidPressEvent(startEvent) || isPressing.has(target))
|
|
504
|
+
return;
|
|
505
|
+
isPressing.add(target);
|
|
506
|
+
capturePointer(startEvent, "set");
|
|
507
|
+
const onPressEnd = onPressStart(target, startEvent);
|
|
508
|
+
const onPointerEnd = (endEvent, success) => {
|
|
509
|
+
target.removeEventListener("pointerup", onPointerUp);
|
|
510
|
+
target.removeEventListener("pointercancel", onPointerCancel);
|
|
511
|
+
capturePointer(endEvent, "release");
|
|
512
|
+
if (!isValidPressEvent(endEvent) || !isPressing.has(target)) {
|
|
513
|
+
return;
|
|
514
|
+
}
|
|
515
|
+
isPressing.delete(target);
|
|
516
|
+
if (typeof onPressEnd === "function") {
|
|
517
|
+
onPressEnd(endEvent, { success });
|
|
518
|
+
}
|
|
519
|
+
};
|
|
520
|
+
const onPointerUp = (upEvent) => {
|
|
521
|
+
const isOutside = !upEvent.isTrusted
|
|
522
|
+
? false
|
|
523
|
+
: checkOutside(upEvent, target instanceof Element
|
|
524
|
+
? target.getBoundingClientRect()
|
|
525
|
+
: {
|
|
526
|
+
left: 0,
|
|
527
|
+
top: 0,
|
|
528
|
+
right: window.innerWidth,
|
|
529
|
+
bottom: window.innerHeight,
|
|
530
|
+
});
|
|
531
|
+
if (isOutside) {
|
|
532
|
+
onPointerEnd(upEvent, false);
|
|
533
|
+
}
|
|
534
|
+
else {
|
|
535
|
+
onPointerEnd(upEvent, !(target instanceof Element) ||
|
|
536
|
+
isNodeOrChild(target, upEvent.target));
|
|
537
|
+
}
|
|
538
|
+
};
|
|
539
|
+
const onPointerCancel = (cancelEvent) => {
|
|
540
|
+
onPointerEnd(cancelEvent, false);
|
|
541
|
+
};
|
|
542
|
+
target.addEventListener("pointerup", onPointerUp, eventOptions);
|
|
543
|
+
target.addEventListener("pointercancel", onPointerCancel, eventOptions);
|
|
544
|
+
target.addEventListener("lostpointercapture", onPointerCancel, eventOptions);
|
|
545
|
+
};
|
|
546
|
+
targets.forEach((target) => {
|
|
547
|
+
target = options.useGlobalTarget ? window : target;
|
|
548
|
+
let canAddKeyboardAccessibility = false;
|
|
549
|
+
if (target instanceof HTMLElement) {
|
|
550
|
+
canAddKeyboardAccessibility = true;
|
|
551
|
+
if (!isElementKeyboardAccessible(target) &&
|
|
552
|
+
target.getAttribute("tabindex") === null) {
|
|
553
|
+
target.tabIndex = 0;
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
target.addEventListener("pointerdown", startPress, eventOptions);
|
|
557
|
+
if (canAddKeyboardAccessibility) {
|
|
558
|
+
target.addEventListener("focus", (event) => enableKeyboardPress(event, eventOptions), eventOptions);
|
|
559
|
+
}
|
|
560
|
+
});
|
|
561
|
+
return cancelEvents;
|
|
562
|
+
}
|
|
563
|
+
function checkOutside(event, rect) {
|
|
564
|
+
return (event.clientX < rect.left ||
|
|
565
|
+
event.clientX > rect.right ||
|
|
566
|
+
event.clientY < rect.top ||
|
|
567
|
+
event.clientY > rect.bottom);
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
const defaultEasing = "easeOut";
|
|
571
|
+
function applyGeneratorOptions(options) {
|
|
572
|
+
var _a;
|
|
573
|
+
if (isGenerator(options.type)) {
|
|
574
|
+
const generatorOptions = createGeneratorEasing(options, 100, options.type);
|
|
575
|
+
options.ease = supportsLinearEasing()
|
|
576
|
+
? generatorOptions.ease
|
|
577
|
+
: defaultEasing;
|
|
578
|
+
options.duration = motionUtils.secondsToMilliseconds(generatorOptions.duration);
|
|
579
|
+
options.type = "keyframes";
|
|
580
|
+
}
|
|
581
|
+
else {
|
|
582
|
+
options.duration = motionUtils.secondsToMilliseconds((_a = options.duration) !== null && _a !== void 0 ? _a : 0.3);
|
|
583
|
+
options.ease = options.ease || defaultEasing;
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
// TODO: Reuse for NativeAnimation
|
|
587
|
+
function convertMotionOptionsToNative(valueName, keyframes, options) {
|
|
588
|
+
var _a;
|
|
589
|
+
const nativeKeyframes = {};
|
|
590
|
+
const nativeOptions = {
|
|
591
|
+
fill: "both",
|
|
592
|
+
easing: "linear",
|
|
593
|
+
composite: "replace",
|
|
594
|
+
};
|
|
595
|
+
nativeOptions.delay = motionUtils.secondsToMilliseconds((_a = options.delay) !== null && _a !== void 0 ? _a : 0);
|
|
596
|
+
applyGeneratorOptions(options);
|
|
597
|
+
nativeOptions.duration = options.duration;
|
|
598
|
+
const { ease, times } = options;
|
|
599
|
+
if (times)
|
|
600
|
+
nativeKeyframes.offset = times;
|
|
601
|
+
nativeKeyframes[valueName] = keyframes;
|
|
602
|
+
const easing = mapEasingToNativeEasing(ease, options.duration);
|
|
603
|
+
/**
|
|
604
|
+
* If this is an easing array, apply to keyframes, not animation as a whole
|
|
605
|
+
*/
|
|
606
|
+
if (Array.isArray(easing)) {
|
|
607
|
+
nativeKeyframes.easing = easing;
|
|
608
|
+
}
|
|
609
|
+
else {
|
|
610
|
+
nativeOptions.easing = easing;
|
|
611
|
+
}
|
|
612
|
+
return {
|
|
613
|
+
keyframes: nativeKeyframes,
|
|
614
|
+
options: nativeOptions,
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
class PseudoAnimation extends NativeAnimationControls {
|
|
619
|
+
constructor(target, pseudoElement, valueName, keyframes, options) {
|
|
620
|
+
const animationOptions = convertMotionOptionsToNative(valueName, keyframes, options);
|
|
621
|
+
const animation = target.animate(animationOptions.keyframes, {
|
|
622
|
+
pseudoElement,
|
|
623
|
+
...animationOptions.options,
|
|
624
|
+
});
|
|
625
|
+
super(animation);
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
function chooseLayerType(valueName) {
|
|
630
|
+
if (valueName === "layout")
|
|
631
|
+
return "group";
|
|
632
|
+
if (valueName === "enter" || valueName === "new")
|
|
633
|
+
return "new";
|
|
634
|
+
if (valueName === "exit" || valueName === "old")
|
|
635
|
+
return "old";
|
|
636
|
+
return "group";
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
let pendingRules = {};
|
|
640
|
+
let style = null;
|
|
641
|
+
const css = {
|
|
642
|
+
set: (selector, values) => {
|
|
643
|
+
pendingRules[selector] = values;
|
|
644
|
+
},
|
|
645
|
+
commit: () => {
|
|
646
|
+
if (!style) {
|
|
647
|
+
style = document.createElement("style");
|
|
648
|
+
style.id = "motion-view";
|
|
649
|
+
}
|
|
650
|
+
let cssText = "";
|
|
651
|
+
for (const selector in pendingRules) {
|
|
652
|
+
const rule = pendingRules[selector];
|
|
653
|
+
cssText += `${selector} {\n`;
|
|
654
|
+
for (const [property, value] of Object.entries(rule)) {
|
|
655
|
+
cssText += ` ${property}: ${value};\n`;
|
|
656
|
+
}
|
|
657
|
+
cssText += "}\n";
|
|
658
|
+
}
|
|
659
|
+
style.textContent = cssText;
|
|
660
|
+
document.head.appendChild(style);
|
|
661
|
+
pendingRules = {};
|
|
662
|
+
},
|
|
663
|
+
remove: () => {
|
|
664
|
+
if (style && style.parentElement) {
|
|
665
|
+
style.parentElement.removeChild(style);
|
|
666
|
+
}
|
|
667
|
+
},
|
|
668
|
+
};
|
|
669
|
+
|
|
670
|
+
function getLayerName(pseudoElement) {
|
|
671
|
+
const match = pseudoElement.match(/::view-transition-(old|new|group|image-pair)\((.*?)\)/);
|
|
672
|
+
if (!match)
|
|
673
|
+
return null;
|
|
674
|
+
return { layer: match[2], type: match[1] };
|
|
675
|
+
}
|
|
676
|
+
|
|
677
|
+
function filterViewAnimations(animation) {
|
|
678
|
+
var _a;
|
|
679
|
+
const { effect } = animation;
|
|
680
|
+
if (!effect)
|
|
681
|
+
return false;
|
|
682
|
+
return (effect.target === document.documentElement &&
|
|
683
|
+
((_a = effect.pseudoElement) === null || _a === void 0 ? void 0 : _a.startsWith("::view-transition")));
|
|
684
|
+
}
|
|
685
|
+
function getViewAnimations() {
|
|
686
|
+
return document.getAnimations().filter(filterViewAnimations);
|
|
687
|
+
}
|
|
688
|
+
|
|
689
|
+
function hasTarget(target, targets) {
|
|
690
|
+
return targets.has(target) && Object.keys(targets.get(target)).length > 0;
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
const definitionNames = ["layout", "enter", "exit", "new", "old"];
|
|
694
|
+
function startViewAnimation(update, defaultOptions, targets) {
|
|
695
|
+
if (!document.startViewTransition) {
|
|
696
|
+
return new Promise(async (resolve) => {
|
|
697
|
+
await update();
|
|
698
|
+
resolve(new BaseGroupPlaybackControls([]));
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
// TODO: Go over existing targets and ensure they all have ids
|
|
702
|
+
/**
|
|
703
|
+
* If we don't have any animations defined for the root target,
|
|
704
|
+
* remove it from being captured.
|
|
705
|
+
*/
|
|
706
|
+
if (!hasTarget("root", targets)) {
|
|
707
|
+
css.set(":root", {
|
|
708
|
+
"view-transition-name": "none",
|
|
709
|
+
});
|
|
710
|
+
}
|
|
711
|
+
/**
|
|
712
|
+
* Set the timing curve to linear for all view transition layers.
|
|
713
|
+
* This gets baked into the keyframes, which can't be changed
|
|
714
|
+
* without breaking the generated animation.
|
|
715
|
+
*
|
|
716
|
+
* This allows us to set easing via updateTiming - which can be changed.
|
|
717
|
+
*/
|
|
718
|
+
css.set("::view-transition-group(*), ::view-transition-old(*), ::view-transition-new(*)", { "animation-timing-function": "linear !important" });
|
|
719
|
+
css.commit(); // Write
|
|
720
|
+
const transition = document.startViewTransition(async () => {
|
|
721
|
+
await update();
|
|
722
|
+
// TODO: Go over new targets and ensure they all have ids
|
|
723
|
+
});
|
|
724
|
+
transition.finished.finally(() => {
|
|
725
|
+
css.remove(); // Write
|
|
726
|
+
});
|
|
727
|
+
return new Promise((resolve) => {
|
|
728
|
+
transition.ready.then(() => {
|
|
729
|
+
var _a;
|
|
730
|
+
const generatedViewAnimations = getViewAnimations();
|
|
731
|
+
const animations = [];
|
|
732
|
+
/**
|
|
733
|
+
* Create animations for our definitions
|
|
734
|
+
*/
|
|
735
|
+
targets.forEach((definition, target) => {
|
|
736
|
+
// TODO: If target is not "root", resolve elements
|
|
737
|
+
// and iterate over each
|
|
738
|
+
for (const key of definitionNames) {
|
|
739
|
+
if (!definition[key])
|
|
740
|
+
continue;
|
|
741
|
+
const { keyframes, options } = definition[key];
|
|
742
|
+
for (let [valueName, valueKeyframes] of Object.entries(keyframes)) {
|
|
743
|
+
if (!valueKeyframes)
|
|
744
|
+
continue;
|
|
745
|
+
const valueOptions = {
|
|
746
|
+
...getValueTransition(defaultOptions, valueName),
|
|
747
|
+
...getValueTransition(options, valueName),
|
|
748
|
+
};
|
|
749
|
+
const type = chooseLayerType(key);
|
|
750
|
+
/**
|
|
751
|
+
* If this is an opacity animation, and keyframes are not an array,
|
|
752
|
+
* we need to convert them into an array and set an initial value.
|
|
753
|
+
*/
|
|
754
|
+
if (valueName === "opacity" &&
|
|
755
|
+
!Array.isArray(valueKeyframes)) {
|
|
756
|
+
const initialValue = type === "new" ? 0 : 1;
|
|
757
|
+
valueKeyframes = [initialValue, valueKeyframes];
|
|
758
|
+
}
|
|
759
|
+
/**
|
|
760
|
+
* Resolve stagger function if provided.
|
|
761
|
+
*/
|
|
762
|
+
if (typeof valueOptions.delay === "function") {
|
|
763
|
+
valueOptions.delay = valueOptions.delay(0, 1);
|
|
764
|
+
}
|
|
765
|
+
const animation = new PseudoAnimation(document.documentElement, `::view-transition-${type}(${target})`, valueName, valueKeyframes, valueOptions);
|
|
766
|
+
animations.push(animation);
|
|
767
|
+
}
|
|
768
|
+
}
|
|
769
|
+
});
|
|
770
|
+
/**
|
|
771
|
+
* Handle browser generated animations
|
|
772
|
+
*/
|
|
773
|
+
for (const animation of generatedViewAnimations) {
|
|
774
|
+
if (animation.playState === "finished")
|
|
775
|
+
continue;
|
|
776
|
+
const { effect } = animation;
|
|
777
|
+
if (!effect || !(effect instanceof KeyframeEffect))
|
|
778
|
+
continue;
|
|
779
|
+
const { pseudoElement } = effect;
|
|
780
|
+
if (!pseudoElement)
|
|
781
|
+
continue;
|
|
782
|
+
const name = getLayerName(pseudoElement);
|
|
783
|
+
if (!name)
|
|
784
|
+
continue;
|
|
785
|
+
const targetDefinition = targets.get(name.layer);
|
|
786
|
+
if (!targetDefinition) {
|
|
787
|
+
/**
|
|
788
|
+
* If transition name is group then update the timing of the animation
|
|
789
|
+
* whereas if it's old or new then we could possibly replace it using
|
|
790
|
+
* the above method.
|
|
791
|
+
*/
|
|
792
|
+
const transitionName = name.type === "group" ? "layout" : "";
|
|
793
|
+
const animationTransition = {
|
|
794
|
+
...getValueTransition(defaultOptions, transitionName),
|
|
795
|
+
};
|
|
796
|
+
applyGeneratorOptions(animationTransition);
|
|
797
|
+
const easing = mapEasingToNativeEasing(animationTransition.ease, animationTransition.duration);
|
|
798
|
+
effect.updateTiming({
|
|
799
|
+
delay: motionUtils.secondsToMilliseconds((_a = animationTransition.delay) !== null && _a !== void 0 ? _a : 0),
|
|
800
|
+
duration: animationTransition.duration,
|
|
801
|
+
easing,
|
|
802
|
+
});
|
|
803
|
+
animations.push(new NativeAnimationControls(animation));
|
|
804
|
+
}
|
|
805
|
+
else if (hasOpacity(targetDefinition, "enter") &&
|
|
806
|
+
hasOpacity(targetDefinition, "exit") &&
|
|
807
|
+
effect
|
|
808
|
+
.getKeyframes()
|
|
809
|
+
.some((keyframe) => keyframe.mixBlendMode)) {
|
|
810
|
+
animations.push(new NativeAnimationControls(animation));
|
|
811
|
+
}
|
|
812
|
+
else {
|
|
813
|
+
animation.cancel();
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
resolve(new BaseGroupPlaybackControls(animations));
|
|
817
|
+
});
|
|
818
|
+
});
|
|
819
|
+
}
|
|
820
|
+
function hasOpacity(target, key) {
|
|
821
|
+
var _a;
|
|
822
|
+
return (_a = target === null || target === void 0 ? void 0 : target[key]) === null || _a === void 0 ? void 0 : _a.keyframes.opacity;
|
|
823
|
+
}
|
|
824
|
+
|
|
825
|
+
/**
|
|
826
|
+
* TODO:
|
|
827
|
+
* - Create view transition on next tick
|
|
828
|
+
* - Replace animations with Motion animations
|
|
829
|
+
* - Return GroupAnimation on next tick
|
|
830
|
+
*/
|
|
831
|
+
class ViewTransitionBuilder {
|
|
832
|
+
constructor(update, options = {}) {
|
|
833
|
+
this.currentTarget = "root";
|
|
834
|
+
this.targets = new Map();
|
|
835
|
+
this.notifyReady = motionUtils.noop;
|
|
836
|
+
this.readyPromise = new Promise((resolve) => {
|
|
837
|
+
this.notifyReady = resolve;
|
|
838
|
+
});
|
|
839
|
+
queueMicrotask(() => {
|
|
840
|
+
startViewAnimation(update, options, this.targets).then((animation) => this.notifyReady(animation));
|
|
841
|
+
});
|
|
842
|
+
}
|
|
843
|
+
get(selector) {
|
|
844
|
+
this.currentTarget = selector;
|
|
845
|
+
return this;
|
|
846
|
+
}
|
|
847
|
+
layout(keyframes, options) {
|
|
848
|
+
this.updateTarget("layout", keyframes, options);
|
|
849
|
+
return this;
|
|
850
|
+
}
|
|
851
|
+
new(keyframes, options) {
|
|
852
|
+
this.updateTarget("new", keyframes, options);
|
|
853
|
+
return this;
|
|
854
|
+
}
|
|
855
|
+
old(keyframes, options) {
|
|
856
|
+
this.updateTarget("old", keyframes, options);
|
|
857
|
+
return this;
|
|
858
|
+
}
|
|
859
|
+
enter(keyframes, options) {
|
|
860
|
+
this.updateTarget("enter", keyframes, options);
|
|
861
|
+
return this;
|
|
862
|
+
}
|
|
863
|
+
exit(keyframes, options) {
|
|
864
|
+
this.updateTarget("exit", keyframes, options);
|
|
865
|
+
return this;
|
|
866
|
+
}
|
|
867
|
+
crossfade(options) {
|
|
868
|
+
this.updateTarget("enter", { opacity: 1 }, options);
|
|
869
|
+
this.updateTarget("exit", { opacity: 0 }, options);
|
|
870
|
+
return this;
|
|
871
|
+
}
|
|
872
|
+
updateTarget(target, keyframes, options = {}) {
|
|
873
|
+
const { currentTarget, targets } = this;
|
|
874
|
+
if (!targets.has(currentTarget)) {
|
|
875
|
+
targets.set(currentTarget, {});
|
|
876
|
+
}
|
|
877
|
+
const targetData = targets.get(currentTarget);
|
|
878
|
+
targetData[target] = { keyframes, options };
|
|
879
|
+
}
|
|
880
|
+
then(resolve, reject) {
|
|
881
|
+
return this.readyPromise.then(resolve, reject);
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
function view(update, defaultOptions = {}) {
|
|
885
|
+
return new ViewTransitionBuilder(update, defaultOptions);
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
function setDragLock(axis) {
|
|
889
|
+
if (axis === "x" || axis === "y") {
|
|
890
|
+
if (isDragging[axis]) {
|
|
891
|
+
return null;
|
|
892
|
+
}
|
|
893
|
+
else {
|
|
894
|
+
isDragging[axis] = true;
|
|
895
|
+
return () => {
|
|
896
|
+
isDragging[axis] = false;
|
|
897
|
+
};
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
else {
|
|
901
|
+
if (isDragging.x || isDragging.y) {
|
|
902
|
+
return null;
|
|
903
|
+
}
|
|
904
|
+
else {
|
|
905
|
+
isDragging.x = isDragging.y = true;
|
|
906
|
+
return () => {
|
|
907
|
+
isDragging.x = isDragging.y = false;
|
|
908
|
+
};
|
|
909
|
+
}
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
|
|
913
|
+
exports.GroupPlaybackControls = GroupPlaybackControls;
|
|
914
|
+
exports.NativeAnimationControls = NativeAnimationControls;
|
|
915
|
+
exports.ViewTransitionBuilder = ViewTransitionBuilder;
|
|
916
|
+
exports.attachTimeline = attachTimeline;
|
|
917
|
+
exports.calcGeneratorDuration = calcGeneratorDuration;
|
|
918
|
+
exports.capturePointer = capturePointer;
|
|
919
|
+
exports.createGeneratorEasing = createGeneratorEasing;
|
|
920
|
+
exports.cubicBezierAsString = cubicBezierAsString;
|
|
921
|
+
exports.generateLinearEasing = generateLinearEasing;
|
|
922
|
+
exports.getValueTransition = getValueTransition;
|
|
923
|
+
exports.hover = hover;
|
|
924
|
+
exports.isBezierDefinition = isBezierDefinition;
|
|
925
|
+
exports.isDragActive = isDragActive;
|
|
926
|
+
exports.isDragging = isDragging;
|
|
927
|
+
exports.isGenerator = isGenerator;
|
|
928
|
+
exports.isNodeOrChild = isNodeOrChild;
|
|
929
|
+
exports.isPrimaryPointer = isPrimaryPointer;
|
|
930
|
+
exports.isWaapiSupportedEasing = isWaapiSupportedEasing;
|
|
931
|
+
exports.mapEasingToNativeEasing = mapEasingToNativeEasing;
|
|
932
|
+
exports.maxGeneratorDuration = maxGeneratorDuration;
|
|
933
|
+
exports.press = press;
|
|
934
|
+
exports.resolveElements = resolveElements;
|
|
935
|
+
exports.setDragLock = setDragLock;
|
|
936
|
+
exports.supportedWaapiEasing = supportedWaapiEasing;
|
|
937
|
+
exports.supportsFlags = supportsFlags;
|
|
938
|
+
exports.supportsLinearEasing = supportsLinearEasing;
|
|
939
|
+
exports.supportsScrollTimeline = supportsScrollTimeline;
|
|
940
|
+
exports.view = view;
|
|
941
|
+
|
|
942
|
+
}));
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("motion-utils")):"function"==typeof define&&define.amd?define(["exports","motion-utils"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).MotionDom={},t.MotionUtils)}(this,(function(t,e){"use strict";const n=e.memo(()=>void 0!==window.ScrollTimeline);class i{constructor(t){this.stop=()=>this.runAll("stop"),this.animations=t.filter(Boolean)}get finished(){return Promise.all(this.animations.map(t=>"finished"in t?t.finished:t))}getAll(t){return this.animations[0][t]}setAll(t,e){for(let n=0;n<this.animations.length;n++)this.animations[n][t]=e}attachTimeline(t,e){const i=this.animations.map(i=>n()&&i.attachTimeline?i.attachTimeline(t):"function"==typeof e?e(i):void 0);return()=>{i.forEach((t,e)=>{t&&t(),this.animations[e].stop()})}}get time(){return this.getAll("time")}set time(t){this.setAll("time",t)}get speed(){return this.getAll("speed")}set speed(t){this.setAll("speed",t)}get startTime(){return this.getAll("startTime")}get duration(){let t=0;for(let e=0;e<this.animations.length;e++)t=Math.max(t,this.animations[e].duration);return t}runAll(t){this.animations.forEach(e=>e[t]())}flatten(){this.runAll("flatten")}play(){this.runAll("play")}pause(){this.runAll("pause")}cancel(){this.runAll("cancel")}complete(){this.runAll("complete")}}function o(t,e){return t?t[e]||t.default||t:void 0}function r(t){let e=0;let n=t.next(e);for(;!n.done&&e<2e4;)e+=50,n=t.next(e);return e>=2e4?1/0:e}function a(t,n=100,i){const o=i({...t,keyframes:[0,n]}),a=Math.min(r(o),2e4);return{type:"keyframes",ease:t=>o.next(a*t).value/n,duration:e.millisecondsToSeconds(a)}}function s(t){return"function"==typeof t}function u(t,e){t.timeline=e,t.onfinish=null}class l{constructor(t){this.animation=t}get duration(){var t,n,i;const o=(null===(n=null===(t=this.animation)||void 0===t?void 0:t.effect)||void 0===n?void 0:n.getComputedTiming().duration)||(null===(i=this.options)||void 0===i?void 0:i.duration)||300;return e.millisecondsToSeconds(Number(o))}get time(){var t;return this.animation?e.millisecondsToSeconds((null===(t=this.animation)||void 0===t?void 0:t.currentTime)||0):0}set time(t){this.animation&&(this.animation.currentTime=e.secondsToMilliseconds(t))}get speed(){return this.animation?this.animation.playbackRate:1}set speed(t){this.animation&&(this.animation.playbackRate=t)}get state(){return this.animation?this.animation.playState:"finished"}get startTime(){return this.animation?this.animation.startTime:null}get finished(){return this.animation?this.animation.finished:Promise.resolve()}play(){this.animation&&this.animation.play()}pause(){this.animation&&this.animation.pause()}stop(){this.animation&&"idle"!==this.state&&"finished"!==this.state&&(this.animation.commitStyles&&this.animation.commitStyles(),this.cancel())}flatten(){var t;this.animation&&(null===(t=this.animation.effect)||void 0===t||t.updateTiming({easing:"linear"}))}attachTimeline(t){return this.animation&&u(this.animation,t),e.noop}complete(){this.animation&&this.animation.finish()}cancel(){try{this.animation&&this.animation.cancel()}catch(t){}}}const c=t=>Array.isArray(t)&&"number"==typeof t[0],d={linearEasing:void 0};function m(t,n){const i=e.memo(t);return()=>{var t;return null!==(t=d[n])&&void 0!==t?t:i()}}const f=m(()=>{try{document.createElement("div").animate({opacity:0},{easing:"linear(0, 1)"})}catch(t){return!1}return!0},"linearEasing"),h=(t,n,i=10)=>{let o="";const r=Math.max(Math.round(n/i),2);for(let n=0;n<r;n++)o+=t(e.progress(0,r-1,n))+", ";return`linear(${o.substring(0,o.length-2)})`};const p=([t,e,n,i])=>`cubic-bezier(${t}, ${e}, ${n}, ${i})`,y={linear:"linear",ease:"ease",easeIn:"ease-in",easeOut:"ease-out",easeInOut:"ease-in-out",circIn:p([0,.65,.55,1]),circOut:p([.55,0,1,.45]),backIn:p([.31,.01,.66,-.59]),backOut:p([.33,1.53,.69,.99])};function g(t,e){return t?"function"==typeof t&&f()?h(t,e):c(t)?p(t):Array.isArray(t)?t.map(t=>g(t,e)||y.easeOut):y[t]:void 0}const v={x:!1,y:!1};function T(){return v.x||v.y}function E(t,e,n){var i;if(t instanceof EventTarget)return[t];if("string"==typeof t){let o=document;e&&(o=e.current);const r=null!==(i=null==n?void 0:n[t])&&void 0!==i?i:o.querySelectorAll(t);return r?Array.from(r):[]}return Array.from(t)}function w(t,e){const n=E(t),i=new AbortController;return[n,{passive:!0,...e,signal:i.signal},()=>i.abort()]}function A(t){return!("touch"===t.pointerType||T())}function b(t,e){const n=e+"PointerCapture";if(t.target instanceof Element&&n in t.target&&void 0!==t.pointerId)try{t.target[n](t.pointerId)}catch(t){}}const x=(t,e)=>!!e&&(t===e||x(t,e.parentElement)),k=t=>"mouse"===t.pointerType?"number"!=typeof t.button||t.button<=0:!1!==t.isPrimary,L=new Set(["BUTTON","INPUT","SELECT","TEXTAREA","A"]);const P=new WeakSet;function M(t){return e=>{"Enter"===e.key&&t(e)}}function S(t,e){t.dispatchEvent(new PointerEvent("pointer"+e,{isPrimary:!0,bubbles:!0}))}function O(t){return k(t)&&!T()}function C(t){var n;if(s(t.type)){const n=a(t,100,t.type);t.ease=f()?n.ease:"easeOut",t.duration=e.secondsToMilliseconds(n.duration),t.type="keyframes"}else t.duration=e.secondsToMilliseconds(null!==(n=t.duration)&&void 0!==n?n:.3),t.ease=t.ease||"easeOut"}class I extends l{constructor(t,n,i,o,r){const a=function(t,n,i){var o;const r={},a={fill:"both",easing:"linear",composite:"replace"};a.delay=e.secondsToMilliseconds(null!==(o=i.delay)&&void 0!==o?o:0),C(i),a.duration=i.duration;const{ease:s,times:u}=i;u&&(r.offset=u),r[t]=n;const l=g(s,i.duration);return Array.isArray(l)?r.easing=l:a.easing=l,{keyframes:r,options:a}}(i,o,r);super(t.animate(a.keyframes,{pseudoElement:n,...a.options}))}}function $(t){return"layout"===t?"group":"enter"===t||"new"===t?"new":"exit"===t||"old"===t?"old":"group"}let B={},D=null;const N=(t,e)=>{B[t]=e},R=()=>{D||(D=document.createElement("style"),D.id="motion-view");let t="";for(const e in B){const n=B[e];t+=e+" {\n";for(const[e,i]of Object.entries(n))t+=` ${e}: ${i};\n`;t+="}\n"}D.textContent=t,document.head.appendChild(D),B={}},G=()=>{D&&D.parentElement&&D.parentElement.removeChild(D)};function W(t){const e=t.match(/::view-transition-(old|new|group|image-pair)\((.*?)\)/);return e?{layer:e[2],type:e[1]}:null}function j(t){var e;const{effect:n}=t;return!!n&&(n.target===document.documentElement&&(null===(e=n.pseudoElement)||void 0===e?void 0:e.startsWith("::view-transition")))}const V=["layout","enter","exit","new","old"];function q(t,n,r){if(!document.startViewTransition)return new Promise(async e=>{await t(),e(new i([]))});(function(t,e){return e.has(t)&&Object.keys(e.get(t)).length>0})("root",r)||N(":root",{"view-transition-name":"none"}),N("::view-transition-group(*), ::view-transition-old(*), ::view-transition-new(*)",{"animation-timing-function":"linear !important"}),R();const a=document.startViewTransition(async()=>{await t()});return a.finished.finally(()=>{G()}),new Promise(t=>{a.ready.then(()=>{var a;const s=document.getAnimations().filter(j),u=[];r.forEach((t,e)=>{for(const i of V){if(!t[i])continue;const{keyframes:r,options:a}=t[i];for(let[t,s]of Object.entries(r)){if(!s)continue;const r={...o(n,t),...o(a,t)},l=$(i);if("opacity"===t&&!Array.isArray(s)){s=["new"===l?0:1,s]}"function"==typeof r.delay&&(r.delay=r.delay(0,1));const c=new I(document.documentElement,`::view-transition-${l}(${e})`,t,s,r);u.push(c)}}});for(const t of s){if("finished"===t.playState)continue;const{effect:i}=t;if(!(i&&i instanceof KeyframeEffect))continue;const{pseudoElement:s}=i;if(!s)continue;const c=W(s);if(!c)continue;const d=r.get(c.layer);if(d)z(d,"enter")&&z(d,"exit")&&i.getKeyframes().some(t=>t.mixBlendMode)?u.push(new l(t)):t.cancel();else{const r="group"===c.type?"layout":"",s={...o(n,r)};C(s);const d=g(s.ease,s.duration);i.updateTiming({delay:e.secondsToMilliseconds(null!==(a=s.delay)&&void 0!==a?a:0),duration:s.duration,easing:d}),u.push(new l(t))}}t(new i(u))})})}function z(t,e){var n;return null===(n=null==t?void 0:t[e])||void 0===n?void 0:n.keyframes.opacity}class U{constructor(t,n={}){this.currentTarget="root",this.targets=new Map,this.notifyReady=e.noop,this.readyPromise=new Promise(t=>{this.notifyReady=t}),queueMicrotask(()=>{q(t,n,this.targets).then(t=>this.notifyReady(t))})}get(t){return this.currentTarget=t,this}layout(t,e){return this.updateTarget("layout",t,e),this}new(t,e){return this.updateTarget("new",t,e),this}old(t,e){return this.updateTarget("old",t,e),this}enter(t,e){return this.updateTarget("enter",t,e),this}exit(t,e){return this.updateTarget("exit",t,e),this}crossfade(t){return this.updateTarget("enter",{opacity:1},t),this.updateTarget("exit",{opacity:0},t),this}updateTarget(t,e,n={}){const{currentTarget:i,targets:o}=this;o.has(i)||o.set(i,{});o.get(i)[t]={keyframes:e,options:n}}then(t,e){return this.readyPromise.then(t,e)}}t.GroupPlaybackControls=class extends i{then(t,e){return Promise.all(this.animations).then(t).catch(e)}},t.NativeAnimationControls=l,t.ViewTransitionBuilder=U,t.attachTimeline=u,t.calcGeneratorDuration=r,t.capturePointer=b,t.createGeneratorEasing=a,t.cubicBezierAsString=p,t.generateLinearEasing=h,t.getValueTransition=o,t.hover=function(t,e,n={}){const[i,o,r]=w(t,n),a=t=>{if(!A(t))return;const{target:n}=t,i=e(n,t);if("function"!=typeof i||!n)return;const r=t=>{A(t)&&(i(t),n.removeEventListener("pointerleave",r))};n.addEventListener("pointerleave",r,o)};return i.forEach(t=>{t.addEventListener("pointerenter",a,o)}),r},t.isBezierDefinition=c,t.isDragActive=T,t.isDragging=v,t.isGenerator=s,t.isNodeOrChild=x,t.isPrimaryPointer=k,t.isWaapiSupportedEasing=function t(e){return Boolean("function"==typeof e&&f()||!e||"string"==typeof e&&(e in y||f())||c(e)||Array.isArray(e)&&e.every(t))},t.mapEasingToNativeEasing=g,t.maxGeneratorDuration=2e4,t.press=function(t,e,n={}){const[i,o,r]=w(t,n),a=t=>{const n=t.currentTarget;if(!n||!O(t)||P.has(n))return;P.add(n),b(t,"set");const i=e(n,t),r=(t,e)=>{n.removeEventListener("pointerup",a),n.removeEventListener("pointercancel",s),b(t,"release"),O(t)&&P.has(n)&&(P.delete(n),"function"==typeof i&&i(t,{success:e}))},a=t=>{const e=!!t.isTrusted&&(i=t,o=n instanceof Element?n.getBoundingClientRect():{left:0,top:0,right:window.innerWidth,bottom:window.innerHeight},i.clientX<o.left||i.clientX>o.right||i.clientY<o.top||i.clientY>o.bottom);var i,o;r(t,!e&&(!(n instanceof Element)||x(n,t.target)))},s=t=>{r(t,!1)};n.addEventListener("pointerup",a,o),n.addEventListener("pointercancel",s,o),n.addEventListener("lostpointercapture",s,o)};return i.forEach(t=>{let e=!1;var i;(t=n.useGlobalTarget?window:t)instanceof HTMLElement&&(e=!0,i=t,L.has(i.tagName)||-1!==i.tabIndex||null!==t.getAttribute("tabindex")||(t.tabIndex=0)),t.addEventListener("pointerdown",a,o),e&&t.addEventListener("focus",t=>((t,e)=>{const n=t.currentTarget;if(!n)return;const i=M(()=>{if(P.has(n))return;S(n,"down");const t=M(()=>{S(n,"up")});n.addEventListener("keyup",t,e),n.addEventListener("blur",()=>S(n,"cancel"),e)});n.addEventListener("keydown",i,e),n.addEventListener("blur",()=>n.removeEventListener("keydown",i),e)})(t,o),o)}),r},t.resolveElements=E,t.setDragLock=function(t){return"x"===t||"y"===t?v[t]?null:(v[t]=!0,()=>{v[t]=!1}):v.x||v.y?null:(v.x=v.y=!0,()=>{v.x=v.y=!1})},t.supportedWaapiEasing=y,t.supportsFlags=d,t.supportsLinearEasing=f,t.supportsScrollTimeline=n,t.view=function(t,e={}){return new U(t,e)}}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "motion-dom",
|
|
3
|
-
"version": "12.4.
|
|
3
|
+
"version": "12.4.10",
|
|
4
4
|
"author": "Matt Perry",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": "https://github.com/motiondivision/motion",
|
|
@@ -16,12 +16,12 @@
|
|
|
16
16
|
}
|
|
17
17
|
},
|
|
18
18
|
"dependencies": {
|
|
19
|
-
"motion-utils": "^12.
|
|
19
|
+
"motion-utils": "^12.4.10"
|
|
20
20
|
},
|
|
21
21
|
"scripts": {
|
|
22
22
|
"clean": "rm -rf types dist lib",
|
|
23
23
|
"build": "yarn clean && tsc -p . && rollup -c",
|
|
24
24
|
"dev": "concurrently -c blue,red -n tsc,rollup --kill-others \"tsc --watch -p . --preserveWatchOutput\" \"rollup --config --watch --no-watch.clearScreen\""
|
|
25
25
|
},
|
|
26
|
-
"gitHead": "
|
|
26
|
+
"gitHead": "18b71d1395e692a13545e85f21bffcbf4a38538b"
|
|
27
27
|
}
|