signal-desktop/ts/hooks/useDevicePixelRatio.ts

28 lines
621 B
TypeScript
Raw Normal View History

2022-01-05 17:59:59 +00:00
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { useEffect, useState } from 'react';
export function useDevicePixelRatio(): number {
const [result, setResult] = useState(window.devicePixelRatio);
useEffect(() => {
const update = () => {
setResult(window.devicePixelRatio);
};
update();
const mediaQuery = window.matchMedia(
`screen and (resolution: ${result}dppx)`
);
mediaQuery.addEventListener('change', update);
return () => {
mediaQuery.removeEventListener('change', update);
};
}, [result]);
return result;
}