Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing unit conversion for translation #1337

Merged
merged 3 commits into from Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,12 @@

Framer Motion adheres to [Semantic Versioning](http://semver.org/).

## [5.2.1] 2021-11-05

### Fixed

- Fixing unit conversion for `x` and `y` styles. [Issue](https://github.com/framer/motion/issues/1336)

## [5.2.0] 2021-11-04

### Added
Expand Down
13 changes: 13 additions & 0 deletions cypress/integration/unit-conversion.ts
@@ -0,0 +1,13 @@
describe("Unit conversion", () => {
it("Animate x from 0 to calc", () => {
cy.visit("?test=unit-conversion")
.wait(50)
.get("#box")
.trigger("click")
.wait(30)
.should(([$box]: any) => {
const { left } = $box.getBoundingClientRect()
expect(left).to.equal(150)
})
})
})
25 changes: 25 additions & 0 deletions dev/examples/Animation-between-value-types-x.tsx
@@ -0,0 +1,25 @@
import * as React from "react"
import { motion, useCycle } from "@framer"

/**
* An example of animating between different value types
*/

export const App = () => {
const [x, cycleX] = useCycle(0, "calc(3 * var(--width))")

return (
<motion.div
initial={false}
animate={{ x }}
transition={{ duration: 5, ease: () => 0.5 }}
style={{
width: 100,
height: 100,
background: "white",
"--width": "100px",
}}
onClick={() => cycleX()}
/>
)
}
26 changes: 26 additions & 0 deletions dev/tests/unit-conversion.tsx
@@ -0,0 +1,26 @@
import * as React from "react"
import { motion, useCycle } from "@framer"

/**
* An example of animating between different value types
*/

export const App = () => {
const [x, cycleX] = useCycle(0, "calc(3 * var(--width))")

return (
<motion.div
initial={false}
animate={{ x }}
transition={{ duration: 5, ease: () => 0.5 }}
style={{
width: 100,
height: 100,
background: "#ffaa00",
"--width": "100px",
}}
onClick={() => cycleX()}
id="box"
/>
)
}
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "framer-motion",
"version": "5.1.0",
"version": "5.2.0",
"description": "A simple and powerful React animation library",
"main": "dist/framer-motion.cjs.js",
"module": "dist/es/index.mjs",
Expand Down
16 changes: 11 additions & 5 deletions src/render/dom/utils/unit-conversion.ts
Expand Up @@ -4,7 +4,7 @@ import { isKeyframesTarget } from "../../../animation/utils/is-keyframes-target"
import { invariant } from "hey-listen"
import { MotionValue } from "../../../value"
import { transformProps } from "../../html/utils/transform"
import { VisualElement } from "../../types"
import { ResolvedValues, VisualElement } from "../../types"
import { findDimensionValueType } from "../value-types/dimensions"
import { Box } from "../../../projection/geometry/types"

Expand Down Expand Up @@ -121,6 +121,7 @@ const convertChangedValueTypes = (
const element = visualElement.getInstance()
const elementComputedStyle = getComputedStyle(element)
const { display } = elementComputedStyle
const origin: ResolvedValues = {}

// If the element is currently set to display: "none", make it visible before
// measuring the target bounding box
Expand All @@ -131,6 +132,13 @@ const convertChangedValueTypes = (
)
}

/**
* Record origins before we render and update styles
*/
changedKeys.forEach((key) => {
origin[key] = positionalValues[key](originBbox, elementComputedStyle)
})

// Apply the latest values (as set in checkAndConvertChangedValueTypes)
visualElement.syncRender()

Expand All @@ -140,10 +148,8 @@ const convertChangedValueTypes = (
// Restore styles to their **calculated computed style**, not their actual
// originally set style. This allows us to animate between equivalent pixel units.
const value = visualElement.getValue(key) as MotionValue
setAndResetVelocity(
value,
positionalValues[key](originBbox, elementComputedStyle)
)

setAndResetVelocity(value, origin[key])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why this would fix it?

Copy link
Collaborator Author

@mattgperry mattgperry Nov 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah its in the comment in the previous line - computed style is live so we need to grab these before we trigger a render

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So how did this regress then?

target[key] = positionalValues[key](targetBbox, elementComputedStyle)
})

Expand Down