Skip to content

Commit

Permalink
refactor: improve prefersReducedMotion function
Browse files Browse the repository at this point in the history
  • Loading branch information
pheralb committed Jul 23, 2024
1 parent 16dd6de commit 2bc42f2
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions library/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,15 @@ export const generateRandomId = () => Math.floor(Math.random() * 1000000);
export const prefersReducedMotion = (() => {
let shouldReduceMotion: boolean | undefined = undefined;
return () => {
if (shouldReduceMotion === undefined && typeof window !== 'undefined') {
const mediaQuery = matchMedia('(prefers-reduced-motion: reduce)');
shouldReduceMotion = !mediaQuery || mediaQuery.matches;
if (shouldReduceMotion === undefined) {
if (typeof window !== 'undefined' && window.matchMedia !== undefined) {
const mediaQuery = window.matchMedia(
'(prefers-reduced-motion: reduce)',
);
shouldReduceMotion = mediaQuery.matches;
} else {
shouldReduceMotion = false;
}
}
return shouldReduceMotion;
};
Expand Down

0 comments on commit 2bc42f2

Please sign in to comment.