mirror of
https://github.com/cheeaun/phanpy.git
synced 2025-01-03 16:17:19 +03:00
15 lines
477 B
JavaScript
15 lines
477 B
JavaScript
|
import { useEffect } from 'preact/hooks';
|
||
|
|
||
|
export default function usePageVisibility(fn = () => {}, deps = []) {
|
||
|
useEffect(() => {
|
||
|
const handleVisibilityChange = () => {
|
||
|
const hidden = document.hidden || document.visibilityState === 'hidden';
|
||
|
fn(!hidden);
|
||
|
};
|
||
|
|
||
|
document.addEventListener('visibilitychange', handleVisibilityChange);
|
||
|
return () =>
|
||
|
document.removeEventListener('visibilitychange', handleVisibilityChange);
|
||
|
}, [fn, ...deps]);
|
||
|
}
|