Created
June 10, 2022 07:55
-
-
Save stekhn/11ec30523c79b146a4755bb4d67b5baa to your computer and use it in GitHub Desktop.
React hook to disable animations for users which prefer reduced motion
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, useEffect } from "react"; | |
export function useReducedMotion() { | |
const [matches, setMatch] = useState( | |
window.matchMedia("(prefers-reduced-motion: reduce)").matches | |
); | |
useEffect(() => { | |
const mediaQuery = window.matchMedia("(prefers-reduced-motion: reduce)"); | |
const handleChange = () => { | |
setMatch(mediaQuery.matches); | |
}; | |
handleChange(); | |
mediaQuery.addEventListener("change", handleChange); | |
return () => { | |
mediaQuery.removeEventListener("change", handleChange); | |
}; | |
}, []); | |
return matches; | |
} | |
// Usage | |
// const prefersReducedMotion = useReducedMotion(); | |
// useEffect(() => { | |
// Globals.assign({ | |
// skipAnimation: prefersReducedMotion, | |
// }); | |
// }, [prefersReducedMotion]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment