andrisuvorov Posted March 10 Share Posted March 10 Hello dear GSAP community. I face a challenge, trying to use "useGSAP" hook and define one reference to main element as scope. I would like to call functions from useGSAP hook but... i still want to respect scope for element selectors. When i target any element to perform .to(), i would like to search that element inside my scope. Currently i feel like i am breaking the code with given approach and once i call functions outside useGSAP then scope is also lost. Can someone aid me with this logic to make right decisions. Thank you to all great professionals out there 🙏 const Home = () => { const mainRef = useRef(null); ... // example of usage const func1 = (tl: Timeline, mainRef: MainRefType) => { const homeIntroSubTitle = `${mainRef.current} .home__intro h2`; const homeIntroButton = `${mainRef.current} .home__intro-button`; const homeFeatureItem = `${mainRef.current} .home__feature-item`; gsap.set([homeIntroSubTitle, homeIntroButton, homeFeatureItem], { y: -10, opacity: 0 }); tl.to(homeIntroSubTitle, { y: 0, opacity: 1, duration: 1 }, 0); tl.to(homeIntroButton, { y: 0, opacity: 1, duration: 1 }, 0); tl.to(homeFeatureItem, { y: 0, opacity: 1, duration: 1, stagger: 0.3 }, 0); } useGSAP(() => { const tl = gsap.timeline(); func1(tl, mainRef); func2(mainRef); func3(mainRef); }, { scope: mainRef }); return ( <main className='home' ref={mainRef}> ... ) } Link to comment Share on other sites More sharing options...
GSAP Helper Posted March 10 Share Posted March 10 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: React (please read this article!) Next Svelte Sveltekit Vue Nuxt 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. ✅ Link to comment Share on other sites More sharing options...
GreenSock Posted March 10 Share Posted March 10 No no, you don't need to do that. This is completely invalid: const homeIntroSubTitle = `${mainRef.current} .home__intro h2`; Because you're having JavaScript convert mainRef.current into a STRING. Try to console.log() what you're getting there and you'll see what I mean - that's not a valid selector string. Since you defined your scope as mainRef, that's all you need to do - GSAP will automatically scope things so that it only selects DESCENDANTS of that element which it looks like you were trying to do in a roundabout way in your selector text concoction. So for example, you'd simply change it to: const homeIntroSubTitle = ".home__intro h2"; That's because your function is running WHILE the useGSAP() hook function is executing, so all selector text is scoped accordingly during that execution. If you're still having trouble, please provide a minimal demo so we can see what's going on. Link to comment Share on other sites More sharing options...
andrisuvorov Posted March 11 Author Share Posted March 11 21 hours ago, GreenSock said: No no, you don't need to do that. This is completely invalid: const homeIntroSubTitle = `${mainRef.current} .home__intro h2`; Because you're having JavaScript convert mainRef.current into a STRING. Try to console.log() what you're getting there and you'll see what I mean - that's not a valid selector string. Since you defined your scope as mainRef, that's all you need to do - GSAP will automatically scope things so that it only selects DESCENDANTS of that element which it looks like you were trying to do in a roundabout way in your selector text concoction. So for example, you'd simply change it to: const homeIntroSubTitle = ".home__intro h2"; That's because your function is running WHILE the useGSAP() hook function is executing, so all selector text is scoped accordingly during that execution. If you're still having trouble, please provide a minimal demo so we can see what's going on. Indeed, i made a silly mistake with ${mainRef.current} as this returns the element object. But, if i understand correctly, then i can safely call functions from within useGSAP with defined scope and all these functions that target elements, will still respect the scope from useGSAP? If that is so, then i can call my functions as i did and i can just use "standard" selector without messing with ref passing. Thank you, Jack from GreenSock 🤝🏻 Link to comment Share on other sites More sharing options...
Solution GreenSock Posted March 11 Solution Share Posted March 11 8 minutes ago, andrisuvorov said: If that is so, then i can call my functions as i did and i can just use "standard" selector without messing with ref passing. Yes, that's exactly what I was saying. Any selector text used in GSAP-related stuff created while the useGSAP() hook function is executing will be locked to that scope. So it's fine if it's in external functions as long as those are called while that useGSAP() hook function is executing. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now