framer-motion 5.5.1 → 5.5.2

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.
@@ -337,13 +337,18 @@ function CanvasComponent(_a, forwardedRef) {
337
337
  var motionContext = React.useContext(useMotionValue.MotionContext);
338
338
  var configContext = React.useContext(useMotionValue.MotionConfigContext);
339
339
  var layoutCamera = React.useRef(null);
340
- var _b = tslib.__read(React.useState({ width: 0, height: 0 }), 2), size = _b[0], setSize = _b[1];
340
+ var _b = tslib.__read(React.useState({
341
+ size: { width: 0, height: 0 },
342
+ }), 2), dimensions = _b[0], setDimensions = _b[1];
343
+ var size = dimensions.size, dpr = dimensions.dpr;
341
344
  var containerRef = React.useRef(null);
342
345
  var handleResize = function () {
343
346
  var container = containerRef.current;
344
- setSize({
345
- width: container.offsetWidth,
346
- height: container.offsetHeight,
347
+ setDimensions({
348
+ size: {
349
+ width: container.offsetWidth,
350
+ height: container.offsetHeight,
351
+ },
347
352
  });
348
353
  };
349
354
  // Set canvas size on mount
@@ -361,9 +366,9 @@ function CanvasComponent(_a, forwardedRef) {
361
366
  if (size.width > 0 && size.height > 0) {
362
367
  fiber.render(React__namespace.createElement(ErrorBoundary, { set: setError },
363
368
  React__namespace.createElement(React__namespace.Suspense, { fallback: React__namespace.createElement(Block, { set: setBlock }) },
364
- React__namespace.createElement(MotionCanvasContext.Provider, { value: { setSize: setSize, layoutCamera: layoutCamera } },
369
+ React__namespace.createElement(MotionCanvasContext.Provider, { value: { setDimensions: setDimensions, layoutCamera: layoutCamera } },
365
370
  React__namespace.createElement(useMotionValue.MotionConfigContext.Provider, { value: configContext },
366
- React__namespace.createElement(useMotionValue.MotionContext.Provider, { value: motionContext }, children))))), canvasRef.current, tslib.__assign(tslib.__assign({}, props), { size: size, events: events || fiber.events }));
371
+ React__namespace.createElement(useMotionValue.MotionContext.Provider, { value: motionContext }, children))))), canvasRef.current, tslib.__assign(tslib.__assign({}, props), { dpr: dpr || props.dpr, size: size, events: events || fiber.events }));
367
372
  }
368
373
  useMotionValue.useIsomorphicLayoutEffect(function () {
369
374
  var container = canvasRef.current;
@@ -385,8 +390,7 @@ function useLayoutCamera(_a, updateCamera) {
385
390
  var _b = _a.makeDefault, makeDefault = _b === void 0 ? true : _b;
386
391
  var context = React.useContext(MotionCanvasContext);
387
392
  heyListen.invariant(Boolean(context), "No MotionCanvas detected. Replace Canvas from @react-three/fiber with MotionCanvas from framer-motion.");
388
- var _c = context, setSize = _c.setSize, layoutCamera = _c.layoutCamera;
389
- var latestLayout = React.useRef(null);
393
+ var _c = context, setDimensions = _c.setDimensions, layoutCamera = _c.layoutCamera;
390
394
  var advance = fiber.useThree(function (three) { return three.advance; });
391
395
  var set = fiber.useThree(function (three) { return three.set; });
392
396
  var camera = fiber.useThree(function (three) { return three.camera; });
@@ -403,17 +407,6 @@ function useLayoutCamera(_a, updateCamera) {
403
407
  * with the projected dimensions.
404
408
  */
405
409
  var removeProjectionUpdateListener = projection.addEventListener("projectionUpdate", function (newProjection) {
406
- // TODO: Removed to prevent iOS from crashing.
407
- // Perhaps a better apporach is to set with setSize
408
- // and then set to window native on animationComplete.
409
- // if (latestLayout.current) {
410
- // const { x, y } = latestLayout.current
411
- // const xRatio = calcLength(newProjection.x) / calcLength(x)
412
- // const yRatio = calcLength(newProjection.y) / calcLength(y)
413
- // gl.setPixelRatio(
414
- // Math.max(xRatio, yRatio) * window.devicePixelRatio
415
- // )
416
- // }
417
410
  updateCamera(calcBoxSize(newProjection));
418
411
  advance(sync.getFrameData().timestamp);
419
412
  });
@@ -422,10 +415,17 @@ function useLayoutCamera(_a, updateCamera) {
422
415
  * output to match the layout dimensions.
423
416
  */
424
417
  var removeLayoutMeasureListener = projection.addEventListener("measure", function (newLayout) {
425
- latestLayout.current = newLayout;
426
418
  var newSize = calcBoxSize(newLayout);
427
419
  gl.setSize(newSize.width, newSize.height);
428
- setSize(newSize);
420
+ var dpr;
421
+ if (newSize.width < size.width ||
422
+ newSize.height < size.height) {
423
+ var xScale = size.width / newSize.width;
424
+ var yScale = size.height / newSize.height;
425
+ var maxScale = Math.max(xScale, yScale);
426
+ dpr = Math.min(maxScale, 8);
427
+ }
428
+ setDimensions({ size: newSize, dpr: dpr });
429
429
  });
430
430
  /**
431
431
  * When a projection animation completes we want to update the camera to
@@ -433,14 +433,18 @@ function useLayoutCamera(_a, updateCamera) {
433
433
  */
434
434
  var removeAnimationCompleteListener = projection.addEventListener("animationComplete", function () {
435
435
  var actual = (projection.layout || {}).actual;
436
- actual && updateCamera(calcBoxSize(actual));
436
+ if (actual) {
437
+ var newSize = calcBoxSize(actual);
438
+ updateCamera(newSize);
439
+ setDimensions({ size: newSize });
440
+ }
437
441
  });
438
442
  return function () {
439
443
  removeProjectionUpdateListener();
440
444
  removeLayoutMeasureListener();
441
445
  removeAnimationCompleteListener();
442
446
  };
443
- }, []);
447
+ }, [size]);
444
448
  React.useLayoutEffect(function () {
445
449
  var cam = layoutCamera.current;
446
450
  if (makeDefault && cam) {
@@ -50,13 +50,18 @@ function CanvasComponent(_a, forwardedRef) {
50
50
  var motionContext = useContext(MotionContext);
51
51
  var configContext = useContext(MotionConfigContext);
52
52
  var layoutCamera = useRef(null);
53
- var _b = __read(useState({ width: 0, height: 0 }), 2), size = _b[0], setSize = _b[1];
53
+ var _b = __read(useState({
54
+ size: { width: 0, height: 0 },
55
+ }), 2), dimensions = _b[0], setDimensions = _b[1];
56
+ var size = dimensions.size, dpr = dimensions.dpr;
54
57
  var containerRef = useRef(null);
55
58
  var handleResize = function () {
56
59
  var container = containerRef.current;
57
- setSize({
58
- width: container.offsetWidth,
59
- height: container.offsetHeight,
60
+ setDimensions({
61
+ size: {
62
+ width: container.offsetWidth,
63
+ height: container.offsetHeight,
64
+ },
60
65
  });
61
66
  };
62
67
  // Set canvas size on mount
@@ -74,9 +79,9 @@ function CanvasComponent(_a, forwardedRef) {
74
79
  if (size.width > 0 && size.height > 0) {
75
80
  render(React.createElement(ErrorBoundary, { set: setError },
76
81
  React.createElement(React.Suspense, { fallback: React.createElement(Block, { set: setBlock }) },
77
- React.createElement(MotionCanvasContext.Provider, { value: { setSize: setSize, layoutCamera: layoutCamera } },
82
+ React.createElement(MotionCanvasContext.Provider, { value: { setDimensions: setDimensions, layoutCamera: layoutCamera } },
78
83
  React.createElement(MotionConfigContext.Provider, { value: configContext },
79
- React.createElement(MotionContext.Provider, { value: motionContext }, children))))), canvasRef.current, __assign(__assign({}, props), { size: size, events: events$1 || events }));
84
+ React.createElement(MotionContext.Provider, { value: motionContext }, children))))), canvasRef.current, __assign(__assign({}, props), { dpr: dpr || props.dpr, size: size, events: events$1 || events }));
80
85
  }
81
86
  useIsomorphicLayoutEffect(function () {
82
87
  var container = canvasRef.current;
@@ -1,4 +1,4 @@
1
- import { useContext, useRef, useLayoutEffect } from 'react';
1
+ import { useContext, useLayoutEffect } from 'react';
2
2
  import { useThree } from '@react-three/fiber';
3
3
  import { useVisualElementContext } from '../../context/MotionContext/index.mjs';
4
4
  import { MotionCanvasContext } from './MotionCanvasContext.mjs';
@@ -17,8 +17,7 @@ function useLayoutCamera(_a, updateCamera) {
17
17
  var _b = _a.makeDefault, makeDefault = _b === void 0 ? true : _b;
18
18
  var context = useContext(MotionCanvasContext);
19
19
  invariant(Boolean(context), "No MotionCanvas detected. Replace Canvas from @react-three/fiber with MotionCanvas from framer-motion.");
20
- var _c = context, setSize = _c.setSize, layoutCamera = _c.layoutCamera;
21
- var latestLayout = useRef(null);
20
+ var _c = context, setDimensions = _c.setDimensions, layoutCamera = _c.layoutCamera;
22
21
  var advance = useThree(function (three) { return three.advance; });
23
22
  var set = useThree(function (three) { return three.set; });
24
23
  var camera = useThree(function (three) { return three.camera; });
@@ -35,17 +34,6 @@ function useLayoutCamera(_a, updateCamera) {
35
34
  * with the projected dimensions.
36
35
  */
37
36
  var removeProjectionUpdateListener = projection.addEventListener("projectionUpdate", function (newProjection) {
38
- // TODO: Removed to prevent iOS from crashing.
39
- // Perhaps a better apporach is to set with setSize
40
- // and then set to window native on animationComplete.
41
- // if (latestLayout.current) {
42
- // const { x, y } = latestLayout.current
43
- // const xRatio = calcLength(newProjection.x) / calcLength(x)
44
- // const yRatio = calcLength(newProjection.y) / calcLength(y)
45
- // gl.setPixelRatio(
46
- // Math.max(xRatio, yRatio) * window.devicePixelRatio
47
- // )
48
- // }
49
37
  updateCamera(calcBoxSize(newProjection));
50
38
  advance(getFrameData().timestamp);
51
39
  });
@@ -54,10 +42,17 @@ function useLayoutCamera(_a, updateCamera) {
54
42
  * output to match the layout dimensions.
55
43
  */
56
44
  var removeLayoutMeasureListener = projection.addEventListener("measure", function (newLayout) {
57
- latestLayout.current = newLayout;
58
45
  var newSize = calcBoxSize(newLayout);
59
46
  gl.setSize(newSize.width, newSize.height);
60
- setSize(newSize);
47
+ var dpr;
48
+ if (newSize.width < size.width ||
49
+ newSize.height < size.height) {
50
+ var xScale = size.width / newSize.width;
51
+ var yScale = size.height / newSize.height;
52
+ var maxScale = Math.max(xScale, yScale);
53
+ dpr = Math.min(maxScale, 8);
54
+ }
55
+ setDimensions({ size: newSize, dpr: dpr });
61
56
  });
62
57
  /**
63
58
  * When a projection animation completes we want to update the camera to
@@ -65,14 +60,18 @@ function useLayoutCamera(_a, updateCamera) {
65
60
  */
66
61
  var removeAnimationCompleteListener = projection.addEventListener("animationComplete", function () {
67
62
  var actual = (projection.layout || {}).actual;
68
- actual && updateCamera(calcBoxSize(actual));
63
+ if (actual) {
64
+ var newSize = calcBoxSize(actual);
65
+ updateCamera(newSize);
66
+ setDimensions({ size: newSize });
67
+ }
69
68
  });
70
69
  return function () {
71
70
  removeProjectionUpdateListener();
72
71
  removeLayoutMeasureListener();
73
72
  removeAnimationCompleteListener();
74
73
  };
75
- }, []);
74
+ }, [size]);
76
75
  useLayoutEffect(function () {
77
76
  var cam = layoutCamera.current;
78
77
  if (makeDefault && cam) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "framer-motion",
3
- "version": "5.5.1",
3
+ "version": "5.5.2",
4
4
  "description": "A simple and powerful React animation library",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/es/index.mjs",
@@ -1,11 +1,13 @@
1
+ import { Size } from "@react-three/fiber";
1
2
  import { RefObject, Ref } from "react";
3
+ export declare type DimensionsState = {
4
+ size: Size;
5
+ dpr?: number;
6
+ };
2
7
  export declare type SetLayoutCamera = (ref: Ref<any>) => void;
3
- export declare type SetSize = (size: {
4
- width: number;
5
- height: number;
6
- }) => void;
8
+ export declare type SetDimensions = (state: DimensionsState) => void;
7
9
  export interface MotionCanvasContextProps {
8
10
  layoutCamera: RefObject<any>;
9
- setSize: SetSize;
11
+ setDimensions: SetDimensions;
10
12
  }
11
13
  export declare const MotionCanvasContext: import("react").Context<MotionCanvasContextProps | undefined>;