Scroll Element
A Framer Motion animation component that allows for directional animations, including movement from left, right, or bottom.
A Framer Motion animation component that allows for directional animations, including movement from left, right, or bottom.
npm install framer-motion
1import { cn } from '@/lib/utils';2import {3motion,4HTMLMotionProps,5SVGMotionProps,6ForwardRefComponent,7Variant,8useAnimation,9} from 'framer-motion';10import React from 'react';11type Direction = 'up' | 'down' | 'left' | 'right';1213const generateVariants = (14direction: Direction15): { hidden: Variant; visible: Variant } => {16const axis = direction === 'left' || direction === 'right' ? 'x' : 'y';17const value = direction === 'right' || direction === 'down' ? 100 : -100;1819return {20hidden: { filter: 'blur(10px)', opacity: 0, [axis]: value },21visible: {22filter: 'blur(0px)',23opacity: 1,24[axis]: 0,25transition: {26duration: 0.5,27ease: 'easeOut',28},29},30};31};3233const defaultViewport = { amount: 0.3, margin: '0px 0px -200px 0px' };34type MotionComponentProps = HTMLMotionProps<any> & SVGMotionProps<any>;3536interface ScrollElementProps extends Omit<MotionComponentProps, 'children'> {37children: React.ReactNode;38className?: string;39variants?: {40hidden?: any;41visible?: any;42};43viewport?: {44amount?: number;45margin?: string;46once?: boolean;47};48delay?: number;49direction?: Direction;50}5152function ScrollElement({53children,54className,55variants,56viewport = defaultViewport,57delay = 0, // Default delay is 058direction = 'down',59...rest60}: ScrollElementProps) {61const baseVariants = variants || generateVariants(direction);62const modifiedVariants = {63hidden: baseVariants.hidden,64visible: {65...baseVariants.visible,66transition: {67...baseVariants.visible.transition,68delay, // Apply custom delay here69},70},71};7273return (74<motion.div75whileInView='visible'76initial='hidden'77variants={modifiedVariants}78viewport={viewport}79className={cn(className)}80{...rest}81>82{children}83</motion.div>84);85}86export default ScrollElement;
Prop | Type | Default | Description |
---|---|---|---|
children | React.ReactNode | The content to be rendered within the scroll element. | |
className | string | Optional CSS class for styling the scroll element. | |
variants | object | Animation variants for different scroll states (hidden/visible). | |
viewport | object | { amount: 0.5, margin: '0px', once: true } | Settings for when the element is considered in view. |
delay | number | 0 | Delay before the animation starts, in milliseconds. |
direction | Direction | 'down' | The scroll direction for the animation (e.g., 'down', 'up'). |