feat(use): usePageVisibility

This commit is contained in:
chenjiahan 2020-09-14 18:50:22 +08:00
parent 1607aa8ba7
commit 36b8748552

View File

@ -0,0 +1,18 @@
import { ref } from 'vue';
import { inBrowser } from '../shared';
import { useEventListener } from '../useEventListener';
export function usePageVisibility() {
const visibility = ref<VisibilityState>('visible');
const setVisibility = () => {
if (inBrowser) {
visibility.value = document.hidden ? 'hidden' : 'visible';
}
};
setVisibility();
useEventListener('visibilitychange', setVisibility);
return visibility;
}