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.
AnimatePresenceallows you to animate components when they're removed from the React tree. It allows components to animate out when unmounting.AnimatePresencerequires aexitprop 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
AnimatePresencehas a bit more complexity when we route and switch components and unmount from the DOM. To get it toexitwork, the<Switch />needs props regarding the path location. See comments made in theApp.jsxfile that hasAnimatePresenceconfigured that is more complex compared to ourModel.jsx. -
And framer variant 🧬 objects are supplied to most React components. See comments made in the
Base.jsxfile.
- 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
motionelements throughout the tree your variant encloses over.
-
With variants we get orchestration between parent
motionand childmotioncomponents. 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 parentmotioncomponents to orchestrate execution of childmotioncomponent animation. See comments made in theOrder.jsxfile. -
We make use of Keyframes in our variants also. See
Home.jsxandLoader.jsxfor more information.
Dependencies:
"dependencies": {
"framer-motion": "^7.6.1",
"react": "^18.2.0",
"react-dom": "^18.2.0"
}Regards,
Luigi Lupini
I ❤️ all things (🇮🇹 / 🛵 / ☕️ / 👨👩👧)
