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

Not working on gatsby v2 #8

Open
vlinas opened this issue May 8, 2020 · 7 comments
Open

Not working on gatsby v2 #8

vlinas opened this issue May 8, 2020 · 7 comments

Comments

@vlinas
Copy link

vlinas commented May 8, 2020

Hey guys, I can't make this plugin to work...

When I import sal.css from node_modules every div with data-sal properties just disappear. Am I missing something?

import React from "react"
import Images from '../images'
import '../../../node_modules/sal.js/dist/sal.css'

const ProjectsBox = props => (
    <div data-sal="slide-up" data-sal-delay="300" data-sal-easing="ease">
        <Images filename="test.jpg"/>
    </div>
);

export default ProjectsBox;
@barjuandavis
Copy link

barjuandavis commented May 20, 2020

Have you configured your gatsby-config.js? And btw you don't have to import sal.css. It should work from the get go when configured properly.

@vlinas
Copy link
Author

vlinas commented May 20, 2020

Have you configured your gatsby-config.js? And btw you don't have to import sal.css. It should work from the get go when configured properly.

Yes, I added it in gatsby-config.js and tried configuring it with and without any options with no luck..

@solubletech
Copy link
Collaborator

Hey @vlinas! Can you share your gatsby-config.js file?

@vlinas
Copy link
Author

vlinas commented May 25, 2020

Hey @vlinas! Can you share your gatsby-config.js file?

Hey @solubletech , here's my gatsby-config.js

module.exports = {
  siteMetadata: {
    title: `Linas`,
    description: `webdev`,
    author: `@linasv`,
  },
  plugins: [
    `gatsby-plugin-react-helmet`,
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `images`,
        path: `${__dirname}/src/images`,
      },
    },
    {
      resolve: "gatsby-source-filesystem",
      options: {
        path: `${__dirname}/src/pages`,
        name: "pages"
      }
    },
    `gatsby-transformer-sharp`,
    `gatsby-plugin-sharp`,
    `gatsby-plugin-sass`,
    'gatsby-plugin-dark-mode',
    `gatsby-plugin-scroll-reveal`,
    {
      resolve: `gatsby-plugin-manifest`,
      options: {
        name: `gatsby-starter-default`,
        short_name: `starter`,
        start_url: `/`,
        background_color: `#663399`,
        theme_color: `#663399`,
        display: `minimal-ui`,
        // icon: `src/images/gatsby-icon.png`, // This path is relative to the root of the site.
      },
    },
    // this (optional) plugin enables Progressive Web App + Offline functionality
    // To learn more, visit: https://gatsby.dev/offline
    // `gatsby-plugin-offline`,
  ],
}

@vlinas
Copy link
Author

vlinas commented May 27, 2020

@solubletech I found the issue here. Scroll reveal doesn't work if div is wrapped in react-lazyload component

@DRD161
Copy link

DRD161 commented Jun 14, 2020

I'm having the same issue as well using Styled Components. Could that cause an issue?

Gatsby Config:

module.exports = {
  siteMetadata: {
    title: `Dylan's portfolio`,
    description: `Dylan Davenport's portfolio`,
    author: `Dylan Davenport`,
  },
  plugins: [
    `gatsby-plugin-sass`,
    `gatsby-plugin-react-helmet`,
    `gatsby-plugin-scroll-reveal`,
    {
      resolve: `gatsby-plugin-typography`,
      options: {
        pathToConfigModule: `src/utils/typography`,
      },
    },
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `images`,
        path: `${__dirname}/src/images`,
      },
    },
    `gatsby-transformer-sharp`,
    `gatsby-plugin-sharp`,
    {
      resolve: `gatsby-plugin-manifest`,
      options: {
        name: `Dylan's portfolio`,
        short_name: `portfolio`,
        start_url: `/`,
        background_color: `#663399`,
        theme_color: `#663399`,
        display: `minimal-ui`,
        // icon: `src/images/gatsby-icon.png`, // This path is relative to the root of the site.
      },
    },
    // this (optional) plugin enables Progressive Web App + Offline functionality
    // To learn more, visit: https://gatsby.dev/offline
    // `gatsby-plugin-offline`,
  ],
}

@coreyward
Copy link

@vlinas @DRD161 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, leading to some of the issues you're experiencing with client-side rendered elements (e.g. LazyLoad) or JS-based CSS solutions (StyledComponents).

Here's an example using a lightweight Intersection Observer wrapper:

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

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

  return (
    <div
      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}
    />
  )
}

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

5 participants