mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-05-11 01:38:56 +08:00
24 lines
502 B
TypeScript
24 lines
502 B
TypeScript
import { ref, Ref } from 'vue';
|
|
import { inBrowser } from '../utils';
|
|
|
|
type VisibilityState = 'hidden' | 'visible';
|
|
|
|
let visibility: Ref<VisibilityState>;
|
|
|
|
export function usePageVisibility() {
|
|
if (!visibility) {
|
|
visibility = ref<VisibilityState>('visible');
|
|
|
|
if (inBrowser) {
|
|
const update = () => {
|
|
visibility.value = document.hidden ? 'hidden' : 'visible';
|
|
};
|
|
|
|
update();
|
|
window.addEventListener('visibilitychange', update);
|
|
}
|
|
}
|
|
|
|
return visibility;
|
|
}
|