jlmakes / scrollreveal

Animate elements as they scroll into view.
https://scrollrevealjs.org/
22.24k stars 2.26k forks source link

ScrollReveal not working with Next JS (create-next-app) ? #541

Open wistie opened 3 years ago

wistie commented 3 years ago

I have some issues using ScrollReveal with create-next-app.

I installed ScrollReveal with npm, then imported it using ES6 :

import sr from 'scrollreveal';

I immediatly have an error message :

Server Error ReferenceError: document is not defined

I use ScrollReveal with create-react-app, and it works perfectly. So I don't understand.

Thanks.

Environment

antwash commented 2 years ago

This issue is happening because the DOM is only available inside the browser client side and not server side. Nextjs is executing your code server side. To get around the error you can leverage nextjs dynamic imports feature and a react useEffect hook. This will ensure the DOM is available before importing scrollreveal

const MyComponent = () => { 
 const refToComponent = React.useRef(null)

  useEffect(() => {
    async function animate() {
      if (refToComponent.current) {
        const sr = (await import("scrollreveal")).default
        sr().reveal(refToComponent.current)
      }
    }
    animate()
  }, [])

   return (
     <div ref={refToComponent}>HEY THERE!</div>
   )
}

Hope this helps 😄

ritmillio commented 1 year ago

If anyone facing the same issue, I have created a helper package to use Scrollreveal with Next.js. You can check out here.

MQDXL commented 1 year ago

use sr().reveal(refToComponent.current, {reset: true}), scroll back to the revealed element, the animation can't animate again, Do you have a solution to solve the bug?

const MyComponent = () => { 
 const refToComponent = React.useRef(null)

  useEffect(() => {
    async function animate() {
      if (refToComponent.current) {
        const sr = (await import("scrollreveal")).default
        sr().reveal(refToComponent.current, {reset: true})
      }
    }
    animate()
  }, [])

   return (
     <div ref={refToComponent}>HEY THERE!</div>
   )
}
ritmillio commented 1 year ago

Hey @MQDXL , due to the nature of Next13/React 18 (Now React is a server side lib) -> you cannot grab elements from the DOM without specifying the 'use client' at the top of the component/page. Also rather than this implementation you may consider next-reveal: https://github.com/ritmillio/next-reveal which provides you with a RevealWrapper for animating single elements and a RevealList to animate multiple items in a sequence.

MQDXL commented 1 year ago

Hey @MQDXL , due to the nature of Next13/React 18 (Now React is a server side lib) -> you cannot grab elements from the DOM without specifying the 'use client' at the top of the component/page. Also rather than this implementation you may consider next-reveal: https://github.com/ritmillio/next-reveal which provides you with a RevealWrapper for animating single elements and a RevealList to animate multiple items in a sequence.

@ritmillio thanks bro, I found my code error.that's the element's parent set height: 100%. say thanks to you again .