Jump to content
Search Community

Using SplitText in React Component including nested components

kacperkodo
Moderator Tag

Recommended Posts

Posted

Hello! I Next.js I got a SectionHeadline component that I need to split into lines to apply animations. The problem is that when I apply SplitText plugin (like I do below) it messes with all of my nested components (it includes things lik WordsLoop slider that has some ScrollTriggers within. All of those scrolltriggers break and nothing works like expected. 

I wonder what am I doing wrong. 

The line splitting code (adding divs for each line works), but interestingly on autoreload it gives an error:

NotFoundError: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.


When I refresh the page, it's fine. What am I doing wrong? Thank you!
 

"use client";
 
import { useGSAP } from "@gsap/react";
import gsap from "gsap";
import { SplitText } from "gsap/SplitText";
import type { PortableTextProps } from "next-sanity";
import { PortableText } from "next-sanity";
import { useRef } from "react";
import { WordsLoop } from "~/components/words-loop";
import { cx } from "~/lib/utils/styled";
 
export function SectionHeadline({
value,
className,
...rest
}: Pick<PortableTextProps, "value"> &
React.ComponentPropsWithoutRef<"div">) {
const headlineRef = useRef<HTMLDivElement>(null);
 
useGSAP(
() => {
gsap.registerPlugin(SplitText);
 
const split = new SplitText("p", { type: "lines" });
},
{ scope: headlineRef }
);
 
return (
<div
ref={headlineRef}
className={cx("block px-32 text-center font-serif text-title-90", className)}
{...rest}
>
<PortableText
value={value}
components={{
marks: {
strong: (p) => {
return <strong className="font-heading text-title-70">{p.children}</strong>;
},
em: (p) => {
return <em className="italic">{p.children}</em>;
},
wordsLoop: (p) => {
if (!p.value.words) {
return <span className="font-heading text-title-70">{p.children}</span>;
}
 
return <WordsLoop words={p.value.words} />;
},
},
}}
/>
</div>
);
}
Posted

The only way I found to make it work is useEffect WITHOUT cleanup: 

 

If I apply ctx.revert or useGSAP, it breaks my nested gsap code. 

Can anyone help me understand what's going on?

Also, would it be a problem for a single landing page if I don't clean the context at all? The only problem I see at the moment is that the code runs twice in dev mode.

 

useEffect(() => {
gsap.registerPlugin(SplitText);
 
// doesn't work with context cleanup
 
// let ctx = gsap.context(() => {
if (!headlineRef.current) return;
new SplitText(headlineRef.current?.querySelector("p"), { type: "lines" });
// });
 
// return () => ctx.revert();
}, []);
useGSAP(
() => {
// doesn't work with useGSAP
// if (!headlineRef.current) return;
// new SplitText(headlineRef.current?.querySelector("p"), { type: "lines" });
},
{ scope: headlineRef }
);
Posted

Without a minimal demo, it's very difficult to troubleshoot; the issue could be caused by CSS, markup, a third party library, a 3rd party script, etc. Would you please provide a very simple CodePen or Stackblitz that illustrates the issue? 

 

Please don't include your whole project. Just some colored <div> elements and the GSAP code is best. See if you can recreate the issue with as few dependencies as possible. Start minimal and then incrementally add code bit by bit until it breaks. Usually people solve their own issues during this process! If not, at least we have a reduced test case which greatly increases your chances of getting a relevant answer.

 

See the Pen aYYOdN by GreenSock (@GreenSock) on CodePen.

that loads all the plugins. Just click "fork" at the bottom right and make your minimal demo

 

Using a framework/library like React, Vue, Next, etc.? 

CodePen isn't always ideal for these tools, so here are some Stackblitz starter templates that you can fork and import the gsap-trial NPM package for using any of the bonus plugins: 

 

Please share the StackBlitz link directly to the file in question (where you've put the GSAP code) so we don't need to hunt through all the files. 

 

Once we see an isolated demo, we'll do our best to jump in and help with your GSAP-specific questions. 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...