motion-dom 14.4.0 → 14.5.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.
Files changed (43) hide show
  1. package/dist/cjs/index.js +269 -24
  2. package/dist/cjs/index.js.map +1 -1
  3. package/dist/es/animation/animate/element.mjs +10 -1
  4. package/dist/es/animation/animate/element.mjs.map +1 -1
  5. package/dist/es/gestures/drag/VisualElementDragControls.mjs.map +1 -1
  6. package/dist/es/gestures/drag/index.mjs +16 -1
  7. package/dist/es/gestures/drag/index.mjs.map +1 -1
  8. package/dist/es/gestures/hover.mjs +2 -0
  9. package/dist/es/gestures/hover.mjs.map +1 -1
  10. package/dist/es/gestures/pan/PanSession.mjs +6 -0
  11. package/dist/es/gestures/pan/PanSession.mjs.map +1 -1
  12. package/dist/es/gestures/press/index.mjs +3 -1
  13. package/dist/es/gestures/press/index.mjs.map +1 -1
  14. package/dist/es/gestures/press/utils/keyboard.mjs +22 -12
  15. package/dist/es/gestures/press/utils/keyboard.mjs.map +1 -1
  16. package/dist/es/gestures/utils/setup.mjs +2 -1
  17. package/dist/es/gestures/utils/setup.mjs.map +1 -1
  18. package/dist/es/index.mjs +4 -1
  19. package/dist/es/index.mjs.map +1 -1
  20. package/dist/es/layout/LayoutAnimationBuilder.mjs +2 -1
  21. package/dist/es/layout/LayoutAnimationBuilder.mjs.map +1 -1
  22. package/dist/es/layout/animate-layout.mjs +7 -1
  23. package/dist/es/layout/animate-layout.mjs.map +1 -1
  24. package/dist/es/resize/index.mjs +2 -1
  25. package/dist/es/resize/index.mjs.map +1 -1
  26. package/dist/es/scope/active.mjs +39 -0
  27. package/dist/es/scope/active.mjs.map +1 -0
  28. package/dist/es/scope/index.mjs +122 -0
  29. package/dist/es/scope/index.mjs.map +1 -0
  30. package/dist/es/scope/media.mjs +42 -0
  31. package/dist/es/scope/media.mjs.map +1 -0
  32. package/dist/es/utils/delay.mjs +3 -1
  33. package/dist/es/utils/delay.mjs.map +1 -1
  34. package/dist/es/utils/resolve-elements.mjs +5 -4
  35. package/dist/es/utils/resolve-elements.mjs.map +1 -1
  36. package/dist/es/view/index.mjs +4 -1
  37. package/dist/es/view/index.mjs.map +1 -1
  38. package/dist/index.d.ts +135 -2
  39. package/dist/motion-dom.dev.js +269 -24
  40. package/dist/motion-dom.js +1 -1
  41. package/dist/size-rollup-style-effect.js +1 -1
  42. package/dist/size-rollup-style-effect.js.map +1 -1
  43. package/package.json +3 -3
@@ -5829,6 +5829,43 @@
5829
5829
  return motionUtils.isObject(element) && "ownerSVGElement" in element;
5830
5830
  }
5831
5831
 
5832
+ const activeScope = {};
5833
+ /**
5834
+ * Register `cleanup` with the active scope, if there is one, so stopping
5835
+ * the scope also runs it.
5836
+ */
5837
+ function addToScope(cleanup) {
5838
+ activeScope.current?.add(cleanup);
5839
+ return cleanup;
5840
+ }
5841
+ /**
5842
+ * Collect the animation a builder like `animateLayout()` or
5843
+ * `animateView()` resolves with. If the scope stops first, the animation
5844
+ * is stopped as soon as it starts.
5845
+ */
5846
+ function addBuilderToScope(builder) {
5847
+ const scope = activeScope.current;
5848
+ if (!scope)
5849
+ return;
5850
+ let isStopped = false;
5851
+ let stop;
5852
+ const remove = scope.add(() => {
5853
+ isStopped = true;
5854
+ stop?.();
5855
+ });
5856
+ builder.then((animation) => {
5857
+ if (!animation)
5858
+ return;
5859
+ stop = () => animation.stop();
5860
+ isStopped ? stop() : animation.finished.then(remove);
5861
+ }, remove);
5862
+ }
5863
+ /**
5864
+ * Bind `callback` to the active scope, so anything it starts later (for
5865
+ * instance an animation in an `inView()` callback) is collected too.
5866
+ */
5867
+ const bindToScope = (callback) => activeScope.current ? activeScope.current.bind(callback) : callback;
5868
+
5832
5869
  function resolveElements(elementOrSelector, scope, selectorCache) {
5833
5870
  if (elementOrSelector == null) {
5834
5871
  return [];
@@ -5837,10 +5874,9 @@
5837
5874
  return [elementOrSelector];
5838
5875
  }
5839
5876
  else if (typeof elementOrSelector === "string") {
5840
- let root = document;
5841
- if (scope) {
5842
- root = scope.current;
5843
- }
5877
+ const root = scope
5878
+ ? scope.current
5879
+ : activeScope.current?.root || document;
5844
5880
  const elements = selectorCache?.[elementOrSelector] ??
5845
5881
  root.querySelectorAll(elementOrSelector);
5846
5882
  return elements ? Array.from(elements) : [];
@@ -6630,12 +6666,20 @@
6630
6666
  * sharing values and a renderer.
6631
6667
  */
6632
6668
  function animateElement(element, keyframes, transition, visualElement) {
6669
+ activeScope.current?.track(element);
6633
6670
  if (visualElement) {
6634
6671
  return animateValues((key) => visualElement.getValue(key, null), keyframes, transition, visualElement);
6635
6672
  }
6636
6673
  const effect = getElementEffect(element);
6637
6674
  return animateEffectSubject(effect, element, keyframes, transition, new EffectSubject(effect, element));
6638
6675
  }
6676
+ /**
6677
+ * Detach the values styleEffect/svgEffect bound to `element`, so the
6678
+ * next animation reads its starting values from the DOM again.
6679
+ */
6680
+ const releaseElement = (element) => getElementEffect(element)
6681
+ .state(element)
6682
+ ?.release();
6639
6683
  /**
6640
6684
  * Hand the values styleEffect/svgEffect are rendering on `element`,
6641
6685
  * whether bound by animate() or directly, to a VisualElement that now
@@ -6726,6 +6770,7 @@
6726
6770
  */
6727
6771
  function delay(callback, timeout) {
6728
6772
  const start = time.now();
6773
+ callback = bindToScope(callback);
6729
6774
  const checkElapsed = ({ timestamp }) => {
6730
6775
  const elapsed = timestamp - start;
6731
6776
  if (elapsed >= timeout) {
@@ -6734,7 +6779,7 @@
6734
6779
  }
6735
6780
  };
6736
6781
  frame.setup(checkElapsed, true);
6737
- return () => cancelFrame(checkElapsed);
6782
+ return addToScope(() => cancelFrame(checkElapsed));
6738
6783
  }
6739
6784
  function delayInSeconds(callback, timeout) {
6740
6785
  return delay(callback, motionUtils.secondsToMilliseconds(timeout));
@@ -10148,7 +10193,7 @@
10148
10193
  function parseAnimateLayoutArgs(scopeOrUpdateDom, updateDomOrOptions, options) {
10149
10194
  if (typeof scopeOrUpdateDom === "function") {
10150
10195
  return {
10151
- scope: document,
10196
+ scope: activeScope.current?.root || document,
10152
10197
  updateDom: scopeOrUpdateDom,
10153
10198
  defaultOptions: updateDomOrOptions,
10154
10199
  };
@@ -10284,7 +10329,7 @@
10284
10329
  }
10285
10330
 
10286
10331
  function resize(a, b) {
10287
- return typeof a === "function" ? resizeWindow(a) : resizeElement(a, b);
10332
+ return addToScope(typeof a === "function" ? resizeWindow(a) : resizeElement(a, b));
10288
10333
  }
10289
10334
 
10290
10335
  const distance = (a, b) => Math.abs(a - b);
@@ -10349,6 +10394,12 @@
10349
10394
  onMove && onMove(this.lastMoveEvent, info);
10350
10395
  };
10351
10396
  this.handlePointerMove = (event, info) => {
10397
+ /**
10398
+ * This event's pageX/pageY already include any window scroll that
10399
+ * hasn't fired its scroll event yet. Settle it now, so that scroll
10400
+ * event doesn't add it to the new point a second time.
10401
+ */
10402
+ this.handleScroll(window);
10352
10403
  this.lastMoveEvent = event;
10353
10404
  this.lastRawMoveEventInfo = info;
10354
10405
  this.lastMoveEventInfo = transformPoint(info, this.transformPagePoint);
@@ -11380,7 +11431,20 @@
11380
11431
  */
11381
11432
  function drag(elementOrSelector, options = {}) {
11382
11433
  const props = toProps(options);
11434
+ const scope = activeScope.current;
11435
+ /**
11436
+ * Run callbacks in the active scope, so work they start is collected.
11437
+ */
11438
+ if (scope) {
11439
+ for (const key in props) {
11440
+ const value = props[key];
11441
+ if (typeof value === "function") {
11442
+ props[key] = scope.bind(value);
11443
+ }
11444
+ }
11445
+ }
11383
11446
  const cleanups = resolveElements(elementOrSelector).map((element) => {
11447
+ scope?.track(element);
11384
11448
  const [node, release] = acquireProjectionNode(element);
11385
11449
  /**
11386
11450
  * Measure layout even when not animating it, as constraints and
@@ -11394,10 +11458,11 @@
11394
11458
  removeStyles();
11395
11459
  removeListeners?.();
11396
11460
  controls.cancel();
11461
+ controls.stopAnimation();
11397
11462
  release();
11398
11463
  };
11399
11464
  });
11400
- return () => cleanups.forEach((cleanup) => cleanup());
11465
+ return addToScope(() => cleanups.forEach((cleanup) => cleanup()));
11401
11466
  }
11402
11467
 
11403
11468
  function setupGesture(elementOrSelector, options) {
@@ -11408,7 +11473,7 @@
11408
11473
  ...options,
11409
11474
  signal: gestureAbortController.signal,
11410
11475
  };
11411
- const cancel = () => gestureAbortController.abort();
11476
+ const cancel = addToScope(() => gestureAbortController.abort());
11412
11477
  return [elements, eventOptions, cancel];
11413
11478
  }
11414
11479
 
@@ -11423,6 +11488,7 @@
11423
11488
  * @public
11424
11489
  */
11425
11490
  function hover(elementOrSelector, onHoverStart, options = {}) {
11491
+ onHoverStart = bindToScope(onHoverStart);
11426
11492
  const [elements, eventOptions, cancel] = setupGesture(elementOrSelector, options);
11427
11493
  elements.forEach((element) => {
11428
11494
  let isPressed = false;
@@ -11511,26 +11577,36 @@
11511
11577
  function firePointerEvent(target, type) {
11512
11578
  target.dispatchEvent(new PointerEvent("pointer" + type, { isPrimary: true, bubbles: true }));
11513
11579
  }
11514
- const enableKeyboardPress = (focusEvent, eventOptions) => {
11515
- const element = focusEvent.currentTarget;
11516
- if (!element)
11517
- return;
11580
+ const enableKeyboardPress = (element, eventOptions) => {
11581
+ /**
11582
+ * Listeners are scoped to this focus and all released on blur, rather
11583
+ * than added per Enter press, so they can't accumulate and fire
11584
+ * duplicate pointerup/pointercancel events. See #3935.
11585
+ */
11586
+ let isKeyPressing = false;
11518
11587
  const handleKeydown = filterEvents(() => {
11519
11588
  if (isPressing.has(element))
11520
11589
  return;
11590
+ isKeyPressing = true;
11521
11591
  firePointerEvent(element, "down");
11522
- const handleKeyup = filterEvents(() => {
11523
- firePointerEvent(element, "up");
11524
- });
11525
- const handleBlur = () => firePointerEvent(element, "cancel");
11526
- element.addEventListener("keyup", handleKeyup, eventOptions);
11527
- element.addEventListener("blur", handleBlur, eventOptions);
11592
+ });
11593
+ const handleKeyup = filterEvents(() => {
11594
+ if (!isKeyPressing)
11595
+ return;
11596
+ isKeyPressing = false;
11597
+ firePointerEvent(element, "up");
11528
11598
  });
11529
11599
  element.addEventListener("keydown", handleKeydown, eventOptions);
11600
+ element.addEventListener("keyup", handleKeyup, eventOptions);
11530
11601
  /**
11531
- * Add an event listener that fires on blur to remove the keydown events.
11602
+ * On blur, remove the key listeners and cancel any keyboard press.
11532
11603
  */
11533
- element.addEventListener("blur", () => element.removeEventListener("keydown", handleKeydown), eventOptions);
11604
+ element.addEventListener("blur", () => {
11605
+ element.removeEventListener("keydown", handleKeydown);
11606
+ element.removeEventListener("keyup", handleKeyup);
11607
+ if (isKeyPressing)
11608
+ firePointerEvent(element, "cancel");
11609
+ }, { ...eventOptions, once: true });
11534
11610
  };
11535
11611
 
11536
11612
  /**
@@ -11561,6 +11637,7 @@
11561
11637
  * @public
11562
11638
  */
11563
11639
  function press(targetOrSelector, onPressStart, options = {}) {
11640
+ onPressStart = bindToScope(onPressStart);
11564
11641
  const [targets, eventOptions, cancelEvents] = setupGesture(targetOrSelector, options);
11565
11642
  const startPress = (startEvent) => {
11566
11643
  const target = startEvent.currentTarget;
@@ -11609,7 +11686,7 @@
11609
11686
  const pointerDownTarget = options.useGlobalTarget ? window : target;
11610
11687
  pointerDownTarget.addEventListener("pointerdown", startPress, eventOptions);
11611
11688
  if (isHTMLElement(target)) {
11612
- target.addEventListener("focus", (event) => enableKeyboardPress(event, eventOptions));
11689
+ target.addEventListener("focus", () => enableKeyboardPress(target, eventOptions), eventOptions);
11613
11690
  if (!isElementKeyboardAccessible(target) &&
11614
11691
  !target.hasAttribute("tabindex")) {
11615
11692
  target.tabIndex = 0;
@@ -11626,6 +11703,160 @@
11626
11703
  : computedStyle[name];
11627
11704
  }
11628
11705
 
11706
+ const keywords = {
11707
+ reducedMotion: "(prefers-reduced-motion: reduce)",
11708
+ motion: "(prefers-reduced-motion: no-preference)",
11709
+ dark: "(prefers-color-scheme: dark)",
11710
+ light: "(prefers-color-scheme: light)",
11711
+ hover: "(hover: hover) and (pointer: fine)",
11712
+ touch: "(pointer: coarse)",
11713
+ portrait: "(orientation: portrait)",
11714
+ landscape: "(orientation: landscape)",
11715
+ };
11716
+ /**
11717
+ * Turn a condition into a media query. Full media queries pass through
11718
+ * unchanged. Otherwise each space-separated token must match:
11719
+ *
11720
+ * - A width comparison: `">=800"`, `"<1200px"`, `"> 40em"`
11721
+ * - A keyword: `reducedMotion`, `motion`, `dark`, `light`, `hover`,
11722
+ * `touch`, `portrait`, `landscape`
11723
+ *
11724
+ * Prefix a token with `!` to negate it, for instance `">=800 !touch"`.
11725
+ */
11726
+ function toMediaQuery(condition) {
11727
+ if (condition.includes("("))
11728
+ return condition;
11729
+ return condition
11730
+ .replace(/([<>!]=?)\s+/g, "$1")
11731
+ .trim()
11732
+ .split(/\s+/)
11733
+ .map((token) => {
11734
+ const isNot = token[0] === "!";
11735
+ if (isNot)
11736
+ token = token.slice(1);
11737
+ const width = /^([<>]=?)([\d.]+)([a-z]*)$/.exec(token);
11738
+ const query = width
11739
+ ? `(width ${width[1]} ${width[2]}${width[3] || "px"})`
11740
+ : keywords[token] || token;
11741
+ return isNot ? `(not (${query}))` : query;
11742
+ })
11743
+ .join(" and ");
11744
+ }
11745
+
11746
+ function scopeMotion(...args) {
11747
+ const root = typeof args[0] === "function" ? undefined : args.shift();
11748
+ const [setup, options = {}] = args;
11749
+ const { transition, matchMedia } = options;
11750
+ const queries = [];
11751
+ const styles = new Map();
11752
+ let cleanups = [];
11753
+ let isPending = false;
11754
+ const active = {
11755
+ root: resolveElements(root)[0],
11756
+ transition,
11757
+ add(cleanup) {
11758
+ cleanups.push(cleanup);
11759
+ return () => motionUtils.removeItem(cleanups, cleanup);
11760
+ },
11761
+ bind: (callback) => ((...args) => {
11762
+ const result = scope.run(() => callback(...args));
11763
+ return typeof result === "function"
11764
+ ? active.bind(result)
11765
+ : result;
11766
+ }),
11767
+ track(element) {
11768
+ styles.has(element) ||
11769
+ styles.set(element, element.getAttribute("style"));
11770
+ },
11771
+ };
11772
+ const clear = (restore) => {
11773
+ const current = cleanups;
11774
+ cleanups = [];
11775
+ for (let i = current.length - 1; i >= 0; i--)
11776
+ current[i]();
11777
+ restore &&
11778
+ styles.forEach((style, element) => {
11779
+ releaseElement(element);
11780
+ style === null
11781
+ ? element.removeAttribute("style")
11782
+ : element.setAttribute("style", style);
11783
+ });
11784
+ styles.clear();
11785
+ };
11786
+ const start = () => {
11787
+ scope.state = "active";
11788
+ let matches = !queries.length;
11789
+ scope.media = {};
11790
+ for (const [name, query] of queries) {
11791
+ matches = (scope.media[name] = query.matches) || matches;
11792
+ }
11793
+ if (matches && setup) {
11794
+ const cleanup = scope.run(() => setup(scope));
11795
+ typeof cleanup === "function" && active.add(cleanup);
11796
+ }
11797
+ };
11798
+ const onChange = () => {
11799
+ if (isPending)
11800
+ return;
11801
+ isPending = true;
11802
+ /**
11803
+ * Several conditions can change together, for instance two
11804
+ * breakpoints crossed by one resize. Refresh once.
11805
+ */
11806
+ queueMicrotask(() => {
11807
+ isPending = false;
11808
+ queries.length && scope.refresh();
11809
+ });
11810
+ };
11811
+ const kill = (restore) => {
11812
+ scope.state = restore ? "cancelled" : "stopped";
11813
+ for (const [, query] of queries) {
11814
+ query.removeEventListener("change", onChange);
11815
+ }
11816
+ queries.length = 0;
11817
+ clear(restore);
11818
+ };
11819
+ const runIn = (target, callback) => {
11820
+ const prev = activeScope.current;
11821
+ activeScope.current = target;
11822
+ try {
11823
+ return callback();
11824
+ }
11825
+ finally {
11826
+ activeScope.current = prev;
11827
+ }
11828
+ };
11829
+ const scope = {
11830
+ state: "active",
11831
+ media: {},
11832
+ run: (callback) => runIn(active, callback),
11833
+ ignore: (callback) => runIn(undefined, callback),
11834
+ bind: active.bind,
11835
+ stop: () => kill(),
11836
+ cancel: () => kill(true),
11837
+ refresh() {
11838
+ clear(true);
11839
+ start();
11840
+ },
11841
+ };
11842
+ if (matchMedia && typeof window !== "undefined") {
11843
+ const conditions = typeof matchMedia === "string"
11844
+ ? { matches: matchMedia }
11845
+ : matchMedia;
11846
+ for (const name in conditions) {
11847
+ const query = window.matchMedia(toMediaQuery(conditions[name]));
11848
+ query.addEventListener("change", onChange);
11849
+ queries.push([name, query]);
11850
+ }
11851
+ }
11852
+ /**
11853
+ * A scope created inside another is cancelled with it.
11854
+ */
11855
+ addToScope(scope.cancel);
11856
+ start();
11857
+ return scope;
11858
+ }
11859
+
11629
11860
  function clearStatsBuffer() {
11630
11861
  statsBuffer.value = null;
11631
11862
  statsBuffer.addProjectionMetrics = null;
@@ -13242,7 +13473,9 @@
13242
13473
  }
13243
13474
  }
13244
13475
  function animateView(update, options = {}) {
13245
- return new ViewTransitionBuilder(update, options);
13476
+ const builder = new ViewTransitionBuilder(update, options);
13477
+ addBuilderToScope(builder);
13478
+ return builder;
13246
13479
  }
13247
13480
 
13248
13481
  /**
@@ -13751,7 +13984,12 @@
13751
13984
  */
13752
13985
  function animateLayout(scopeOrUpdateDom, updateDomOrOptions, options) {
13753
13986
  const { scope, updateDom, defaultOptions } = parseAnimateLayoutArgs(scopeOrUpdateDom, updateDomOrOptions, options);
13754
- return new LayoutAnimationBuilder(scope, updateDom, defaultOptions);
13987
+ const builder = new LayoutAnimationBuilder(scope, updateDom, {
13988
+ ...activeScope.current?.transition,
13989
+ ...defaultOptions,
13990
+ });
13991
+ addBuilderToScope(builder);
13992
+ return builder;
13755
13993
  }
13756
13994
 
13757
13995
  /**
@@ -13796,7 +14034,9 @@
13796
14034
  exports.VisualElement = VisualElement;
13797
14035
  exports.VisualElementDragControls = VisualElementDragControls;
13798
14036
  exports.acceleratedValues = acceleratedValues;
14037
+ exports.activeScope = activeScope;
13799
14038
  exports.addAttrValue = addAttrValue;
14039
+ exports.addBuilderToScope = addBuilderToScope;
13800
14040
  exports.addDomEvent = addDomEvent;
13801
14041
  exports.addEffect = addEffect;
13802
14042
  exports.addPointerEvent = addPointerEvent;
@@ -13804,6 +14044,7 @@
13804
14044
  exports.addSVGValue = addSVGValue;
13805
14045
  exports.addScaleCorrector = addScaleCorrector;
13806
14046
  exports.addStyleValue = addStyleValue;
14047
+ exports.addToScope = addToScope;
13807
14048
  exports.addValueToWillChange = addValueToWillChange;
13808
14049
  exports.alpha = alpha;
13809
14050
  exports.analyseComplexValue = analyseComplexValue;
@@ -13833,6 +14074,7 @@
13833
14074
  exports.axisDeltaEquals = axisDeltaEquals;
13834
14075
  exports.axisEquals = axisEquals;
13835
14076
  exports.axisEqualsRounded = axisEqualsRounded;
14077
+ exports.bindToScope = bindToScope;
13836
14078
  exports.boxEquals = boxEquals;
13837
14079
  exports.boxEqualsRounded = boxEqualsRounded;
13838
14080
  exports.buildHTMLStyles = buildHTMLStyles;
@@ -14002,6 +14244,7 @@
14002
14244
  exports.readStyleValue = readStyleValue;
14003
14245
  exports.readTransformValue = readTransformValue;
14004
14246
  exports.recordStats = recordStats;
14247
+ exports.releaseElement = releaseElement;
14005
14248
  exports.removeAxisDelta = removeAxisDelta;
14006
14249
  exports.removeAxisTransforms = removeAxisTransforms;
14007
14250
  exports.removeBoxTransforms = removeBoxTransforms;
@@ -14023,6 +14266,7 @@
14023
14266
  exports.scale = scale;
14024
14267
  exports.scaleCorrectors = scaleCorrectors;
14025
14268
  exports.scalePoint = scalePoint;
14269
+ exports.scopeMotion = scopeMotion;
14026
14270
  exports.scrapeHTMLMotionValuesFromProps = scrapeMotionValuesFromProps$1;
14027
14271
  exports.scrapeSVGMotionValuesFromProps = scrapeMotionValuesFromProps;
14028
14272
  exports.setDragLock = setDragLock;
@@ -14049,6 +14293,7 @@
14049
14293
  exports.sync = sync;
14050
14294
  exports.testValueType = testValueType;
14051
14295
  exports.time = time;
14296
+ exports.toMediaQuery = toMediaQuery;
14052
14297
  exports.transform = transform;
14053
14298
  exports.transformAxis = transformAxis;
14054
14299
  exports.transformBox = transformBox;