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

Plugin don't work at all. #6

Open
rzubrycki opened this issue Apr 3, 2020 · 6 comments
Open

Plugin don't work at all. #6

rzubrycki opened this issue Apr 3, 2020 · 6 comments

Comments

@rzubrycki
Copy link

Hi, i followed your example, but nothing happens when scrolling.

@solubletech
Copy link
Collaborator

Hi @rzubrycki, can you explain us more about your problem?.
Maybe this plugin fail if you are using other plugins like transition-link.

@rzubrycki
Copy link
Author

rzubrycki commented Apr 3, 2020

No, i don't use it.
this is my dependencies:

"dependencies": {
    "@rhysforyou/gatsby-plugin-react-helmet-async": "^0.1.3",
    "formik": "^2.1.4",
    "gatsby": "^2.20.8",
    "gatsby-background-image": "^0.10.2",
    "gatsby-image": "^2.3.1",
    "gatsby-plugin-google-analytics": "^2.2.2",
    "gatsby-plugin-manifest": "^2.3.3",
    "gatsby-plugin-offline": "^3.1.2",
    "gatsby-plugin-robots-txt": "^1.5.0",
    "gatsby-plugin-sass": "^2.2.1",
    "gatsby-plugin-sharp": "^2.5.3",
    "gatsby-plugin-sitemap": "^2.3.1",
    "gatsby-source-filesystem": "^2.2.2",
    "gatsby-transformer-sharp": "^2.4.3",
    "node-sass": "^4.13.1",
    "react": "^16.13.1",
    "react-dom": "^16.13.1",
    "react-helmet-async": "^1.0.4",
    "semantic-ui-css": "^2.4.1",
    "semantic-ui-react": "^0.88.2",
    "typeface-alata": "^1.1.3",
    "yup": "^0.28.3"
  },

After start of an app, i see ease-in animation on header and then nothing happen on scroll.
Chrome Version 80.0.3987.163

@solubletech
Copy link
Collaborator

Hi! We can't see the plugin in the dependencies. Can you show your gatsby-config.js plugins?

@coreyward
Copy link

coreyward commented Sep 24, 2020

@rzubrycki You might have better luck with a different solution. There's nothing Gatsby-specific about this behavior, and the dependency doing the heavy lifting uses a very non-React approach.

import { useInView } from "react-intersection-observer"

const AnimateIn = ({ threshold = 0.15, triggerOnce = false, ...remainingProps }) => {
  const [ref, inView] = useInView({ threshold, triggerOnce })

  return (
    <div
      ref={ref}
      style={{
        // adjust these as desired
        transition: "opacity 300ms, transform 300ms",
        opacity: inView ? 1 : 0,
        transform: `translateY(${inView ? 0 : 100}px)`,
      }}
      {...remainingProps}
    />
  )
}

Then just wrap any components you want entrance animations on with this component, overriding the default threshold and triggerOnce values as desired:

<AnimateIn triggerOnce={true}>
  <h1>Hi Mom!</h1>
</AnimateIn>

This is much easier to reason about, doesn't couple itself to Gatsby, and you can easily extend or modify it to do anything else you need, all without adding a bunch of additional dependencies to your client-side code. You can even use this approach to auto-play videos when they're in view and pause them when they're not:

import { useInView } from "react-intersection-observer"

const AutoPlayingVideo = ({ threshold = 0.15, ...playerProps }) => {
  const [ref, inView] = useInView({ threshold })

  useEffect(() => {
    if (inView) {
      ref.current?.play()
    } else {
      ref.current?.pause()
    }
  }, [ref, inView])

  return (
    <video
      style={{
        transition: "opacity 300ms, transform 300ms",
        opacity: inView ? 1 : 0,
        transform: `translateY(${inView ? 0 : 100}px)`,
      }}
      ref={ref}
      autoPlay
      playsInline
      muted
      loop
      {...playerProps}
    />
  )
}

@Trunksome
Copy link

@coreyward Thank you so much. I experienced strange problems with the plugin - your approach works much better.
In your first snipped though, you forgot to add "ref={ref}" to the div.

@coreyward
Copy link

@Trunksome Fixed!

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

No branches or pull requests

4 participants