import { useEffect, useState } from "react"; const QUERY = "(prefers-reduced-motion: reduce)"; function getInitialValue(): boolean { if (typeof window === "undefined" || !window.matchMedia) return false; return window.matchMedia(QUERY).matches; } export function usePrefersReducedMotion(): boolean { const [reduced, setReduced] = useState(getInitialValue); useEffect(() => { if (typeof window === "undefined" || !window.matchMedia) return; const media = window.matchMedia(QUERY); const handler = (event: MediaQueryListEvent) => setReduced(event.matches); setReduced(media.matches); media.addEventListener("change", handler); return () => media.removeEventListener("change", handler); }, []); return reduced; }