2021-09-29 20:59:37 +00:00
|
|
|
// 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;
|
2021-09-29 20:59:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function useReducedMotion(): boolean {
|
2023-08-04 15:17:12 +00:00
|
|
|
const [matches, setMatches] = useState(getReducedMotionQuery().matches);
|
2021-09-29 20:59:37 +00:00
|
|
|
useEffect(() => {
|
2023-08-04 15:17:12 +00:00
|
|
|
function onChange(event: MediaQueryListEvent) {
|
|
|
|
setMatches(event.matches);
|
2021-09-29 20:59:37 +00:00
|
|
|
}
|
2023-08-04 15:17:12 +00:00
|
|
|
getReducedMotionQuery().addEventListener('change', onChange);
|
2021-09-29 20:59:37 +00:00
|
|
|
return () => {
|
2023-08-04 15:17:12 +00:00
|
|
|
getReducedMotionQuery().removeEventListener('change', onChange);
|
2021-09-29 20:59:37 +00:00
|
|
|
};
|
2023-08-04 15:17:12 +00:00
|
|
|
}, []);
|
|
|
|
return matches;
|
2021-09-29 20:59:37 +00:00
|
|
|
}
|