Skip to content

Instantly share code, notes, and snippets.

@tom2strobl
Created July 10, 2019 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tom2strobl/5603c62e817281cdc2b601a50fa098e6 to your computer and use it in GitHub Desktop.
Save tom2strobl/5603c62e817281cdc2b601a50fa098e6 to your computer and use it in GitHub Desktop.
import { useState, setState, useRef } from "react";
import Observer from "@researchgate/react-intersection-observer";
import anime from "animejs";
import { ANIMATIONS } from "constants";
function handleIntersection(event, ref, delay, callback) {
if (event.isIntersecting) {
callback();
anime.set(ref, { opacity: 0 });
anime({
targets: ref,
opacity: [0, 1],
translateY: [25, 0],
duration: ANIMATIONS.DURATION,
easing: ANIMATIONS.EASING,
delay: delay || 0
});
}
}
const RevealAnimation = ({ children, delay }) => {
const [animationState, setAnimationState] = useState(false);
const containerRef = useRef(null);
const options = {
onChange: event =>
handleIntersection(event, containerRef.current, delay, () => {
setAnimationState(true);
}),
disabled: animationState,
rootMargin: "0% 0% -10%"
};
return (
<Observer {...options}>
<div ref={containerRef} style={{ opacity: 0 }}>
{children}
</div>
</Observer>
);
};
export default RevealAnimation;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment