27 lines
636 B
TypeScript
27 lines
636 B
TypeScript
|
|
"use client";
|
||
|
|
|
||
|
|
import { useEffect, useState } from "react";
|
||
|
|
|
||
|
|
export function usePrefersReducedMotion() {
|
||
|
|
const [prefersReducedMotion, setPrefersReducedMotion] = useState(false);
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
if (typeof window === "undefined" || !window.matchMedia) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
const mediaQuery = window.matchMedia("(prefers-reduced-motion: reduce)");
|
||
|
|
const apply = () => {
|
||
|
|
setPrefersReducedMotion(mediaQuery.matches);
|
||
|
|
};
|
||
|
|
|
||
|
|
apply();
|
||
|
|
mediaQuery.addEventListener("change", apply);
|
||
|
|
return () => {
|
||
|
|
mediaQuery.removeEventListener("change", apply);
|
||
|
|
};
|
||
|
|
}, []);
|
||
|
|
|
||
|
|
return prefersReducedMotion;
|
||
|
|
}
|