open-navigator / web_app /src /hooks /usePrefersReducedMotion.ts
jcbowyer's picture
Clean HuggingFace deployment without binary files
a8fb296
Raw History Blame Contribute Delete
811 Bytes
import { useEffect, useState } from 'react'
const QUERY = '(prefers-reduced-motion: reduce)'
/** True when the visitor asked the OS for reduced motion; updates if the setting changes while the page is open. */
export function usePrefersReducedMotion(): boolean {
const get = () => typeof window !== 'undefined' && typeof window.matchMedia === 'function' && window.matchMedia(QUERY).matches
const [reduced, setReduced] = useState<boolean>(get)
useEffect(() => {
if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') return
const mq = window.matchMedia(QUERY)
const on = () => setReduced(mq.matches)
on()
mq.addEventListener('change', on)
return () => mq.removeEventListener('change', on)
}, [])
return reduced
}
export default usePrefersReducedMotion