vben-admin-thin-next/src/hooks/web/useVisibilityState.ts
2020-09-28 20:19:10 +08:00

21 lines
584 B
TypeScript

import { ref, onUnmounted, computed } from '@vue/runtime-dom';
import { isDef } from '/@/utils/is';
export default function useVisibilityState() {
const refVisibility = ref(true);
if (isDef(document) && isDef(document.visibilityState)) {
const setVisibility = () => {
refVisibility.value = document.visibilityState === 'visible';
};
document.addEventListener('visibilitychange', setVisibility, false);
onUnmounted(() => {
document.removeEventListener('visibilitychange', setVisibility);
});
}
return computed(() => refVisibility.value);
}