Jump to content
Search Community

Selector leak outside scope with SplitText plugin inside gsap.context in React

chacrasoftware test
Moderator Tag

Go to solution Solved by GreenSock,

Recommended Posts

// MySplitText.tsx

const ref = useRef<HTMLDivElement>(null)

const splitText = useRef<SplitText>()

useEffect(() => {
	let ctx = gsap.context(() => {
    	splitText.current = new SplitText('.text',{
        	type: 'chars'
        })
    },ref)
},[])


<div ref={ref}>
	<p className="text">My Text</p>
</div>


/**
* Root component- App.tsx
*/

<div>
	<MySpliteText/>
    <p className="text">My Text at root component</p> // <-- this is got selected
</div>

Does someone also experience this scenario? The selector inside the context is leaking outside its scope, or is there something wrong with instantiation of the `SplitText` plugin? I have to pass it to a `ref` so I can use it on a callback animation.

 

Link to comment
Share on other sites

  • Solution

Great catch, @chacrasoftware. Sorry about any confusion there - SplitText wasn't fully context()-aware in that sense, but the upcoming release (due out VERY soon) is. Previously, SplitText had its own selector code but in the next release it leverages the central one in the GSAP core which is wired into gsap.context() for scoping. Again, I apologize for the confusion that caused. You didn't do anything "wrong". 

 

In the meantime, it's easy to work around by wrapping gsap.utils.toArray() around your selector text: 

// old (but will work in the next release)
splitText.current = new SplitText('.text', { ... });
                                            
// new
splitText.current = new SplitText(gsap.utils.toArray('.text'), { ... });

The trial file (only works on CodePen/CodeSandbox/Stackblitz/locally) has already been updated if you want to try it: https://assets.codepen.io/16327/SplitText3.min.js

  • Like 2
Link to comment
Share on other sites

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...