signal-desktop/ts/hooks/usePageVisibility.ts

29 lines
627 B
TypeScript
Raw Normal View History

2021-09-17 22:24:21 +00:00
// 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 = () => {
2021-09-17 22:24:21 +00:00
setResult(!document.hidden);
};
updatePageVisibility();
document.addEventListener('visibilitychange', updatePageVisibility, false);
2021-09-17 22:24:21 +00:00
return () => {
document.removeEventListener(
'visibilitychange',
updatePageVisibility,
2021-09-17 22:24:21 +00:00
false
);
};
}, []);
return result;
}