website/components/FadeIn.tsx

44 lines
946 B
TypeScript

"use client";
import { motion, useInView } from "framer-motion";
import { useRef } from "react";
interface FadeInProps {
children: React.ReactNode;
delay?: number;
direction?: "up" | "down" | "left" | "right" | "none";
className?: string;
}
export default function FadeIn({
children,
delay = 0,
direction = "up",
className,
}: FadeInProps) {
const ref = useRef(null);
const inView = useInView(ref, { once: true, margin: "-80px" });
const offsets = {
up: { y: 28, x: 0 },
down: { y: -28, x: 0 },
left: { y: 0, x: 28 },
right: { y: 0, x: -28 },
none: { y: 0, x: 0 },
};
const { x, y } = offsets[direction];
return (
<motion.div
ref={ref}
className={className}
initial={{ opacity: 0, x, y }}
animate={inView ? { opacity: 1, x: 0, y: 0 } : {}}
transition={{ duration: 0.6, delay, ease: [0.22, 1, 0.36, 1] }}
>
{children}
</motion.div>
);
}