A sandbox explaining the use of Framer Motion in each routed React component.
Featuring:
- This is a React.js app deployed with
create vite
. - We switch
<Route />
paths to React components with React Router. - The main demonstration in this sandbox app, is the use of Framer Motion.
AnimatePresence
allows you to animate components when they're removed from the React tree. It allows components to animate out when unmounting.AnimatePresence
requires aexit
prop enabled for animations. Because React lacks a lifecycle method that:
- Notifies components when they're going to be unmounted and...
- Allows them to defer that unmounting until the operation/animation is complete.
-
But
AnimatePresence
has a bit more complexity when we route and switch components and unmount from the DOM. To get it toexit
work, the<Switch />
needs props regarding the path location. See comments made in theApp.jsx
file that hasAnimatePresence
configured that is more complex compared to ourModel.jsx
. -
And framer variant 🧬 objects are supplied to most React components. See comments made in the
Base.jsx
file.
- We get to extract our initial & animate object(s) into a single outside variant object. We can then reference to that outside variant object, keeping our JSX code cleaner.
- We can then propagate these variant(s) or changes to them, throughout the DOM tree resulting in cleaner code also.
- The bigger advantage is that variants allow timing-relationships between parent, and their directly/enclosed children
motion
elements throughout the tree your variant encloses over.
-
With variants we get orchestration between parent
motion
and childmotion
components. By default, all animations will start simultaneously. But by using variants, we gain access to extra transition props likewhen
,delayChildren
, and also thestaggerChildren
, that lets parentmotion
components to orchestrate execution of childmotion
component animation. See comments made in theOrder.jsx
file. -
We make use of Keyframes in our variants also. See
Home.jsx
andLoader.jsx
for more information.
Dependencies:
"dependencies": {
"framer-motion": "^7.6.1",
"react": "^18.2.0",
"react-dom": "^18.2.0"
}
Regards,
Luigi Lupini
I ❤️ all things (🇮🇹 / 🛵 / ☕️ / 👨👩👧)