Created
February 28, 2020 02:03
-
-
Save zaydek/bea9397f20a6f1fc78a57c6e39b4a110 to your computer and use it in GitHub Desktop.
This file contains 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 React from "react" | |
const MICRO_DELAY = 25 | |
function useTransition({ ref, state, enterClass, activeClass, durationMs }) { | |
// Once: | |
React.useEffect(() => { | |
ref.current.classList.add(enterClass) | |
ref.current.style.display = "none" | |
}, [ref, enterClass]) | |
// Per change: | |
React.useEffect(() => { | |
if (!state) { | |
ref.current.classList.remove(activeClass) | |
setTimeout(() => { | |
ref.current.style.display = "none" | |
}, durationMs) | |
} else { | |
ref.current.style.display = "" | |
setTimeout(() => { | |
ref.current.classList.add(activeClass) | |
}, MICRO_DELAY) | |
} | |
}, [ref, state, enterClass, activeClass, durationMs]) | |
} | |
export default useTransition |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment