Created
June 5, 2025 07:43
-
-
Save uicodee/f0d0cdf2f92e4f4699294b9c4ae3637d 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 { | |
| AnimatePresence, | |
| motion, | |
| useInView, | |
| UseInViewOptions, | |
| Variants, | |
| MotionProps, | |
| } from "motion/react"; | |
| import { useRef } from "react"; | |
| type MarginType = UseInViewOptions["margin"]; | |
| interface AnimateProps extends MotionProps { | |
| children: React.ReactNode; | |
| className?: string; | |
| variant?: { | |
| hidden: { y: number }; | |
| visible: { y: number }; | |
| }; | |
| duration?: number; | |
| delay?: number; | |
| offset?: number; | |
| direction?: "up" | "down" | "left" | "right"; | |
| inView?: boolean; | |
| inViewMargin?: MarginType; | |
| blur?: string; | |
| } | |
| export function Animated({ | |
| children, | |
| className, | |
| variant, | |
| duration = 0.3, | |
| delay = 0, | |
| offset = 6, | |
| direction = "down", | |
| inView = false, | |
| inViewMargin = "-50px", | |
| blur = "6px", | |
| ...props | |
| }: AnimateProps) { | |
| const ref = useRef(null); | |
| const inViewResult = useInView(ref, { once: true, margin: inViewMargin }); | |
| const isInView = !inView || inViewResult; | |
| const defaultVariants: Variants = { | |
| hidden: { | |
| [direction === "left" || direction === "right" ? "x" : "y"]: | |
| direction === "right" || direction === "down" ? -offset : offset, | |
| opacity: 0, | |
| filter: `blur(${blur})`, | |
| }, | |
| visible: { | |
| [direction === "left" || direction === "right" ? "x" : "y"]: 0, | |
| opacity: 1, | |
| filter: `blur(0px)`, | |
| }, | |
| }; | |
| const combinedVariants = variant || defaultVariants; | |
| return ( | |
| <AnimatePresence> | |
| <motion.div | |
| ref={ref} | |
| initial="hidden" | |
| animate={isInView ? "visible" : "hidden"} | |
| exit="hidden" | |
| variants={combinedVariants} | |
| transition={{ | |
| delay: 0.04 + delay, | |
| duration, | |
| ease: "easeOut", | |
| }} | |
| className={className} | |
| {...props} | |
| > | |
| {children} | |
| </motion.div> | |
| </AnimatePresence> | |
| ); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment