Bouncing Text

PreviousNext

A playful animation where characters bounce in sequence across the text.

Docs
paceuiui

Preview

Loading preview…
gsap/bouncing-text.tsx
"use client";

import { ComponentProps, useRef } from "react";

import { useGSAP } from "@gsap/react";
import gsap from "gsap";
import { SplitText } from "gsap/SplitText";

type BouncingTextProps = {
    repeat?: boolean | number;
} & ComponentProps<"p">;

export const BouncingText = ({ repeat = true, ...props }: BouncingTextProps) => {
    const textRef = useRef<HTMLParagraphElement | null>(null);

    useGSAP(
        () => {
            if (!textRef.current) return;
            let bounceCount = 0;
            const bounceChars = new SplitText(textRef.current, { type: "words,chars" }).chars;
            bounceChars.forEach((el) => {
                const tl = gsap.timeline({ repeat: repeat === true ? -1 : repeat === false ? 0 : repeat });
                tl.to(el, {
                    duration: 0,
                    y: -200,
                });
                tl.to(el, {
                    duration: 2,
                    y: 0,
                    rotate: -10,
                    ease: "bounce",
                });
                tl.to(el, {
                    duration: 1,
                    y: 0,
                    rotate: 0,
                    ease: "bounce",
                });
                tl.to(el, {
                    duration: 2,
                    y: -200,
                    rotate: 0,
                    delay: 1,
                    ease: "elastic",
                });
                tl.delay(bounceCount / 8);
                bounceCount++;
            });
        },
        { scope: textRef },
    );

    return <p {...props} ref={textRef} />;
};

Installation

npx shadcn@latest add @paceui/bouncing-text

Usage

import { BouncingText } from "@/components/ui/bouncing-text"
<BouncingText />