Created
July 10, 2019 12:10
-
-
Save tom2strobl/5603c62e817281cdc2b601a50fa098e6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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