Files
codeaashu-claude-code/web/hooks/useReducedMotion.ts
ashutoshpythoncs@gmail.com b564857c0b claude-code
2026-03-31 18:58:05 +05:30

28 lines
892 B
TypeScript

"use client";
import { useEffect, useState } from "react";
/**
* Returns true when the user has requested reduced motion via OS settings.
* Use this to disable or simplify animations for users who need it.
*
* @example
* const reducedMotion = useReducedMotion();
* <div className={reducedMotion ? "" : "animate-fade-in"}>...</div>
*/
export function useReducedMotion(): boolean {
const [reducedMotion, setReducedMotion] = useState(() => {
if (typeof window === "undefined") return false;
return window.matchMedia("(prefers-reduced-motion: reduce)").matches;
});
useEffect(() => {
const mq = window.matchMedia("(prefers-reduced-motion: reduce)");
const handler = (e: MediaQueryListEvent) => setReducedMotion(e.matches);
mq.addEventListener("change", handler);
return () => mq.removeEventListener("change", handler);
}, []);
return reducedMotion;
}