-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add visibilityState * visibility add tests and add hidden * rename visibilityState to pageVisibility * added documentation
- Loading branch information
Showing
10 changed files
with
187 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
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,36 @@ | ||
<template> | ||
<div> | ||
<h4> | ||
Hidden: <b>{{ hidden }}</b> | ||
</h4> | ||
<h4> | ||
State: <b>{{ visibility }}</b> | ||
</h4> | ||
<p>You can change the state by switching tab - Check console</p> | ||
<p> | ||
<a | ||
href="https://sqa.stackexchange.com/a/32355" | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
>Check this link</a | ||
> | ||
</p> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import { usePageVisibility, unwrap } from "vue-composable"; | ||
import { watch, reactive } from "@vue/composition-api"; | ||
export default { | ||
setup() { | ||
const { visibility, hidden } = usePageVisibility(); | ||
watch(visibility, () => { | ||
console.log("visibility changed", { | ||
visibility: visibility.value, | ||
hidden: hidden.value | ||
}); | ||
}); | ||
return { visibility, hidden }; | ||
} | ||
}; | ||
</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
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,65 @@ | ||
# Page Visibility | ||
|
||
> [Page Visibility API](https://developer.mozilla.org/en-US/docs/Web/API/Page_Visibility_API). | ||
## State | ||
|
||
The `usePageVisibility` function exposes the following reactive state: | ||
|
||
```js | ||
import { usePageVisibility } from "vue-composable"; | ||
|
||
const { visibility, hidden } = usePageVisibility(); | ||
``` | ||
|
||
| State | Type | Description | | ||
| ---------- | ---------------------- | --------------------------------- | | ||
| visibility | `Ref(VisibilityState)` | Current document visibility state | | ||
| hidden | `Ref(Boolean)` | `document.hidden` | | ||
|
||
## Example | ||
|
||
<ClientOnly> | ||
<page-visibility-example/> | ||
</ClientOnly> | ||
|
||
### Code | ||
|
||
```vue | ||
<template> | ||
<div> | ||
<h4> | ||
Hidden: <b>{{ hidden }}</b> | ||
</h4> | ||
<h4> | ||
State: <b>{{ visibility }}</b> | ||
</h4> | ||
<p>You can change the state by switching tab - Check console</p> | ||
<p> | ||
<a | ||
href="https://sqa.stackexchange.com/a/32355" | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
>Check this link</a | ||
> | ||
</p> | ||
</div> | ||
</template> | ||
<script> | ||
import { usePageVisibility, unwrap } from "vue-composable"; | ||
import { watch, reactive } from "@vue/composition-api"; | ||
export default { | ||
setup() { | ||
const { visibility, hidden } = usePageVisibility(); | ||
watch(visibility, () => { | ||
console.log("visibility changed", { | ||
visibility: visibility.value, | ||
hidden: hidden.value | ||
}); | ||
}); | ||
return { visibility, hidden }; | ||
} | ||
}; | ||
</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
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,52 @@ | ||
import { usePageVisibility } from "../../src"; | ||
|
||
describe("pageVisibility", () => { | ||
const documentEventSpy = jest.fn(); | ||
const documentEvent = document.addEventListener; | ||
|
||
const raiseVisibilityChange = () => documentEventSpy.mock.calls[0][1](); | ||
|
||
const visibilityGetter = jest.spyOn(document, 'visibilityState', 'get'); | ||
const hiddenGetter = jest.spyOn(document, 'hidden', 'get'); | ||
|
||
const updateVisibility = (v: VisibilityState, hidden: boolean) => { | ||
visibilityGetter.mockImplementation(() => v); | ||
hiddenGetter.mockImplementation(() => hidden); | ||
|
||
raiseVisibilityChange(); | ||
} | ||
|
||
beforeAll(() => { | ||
document.addEventListener = documentEventSpy; | ||
}) | ||
afterAll(() => { | ||
document.addEventListener = documentEvent; | ||
}) | ||
|
||
it('should only add event listener once', () => { | ||
expect(documentEventSpy).not.toHaveBeenCalled(); | ||
|
||
usePageVisibility(); | ||
usePageVisibility(); | ||
|
||
expect(documentEventSpy).toHaveBeenCalled(); | ||
expect(documentEventSpy).toHaveBeenCalledTimes(1); | ||
|
||
expect(documentEventSpy).toHaveBeenCalledWith('visibilitychange', expect.anything(), expect.objectContaining({ passive: true })) | ||
}) | ||
|
||
it('should update visibilityState and hidden', () => { | ||
const { visibility, hidden } = usePageVisibility(); | ||
|
||
expect(visibility.value).toBe(document.visibilityState); | ||
expect(hidden.value).toBe(document.hidden); | ||
|
||
updateVisibility('hidden', true); | ||
expect(visibility.value).toBe('hidden'); | ||
expect(hidden.value).toBe(true); | ||
|
||
updateVisibility('visible', false); | ||
expect(visibility.value).toBe('visible'); | ||
expect(hidden.value).toBe(false); | ||
}) | ||
}) |
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,26 @@ | ||
import { ref, Ref } from "@vue/composition-api"; | ||
|
||
let visibility: Ref<VisibilityState> | undefined = undefined; | ||
let hidden: Ref<boolean> | undefined = undefined; | ||
|
||
export function usePageVisibility() { | ||
if (!hidden) { | ||
hidden = ref(document.hidden); | ||
} | ||
if (!visibility) { | ||
visibility = ref(document.visibilityState); | ||
document.addEventListener( | ||
"visibilitychange", | ||
() => { | ||
visibility!.value = document.visibilityState | ||
hidden!.value = document.hidden; | ||
}, | ||
{ passive: true } | ||
// true | ||
); | ||
} | ||
return { | ||
visibility, | ||
hidden | ||
}; | ||
} |