From 36b8748552159f33795ff7deaf4391425b529b27 Mon Sep 17 00:00:00 2001 From: chenjiahan Date: Mon, 14 Sep 2020 18:50:22 +0800 Subject: [PATCH] feat(use): usePageVisibility --- .../vant-use/src/usePageVisibility/index.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 packages/vant-use/src/usePageVisibility/index.ts 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; +}