Skip to content

Commit 1494a61

Browse files
committed
Fixing layout animations with z transform
1 parent 5cfcdca commit 1494a61

File tree

3 files changed

+113
-2
lines changed

3 files changed

+113
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<html>
2+
<head>
3+
<style>
4+
body {
5+
padding: 0;
6+
margin: 0;
7+
}
8+
9+
#container {
10+
width: 100px;
11+
height: 200px;
12+
display: flex;
13+
align-items: flex-end;
14+
transform-style: preserve-3d;
15+
background-color: blue;
16+
}
17+
18+
#container.b {
19+
align-items: flex-start;
20+
}
21+
22+
#box {
23+
width: 100px;
24+
height: 100px;
25+
background-color: #00cc88;
26+
padding: 10px;
27+
display: flex;
28+
align-items: flex-start;
29+
}
30+
31+
.b #box {
32+
align-items: flex-end;
33+
}
34+
35+
#box-child {
36+
width: 50px;
37+
height: 50px;
38+
background-color: #cc00cc;
39+
}
40+
41+
#trigger-overflow {
42+
width: 1px;
43+
height: 1px;
44+
position: absolute;
45+
top: 2000px;
46+
left: 2000px;
47+
}
48+
49+
[data-layout-correct="false"] {
50+
background: #dd1144 !important;
51+
opacity: 0.5;
52+
}
53+
</style>
54+
</head>
55+
56+
<body>
57+
<div id="container">
58+
<div id="box" data-layout-correct="true">
59+
<div id="box-child"></div>
60+
</div>
61+
</div>
62+
<div id="trigger-overflow"></div>
63+
64+
<script src="../../packages/framer-motion/dist/projection.dev.js"></script>
65+
<script src="./script-assert.js"></script>
66+
<script src="./script-undo.js"></script>
67+
<script>
68+
const { createNode } = window.Undo
69+
const { matchViewportBox, matchSkewX } = window.Assert
70+
const container = document.getElementById("container")
71+
const containerProjection = createNode(container, undefined, {
72+
layout: false,
73+
})
74+
const box = document.getElementById("box")
75+
const boxProjection = createNode(box, containerProjection)
76+
const boxChild = document.getElementById("box-child")
77+
const boxChildProjection = createNode(boxChild, boxProjection)
78+
79+
containerProjection.setValue("rotateX", 40)
80+
containerProjection.setValue("transformPerspective", 500)
81+
boxProjection.setValue("rotateX", 10)
82+
boxProjection.setValue("z", 20)
83+
84+
requestAnimationFrame(() => {
85+
const boxOrigin = box.getBoundingClientRect()
86+
boxProjection.willUpdate()
87+
boxChildProjection.willUpdate()
88+
containerProjection.willUpdate()
89+
container.classList.add("b")
90+
91+
requestAnimationFrame(() => {
92+
containerProjection.root.didUpdate()
93+
frame.postRender(() => {
94+
matchViewportBox(box, boxOrigin)
95+
})
96+
})
97+
})
98+
</script>
99+
</body>
100+
</html>

packages/framer-motion/src/projection/node/create-projection-node.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1702,6 +1702,7 @@ export function createProjectionNode<I>({
17021702
*/
17031703
const { latestValues } = visualElement
17041704
if (
1705+
latestValues.z ||
17051706
latestValues.rotate ||
17061707
latestValues.rotateX ||
17071708
latestValues.rotateY ||
@@ -1717,6 +1718,15 @@ export function createProjectionNode<I>({
17171718

17181719
const resetValues: ResolvedValues = {}
17191720

1721+
if (latestValues.z) {
1722+
resetDistortingTransform(
1723+
"z",
1724+
visualElement,
1725+
resetValues,
1726+
this.animationValues
1727+
)
1728+
}
1729+
17201730
// Check the skew and rotate value of all axes and reset to 0
17211731
for (let i = 0; i < transformAxes.length; i++) {
17221732
resetDistortingTransform(

packages/framer-motion/src/projection/styles/transform.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ export function buildProjectionTransform(
1616
*/
1717
const xTranslate = delta.x.translate / treeScale.x
1818
const yTranslate = delta.y.translate / treeScale.y
19-
if (xTranslate || yTranslate) {
20-
transform = `translate3d(${xTranslate}px, ${yTranslate}px, 0) `
19+
const zTranslate = latestTransform?.z || 0
20+
if (xTranslate || yTranslate || zTranslate) {
21+
transform = `translate3d(${xTranslate}px, ${yTranslate}px, ${zTranslate}px) `
2122
}
2223

2324
/**

0 commit comments

Comments
 (0)