Fade-in animations can significantly enhance the user experience by introducing elements gracefully into the UI. In React development, creating reusable components can streamline the process and maintain code consistency across a project. In this article, we'll explore a concise method using React, Framer Motion, and Intersection Observer to achieve a reusable fade-in animation component.
The FadeIn component is designed to animate its children by fading them in as they become visible in the viewport. Here's a breakdown of the key components and their roles:
Dependencies: The component imports necessary dependencies such as motion and useAnimation from Framer Motion, as well as useInView from react-intersection-observer.
Props: The FadeIn component accepts three props:
children: Represents the content or elements to be faded in.
className: Allows for additional styling.
checkInView: A boolean flag to determine if the animation should occur when the element is in view. Logic:
useAnimation: Initializes animation controls provided by Framer Motion.
useInView: Provides a reference and a boolean flag (inView) to detect if the component is in the viewport.
useEffect: Triggers the animation when the component enters the viewport (inView) or based on the checkInView prop.
Motion Component: Utilizes motion.div from Framer Motion to wrap the children. Animates the element's opacity and y-axis position (from a hidden state off the screen to visible).
Developers can easily implement the FadeIn component by following these steps:
Import the Component: Import the
FadeIncomponent into your React project where you wish to apply fade-in animations.Usage: Use the
FadeIncomponent and pass the elements you want to animate as its children.
import FadeIn from './FadeInComponentPath';
const MyComponent = () => {
return (
<div>
<FadeIn>
{/* Elements to be faded in */}
<h1>Your Content Here</h1>
<p>Additional elements</p>
</FadeIn>
</div>
);
};
Customization: Customize the animation by adjusting the
transitionduration, variants, or additional styling via theclassNameprop.
Implementing reusable components like the FadeIn component not only simplifies the integration of animations but also promotes maintainability and consistency within a React application. This approach empowers React UI developers to create engaging user experiences with minimal code repetition.
By leveraging the power of React, Framer Motion, and Intersection Observer, developers can effortlessly incorporate subtle yet impactful animations into their projects, enhancing the overall aesthetics and usability of their applications.
Feel free to integrate this FadeIn component into your React projects and explore its adaptability to different UI elements, bringing life to your user interfaces through elegant fade-in animations.
import { motion, useAnimation } from "framer-motion"
import { useInView } from "react-intersection-observer"
import { FC, useEffect } from "react"
interface FadeInProps {
children: any
className?: string
checkInview?: boolean
}
const FadeIn: FC<FadeInProps> = ({ children, className = "", checkInview = true }) => {
const controls = useAnimation()
const [ref, inView] = useInView()
useEffect(() => {
if (inView || !checkInview) {
controls.start("visible")
return
}
controls.start("hidden")
}, [controls, inView, checkInview])
return (
<motion.div
ref={ref}
animate={controls}
initial="hidden"
transition={{ duration: 0.5 }}
variants={{
visible: { opacity: 1, y: 0 },
hidden: { opacity: 0, y: 100 },
}}
className={`w-full ${className}`}
>
{children}
</motion.div>
)
}
export default FadeIn

