Skip to content

Commit db737e3

Browse files
committed
Added useReducedMotionPreference
1 parent 1e54910 commit db737e3

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/hooks/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from "./useColorSchemePreference";
22
export * from "./useFloating";
33
export * from "./useFormValues";
4+
export * from "./useReducedMotionPreference";
45
export * from "./useWindowSize";
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { useEffect, useRef, useState } from "react";
2+
3+
/** Provides the user's reduced motion preference and subscribes to changes. */
4+
export const useReducedMotionPreference = (): boolean => {
5+
const match = useRef<MediaQueryList>(
6+
window.matchMedia("(prefers-reduced-motion)"),
7+
);
8+
const [isReducedPreferred, setIsReducedPreferred] = useState(
9+
!!match.current.matches,
10+
);
11+
12+
useEffect(() => {
13+
const currentMatch = match.current;
14+
const onChange = () => {
15+
setIsReducedPreferred(!!currentMatch.matches);
16+
};
17+
18+
currentMatch.addEventListener("change", onChange);
19+
return () => currentMatch.removeEventListener("change", onChange);
20+
}, [match]);
21+
22+
return isReducedPreferred;
23+
};

0 commit comments

Comments
 (0)