Skip to content

Instantly share code, notes, and snippets.

@PaulieScanlon
Last active February 1, 2022 19:37
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 PaulieScanlon/295ba4267d44f7974bd914bff70663c0 to your computer and use it in GitHub Desktop.
Save PaulieScanlon/295ba4267d44f7974bd914bff70663c0 to your computer and use it in GitHub Desktop.
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