signal-desktop/ts/hooks/usePageVisibility.ts

29 lines
622 B
TypeScript
Raw Normal View History

2023-01-03 19:55:46 +00:00
// Copyright 2020 Signal Messenger, LLC
2021-09-17 22:24:21 +00:00
// 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;
}