framer-motion 12.4.11 → 12.4.13

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.
@@ -0,0 +1,83 @@
1
+ const radToDeg = (rad) => (rad * 180) / Math.PI;
2
+ const rotate = (v) => {
3
+ const angle = radToDeg(Math.atan2(v[1], v[0]));
4
+ return rebaseAngle(angle);
5
+ };
6
+ const matrix2dParsers = {
7
+ x: 4,
8
+ y: 5,
9
+ translateX: 4,
10
+ translateY: 5,
11
+ scaleX: 0,
12
+ scaleY: 3,
13
+ scale: (v) => (Math.abs(v[0]) + Math.abs(v[3])) / 2,
14
+ rotate,
15
+ rotateZ: rotate,
16
+ skewX: (v) => radToDeg(Math.atan(v[1])),
17
+ skewY: (v) => radToDeg(Math.atan(v[2])),
18
+ skew: (v) => (Math.abs(v[1]) + Math.abs(v[2])) / 2,
19
+ };
20
+ const rebaseAngle = (angle) => {
21
+ angle = angle % 360;
22
+ if (angle < 0)
23
+ angle += 360;
24
+ return angle;
25
+ };
26
+ const rotateZ = rotate;
27
+ const scaleX = (v) => Math.sqrt(v[0] * v[0] + v[1] * v[1]);
28
+ const scaleY = (v) => Math.sqrt(v[4] * v[4] + v[5] * v[5]);
29
+ const matrix3dParsers = {
30
+ x: 12,
31
+ y: 13,
32
+ z: 14,
33
+ translateX: 12,
34
+ translateY: 13,
35
+ translateZ: 14,
36
+ scaleX,
37
+ scaleY,
38
+ scale: (v) => (scaleX(v) + scaleY(v)) / 2,
39
+ rotateX: (v) => rebaseAngle(radToDeg(Math.atan2(v[6], v[5]))),
40
+ rotateY: (v) => rebaseAngle(radToDeg(Math.atan2(-v[2], v[0]))),
41
+ rotateZ,
42
+ rotate: rotateZ,
43
+ skewX: (v) => radToDeg(Math.atan(v[4])),
44
+ skewY: (v) => radToDeg(Math.atan(v[1])),
45
+ skew: (v) => (Math.abs(v[1]) + Math.abs(v[4])) / 2,
46
+ };
47
+ function defaultTransformValue(name) {
48
+ return name.includes("scale") ? 1 : 0;
49
+ }
50
+ function parseValueFromTransform(transform, name) {
51
+ if (!transform || transform === "none") {
52
+ return defaultTransformValue(name);
53
+ }
54
+ const matrix3dMatch = transform.match(/^matrix3d\(([-\d.e\s,]+)\)$/u);
55
+ let parsers;
56
+ let match;
57
+ if (matrix3dMatch) {
58
+ parsers = matrix3dParsers;
59
+ match = matrix3dMatch;
60
+ }
61
+ else {
62
+ const matrix2dMatch = transform.match(/^matrix\(([-\d.e\s,]+)\)$/u);
63
+ parsers = matrix2dParsers;
64
+ match = matrix2dMatch;
65
+ }
66
+ if (!match) {
67
+ return defaultTransformValue(name);
68
+ }
69
+ const valueParser = parsers[name];
70
+ const values = match[1].split(",").map(convertTransformToNumber);
71
+ return typeof valueParser === "function"
72
+ ? valueParser(values)
73
+ : values[valueParser];
74
+ }
75
+ const readTransformValue = (instance, name) => {
76
+ const { transform = "none" } = getComputedStyle(instance);
77
+ return parseValueFromTransform(transform, name);
78
+ };
79
+ function convertTransformToNumber(value) {
80
+ return parseFloat(value.trim());
81
+ }
82
+
83
+ export { parseValueFromTransform, readTransformValue };
@@ -17,7 +17,7 @@ function updateMotionValuesFromProps(element, next, prev) {
17
17
  * and warn against mismatches.
18
18
  */
19
19
  if (process.env.NODE_ENV === "development") {
20
- warnOnce(nextValue.version === "12.4.11", `Attempting to mix Motion versions ${nextValue.version} with 12.4.11 may not work as expected.`);
20
+ warnOnce(nextValue.version === "12.4.13", `Attempting to mix Motion versions ${nextValue.version} with 12.4.13 may not work as expected.`);
21
21
  }
22
22
  }
23
23
  else if (isMotionValue(prevValue)) {
@@ -0,0 +1,6 @@
1
+ // Fixes https://github.com/motiondivision/motion/issues/2270
2
+ const getContextWindow = ({ current }) => {
3
+ return current ? current.ownerDocument.defaultView : null;
4
+ };
5
+
6
+ export { getContextWindow };
@@ -34,7 +34,7 @@ class MotionValue {
34
34
  * This will be replaced by the build step with the latest version number.
35
35
  * When MotionValues are provided to motion components, warn if versions are mixed.
36
36
  */
37
- this.version = "12.4.11";
37
+ this.version = "12.4.13";
38
38
  /**
39
39
  * Tracks whether this value can output a velocity. Currently this is only true
40
40
  * if the value is numerical, but we might be able to widen the scope here and support
@@ -1106,7 +1106,7 @@
1106
1106
  * This will be replaced by the build step with the latest version number.
1107
1107
  * When MotionValues are provided to motion components, warn if versions are mixed.
1108
1108
  */
1109
- this.version = "12.4.11";
1109
+ this.version = "12.4.13";
1110
1110
  /**
1111
1111
  * Tracks whether this value can output a velocity. Currently this is only true
1112
1112
  * if the value is numerical, but we might be able to widen the scope here and support
@@ -1949,25 +1949,89 @@
1949
1949
  }
1950
1950
  }
1951
1951
 
1952
- const isNumOrPxType = (v) => v === number || v === px;
1953
- const getPosFromMatrix = (matrix, pos) => parseFloat(matrix.split(", ")[pos]);
1954
- const getTranslateFromMatrix = (pos2, pos3) => (_bbox, { transform }) => {
1955
- if (transform === "none" || !transform)
1956
- return 0;
1957
- const matrix3d = transform.match(/^matrix3d\((.+)\)$/u);
1958
- if (matrix3d) {
1959
- return getPosFromMatrix(matrix3d[1], pos3);
1952
+ const radToDeg = (rad) => (rad * 180) / Math.PI;
1953
+ const rotate = (v) => {
1954
+ const angle = radToDeg(Math.atan2(v[1], v[0]));
1955
+ return rebaseAngle(angle);
1956
+ };
1957
+ const matrix2dParsers = {
1958
+ x: 4,
1959
+ y: 5,
1960
+ translateX: 4,
1961
+ translateY: 5,
1962
+ scaleX: 0,
1963
+ scaleY: 3,
1964
+ scale: (v) => (Math.abs(v[0]) + Math.abs(v[3])) / 2,
1965
+ rotate,
1966
+ rotateZ: rotate,
1967
+ skewX: (v) => radToDeg(Math.atan(v[1])),
1968
+ skewY: (v) => radToDeg(Math.atan(v[2])),
1969
+ skew: (v) => (Math.abs(v[1]) + Math.abs(v[2])) / 2,
1970
+ };
1971
+ const rebaseAngle = (angle) => {
1972
+ angle = angle % 360;
1973
+ if (angle < 0)
1974
+ angle += 360;
1975
+ return angle;
1976
+ };
1977
+ const rotateZ = rotate;
1978
+ const scaleX = (v) => Math.sqrt(v[0] * v[0] + v[1] * v[1]);
1979
+ const scaleY = (v) => Math.sqrt(v[4] * v[4] + v[5] * v[5]);
1980
+ const matrix3dParsers = {
1981
+ x: 12,
1982
+ y: 13,
1983
+ z: 14,
1984
+ translateX: 12,
1985
+ translateY: 13,
1986
+ translateZ: 14,
1987
+ scaleX,
1988
+ scaleY,
1989
+ scale: (v) => (scaleX(v) + scaleY(v)) / 2,
1990
+ rotateX: (v) => rebaseAngle(radToDeg(Math.atan2(v[6], v[5]))),
1991
+ rotateY: (v) => rebaseAngle(radToDeg(Math.atan2(-v[2], v[0]))),
1992
+ rotateZ,
1993
+ rotate: rotateZ,
1994
+ skewX: (v) => radToDeg(Math.atan(v[4])),
1995
+ skewY: (v) => radToDeg(Math.atan(v[1])),
1996
+ skew: (v) => (Math.abs(v[1]) + Math.abs(v[4])) / 2,
1997
+ };
1998
+ function defaultTransformValue(name) {
1999
+ return name.includes("scale") ? 1 : 0;
2000
+ }
2001
+ function parseValueFromTransform(transform, name) {
2002
+ if (!transform || transform === "none") {
2003
+ return defaultTransformValue(name);
1960
2004
  }
1961
- else {
1962
- const matrix = transform.match(/^matrix\((.+)\)$/u);
1963
- if (matrix) {
1964
- return getPosFromMatrix(matrix[1], pos2);
1965
- }
1966
- else {
1967
- return 0;
1968
- }
2005
+ const matrix3dMatch = transform.match(/^matrix3d\(([-\d.e\s,]+)\)$/u);
2006
+ let parsers;
2007
+ let match;
2008
+ if (matrix3dMatch) {
2009
+ parsers = matrix3dParsers;
2010
+ match = matrix3dMatch;
1969
2011
  }
2012
+ else {
2013
+ const matrix2dMatch = transform.match(/^matrix\(([-\d.e\s,]+)\)$/u);
2014
+ parsers = matrix2dParsers;
2015
+ match = matrix2dMatch;
2016
+ }
2017
+ if (!match) {
2018
+ return defaultTransformValue(name);
2019
+ }
2020
+ const valueParser = parsers[name];
2021
+ const values = match[1].split(",").map(convertTransformToNumber);
2022
+ return typeof valueParser === "function"
2023
+ ? valueParser(values)
2024
+ : values[valueParser];
2025
+ }
2026
+ const readTransformValue = (instance, name) => {
2027
+ const { transform = "none" } = getComputedStyle(instance);
2028
+ return parseValueFromTransform(transform, name);
1970
2029
  };
2030
+ function convertTransformToNumber(value) {
2031
+ return parseFloat(value.trim());
2032
+ }
2033
+
2034
+ const isNumOrPxType = (v) => v === number || v === px;
1971
2035
  const transformKeys = new Set(["x", "y", "z"]);
1972
2036
  const nonTranslationalTransformKeys = transformPropOrder.filter((key) => !transformKeys.has(key));
1973
2037
  function removeNonTranslationalTransform(visualElement) {
@@ -1990,8 +2054,8 @@
1990
2054
  bottom: ({ y }, { top }) => parseFloat(top) + (y.max - y.min),
1991
2055
  right: ({ x }, { left }) => parseFloat(left) + (x.max - x.min),
1992
2056
  // Transform
1993
- x: getTranslateFromMatrix(4, 13),
1994
- y: getTranslateFromMatrix(5, 14),
2057
+ x: (_bbox, { transform }) => parseValueFromTransform(transform, "x"),
2058
+ y: (_bbox, { transform }) => parseValueFromTransform(transform, "y"),
1995
2059
  };
1996
2060
  // Alias translate longform names
1997
2061
  positionalValues.translateX = positionalValues.x;
@@ -5965,7 +6029,7 @@
5965
6029
  * and warn against mismatches.
5966
6030
  */
5967
6031
  {
5968
- warnOnce(nextValue.version === "12.4.11", `Attempting to mix Motion versions ${nextValue.version} with 12.4.11 may not work as expected.`);
6032
+ warnOnce(nextValue.version === "12.4.13", `Attempting to mix Motion versions ${nextValue.version} with 12.4.13 may not work as expected.`);
5969
6033
  }
5970
6034
  }
5971
6035
  else if (isMotionValue(prevValue)) {
@@ -7065,8 +7129,7 @@
7065
7129
  }
7066
7130
  readValueFromInstance(instance, key) {
7067
7131
  if (transformProps.has(key)) {
7068
- const defaultType = getDefaultValueType(key);
7069
- return defaultType ? defaultType.default || 0 : 0;
7132
+ return readTransformValue(instance, key);
7070
7133
  }
7071
7134
  else {
7072
7135
  const computedStyle = getComputedStyle$1(instance);
@@ -10291,6 +10354,11 @@
10291
10354
  });
10292
10355
  }
10293
10356
 
10357
+ // Fixes https://github.com/motiondivision/motion/issues/2270
10358
+ const getContextWindow = ({ current }) => {
10359
+ return current ? current.ownerDocument.defaultView : null;
10360
+ };
10361
+
10294
10362
  function isRefObject(ref) {
10295
10363
  return (ref &&
10296
10364
  typeof ref === "object" &&
@@ -10301,7 +10369,7 @@
10301
10369
  * @internal
10302
10370
  */
10303
10371
  class PanSession {
10304
- constructor(event, handlers, { transformPagePoint, dragSnapToOrigin = false } = {}) {
10372
+ constructor(event, handlers, { transformPagePoint, contextWindow, dragSnapToOrigin = false, } = {}) {
10305
10373
  /**
10306
10374
  * @internal
10307
10375
  */
@@ -10318,6 +10386,10 @@
10318
10386
  * @internal
10319
10387
  */
10320
10388
  this.handlers = {};
10389
+ /**
10390
+ * @internal
10391
+ */
10392
+ this.contextWindow = window;
10321
10393
  this.updatePoint = () => {
10322
10394
  if (!(this.lastMoveEvent && this.lastMoveEventInfo))
10323
10395
  return;
@@ -10340,32 +10412,19 @@
10340
10412
  onMove && onMove(this.lastMoveEvent, info);
10341
10413
  };
10342
10414
  this.handlePointerMove = (event, info) => {
10343
- this.index = getElementIndex(event.currentTarget);
10344
- if (event.target instanceof Element &&
10345
- event.target.hasPointerCapture &&
10346
- event.pointerId !== undefined) {
10347
- try {
10348
- if (!event.target.hasPointerCapture(event.pointerId)) {
10349
- return;
10350
- }
10351
- }
10352
- catch (e) { }
10353
- }
10354
10415
  this.lastMoveEvent = event;
10355
10416
  this.lastMoveEventInfo = transformPoint(info, this.transformPagePoint);
10356
10417
  // Throttle mouse move event to once per frame
10357
10418
  frame.update(this.updatePoint, true);
10358
10419
  };
10359
10420
  this.handlePointerUp = (event, info) => {
10360
- capturePointer(event, "release");
10361
10421
  this.end();
10362
10422
  const { onEnd, onSessionEnd, resumeAnimation } = this.handlers;
10363
10423
  if (this.dragSnapToOrigin)
10364
10424
  resumeAnimation && resumeAnimation();
10365
10425
  if (!(this.lastMoveEvent && this.lastMoveEventInfo))
10366
10426
  return;
10367
- const panInfo = getPanInfo(event.type === "pointercancel" ||
10368
- event.type === "lostpointercapture"
10427
+ const panInfo = getPanInfo(event.type === "pointercancel"
10369
10428
  ? this.lastMoveEventInfo
10370
10429
  : transformPoint(info, this.transformPagePoint), this.history);
10371
10430
  if (this.startEvent && onEnd) {
@@ -10379,6 +10438,7 @@
10379
10438
  this.dragSnapToOrigin = dragSnapToOrigin;
10380
10439
  this.handlers = handlers;
10381
10440
  this.transformPagePoint = transformPagePoint;
10441
+ this.contextWindow = contextWindow || window;
10382
10442
  const info = extractEventInfo(event);
10383
10443
  const initialInfo = transformPoint(info, this.transformPagePoint);
10384
10444
  const { point } = initialInfo;
@@ -10387,20 +10447,7 @@
10387
10447
  const { onSessionStart } = handlers;
10388
10448
  onSessionStart &&
10389
10449
  onSessionStart(event, getPanInfo(initialInfo, this.history));
10390
- capturePointer(event, "set");
10391
- this.removeListeners = pipe(addPointerEvent(event.currentTarget, "pointermove", this.handlePointerMove), addPointerEvent(event.currentTarget, "pointerup", this.handlePointerUp), addPointerEvent(event.currentTarget, "pointercancel", this.handlePointerUp), addPointerEvent(event.currentTarget, "lostpointercapture", (lostPointerEvent, lostPointerInfo) => {
10392
- const index = getElementIndex(lostPointerEvent.currentTarget);
10393
- /**
10394
- * If the pointer has lost capture because it's moved in the DOM
10395
- * then we need to re-capture it.
10396
- */
10397
- if (index !== this.index) {
10398
- capturePointer(lostPointerEvent, "set");
10399
- }
10400
- else {
10401
- this.handlePointerUp(lostPointerEvent, lostPointerInfo);
10402
- }
10403
- }));
10450
+ this.removeListeners = pipe(addPointerEvent(this.contextWindow, "pointermove", this.handlePointerMove), addPointerEvent(this.contextWindow, "pointerup", this.handlePointerUp), addPointerEvent(this.contextWindow, "pointercancel", this.handlePointerUp));
10404
10451
  }
10405
10452
  updateHandlers(handlers) {
10406
10453
  this.handlers = handlers;
@@ -10464,11 +10511,6 @@
10464
10511
  }
10465
10512
  return currentVelocity;
10466
10513
  }
10467
- function getElementIndex(element) {
10468
- if (!element.parentNode)
10469
- return -1;
10470
- return Array.from(element.parentNode.children).indexOf(element);
10471
- }
10472
10514
 
10473
10515
  /**
10474
10516
  * Apply constraints to a point. These constraints are both physical along an
@@ -10725,6 +10767,7 @@
10725
10767
  }, {
10726
10768
  transformPagePoint: this.visualElement.getTransformPagePoint(),
10727
10769
  dragSnapToOrigin,
10770
+ contextWindow: getContextWindow(this.visualElement),
10728
10771
  });
10729
10772
  }
10730
10773
  stop(event, info) {
@@ -11090,6 +11133,7 @@
11090
11133
  onPointerDown(pointerDownEvent) {
11091
11134
  this.session = new PanSession(pointerDownEvent, this.createPanHandlers(), {
11092
11135
  transformPagePoint: this.node.getTransformPagePoint(),
11136
+ contextWindow: getContextWindow(this.node),
11093
11137
  });
11094
11138
  }
11095
11139
  createPanHandlers() {