signal-desktop/ts/hooks/usePageVisibility.ts
2021-10-07 13:52:07 -05:00

28 lines
627 B
TypeScript

// Copyright 2020-2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { useEffect, useState } from 'react';
export function usePageVisibility(): boolean {
const [result, setResult] = useState(!document.hidden);
useEffect(() => {
const updatePageVisibility = () => {
setResult(!document.hidden);
};
updatePageVisibility();
document.addEventListener('visibilitychange', updatePageVisibility, false);
return () => {
document.removeEventListener(
'visibilitychange',
updatePageVisibility,
false
);
};
}, []);
return result;
}