Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wrap elements in custom element to animate #74

Open
adamjw3 opened this issue Jan 18, 2024 · 0 comments
Open

wrap elements in custom element to animate #74

adamjw3 opened this issue Jan 18, 2024 · 0 comments

Comments

@adamjw3
Copy link

adamjw3 commented Jan 18, 2024

Got the following code in react

`
 import React, { useEffect, useRef } from "react";
import { gsap } from "gsap";
import { ScrollTrigger } from "gsap/ScrollTrigger";
import SplitType from "split-type";

interface SplitProps {
  children: React.ReactNode;
  splitMode: String;
  delay: number;
}

const Split = ({ children, splitMode, delay = 0 }: SplitProps) => {
  const trigger = useRef<HTMLDivElement>(null);
  const target = useRef<HTMLDivElement>(null);

  useEffect(() => {
    gsap.registerPlugin(ScrollTrigger);

    const init = () => {
      split();
    };

    const animate = (text) => {
      gsap.from(text, {
        delay: delay,
        y: "105%",
        duration: 0.5,
        stagger: 0.05,
        ease: "power2.in",
        force3D: true,
        scrollTrigger: {
          trigger: trigger.current,
          scrub: false,
        },
      });
    };

    const splitChars = () => {
      const text = new SplitType(target.current!, { splitClass: "split-text", types: "chars" });
      animate(text.chars);
    };

    const splitLines = () => {
      const text = new SplitType(target.current!, { splitClass: "split-text", types: "lines" });
      animate(text.lines);
    };

    const splitWords = () => {
      const text = new SplitType(target.current!, { splitClass: "split-text", types: "words" });
      animate(text.words);
    };

    const split = () => {
      if (splitMode === "chars") {
        splitChars();
      } else if (splitMode === "lines") {
        splitLines();
      } else if (splitMode === "words") {
        splitWords();
      }
    };

    init();
  }, []); // Empty dependency array ensures that the effect runs once after the initial render

  return (
    <span ref={trigger} className="inline-block">
      <span ref={target} className="inline-block overflow-hidden overflow-clip">
        {children}
      </span>
    </span>
  );
};

export default Split;
`

This outputs code like this for example if we are using lines

<span class="inline-block overflow-hidden overflow-clip">
       <div class="split-text line">Lorem ipsum dolor sit amet, consectetur..</div>
        <div class="split-text line">Lorem ipsum dolor sit amet, consectetur..</div>
</span>

What i want to do is wrap the

div in another div that will act as the overflow-hidden, so the output would look like

<span class="inline-block overflow-hidden overflow-clip">
       <div class="split-text-wrapper">
                <div class="split-text line">Lorem ipsum dolor sit amet, consectetur..</div>
        </div>
        <div class="split-text-wrapper">
                <div class="split-text line">Lorem ipsum dolor sit amet, consectetur..</div>
        </div>
</span>

is this possible?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant