diff --git a/README.md b/README.md
index 8d38780..1ab8d90 100755
--- a/README.md
+++ b/README.md
@@ -18,12 +18,16 @@
> 🛠️ Vue kit of useful [Vue Composition API](https://vue-composition-api-rfc.netlify.com) functions.
-Please note that Vue 3.0 has not been released yet, therefore the installation and setup of [@vue/composition-api](https://github.com/vuejs/composition-api) is required for this library to work.
-
## Install
```shell script
-npm install @vue/composition-api vue-use-kit
+npm install vue-use-kit
+```
+
+Since Vue 3.0 has not yet been released, you must also install [@vue/composition-api](https://github.com/vuejs/composition-api) library, which will enable the composition API in Vue 2.0.
+
+```shell script
+npm install @vue/composition-api
```
## Setup
@@ -78,6 +82,8 @@ Vue.use(VueCompositionAPI);
[](https://microcipcip.github.io/vue-use-kit/?path=/story/sensors-usemouse--advanced-demo)
- [`useMouseElement`](./src/components/useMouseElement/stories/useMouseElement.md) — tracks the mouse position relative to given element.
[](https://microcipcip.github.io/vue-use-kit/?path=/story/sensors-usemouseelement--demo)
+ - [`useSearchParams`](./src/components/useSearchParams/stories/useSearchParams.md) — tracks browser's location search params.
+ [](https://microcipcip.github.io/vue-use-kit/?path=/story/sensors-usesearchparams--demo)
- Animations
- [`useInterval`](./src/components/useInterval/stories/useInterval.md) — updates `counter` value repeatedly on a fixed time delay.
[](https://microcipcip.github.io/vue-use-kit/?path=/story/animations-useinterval--demo)
diff --git a/src/components/useClickAway/useClickAway.spec.ts b/src/components/useClickAway/useClickAway.spec.ts
index 9b7e58f..e949b38 100755
--- a/src/components/useClickAway/useClickAway.spec.ts
+++ b/src/components/useClickAway/useClickAway.spec.ts
@@ -28,7 +28,6 @@ const testComponent = () => ({
describe('useClickAway', () => {
it('should call document.addEventListener', async () => {
const addEventListenerSpy = jest.spyOn(document, 'addEventListener')
- expect(addEventListenerSpy).not.toHaveBeenCalled()
const removeEventListenerSpy = jest.spyOn(document, 'removeEventListener')
const wrapper = mount(testComponent())
await wrapper.vm.$nextTick()
diff --git a/src/components/useIdle/stories/UseIdleDemo.vue b/src/components/useIdle/stories/UseIdleDemo.vue
index 29e4f3e..0758761 100755
--- a/src/components/useIdle/stories/UseIdleDemo.vue
+++ b/src/components/useIdle/stories/UseIdleDemo.vue
@@ -15,12 +15,12 @@
- |
@@ -37,19 +37,8 @@ import { useIdle } from '@src/vue-use-kit'
export default Vue.extend({
name: 'UseIdleDemo',
setup() {
- const { isIdle, start, stop } = useIdle(2500)
-
- const isTracking = ref(true)
- const startTracking = () => {
- isTracking.value = true
- start()
- }
- const stopTracking = () => {
- isTracking.value = false
- stop()
- }
-
- return { isIdle, isTracking, startTracking, stopTracking }
+ const { isIdle, isTracking, start, stop } = useIdle(2500)
+ return { isIdle, isTracking, start, stop }
}
})
diff --git a/src/components/useIdle/stories/useIdle.md b/src/components/useIdle/stories/useIdle.md
index 150c7c0..9f4e89c 100755
--- a/src/components/useIdle/stories/useIdle.md
+++ b/src/components/useIdle/stories/useIdle.md
@@ -11,6 +11,7 @@ function useIdle(
runOnMount?: boolean
): {
isIdle: Ref;
+ isTracking: Ref;
start: () => void;
stop: () => void;
}
@@ -25,6 +26,7 @@ function useIdle(
### Returns
- `isIdle: Ref` it is `true` when the user is idle, `false` otherwise
+- `isTracking: Ref` whether the function is tracking the user idle state or not
- `start: Function` the function used for start tracking the user's idle state
- `stop: Function` the function used for stop tracking the user's idle state
@@ -34,8 +36,8 @@ function useIdle(
isIdle: {{ isIdle }}
-
Start tracking
-
Stop tracking
+
Start tracking
+
Stop tracking
@@ -46,19 +48,8 @@ function useIdle(
export default Vue.extend({
name: 'UseIdleDemo',
setup() {
- const { isIdle, start, stop } = useIdle(2500)
-
- const isTracking = ref(true)
- const startTracking = () => {
- isTracking.value = true
- start()
- }
- const stopTracking = () => {
- isTracking.value = false
- stop()
- }
-
- return { isIdle, isTracking, startTracking, stopTracking }
+ const { isIdle, isTracking, start, stop } = useIdle(2500)
+ return { isIdle, isTracking, start, stop }
}
})
diff --git a/src/components/useIdle/useIdle.spec.ts b/src/components/useIdle/useIdle.spec.ts
index eef4040..68ac1d4 100755
--- a/src/components/useIdle/useIdle.spec.ts
+++ b/src/components/useIdle/useIdle.spec.ts
@@ -25,7 +25,6 @@ describe('useIdle', () => {
it('should call document.addEventListener', async () => {
const addEventListenerSpy = jest.spyOn(document, 'addEventListener')
- expect(addEventListenerSpy).not.toHaveBeenCalled()
const removeEventListenerSpy = jest.spyOn(document, 'removeEventListener')
const wrapper = mount(testComponent())
await wrapper.vm.$nextTick()
diff --git a/src/components/useIdle/useIdle.ts b/src/components/useIdle/useIdle.ts
index f78e904..5b06a7b 100755
--- a/src/components/useIdle/useIdle.ts
+++ b/src/components/useIdle/useIdle.ts
@@ -18,6 +18,7 @@ export function useIdle(
) {
let timeout: any = null
const isIdle = ref(false)
+ const isTracking = ref(false)
const handleChange = throttle(50, () => {
isIdle.value = false
@@ -34,14 +35,17 @@ export function useIdle(
}
const start = () => {
+ if (isTracking.value) return
events.forEach(evtName => document.addEventListener(evtName, handleChange))
document.addEventListener('visibilitychange', handleVisibility)
// Initialize it since the events above may not run immediately
handleChange()
+ isTracking.value = true
}
const stop = () => {
+ if (!isTracking.value) return
events.forEach(evtName =>
document.removeEventListener(evtName, handleChange)
)
@@ -53,10 +57,11 @@ export function useIdle(
// Restore initial status
timeout = null
isIdle.value = false
+ isTracking.value = false
}
onMounted(() => runOnMount && start())
onUnmounted(stop)
- return { isIdle, start, stop }
+ return { isIdle, isTracking, start, stop }
}
diff --git a/src/components/useLocation/useLocation.spec.ts b/src/components/useLocation/useLocation.spec.ts
index b5c7724..7bedb18 100755
--- a/src/components/useLocation/useLocation.spec.ts
+++ b/src/components/useLocation/useLocation.spec.ts
@@ -39,7 +39,6 @@ describe('useLocation', () => {
it('should call popstate, pushstate and replacestate onMounted', async () => {
const addEventListenerSpy = jest.spyOn(window, 'addEventListener')
- expect(addEventListenerSpy).not.toHaveBeenCalled()
const removeEventListenerSpy = jest.spyOn(window, 'removeEventListener')
const wrapper = mount(testComponent())
await wrapper.vm.$nextTick()
diff --git a/src/components/useLocation/useLocation.ts b/src/components/useLocation/useLocation.ts
index 2e5fd2a..c0d6a71 100755
--- a/src/components/useLocation/useLocation.ts
+++ b/src/components/useLocation/useLocation.ts
@@ -1,4 +1,5 @@
import { ref, onMounted, onUnmounted, Ref } from '@src/api'
+import { patchHistoryMethodsOnce } from '@src/utils'
export interface UseLocationState {
trigger: string
@@ -15,28 +16,6 @@ export interface UseLocationState {
search: string
}
-// The history methods 'pushState' and 'replaceState' by default do not fire an event
-// unless it is coming from user interaction with the browser navigation bar,
-// so we are adding a patch to make them detectable
-let isPatched = false
-const patchHistoryMethodsOnce = () => {
- if (isPatched) return
- const methods = ['pushState', 'replaceState']
- methods.forEach(method => {
- const original = (history as any)[method]
- ;(history as any)[method] = function(state: any) {
- // eslint-disable-next-line prefer-rest-params
- const result = original.apply(this, arguments)
- const event = new Event(method.toLowerCase())
- ;(event as any).state = state
- window.dispatchEvent(event)
- return result
- }
- })
-
- isPatched = true
-}
-
export function useLocation(runOnMount = true) {
const buildState = (trigger: string) => {
const { state, length } = history
@@ -76,23 +55,21 @@ export function useLocation(runOnMount = true) {
const replaceState = () => (locationState.value = buildState('replacestate'))
const start = () => {
- patchHistoryMethodsOnce()
-
if (isTracking.value) return
- isTracking.value = true
-
+ patchHistoryMethodsOnce()
locationState.value = buildState('start')
window.addEventListener('popstate', popState)
window.addEventListener('pushstate', pushState)
window.addEventListener('replacestate', replaceState)
+ isTracking.value = true
}
const stop = () => {
if (!isTracking.value) return
- isTracking.value = false
window.removeEventListener('popstate', popState)
window.removeEventListener('pushstate', pushState)
window.removeEventListener('replacestate', replaceState)
+ isTracking.value = false
}
onMounted(() => runOnMount && start())
diff --git a/src/components/useMouse/useMouse.spec.ts b/src/components/useMouse/useMouse.spec.ts
index 6061eae..c2695de 100755
--- a/src/components/useMouse/useMouse.spec.ts
+++ b/src/components/useMouse/useMouse.spec.ts
@@ -21,7 +21,6 @@ const testComponent = () => ({
describe('useMouse', () => {
it('should call document.addEventListener', async () => {
const addEventListenerSpy = jest.spyOn(document, 'addEventListener')
- expect(addEventListenerSpy).not.toHaveBeenCalled()
const removeEventListenerSpy = jest.spyOn(document, 'removeEventListener')
const wrapper = mount(testComponent())
await wrapper.vm.$nextTick()
diff --git a/src/components/useMouseElement/useMouseElement.spec.ts b/src/components/useMouseElement/useMouseElement.spec.ts
index c1f3e7d..2e0c202 100755
--- a/src/components/useMouseElement/useMouseElement.spec.ts
+++ b/src/components/useMouseElement/useMouseElement.spec.ts
@@ -38,7 +38,6 @@ const testComponent = () => ({
describe('useMouseElement', () => {
it('should call document.addEventListener', async () => {
const addEventListenerSpy = jest.spyOn(document, 'addEventListener')
- expect(addEventListenerSpy).not.toHaveBeenCalled()
const removeEventListenerSpy = jest.spyOn(document, 'removeEventListener')
const wrapper = mount(testComponent())
await wrapper.vm.$nextTick()
diff --git a/src/components/useSearchParams/index.ts b/src/components/useSearchParams/index.ts
new file mode 100755
index 0000000..7b84462
--- /dev/null
+++ b/src/components/useSearchParams/index.ts
@@ -0,0 +1 @@
+export * from './useSearchParams'
diff --git a/src/components/useSearchParams/stories/Field.vue b/src/components/useSearchParams/stories/Field.vue
new file mode 100755
index 0000000..497d941
--- /dev/null
+++ b/src/components/useSearchParams/stories/Field.vue
@@ -0,0 +1,21 @@
+
+
+
+
+
diff --git a/src/components/useSearchParams/stories/UseSearchParamsDemo.vue b/src/components/useSearchParams/stories/UseSearchParamsDemo.vue
new file mode 100755
index 0000000..336f070
--- /dev/null
+++ b/src/components/useSearchParams/stories/UseSearchParamsDemo.vue
@@ -0,0 +1,87 @@
+
+
+
+
+
diff --git a/src/components/useSearchParams/stories/useSearchParams.md b/src/components/useSearchParams/stories/useSearchParams.md
new file mode 100755
index 0000000..10f4712
--- /dev/null
+++ b/src/components/useSearchParams/stories/useSearchParams.md
@@ -0,0 +1,70 @@
+# useSearchParams
+
+Vue function that tracks browser's location search parameters.
+
+## Reference
+
+```typescript
+function useSearchParams(
+ parameters: string[],
+ runOnMount?: boolean
+): {
+ searchParams: Ref