diff --git a/CHANGELOG.md b/CHANGELOG.md index cab54d0075..36b701042d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ Framer Motion adheres to [Semantic Versioning](http://semver.org/). +### Added + +- `layout="size"` for size-only layout animations. + ## [4.1.17] 2021-05-17 ### Fixed diff --git a/api/framer-motion.api.md b/api/framer-motion.api.md index 90175cd6ee..c834440f12 100644 --- a/api/framer-motion.api.md +++ b/api/framer-motion.api.md @@ -432,7 +432,7 @@ export const LayoutGroupContext: import("react").Context; // @public (undocumented) export interface LayoutProps { - layout?: boolean | "position"; + layout?: boolean | "position" | "size"; layoutId?: string; // @internal _layoutResetTransform?: boolean; @@ -961,11 +961,11 @@ export interface VisualElement extends Lifecy // (undocumented) getStaticValue(key: string): number | string | undefined; // (undocumented) - getValue(key: string, defaultValue?: string | number): undefined | MotionValue; + getValue(key: string): undefined | MotionValue; // (undocumented) getValue(key: string, defaultValue: string | number): MotionValue; // (undocumented) - getValue(key: string): undefined | MotionValue; + getValue(key: string, defaultValue?: string | number): undefined | MotionValue; // (undocumented) getVariant(name: string): Variant | undefined; // (undocumented) diff --git a/cypress/integration/layout-shared.ts b/cypress/integration/layout-shared.ts index e17ac36007..b83cfccf68 100644 --- a/cypress/integration/layout-shared.ts +++ b/cypress/integration/layout-shared.ts @@ -95,6 +95,42 @@ describe("AnimateSharedLayout: A -> B transition", () => { }) }) }) + + it(`It correctly fires layout="size" animations`, () => { + cy.visit("?test=layout-shared-switch-a-b&type=size") + .wait(50) + .get("#a") + .should(([$box]: any) => { + expectBbox($box, { + top: 0, + left: 0, + width: 100, + height: 200, + }) + }) + .trigger("click") + .wait(50) + .get("#b") + .should(([$box]: any) => { + expectBbox($box, { + top: 50, + left: 100, + width: 300, + height: 300, + }) + }) + .trigger("click") + .wait(50) + .get("#a") + .should(([$box]: any) => { + expectBbox($box, { + top: 25, + left: 50, + width: 100, + height: 200, + }) + }) + }) }) describe("AnimateSharedLayout: A -> AB -> A switch transition", () => { @@ -345,6 +381,42 @@ describe("AnimateSharedLayout: A -> B crossfade transition", () => { }) }) }) + + it(`It correctly fires layout="size" animations`, () => { + cy.visit("?test=layout-shared-switch-a-b&type=size") + .wait(50) + .get("#a") + .should(([$box]: any) => { + expectBbox($box, { + top: 0, + left: 0, + width: 100, + height: 200, + }) + }) + .trigger("click") + .wait(50) + .get("#b") + .should(([$box]: any) => { + expectBbox($box, { + top: 50, + left: 100, + width: 300, + height: 300, + }) + }) + .trigger("click") + .wait(50) + .get("#a") + .should(([$box]: any) => { + expectBbox($box, { + top: 25, + left: 50, + width: 100, + height: 200, + }) + }) + }) }) describe("AnimateSharedLayout: A -> AB -> A crossfade transition", () => { diff --git a/cypress/integration/layout.ts b/cypress/integration/layout.ts index b3fd2c6959..4af919571e 100644 --- a/cypress/integration/layout.ts +++ b/cypress/integration/layout.ts @@ -69,6 +69,30 @@ describe("Layout animation", () => { }) }) + it(`It correctly fires layout="size" animations`, () => { + cy.visit("?test=layout&type=size") + .wait(50) + .get("#box") + .should(([$box]: any) => { + expectBbox($box, { + top: 0, + left: 0, + width: 100, + height: 200, + }) + }) + .trigger("click") + .wait(50) + .should(([$box]: any) => { + expectBbox($box, { + top: 50, + left: 100, + width: 300, + height: 300, + }) + }) + }) + it("Doesn't initiate a new animation if the viewport box hasn't updated between renders", () => { cy.visit("?test=layout-interrupt") .wait(50) diff --git a/dev/examples/Layout-Projection-scale-size.tsx b/dev/examples/Layout-Projection-scale-size.tsx new file mode 100644 index 0000000000..35b0cc73f1 --- /dev/null +++ b/dev/examples/Layout-Projection-scale-size.tsx @@ -0,0 +1,44 @@ +import React from "react" +import { useState } from "react" +import { motion } from "@framer" + +const textA = ` +It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like). +` + +const textB = ` +It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. +` + +export function App() { + const [c, setC] = useState(1) + + return ( +
setC((i) => -1 * i)} + style={{ + backgroundColor: "#fff", + padding: 40, + overflow: "hidden", + maxWidth: 500, + position: "absolute", + top: c * 10 + 100, + left: c * 10 + 100, + }} + > + + {c === 1 ? textA : textB} + +
+ ) +} + +const transition = { + duration: 3, +} diff --git a/src/motion/features/layout/Animate.tsx b/src/motion/features/layout/Animate.tsx index dca6c0c4f0..51945ab5de 100644 --- a/src/motion/features/layout/Animate.tsx +++ b/src/motion/features/layout/Animate.tsx @@ -170,13 +170,21 @@ class Animate extends React.Component { const boxHasMoved = hasMoved(origin, target) const animations = eachAxis((axis) => { - /** - * If layout is set to "position", we can resize the origin box based on the target - * box and only animate its position. - */ if (layout === "position") { + /** + * If layout is set to "position", we can resize the origin box based on the target + * box and only animate its position. + */ const targetLength = target[axis].max - target[axis].min origin[axis].max = origin[axis].min + targetLength + } else if (layout === "size") { + /** + * If layout is set to "size", we move the origin to the target box and only animate + * its length. + */ + const originLength = origin[axis].max - origin[axis].min + origin[axis].min = target[axis].min + origin[axis].max = origin[axis].min + originLength } if (visualElement.projection.isTargetLocked) { diff --git a/src/motion/features/layout/types.ts b/src/motion/features/layout/types.ts index 2a86759ffe..372cac9b7b 100644 --- a/src/motion/features/layout/types.ts +++ b/src/motion/features/layout/types.ts @@ -20,11 +20,12 @@ export interface LayoutProps { * animated on this component. Otherwise, set them directly via the `initial` prop. * * If `layout` is set to `"position"`, the size of the component will change instantly and - * only its position will animate. + * only its position will animate. If `layout` is set to `"size"`, the position of the + * component will change instantly but its size will animate. * * @public */ - layout?: boolean | "position" + layout?: boolean | "position" | "size" /** * Enable shared layout transitions between components for children of `AnimateSharedLayout`.