-
Notifications
You must be signed in to change notification settings - Fork 790
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2194 from framer/fix/reverse-animation
Fix for reverse animation with negative speed
- Loading branch information
Showing
5 changed files
with
145 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { useAnimate } from "framer-motion" | ||
import * as React from "react" | ||
|
||
export const App = () => { | ||
const [scope, animate] = useAnimate() | ||
|
||
return ( | ||
<div className="App" ref={scope}> | ||
<div | ||
className="four" | ||
style={{ width: 50, height: 50, backgroundColor: "blue" }} | ||
></div> | ||
<p>reverse</p> | ||
<button | ||
onClick={() => { | ||
const animation = animate( | ||
".four", | ||
{ x: 90 }, | ||
{ duration: 2 } | ||
) | ||
animation.time = animation.duration | ||
animation.speed = -1 | ||
}} | ||
> | ||
play | ||
</button> | ||
</div> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { motion, animate } from "framer-motion" | ||
import * as React from "react" | ||
import { useEffect, useState } from "react" | ||
import styled from "styled-components" | ||
|
||
const Container = styled.section` | ||
position: relative; | ||
display: flex; | ||
flex-direction: column; | ||
padding: 100px; | ||
div { | ||
width: 100px; | ||
height: 100px; | ||
background-color: red; | ||
} | ||
` | ||
|
||
export const App = () => { | ||
const [count, setCount] = useState(0) | ||
const [result, setResult] = useState("") | ||
|
||
useEffect(() => { | ||
if (count % 2 === 0) return | ||
|
||
const output: number[] = [] | ||
const controls = animate(0, 100, { | ||
duration: 0.5, | ||
ease: "linear", | ||
onUpdate: (v: number) => output.push(v), | ||
onComplete: () => | ||
setResult( | ||
output[1] === 100 && output.length !== 2 | ||
? "Success" | ||
: "Fail" | ||
), | ||
}) | ||
controls.time = controls.duration | ||
controls.speed = -1 | ||
|
||
return controls.stop | ||
}, [count]) | ||
|
||
return ( | ||
<Container> | ||
<button id="action" onClick={() => setCount((c) => c + 1)}> | ||
Animate | ||
</button> | ||
<input id="result" readOnly value={result} /> | ||
<motion.div className="box" layout /> | ||
</Container> | ||
) | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/framer-motion/cypress/integration/animate-reverse.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
describe("animate() x layout prop in reverse speed", () => { | ||
it("animate() plays as expected when layout prop is present", () => { | ||
cy.visit("?test=animate-reverse") | ||
.wait(1000) | ||
.get("#action") | ||
.trigger("click", 1, 1, { force: true }) | ||
.wait(600) | ||
.get("#result") | ||
.should(([$element]: any) => { | ||
expect($element.value).to.equal("Success") | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters