signal-desktop/ts/hooks/useDevicePixelRatio.ts
2022-01-05 09:59:59 -08:00

27 lines
621 B
TypeScript

// 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;
}