framer-motion 5.0.1 → 5.2.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.
@@ -331,12 +331,28 @@
331
331
  var _a;
332
332
  if (currentValues === void 0) { currentValues = {}; }
333
333
  if (currentVelocity === void 0) { currentVelocity = {}; }
334
+ /**
335
+ * If the variant definition is a function, resolve.
336
+ */
337
+ if (typeof definition === "function") {
338
+ definition = definition(custom !== null && custom !== void 0 ? custom : props.custom, currentValues, currentVelocity);
339
+ }
340
+ /**
341
+ * If the variant definition is a variant label, or
342
+ * the function returned a variant label, resolve.
343
+ */
334
344
  if (typeof definition === "string") {
335
345
  definition = (_a = props.variants) === null || _a === void 0 ? void 0 : _a[definition];
336
346
  }
337
- return typeof definition === "function"
338
- ? definition(custom !== null && custom !== void 0 ? custom : props.custom, currentValues, currentVelocity)
339
- : definition;
347
+ /**
348
+ * At this point we've resolved both functions and variant labels,
349
+ * but the resolved variant label might itself have been a function.
350
+ * If so, resolve. This can only have returned a valid target object.
351
+ */
352
+ if (typeof definition === "function") {
353
+ definition = definition(custom !== null && custom !== void 0 ? custom : props.custom, currentValues, currentVelocity);
354
+ }
355
+ return definition;
340
356
  }
341
357
  function resolveVariant(visualElement, definition, custom) {
342
358
  var props = visualElement.getProps();
@@ -913,6 +929,46 @@
913
929
  return functions ? functions.map(applyDefaultFilter).join(' ') : v;
914
930
  } });
915
931
 
932
+ function hueToRgb(p, q, t) {
933
+ if (t < 0)
934
+ t += 1;
935
+ if (t > 1)
936
+ t -= 1;
937
+ if (t < 1 / 6)
938
+ return p + (q - p) * 6 * t;
939
+ if (t < 1 / 2)
940
+ return q;
941
+ if (t < 2 / 3)
942
+ return p + (q - p) * (2 / 3 - t) * 6;
943
+ return p;
944
+ }
945
+ function hslaToRgba({ hue, saturation, lightness, alpha }) {
946
+ hue /= 360;
947
+ saturation /= 100;
948
+ lightness /= 100;
949
+ let red = 0;
950
+ let green = 0;
951
+ let blue = 0;
952
+ if (!saturation) {
953
+ red = green = blue = lightness;
954
+ }
955
+ else {
956
+ const q = lightness < 0.5
957
+ ? lightness * (1 + saturation)
958
+ : lightness + saturation - lightness * saturation;
959
+ const p = 2 * lightness - q;
960
+ red = hueToRgb(p, q, hue + 1 / 3);
961
+ green = hueToRgb(p, q, hue);
962
+ blue = hueToRgb(p, q, hue - 1 / 3);
963
+ }
964
+ return {
965
+ red: Math.round(red * 255),
966
+ green: Math.round(green * 255),
967
+ blue: Math.round(blue * 255),
968
+ alpha,
969
+ };
970
+ }
971
+
916
972
  const mixLinearColor = (from, to, v) => {
917
973
  const fromExpo = from * from;
918
974
  const toExpo = to * to;
@@ -922,24 +978,25 @@
922
978
  const getColorType = (v) => colorTypes.find((type) => type.test(v));
923
979
  const notAnimatable = (color) => `'${color}' is not an animatable color. Use the equivalent color code instead.`;
924
980
  const mixColor = (from, to) => {
925
- const fromColorType = getColorType(from);
926
- const toColorType = getColorType(to);
981
+ let fromColorType = getColorType(from);
982
+ let toColorType = getColorType(to);
927
983
  invariant(!!fromColorType, notAnimatable(from));
928
984
  invariant(!!toColorType, notAnimatable(to));
929
- invariant(fromColorType.transform === toColorType.transform, "Both colors must be hex/RGBA, OR both must be HSLA.");
930
- if (!fromColorType ||
931
- !toColorType ||
932
- fromColorType.transform !== toColorType.transform) {
933
- return (p) => `${p > 0 ? to : from}`;
934
- }
935
- const fromColor = fromColorType.parse(from);
936
- const toColor = toColorType.parse(to);
985
+ let fromColor = fromColorType.parse(from);
986
+ let toColor = toColorType.parse(to);
987
+ if (fromColorType === hsla) {
988
+ fromColor = hslaToRgba(fromColor);
989
+ fromColorType = rgba;
990
+ }
991
+ if (toColorType === hsla) {
992
+ toColor = hslaToRgba(toColor);
993
+ toColorType = rgba;
994
+ }
937
995
  const blended = Object.assign({}, fromColor);
938
- const mixFunc = fromColorType === hsla ? mix : mixLinearColor;
939
996
  return (v) => {
940
997
  for (const key in blended) {
941
998
  if (key !== "alpha") {
942
- blended[key] = mixFunc(fromColor[key], toColor[key], v);
999
+ blended[key] = mixLinearColor(fromColor[key], toColor[key], v);
943
1000
  }
944
1001
  }
945
1002
  blended.alpha = mix(fromColor.alpha, toColor.alpha, v);
@@ -4522,6 +4579,7 @@
4522
4579
  "dragListener",
4523
4580
  "dragConstraints",
4524
4581
  "dragDirectionLock",
4582
+ "dragSnapToOrigin",
4525
4583
  "_dragX",
4526
4584
  "_dragY",
4527
4585
  "dragElastic",
@@ -4611,10 +4669,6 @@
4611
4669
  return pxOriginX + " " + pxOriginY;
4612
4670
  }
4613
4671
 
4614
- // Convert a progress 0-1 to a pixels value based on the provided length
4615
- var progressToPixels = function (progress, length) {
4616
- return px.transform(progress * length);
4617
- };
4618
4672
  var dashKeys = {
4619
4673
  offset: "stroke-dashoffset",
4620
4674
  array: "stroke-dasharray",
@@ -4630,18 +4684,20 @@
4630
4684
  *
4631
4685
  * This function is mutative to reduce per-frame GC.
4632
4686
  */
4633
- function buildSVGPath(attrs, totalLength, length, spacing, offset, useDashCase) {
4687
+ function buildSVGPath(attrs, length, spacing, offset, useDashCase) {
4634
4688
  if (spacing === void 0) { spacing = 1; }
4635
4689
  if (offset === void 0) { offset = 0; }
4636
4690
  if (useDashCase === void 0) { useDashCase = true; }
4691
+ // Normalise path length by setting SVG attribute pathLength to 1
4692
+ attrs.pathLength = 1;
4637
4693
  // We use dash case when setting attributes directly to the DOM node and camel case
4638
4694
  // when defining props on a React component.
4639
4695
  var keys = useDashCase ? dashKeys : camelKeys;
4640
4696
  // Build the dash offset
4641
- attrs[keys.offset] = progressToPixels(-offset, totalLength);
4697
+ attrs[keys.offset] = px.transform(-offset);
4642
4698
  // Build the dash array
4643
- var pathLength = progressToPixels(length, totalLength);
4644
- var pathSpacing = progressToPixels(spacing, totalLength);
4699
+ var pathLength = px.transform(length);
4700
+ var pathSpacing = px.transform(spacing);
4645
4701
  attrs[keys.array] = pathLength + " " + pathSpacing;
4646
4702
  }
4647
4703
 
@@ -4655,7 +4711,7 @@
4655
4711
  buildHTMLStyles(state, latest, options, transformTemplate);
4656
4712
  state.attrs = state.style;
4657
4713
  state.style = {};
4658
- var attrs = state.attrs, style = state.style, dimensions = state.dimensions, totalPathLength = state.totalPathLength;
4714
+ var attrs = state.attrs, style = state.style, dimensions = state.dimensions;
4659
4715
  /**
4660
4716
  * However, we apply transforms as CSS transforms. So if we detect a transform we take it from attrs
4661
4717
  * and copy it into style.
@@ -4675,9 +4731,9 @@
4675
4731
  attrs.x = attrX;
4676
4732
  if (attrY !== undefined)
4677
4733
  attrs.y = attrY;
4678
- // Build SVG path if one has been measured
4679
- if (totalPathLength !== undefined && pathLength !== undefined) {
4680
- buildSVGPath(attrs, totalPathLength, pathLength, pathSpacing, pathOffset, false);
4734
+ // Build SVG path if one has been defined
4735
+ if (pathLength !== undefined) {
4736
+ buildSVGPath(attrs, pathLength, pathSpacing, pathOffset, false);
4681
4737
  }
4682
4738
  }
4683
4739
 
@@ -4756,6 +4812,7 @@
4756
4812
  "tableValues",
4757
4813
  "viewBox",
4758
4814
  "gradientTransform",
4815
+ "pathLength",
4759
4816
  ]);
4760
4817
 
4761
4818
  function renderSVG(element, renderState) {
@@ -4867,18 +4924,12 @@
4867
4924
  height: 0,
4868
4925
  };
4869
4926
  }
4870
- if (isPath(instance)) {
4871
- renderState.totalPathLength = instance.getTotalLength();
4872
- }
4873
4927
  buildSVGAttrs(renderState, latestValues, { enableHardwareAcceleration: false }, props.transformTemplate);
4874
4928
  // TODO: Replace with direct assignment
4875
4929
  renderSVG(instance, renderState);
4876
4930
  },
4877
4931
  }),
4878
4932
  };
4879
- function isPath(element) {
4880
- return element.tagName === "path";
4881
- }
4882
4933
 
4883
4934
  var htmlMotionConfig = {
4884
4935
  useVisualState: makeUseVisualState({
@@ -5587,10 +5638,10 @@
5587
5638
 
5588
5639
  var variantPriorityOrder = [
5589
5640
  AnimationType.Animate,
5641
+ AnimationType.Focus,
5590
5642
  AnimationType.Hover,
5591
5643
  AnimationType.Tap,
5592
5644
  AnimationType.Drag,
5593
- AnimationType.Focus,
5594
5645
  AnimationType.Exit,
5595
5646
  ];
5596
5647
  var reversePriorityOrder = __spreadArray([], __read(variantPriorityOrder), false).reverse();
@@ -6319,7 +6370,19 @@
6319
6370
  * Record gesture origin
6320
6371
  */
6321
6372
  eachAxis(function (axis) {
6322
- _this.originPoint[axis] = _this.getAxisMotionValue(axis).get();
6373
+ var _a, _b;
6374
+ var current = _this.getAxisMotionValue(axis).get() || 0;
6375
+ /**
6376
+ * If the MotionValue is a percentage value convert to px
6377
+ */
6378
+ if (percent.test(current)) {
6379
+ var measuredAxis = (_b = (_a = _this.visualElement.projection) === null || _a === void 0 ? void 0 : _a.layout) === null || _b === void 0 ? void 0 : _b.actual[axis];
6380
+ if (measuredAxis) {
6381
+ var length_1 = calcLength(measuredAxis);
6382
+ current = length_1 * (parseFloat(current) / 100);
6383
+ }
6384
+ }
6385
+ _this.originPoint[axis] = current;
6323
6386
  });
6324
6387
  // Fire onDragStart event
6325
6388
  onDragStart === null || onDragStart === void 0 ? void 0 : onDragStart(event, info);
@@ -7363,13 +7426,15 @@
7363
7426
  }
7364
7427
  var positionalValues = {
7365
7428
  // Dimensions
7366
- width: function (_a) {
7429
+ width: function (_a, _b) {
7367
7430
  var x = _a.x;
7368
- return x.max - x.min;
7431
+ var _c = _b.paddingLeft, paddingLeft = _c === void 0 ? "0" : _c, _d = _b.paddingRight, paddingRight = _d === void 0 ? "0" : _d;
7432
+ return x.max - x.min - parseFloat(paddingLeft) - parseFloat(paddingRight);
7369
7433
  },
7370
- height: function (_a) {
7434
+ height: function (_a, _b) {
7371
7435
  var y = _a.y;
7372
- return y.max - y.min;
7436
+ var _c = _b.paddingTop, paddingTop = _c === void 0 ? "0" : _c, _d = _b.paddingBottom, paddingBottom = _d === void 0 ? "0" : _d;
7437
+ return y.max - y.min - parseFloat(paddingTop) - parseFloat(paddingBottom);
7373
7438
  },
7374
7439
  top: function (_bbox, _a) {
7375
7440
  var top = _a.top;
@@ -7397,8 +7462,7 @@
7397
7462
  var originBbox = visualElement.measureViewportBox();
7398
7463
  var element = visualElement.getInstance();
7399
7464
  var elementComputedStyle = getComputedStyle(element);
7400
- var display = elementComputedStyle.display, top = elementComputedStyle.top, left = elementComputedStyle.left, bottom = elementComputedStyle.bottom, right = elementComputedStyle.right, transform = elementComputedStyle.transform;
7401
- var originComputedStyle = { top: top, left: left, bottom: bottom, right: right, transform: transform };
7465
+ var display = elementComputedStyle.display;
7402
7466
  // If the element is currently set to display: "none", make it visible before
7403
7467
  // measuring the target bounding box
7404
7468
  if (display === "none") {
@@ -7411,7 +7475,7 @@
7411
7475
  // Restore styles to their **calculated computed style**, not their actual
7412
7476
  // originally set style. This allows us to animate between equivalent pixel units.
7413
7477
  var value = visualElement.getValue(key);
7414
- setAndResetVelocity(value, positionalValues[key](originBbox, originComputedStyle));
7478
+ setAndResetVelocity(value, positionalValues[key](originBbox, elementComputedStyle));
7415
7479
  target[key] = positionalValues[key](targetBbox, elementComputedStyle);
7416
7480
  });
7417
7481
  return target;