-
-
Notifications
You must be signed in to change notification settings - Fork 855
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 #2672 from framer/fix/appear
Fixing optimised appear jump with `useLayoutEffect` and layout animations
- Loading branch information
Showing
5 changed files
with
287 additions
and
5 deletions.
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
dev/optimized-appear/defer-handoff-layout-useeffect.html
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,135 @@ | ||
<html> | ||
<head> | ||
<style> | ||
body { | ||
padding: 100px; | ||
margin: 0; | ||
} | ||
|
||
#box { | ||
width: 100px; | ||
height: 100px; | ||
background-color: #0077ff; | ||
} | ||
|
||
[data-layout-correct="false"] { | ||
background: #dd1144 !important; | ||
opacity: 1 !important; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script src="../../node_modules/react/umd/react.development.js"></script> | ||
<script src="../../node_modules/react-dom/umd/react-dom.development.js"></script> | ||
<script src="../../node_modules/react-dom/umd/react-dom-server-legacy.browser.development.js"></script> | ||
<script src="../../packages/framer-motion/dist/framer-motion.dev.js"></script> | ||
<script src="../projection/script-assert.js"></script> | ||
|
||
<script> | ||
const { | ||
motion, | ||
animateStyle, | ||
animate, | ||
startOptimizedAppearAnimation, | ||
optimizedAppearDataAttribute, | ||
motionValue, | ||
frame, | ||
} = window.Motion | ||
const { matchViewportBox } = window.Assert | ||
const root = document.getElementById("root") | ||
|
||
const duration = 0.5 | ||
const x = motionValue(0) | ||
|
||
let isFirstFrame = true | ||
|
||
function Component() { | ||
const [top, setTop] = React.useState(0) | ||
React.useEffect(() => { | ||
setTop(100) | ||
}, []) | ||
|
||
return React.createElement(motion.div, { | ||
id: "box", | ||
initial: { x: 0, opacity: 0 }, | ||
animate: { x: 100, opacity: 1 }, | ||
transition: { | ||
duration, | ||
ease: "linear", | ||
layout: { ease: () => 0, duration: 10 }, | ||
}, | ||
style: { | ||
x, | ||
top, | ||
position: "relative", | ||
background: top ? "red" : "blue", | ||
}, | ||
layout: true, | ||
onLayoutAnimationStart: () => { | ||
requestAnimationFrame(() => { | ||
const box = document.getElementById("box") | ||
const { top } = box.getBoundingClientRect() | ||
|
||
if (top !== 100) { | ||
showError( | ||
box, | ||
`layout animation overridden by optimised animation` | ||
) | ||
} | ||
}) | ||
}, | ||
onAnimationComplete: () => { | ||
const box = document.getElementById("box") | ||
const { left } = box.getBoundingClientRect() | ||
|
||
console.log(left) | ||
if (left !== 200) { | ||
showError( | ||
box, | ||
`optimised animation conflict with layout measurements` | ||
) | ||
} | ||
}, | ||
[optimizedAppearDataAttribute]: "a", | ||
children: "Content", | ||
}) | ||
} | ||
|
||
// Emulate server rendering of element | ||
root.innerHTML = ReactDOMServer.renderToString( | ||
React.createElement(Component) | ||
) | ||
|
||
// Start optimised opacity animation | ||
startOptimizedAppearAnimation( | ||
document.getElementById("box"), | ||
"opacity", | ||
[0, 1], | ||
{ | ||
duration: duration * 1000, | ||
ease: "linear", | ||
} | ||
) | ||
|
||
// Start WAAPI animation | ||
const animation = startOptimizedAppearAnimation( | ||
document.getElementById("box"), | ||
"transform", | ||
["translateX(0px)", "translateX(100px)"], | ||
{ | ||
duration: duration * 1000, | ||
ease: "linear", | ||
}, | ||
(animation) => { | ||
setTimeout(() => { | ||
ReactDOM.hydrateRoot( | ||
root, | ||
React.createElement(Component) | ||
) | ||
}, (duration * 1000) / 2) | ||
} | ||
) | ||
</script> | ||
</body> | ||
</html> |
134 changes: 134 additions & 0 deletions
134
dev/optimized-appear/defer-handoff-layout-uselayouteffect.html
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,134 @@ | ||
<html> | ||
<head> | ||
<style> | ||
body { | ||
padding: 100px; | ||
margin: 0; | ||
} | ||
|
||
#box { | ||
width: 100px; | ||
height: 100px; | ||
background-color: #0077ff; | ||
} | ||
|
||
[data-layout-correct="false"] { | ||
background: #dd1144 !important; | ||
opacity: 1 !important; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script src="../../node_modules/react/umd/react.development.js"></script> | ||
<script src="../../node_modules/react-dom/umd/react-dom.development.js"></script> | ||
<script src="../../node_modules/react-dom/umd/react-dom-server-legacy.browser.development.js"></script> | ||
<script src="../../packages/framer-motion/dist/framer-motion.dev.js"></script> | ||
<script src="../projection/script-assert.js"></script> | ||
|
||
<script> | ||
const { | ||
motion, | ||
animateStyle, | ||
animate, | ||
startOptimizedAppearAnimation, | ||
optimizedAppearDataAttribute, | ||
motionValue, | ||
frame, | ||
} = window.Motion | ||
const { matchViewportBox } = window.Assert | ||
const root = document.getElementById("root") | ||
|
||
const duration = 0.5 | ||
const x = motionValue(0) | ||
|
||
let isFirstFrame = true | ||
|
||
function Component() { | ||
const [top, setTop] = React.useState(0) | ||
React.useLayoutEffect(() => { | ||
setTop(100) | ||
}, []) | ||
|
||
return React.createElement(motion.div, { | ||
id: "box", | ||
initial: { x: 0, opacity: 0 }, | ||
animate: { x: 100, opacity: 1 }, | ||
transition: { | ||
duration, | ||
ease: "linear", | ||
layout: { ease: () => 0, duration: 10 }, | ||
}, | ||
style: { | ||
x, | ||
top, | ||
position: "relative", | ||
background: top ? "red" : "blue", | ||
}, | ||
layout: true, | ||
onLayoutAnimationStart: () => { | ||
requestAnimationFrame(() => { | ||
const box = document.getElementById("box") | ||
const { top } = box.getBoundingClientRect() | ||
|
||
if (top !== 100) { | ||
showError( | ||
box, | ||
`layout animation overridden by optimised animation` | ||
) | ||
} | ||
}) | ||
}, | ||
onAnimationComplete: () => { | ||
const box = document.getElementById("box") | ||
const { left } = box.getBoundingClientRect() | ||
|
||
if (left !== 200) { | ||
showError( | ||
box, | ||
`optimised animation conflict with layout measurements` | ||
) | ||
} | ||
}, | ||
[optimizedAppearDataAttribute]: "a", | ||
children: "Content", | ||
}) | ||
} | ||
|
||
// Emulate server rendering of element | ||
root.innerHTML = ReactDOMServer.renderToString( | ||
React.createElement(Component) | ||
) | ||
|
||
// Start optimised opacity animation | ||
startOptimizedAppearAnimation( | ||
document.getElementById("box"), | ||
"opacity", | ||
[0, 1], | ||
{ | ||
duration: duration * 1000, | ||
ease: "linear", | ||
} | ||
) | ||
|
||
// Start WAAPI animation | ||
const animation = startOptimizedAppearAnimation( | ||
document.getElementById("box"), | ||
"transform", | ||
["translateX(0px)", "translateX(100px)"], | ||
{ | ||
duration: duration * 1000, | ||
ease: "linear", | ||
}, | ||
(animation) => { | ||
setTimeout(() => { | ||
ReactDOM.hydrateRoot( | ||
root, | ||
React.createElement(Component) | ||
) | ||
}, (duration * 1000) / 2) | ||
} | ||
) | ||
</script> | ||
</body> | ||
</html> |
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
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