2023-02-08 00:31:46 +08:00
|
|
|
import { useEffect } from 'preact/hooks';
|
|
|
|
|
|
|
|
export default function usePageVisibility(fn = () => {}, deps = []) {
|
|
|
|
useEffect(() => {
|
|
|
|
const handleVisibilityChange = () => {
|
|
|
|
const hidden = document.hidden || document.visibilityState === 'hidden';
|
2023-02-08 19:11:33 +08:00
|
|
|
console.log('👀 Page visibility changed', hidden);
|
2023-02-08 00:31:46 +08:00
|
|
|
fn(!hidden);
|
|
|
|
};
|
|
|
|
|
|
|
|
document.addEventListener('visibilitychange', handleVisibilityChange);
|
|
|
|
return () =>
|
|
|
|
document.removeEventListener('visibilitychange', handleVisibilityChange);
|
|
|
|
}, [fn, ...deps]);
|
|
|
|
}
|