Skip to content

Commit 2bc42f2

Browse files
committed
refactor: improve prefersReducedMotion function
1 parent 16dd6de commit 2bc42f2

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

library/src/utils/index.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ export const generateRandomId = () => Math.floor(Math.random() * 1000000);
99
export const prefersReducedMotion = (() => {
1010
let shouldReduceMotion: boolean | undefined = undefined;
1111
return () => {
12-
if (shouldReduceMotion === undefined && typeof window !== 'undefined') {
13-
const mediaQuery = matchMedia('(prefers-reduced-motion: reduce)');
14-
shouldReduceMotion = !mediaQuery || mediaQuery.matches;
12+
if (shouldReduceMotion === undefined) {
13+
if (typeof window !== 'undefined' && window.matchMedia !== undefined) {
14+
const mediaQuery = window.matchMedia(
15+
'(prefers-reduced-motion: reduce)',
16+
);
17+
shouldReduceMotion = mediaQuery.matches;
18+
} else {
19+
shouldReduceMotion = false;
20+
}
1521
}
1622
return shouldReduceMotion;
1723
};

0 commit comments

Comments
 (0)