Skip to content

Instantly share code, notes, and snippets.

@duarten
Created July 10, 2020 19:48
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 duarten/d9467b5e4154c658f0010c4a6103c70f to your computer and use it in GitHub Desktop.
Save duarten/d9467b5e4154c658f0010c4a6103c70f to your computer and use it in GitHub Desktop.
export function useAnimationDelay(
delay: number,
shouldShow: boolean,
onHide: () => void = () => {
return
},
): boolean {
const [display, setDisplay] = useState(shouldShow)
useEffect(() => {
if (shouldShow) {
setDisplay(true)
return _.identity
}
// Wait for the animation to end before
// removing the element.
let tm: number
if (display) {
tm = window.setTimeout(() => {
setDisplay(false)
onHide()
}, delay)
}
return (): void => {
clearTimeout(tm)
}
}, [delay, display, shouldShow, onHide])
return display
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment