Skip to content

Commit

Permalink
feat(hooks): "新增useDocumentVisibility钩子以检测页面可见性"
Browse files Browse the repository at this point in the history
"新增useDocumentVisibility钩子以检测页面可见性"
  • Loading branch information
Null committed Aug 12, 2024
1 parent 042cab1 commit 970adda
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 3 deletions.
4 changes: 2 additions & 2 deletions docs/.vitepress/generate-router.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const Router = {
Dom: ['useClickAway', 'useEventListener', "useFullscreen", 'useHover', 'useRect', "useTitle", 'useWindowSize'],
Dom: ['useClickAway', 'useEventListener', "useFullscreen", 'useHover', 'useRect', "useTitle", 'useWindowSize', "useDocumentVisibility"],
State: ["useBoolean", "useDebounce", "useLocalStorage", "useThrottle", 'useToggle'],
Worker: ['useCountDown'],
Effect: ["useDebounceFn", "useThrottleFn"],
Effect: ["useDebounceFn", "useThrottleFn"]
};
export default Router;
9 changes: 9 additions & 0 deletions docs/useDocumentVisibility/demo/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<template>
<div>{{ documentVisibility }}</div>
</template>

<script setup>
import { useDocumentVisibility } from 'zhongjiayao_v3_hooks';
const documentVisibility = useDocumentVisibility();
</script>
11 changes: 11 additions & 0 deletions docs/useDocumentVisibility/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# useDocumentVisibility

监听页面是否可见,参考 [visibilityState API](https://developer.mozilla.org/docs/Web/API/Document/visibilityState)

<preview path="./demo/index.vue" title="基本使用" description='监听 document 的可见状态'></preview>

### Result

| 参数 | 说明 | 类型 |
| ------------------ | ------------------------------ | -------------------------------------------------- |
| documentVisibility | 判断 document 是否处于可见状态 | `visible`\| `hidden` \| `prerender` \| `undefined` |
4 changes: 3 additions & 1 deletion packages/hooks/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import useDebounceFn from './useDebounceFn';
import useThrottle from './useThrottle';
import useThrottleFn from './useThrottleFn';
import useTitle from './useTitle';
import useDocumentVisibility from "./useDocumentVisibility";
export {
useBoolean,
useToggle,
Expand All @@ -28,5 +29,6 @@ export {
useDebounceFn,
useThrottle,
useThrottleFn,
useTitle
useTitle,
useDocumentVisibility
};
9 changes: 9 additions & 0 deletions packages/hooks/src/useDocumentVisibility/demo/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<template>
<div>{{ documentVisibility }}</div>
</template>

<script setup>
import { useDocumentVisibility } from 'zhongjiayao_v3_hooks';
const documentVisibility = useDocumentVisibility();
</script>
11 changes: 11 additions & 0 deletions packages/hooks/src/useDocumentVisibility/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# useDocumentVisibility

监听页面是否可见,参考 [visibilityState API](https://developer.mozilla.org/docs/Web/API/Document/visibilityState)

<preview path="./demo/index.vue" title="基本使用" description='监听 document 的可见状态'></preview>

### Result

| 参数 | 说明 | 类型 |
| ------------------ | ------------------------------ | -------------------------------------------------- |
| documentVisibility | 判断 document 是否处于可见状态 | `visible`\| `hidden` \| `prerender` \| `undefined` |
29 changes: 29 additions & 0 deletions packages/hooks/src/useDocumentVisibility/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ref, Ref } from 'vue';
import { isBrowser } from '../utils';
import useEventListener from '../useEventListener';

type VisibilityState = 'hidden' | 'visible' | 'prerender' | undefined;

const getVisibility = () => {
if (!isBrowser) {
return 'visible';
}
return document.visibilityState;
};

function useDocumentVisibility(): VisibilityState {
// 创建一个 ref 来存储文档的可见性状态
const documentVisibility: Ref<VisibilityState> = ref(getVisibility());

const handleVisibilityChange = () => {
if (typeof document !== 'undefined') {
documentVisibility.value = document.visibilityState;
}
};

useEventListener('visibilitychange', handleVisibilityChange, { target: document });

return documentVisibility.value;
}

export default useDocumentVisibility;
3 changes: 3 additions & 0 deletions packages/hooks/src/useDocumentVisibility/meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "Dom"
}

0 comments on commit 970adda

Please sign in to comment.