diff --git a/packages/vant-use/src/usePageVisibility/index.ts b/packages/vant-use/src/usePageVisibility/index.ts new file mode 100644 index 000000000..51f4dc82b --- /dev/null +++ b/packages/vant-use/src/usePageVisibility/index.ts @@ -0,0 +1,18 @@ +import { ref } from 'vue'; +import { inBrowser } from '../shared'; +import { useEventListener } from '../useEventListener'; + +export function usePageVisibility() { + const visibility = ref('visible'); + + const setVisibility = () => { + if (inBrowser) { + visibility.value = document.hidden ? 'hidden' : 'visible'; + } + }; + + setVisibility(); + useEventListener('visibilitychange', setVisibility); + + return visibility; +}