Using Framer Motion to transition (position) between two React components<!-- --> | <!-- -->Patrick Desjardins Blog
Patrick Desjardins Blog
Patrick Desjardins picture from a conference

Using Framer Motion to transition (position) between two React components

Posted on: October 4, 2022

In this article, we will see how to have an HTML div at a specific boundary (x, y, width and height) and moving it to a second boundary to actually load another component. Visually the transparent to the user we will mount/unmount behind the curtain! Confusing! In other words, we have two React components and one is visible to the user. At some point in time, the user interact to have this visible component move to a second location where it goes away to reveal a new React components. A use case is that you have a HTML div showing information and you click "edit" to give the posibility to change the content. This editing mode might be bigger than the original content and also you may want the content to be in the middle of your screen.

The challenge is to handle the back and forth between the two React component. At one time, one is mounted while the other unmounted and they swap their state. Using React Framer Motion, we can leverage the capability of animating using their state management of when a component is mounted and unmounted.

Basic Animation

Let start with a foundation with basic code. The first snippet of code is mostly what you can have with Framer Motion using the motion.div inside two React components.

The application consists of a top and bottom panel. The top host one of our two components and the bottom has a single button that toggle between the two components. The toggle mount and unmount each component.

Initial, animated and exit

A next step is to remove setting the position and dimension with styles but to rely solely on Framer Motion. To do so, let's refactor our code to have all the position inside an object. It will simplify the repetition into a central place. Then, let's inject all the position into our two boxes. That way, both know each other boundary. The code set the initial which is the position when mounted. For Box1 component, we set the initial to be the other box: Box2. That way, when we mount the first box we start at box two position and dimension. We do the opposite for box two. The animate is the final position. For Box1 we use the boxInfo.box1 and for Box2 the final position that is inside boxInfo.box2. The exit is the transition when unmount is called which is the same as the initial.

Scaling

The problem with the actual code is that we have Box 1 and Box 2 that are swapping. However, it does not scale well if we have 10 boxes which has two states meaning 20 configurations.

Let's change our TypeScript interface, our model, to support a collection of Box that has two states.

1export interface BoxInfo {
2 left: number;
3 top: number;
4 width: number;
5 height: number;
6}
7
8export interface BoxState {
9 name: string;
10 state1: BoxInfo;
11 state2: BoxInfo;
12}
13
14export interface BoxProps {
15 boxState: BoxState;
16}

Creating two differents boxes can be done using a collection:

1const data = {
2 boxInfos: [
3 {
4 name: "Alpha",
5 state1: {
6 left: 10,
7 top: 10,
8 width: 100,
9 height: 100
10 },
11 state2: {
12 left: 120,
13 top: 50,
14 width: 100,
15 height: 150
16 }
17 },
18 {
19 name: "Beta",
20 state1: {
21 left: 170,
22 top: 10,
23 width: 100,
24 height: 100
25 },
26 state2: {
27 left: 20,
28 top: 150,
29 width: 100,
30 height: 100
31 }
32 }
33 ]
34};

and the mounting and unmounting

1<div className="Main">
2 <div className="BoxContainer">
3 {data.boxInfos.map((box, index) => {
4 return box1 ? (
5 <Box1 key={index} boxState={box} />
6 ) : (
7 <Box2 key={index} boxState={box} />
8 );
9 })}
10 </div>
11</div>

The animation works as expected between the Box1 and Box2. Indeed, for now, both React component are almost the same expect the background color. However, let's imagine that they are two distinct components with more than just some visual change.

Still, something is off. First, the initial/animate/exit is repetitive regardless of the unique feature of each box. The next step will be to extract the commonality between the two. framermotion reverted code

The wrapper is called BoxWrapper and has the role to handle the position and dimension of the actual React component.

1export default function App() {
2 const [firstState, setFirstState] = useState(true);
3 return (
4 <div className="App">
5 <div className="Main">
6 <div className="BoxContainer">
7 {data.boxInfos.map((box, index) => {
8 return (
9 <BoxWrapper firstState={firstState} boxState={box}>
10 {firstState ? (
11 <Box1 key={index} boxState={box} />
12 ) : (
13 <Box2 key={index} boxState={box} />
14 )}
15 </BoxWrapper>
16 );
17 })}
18 </div>
19 </div>
20 ...
21 ...

Improving React Framer Motion Translation

There is still quite a bit of redundancy in the BoxWrapper and a loading animation that is not needed. Let's take a look of the component:

1export interface BoxWrapperProps {
2 firstState: boolean;
3 boxState: BoxState;
4}
5export const BoxWrapper = (props: PropsWithChildren<BoxWrapperProps>) => {
6 const init = props.firstState ? props.boxState.state1 : props.boxState.state2;
7 const ani = props.firstState ? props.boxState.state2 : props.boxState.state1;
8 return (
9 <motion.div
10 className="boxWrapper"
11 initial={{
12 x: init.left,
13 y: init.top,
14 width: init.width,
15 height: init.height
16 }}
17 animate={{
18 x: ani.left,
19 y: ani.top,
20 width: ani.width,
21 height: ani.height
22 }}
23 exit={{
24 x: init.left,
25 y: init.top,
26 width: init.width,
27 height: init.height
28 }}
29 transition={{ duration: 1 }}
30 >
31 {props.children}
32 </motion.div>
33 );
34};

The redundancy part is between initial and exit are the same. We can extract the boundary information outside the <motion.div>. Actually, Framer Motion has the concept of variants that we can leverage in that particular case.

1export const BoxWrapper = (props: PropsWithChildren<BoxWrapperProps>) => {
2 const init = props.firstState ? props.boxState.state1 : props.boxState.state2;
3 const ani = props.firstState ? props.boxState.state2 : props.boxState.state1;
4 const variants: Variants = {
5 init: {
6 x: init.left,
7 y: init.top,
8 width: init.width,
9 height: init.height,
10 transition: { duration: 1 }
11 },
12 ani: {
13 x: ani.left,
14 y: ani.top,
15 width: ani.width,
16 height: ani.height,
17 transition: { duration: 1 }
18 }
19 };
20
21 return (
22 <motion.div
23 className="boxWrapper"
24 variants={variants}
25 initial="init"
26 animate="ani"
27 exit="init"
28 >
29 {props.children}
30 </motion.div>
31 );
32};

To fix the first mounting animation that start from the state 2 toward state 1, we need to not have an initial state. Instead, setting to false will skip the animation and go directly to the animate end result.

1export const BoxWrapper = (props: PropsWithChildren<BoxWrapperProps>) => {
2 const init = props.firstState ? props.boxState.state2 : props.boxState.state1;
3 const ani = props.firstState ? props.boxState.state1 : props.boxState.state2;
4
5 const variants: Variants = {
6 ex: {
7 x: init.left,
8 y: init.top,
9 width: init.width,
10 height: init.height,
11 transition: { duration: 1 }
12 },
13 ani: {
14 x: ani.left,
15 y: ani.top,
16 width: ani.width,
17 height: ani.height,
18 transition: { duration: 1 }
19 }
20 };
21
22 return (
23 <motion.div
24 className="boxWrapper"
25 variants={variants}
26 initial={false}
27 animate="ani"
28 exit="ex"
29 >
30 {props.children}
31 </motion.div>
32 );
33};

Conclusion

The result is not as expected. When we load the application, the two HTML divs (Box 1 and Box 2) are both in the state 1 position. Clicking the button, change the state to two. Both HTML div are moving to their state 2 positions and size. Under the scene, the two components Box1 are unmounting and two new components of type Box2 are mounted. It is transparent to the user.