-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(hooks): "新增useDocumentVisibility钩子以检测页面可见性"
"新增useDocumentVisibility钩子以检测页面可见性"
- Loading branch information
Null
committed
Aug 12, 2024
1 parent
042cab1
commit 970adda
Showing
8 changed files
with
77 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"type": "Dom" | ||
} |