Skip to content

Commit a9da872

Browse files
committed
docs(useBeforeUnload): Adding docs for useBeforeUnload
1 parent 8453eda commit a9da872

File tree

9 files changed

+55
-18
lines changed

9 files changed

+55
-18
lines changed

src/components/useBeforeUnload/stories/useBeforeUnload.md

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,56 @@ Vue function that shows browser alert when user try to reload or close the page.
55
## Reference
66

77
```typescript
8-
// function useBeforeUnload()
8+
function useBeforeUnload(
9+
isPageDirty: Ref<boolean>,
10+
runOnMount?: boolean
11+
): {
12+
isTracking: Ref<boolean>
13+
start: () => void
14+
stop: () => void
15+
}
916
```
1017

1118
### Parameters
1219

13-
- `value: string` lorem ipsa
20+
- `isPageDirty: Ref<boolean>` when this value is `true` value, it will show the browser alert on page change
21+
- `runOnMount: boolean` whether to listen to the 'beforeunload' event on mount, `true` by default
1422

1523
### Returns
1624

17-
- `value: Ref<string>` lorem ipsa
25+
- `isTracking: Ref<boolean>` whether this function events are running or not
26+
- `start: Function` the function used to start tracking page change or reload
27+
- `stop: Function` the function used to stop tracking page change or reload
1828

1929
## Usage
2030

2131
```html
22-
<template></template>
32+
<template>
33+
<div>
34+
<div>
35+
<a href="https://google.com">Change page</a>
36+
</div>
37+
<div>
38+
<button @click="start" v-if="!isTracking">
39+
Start tracking page change or reload
40+
</button>
41+
<button @click="stop" v-else>Stop tracking page change or reload</button>
42+
</div>
43+
</div>
44+
</template>
45+
46+
<script lang="ts">
47+
import Vue from 'vue'
48+
import { ref, watch } from '@vue/composition-api'
49+
import { useBeforeUnload } from 'vue-use-kit'
50+
51+
export default Vue.extend({
52+
name: 'UseBeforeUnloadDemo',
53+
setup() {
54+
const isPageDirty = ref(true)
55+
const { isTracking, start, stop } = useBeforeUnload(isPageDirty)
56+
return { isTracking, start, stop, isPageDirty }
57+
}
58+
})
59+
</script>
2360
```

src/components/useClickAway/useClickAway.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const testComponent = () => ({
2626
})
2727

2828
describe('useClickAway', () => {
29-
it('should call document.addEventListener', async () => {
29+
it('should call addEventListener', async () => {
3030
const addEventListenerSpy = jest.spyOn(document, 'addEventListener')
3131
const removeEventListenerSpy = jest.spyOn(document, 'removeEventListener')
3232
const wrapper = mount(testComponent())

src/components/useIdle/useIdle.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ describe('useIdle', () => {
2323
// the total of the events is idleEventsList + visibilitychange
2424
const totEvents = idleEventsList.length + 1
2525

26-
it('should call document.addEventListener', async () => {
26+
it('should call addEventListener', async () => {
2727
const addEventListenerSpy = jest.spyOn(document, 'addEventListener')
2828
const removeEventListenerSpy = jest.spyOn(document, 'removeEventListener')
2929
const wrapper = mount(testComponent())
@@ -43,7 +43,7 @@ describe('useIdle', () => {
4343
)
4444
})
4545

46-
it('should call document.addEventListener again when start is called', async () => {
46+
it('should call addEventListener again when start is called', async () => {
4747
const addEventListenerSpy = jest.spyOn(document, 'addEventListener')
4848
const wrapper = mount(testComponent())
4949
expect(addEventListenerSpy).toHaveBeenCalledTimes(totEvents)
@@ -55,7 +55,7 @@ describe('useIdle', () => {
5555
expect(addEventListenerSpy).toHaveBeenCalledTimes(totEvents * 2)
5656
})
5757

58-
it('should call document.removeEventListener when stop is called', async () => {
58+
it('should call removeEventListener when stop is called', async () => {
5959
const removeEventListenerSpy = jest.spyOn(document, 'removeEventListener')
6060
const wrapper = mount(testComponent())
6161
wrapper.find('#stop').trigger('click')

src/components/useLocation/useLocation.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('useLocation', () => {
5555
})
5656
})
5757

58-
it('should call document.addEventListener again when start is called', async () => {
58+
it('should call addEventListener again when start is called', async () => {
5959
const addEventListenerSpy = jest.spyOn(window, 'addEventListener')
6060
const wrapper = mount(testComponent())
6161
expect(addEventListenerSpy).toHaveBeenCalledTimes(events.length)
@@ -67,7 +67,7 @@ describe('useLocation', () => {
6767
expect(addEventListenerSpy).toHaveBeenCalledTimes(events.length * 2)
6868
})
6969

70-
it('should call document.removeEventListener when stop is called', async () => {
70+
it('should call removeEventListener when stop is called', async () => {
7171
const removeEventListenerSpy = jest.spyOn(window, 'removeEventListener')
7272
const wrapper = mount(testComponent())
7373
wrapper.find('#stop').trigger('click')

src/components/useMediaDevices/useMediaDevices.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('useMediaDevices', () => {
5959
expect(removeEventListenerSpy).toBeCalledWith(event, expect.any(Function))
6060
})
6161

62-
it('should call document.addEventListener again when start is called', async () => {
62+
it('should call addEventListener again when start is called', async () => {
6363
const addEventListenerSpy = jest.spyOn(
6464
navigator.mediaDevices,
6565
'addEventListener'
@@ -74,7 +74,7 @@ describe('useMediaDevices', () => {
7474
expect(addEventListenerSpy).toHaveBeenCalledTimes(1 * 2)
7575
})
7676

77-
it('should call document.removeEventListener when stop is called', async () => {
77+
it('should call removeEventListener when stop is called', async () => {
7878
const removeEventListenerSpy = jest.spyOn(
7979
navigator.mediaDevices,
8080
'removeEventListener'

src/components/useMouse/useMouse.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const testComponent = () => ({
1919
})
2020

2121
describe('useMouse', () => {
22-
it('should call document.addEventListener', async () => {
22+
it('should call addEventListener', async () => {
2323
const addEventListenerSpy = jest.spyOn(document, 'addEventListener')
2424
const removeEventListenerSpy = jest.spyOn(document, 'removeEventListener')
2525
const wrapper = mount(testComponent())

src/components/useMouseElement/useMouseElement.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const testComponent = () => ({
3636
})
3737

3838
describe('useMouseElement', () => {
39-
it('should call document.addEventListener', async () => {
39+
it('should call addEventListener', async () => {
4040
const addEventListenerSpy = jest.spyOn(document, 'addEventListener')
4141
const removeEventListenerSpy = jest.spyOn(document, 'removeEventListener')
4242
const wrapper = mount(testComponent())

src/components/useMouseLeavePage/useMouseLeavePage.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ describe('useMouseLeavePage', () => {
3939
)
4040
})
4141

42-
it('should call document.addEventListener again when start is called', async () => {
42+
it('should call addEventListener again when start is called', async () => {
4343
const addEventListenerSpy = jest.spyOn(document, 'addEventListener')
4444
const wrapper = mount(testComponent())
4545
expect(addEventListenerSpy).toHaveBeenCalledTimes(1)
@@ -51,7 +51,7 @@ describe('useMouseLeavePage', () => {
5151
expect(addEventListenerSpy).toHaveBeenCalledTimes(1 * 2)
5252
})
5353

54-
it('should call document.removeEventListener when stop is called', async () => {
54+
it('should call removeEventListener when stop is called', async () => {
5555
const removeEventListenerSpy = jest.spyOn(document, 'removeEventListener')
5656
const wrapper = mount(testComponent())
5757
wrapper.find('#stop').trigger('click')

src/components/useSearchParams/useSearchParams.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ describe('useSearchParams', () => {
4949
})
5050
})
5151

52-
it('should call document.addEventListener again when start is called', async () => {
52+
it('should call addEventListener again when start is called', async () => {
5353
const addEventListenerSpy = jest.spyOn(window, 'addEventListener')
5454
const wrapper = mount(testComponent())
5555
expect(addEventListenerSpy).toHaveBeenCalledTimes(events.length)
@@ -61,7 +61,7 @@ describe('useSearchParams', () => {
6161
expect(addEventListenerSpy).toHaveBeenCalledTimes(events.length * 2)
6262
})
6363

64-
it('should call document.removeEventListener when stop is called', async () => {
64+
it('should call removeEventListener when stop is called', async () => {
6565
const removeEventListenerSpy = jest.spyOn(window, 'removeEventListener')
6666
const wrapper = mount(testComponent())
6767
wrapper.find('#stop').trigger('click')

0 commit comments

Comments
 (0)