Last active
January 6, 2023 23:09
-
-
Save Yvad60/5598dfaec3bba0c12885d1a0338c399a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import { motion, useAnimation } from "framer-motion"; | |
| import { useEffect } from "react"; | |
| import { useInView } from "react-intersection-observer"; | |
| export default function BoxShow() { | |
| const control = useAnimation(); | |
| const [ref, isBoxInView] = useInView(); | |
| const boxVariants = { | |
| visible: { opacity: 1, scale: 2 }, | |
| hidden: { opacity: 0, scale: 0 }, | |
| }; | |
| useEffect(() => { | |
| if (inView) control.start("visible"); | |
| else control.start("hidden"); | |
| }, [isBoxInView, control]); | |
| return ( | |
| <main className="py-10"> | |
| <motion.div className="w-[200px] h-[200px] bg-red-50"> | |
| <span className="text-white">Box 1</span> | |
| </motion.div> | |
| <motion.div variants={boxVariants} initial="hidden" animate={control} ref={ref} className="w-[200px] h-[200px] bg-red-50"> | |
| <span className="text-white"> Box 1 </span> | |
| </motion.div> | |
| <motion.div className="w-[200px] h-[200px] bg-red-50"> | |
| <span className="text-white">Box 1</span> | |
| </motion.div> | |
| </main> | |
| ); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The above code snippet is using react-intersection-observer and framer-motion to animate a box (div) when it is in view (visible to the user) (classNames used above are for tailwindcss)
about the UI we have 3
divsin a parent container and the purpose is to make the box in the middle scale up by the time the user scrolls to it and be hidden when the box is not in view.To have these animations we are using
motion.divwhich renders a div that can be animated by framer motionwe defined animation variants here
this means we have two states the state of hidden where the scale and opacity will be zero (box not visible at all ) and the state
visiblewhere it will be bigger and visiblewe are then passing these boxVariants into
variantsprops ofmotion.divso that it can know how to animate our element. By default we want the box to have the hidden state so we specifyhiddenas the state in theinitialprop.By doing this Framer motion will apply the style specified in thehiddenvariant.Framer motion provides a hook
UseAnimationthat can allow us to control our animation and that's where we are using thecontrolobject. We pass this control object to theanimateprop so that with this control object we can be able to control the animation of the boxFinally, we need a way to know whether our Box is in view or not and that's where we use react-intersection-observer. React intersection observer provides a hook
useInViewthat will used to create theisBoxInViewboolean that which will be true when the element is in view and false otherwise and it also provide a ref that we pass to the element we want to observein the useEffect we are listenning to when our boolean changes and if the element is inview (isBoxInView true) then we use the control object to trigger
visiblestate and trigglehiddenstate other wiseand this will achieve our animation