Animated Content
Wrapper that animates any children on scroll or mount with configurable direction, distance, duration, easing and disappear options.
pnpm dlx shadcn@latest add @leo/animated-contentProps
| Name | Type | Default | Description |
|---|---|---|---|
| animateOpacity | boolean | undefined | true | |
| children* | ReactNode | - | |
| className | string | undefined | ||
| container | string | Element | null | undefined | - | |
| delay | number | undefined | 0 | |
| direction | undefined | "vertical" | "horizontal" | vertical | |
| disappearAfter | number | undefined | 0 | |
| disappearDuration | number | undefined | 0.5 | |
| disappearEase | string | undefined | power3.in | |
| distance | number | undefined | 100 | |
| duration | number | undefined | 0.8 | |
| ease | string | undefined | power3.out | |
| initialOpacity | number | undefined | 0 | |
| onComplete | (() => void) | undefined | - | |
| onDisappearanceComplete | (() => void) | undefined | - | |
| reverse | boolean | undefined | false | |
| scale | number | undefined | 1 | |
| threshold | number | undefined | 0.1 |
Dependencies
- gsap@^3.13.0
Source
"use client";
/*
* Ported from react-bits (MIT, © David Haz): src/ts-tailwind/Animations/AnimatedContent/AnimatedContent.tsx @ 9d0270d
*
* MIT License
*
* Copyright (c) 2026 David Haz
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
import type React from "react";
import { useEffect, useRef } from "react";
import { cn } from "@/lib/utils";
gsap.registerPlugin(ScrollTrigger);
export interface AnimatedContentProps extends React.HTMLAttributes<HTMLDivElement> {
children: React.ReactNode;
container?: Element | string | null;
distance?: number;
direction?: "vertical" | "horizontal";
reverse?: boolean;
duration?: number;
ease?: string;
initialOpacity?: number;
animateOpacity?: boolean;
scale?: number;
threshold?: number;
delay?: number;
disappearAfter?: number;
disappearDuration?: number;
disappearEase?: string;
onComplete?: () => void;
onDisappearanceComplete?: () => void;
}
const AnimatedContent: React.FC<AnimatedContentProps> = ({
children,
container,
distance = 100,
direction = "vertical",
reverse = false,
duration = 0.8,
ease = "power3.out",
initialOpacity = 0,
animateOpacity = true,
scale = 1,
threshold = 0.1,
delay = 0,
disappearAfter = 0,
disappearDuration = 0.5,
disappearEase = "power3.in",
onComplete,
onDisappearanceComplete,
className = "",
...props
}) => {
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
const el = ref.current;
if (!el) return;
let scrollerTarget: Element | string | null =
container || document.getElementById("snap-main-container") || null;
if (typeof scrollerTarget === "string") {
scrollerTarget = document.querySelector(scrollerTarget);
}
const axis = direction === "horizontal" ? "x" : "y";
const offset = reverse ? -distance : distance;
const startPct = (1 - threshold) * 100;
gsap.set(el, {
[axis]: offset,
scale,
opacity: animateOpacity ? initialOpacity : 1,
visibility: "visible",
});
const tl = gsap.timeline({
paused: true,
delay,
onComplete: () => {
if (onComplete) onComplete();
if (disappearAfter > 0) {
gsap.to(el, {
[axis]: reverse ? distance : -distance,
scale: 0.8,
opacity: animateOpacity ? initialOpacity : 0,
delay: disappearAfter,
duration: disappearDuration,
ease: disappearEase,
onComplete: () => onDisappearanceComplete?.(),
});
}
},
});
tl.to(el, {
[axis]: 0,
scale: 1,
opacity: 1,
duration,
ease,
});
const st = ScrollTrigger.create({
trigger: el,
scroller: scrollerTarget || window,
start: `top ${startPct}%`,
once: true,
onEnter: () => tl.play(),
});
return () => {
st.kill();
tl.kill();
};
}, [
container,
distance,
direction,
reverse,
duration,
ease,
initialOpacity,
animateOpacity,
scale,
threshold,
delay,
disappearAfter,
disappearDuration,
disappearEase,
onComplete,
onDisappearanceComplete,
]);
return (
<div ref={ref} className={cn("invisible", className)} {...props}>
{children}
</div>
);
};
export { AnimatedContent };
export default AnimatedContent;