signal-desktop/ts/hooks/useReducedMotion.ts

28 lines
812 B
TypeScript
Raw Normal View History

// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { useEffect, useState } from 'react';
2023-08-04 15:17:12 +00:00
// Allows this to work in Node process
let reducedMotionQuery: MediaQueryList;
function getReducedMotionQuery() {
if (reducedMotionQuery == null) {
reducedMotionQuery = window.matchMedia('(prefers-reduced-motion)');
}
return reducedMotionQuery;
}
export function useReducedMotion(): boolean {
2023-08-04 15:17:12 +00:00
const [matches, setMatches] = useState(getReducedMotionQuery().matches);
useEffect(() => {
2023-08-04 15:17:12 +00:00
function onChange(event: MediaQueryListEvent) {
setMatches(event.matches);
}
2023-08-04 15:17:12 +00:00
getReducedMotionQuery().addEventListener('change', onChange);
return () => {
2023-08-04 15:17:12 +00:00
getReducedMotionQuery().removeEventListener('change', onChange);
};
2023-08-04 15:17:12 +00:00
}, []);
return matches;
}