Last active
February 1, 2022 19:37
-
-
Save PaulieScanlon/295ba4267d44f7974bd914bff70663c0 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 { useState, useEffect } from 'react' | |
const QUERY_STRING = '(prefers-reduced-motion: no-preference)' | |
const EVENT = 'change' | |
const usePrefersReducedMotion = () => { | |
const [prefersReducedMotion, setPrefersReducedMotion] = useState(true) | |
useEffect(() => { | |
const query = window.matchMedia(QUERY_STRING) | |
setPrefersReducedMotion(!window.matchMedia(QUERY_STRING).matches) | |
const setState = (event) => { | |
setPrefersReducedMotion(!event.matches) | |
} | |
query.addEventListener(EVENT, setState) | |
return () => { | |
query.removeEventListener(EVENT, setState) | |
} | |
}, []) | |
return prefersReducedMotion | |
} | |
export default usePrefersReducedMotion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment