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

Issue integrating with GSAP and Next.js #319

Open
rnrh opened this issue May 18, 2021 · 2 comments
Open

Issue integrating with GSAP and Next.js #319

rnrh opened this issue May 18, 2021 · 2 comments

Comments

@rnrh
Copy link

rnrh commented May 18, 2021

Environment

  • Browser: All
  • Version of smooth-scrollbar: 8.6.2

Issue Summary

I am using Smooth Scrollbar inside a Next.js project with GSAP (animation library). I am unable to get a nested component with an animation inside of it to render before Smooth Scrollbar, which means It is not available on load, only after a hot reload/refresh locally.

Current Behavior

If you go to this CodeSandbox you will see the effect as it is intended. The text should be pinned to the top of the viewport as your scroll with a 100px transform on the x-axis. This only works as I have a setTimeout() around the GSAP animation, though. This is forcing things to load so that Smooth Scrollbar goes first, then the GSAP animation instance.

The setTimeout() thing seems really bad and hacky, but you will see that if you remove it and refresh the page, the effect no works, but if you change the "end" value in the scrollTrigger object inside the animation object, this will trigger a hot reload, which will now render the effect correctly. I believe this works because React is only reloading the component, so the Smooth Scrollbar instance already exists, and now things have happened in the right order

Expected Behavior

Remove the setTimeout(), save and reload, and the effect will no longer work. You will need to trigger a hot reload on the component in order for the effect to be applied.

Steps to Reproduce

  1. Go to the navigation.js file, remove the setTimeout(), save, and refresh.
  2. [Effect is no longer working] Go into navigation.js and update the 'end' value in the scrollTrigger object to some other number, save, scroll back to page top, scroll down, and see the effect is applied.

Online demo

CodeSandbox

@tdaulbaev
Copy link

you will need initialize scrollTrigger animation after scrollbar initialized.

something like:

  React.useEffect(() => {
    if (isScrollInit) {
      initScrollTriggerAnimation();
    }
  }, [isScrollInit])

@sadeghbarati
Copy link
Collaborator

Next.js 13

#503 (reply in thread)

'use client';
import gsap from 'gsap'
import ScrollTrigger' from 'gsap/scrolltrigger'

gsap.registerPlugin(ScrollTrigger)

Next.js 12

  • useEffect + typeof window !== 'undefined'
import gsap from 'gsap'
import ScrollTrigger' from 'gsap/scrolltrigger'

if (typeof window !== 'undefined') {
  // client-side-only code
  gsap.registerPlugin(ScrollTrigger)
}

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

No branches or pull requests

4 participants