import { useSyncExternalStore } from "react"; const REDUCED_MOTION_QUERY = "(prefers-reduced-motion: reduce)"; function subscribeToReducedMotion(callback: () => void): () => void { const query = window.matchMedia(REDUCED_MOTION_QUERY); query.addEventListener("change", callback); return () => query.removeEventListener("change", callback); } function getReducedMotionSnapshot(): boolean { return window.matchMedia(REDUCED_MOTION_QUERY).matches; } function getServerReducedMotionSnapshot(): boolean { return false; } /** * True when the user has requested reduced motion at the OS level. * UI fades and transitions should collapse to 0ms when this is true. */ export function usePrefersReducedMotion(): boolean { return useSyncExternalStore( subscribeToReducedMotion, getReducedMotionSnapshot, getServerReducedMotionSnapshot, ); }