Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ Vue.use(VueCompositionAPI);
[![Demo](https://img.shields.io/badge/demo-🚀-yellow.svg)](https://microcipcip.github.io/vue-use-kit/?path=/story/sensors-usegeolocation--demo)
- [`useHover`](./src/components/useHover/stories/useHover.md) — tracks mouse hover state of a given element.
[![Demo](https://img.shields.io/badge/demo-🚀-yellow.svg)](https://microcipcip.github.io/vue-use-kit/?path=/story/sensors-usehover--demo)
- [`useIdle`](./src/components/useIdle/stories/useIdle.md) — tracks whether user is being inactive.
[![Demo](https://img.shields.io/badge/demo-🚀-yellow.svg)](https://microcipcip.github.io/vue-use-kit/?path=/story/sensors-useidle--demo)
- [`useIntersection`](./src/components/useIntersection/stories/useIntersection.md) — tracks intersection of target element with an ancestor element.
[![Demo](https://img.shields.io/badge/demo-🚀-yellow.svg)](https://microcipcip.github.io/vue-use-kit/?path=/story/sensors-useintersection--demo)
[![Demo](https://img.shields.io/badge/advanced_demo-🚀-yellow.svg)](https://microcipcip.github.io/vue-use-kit/?path=/story/sensors-useintersection--advanced-demo)
Expand Down
55 changes: 50 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
"@storybook/vue": "^5.3.13",
"@types/jest": "^23.3.2",
"@types/node": "^10.11.0",
"@types/throttle-debounce": "^2.1.0",
"@typescript-eslint/eslint-plugin": "^2.18.0",
"@typescript-eslint/parser": "^2.18.0",
"@vue/composition-api": "^0.3.4",
Expand Down Expand Up @@ -136,5 +137,7 @@
"vue-loader": "^15.8.3",
"vue-template-compiler": "^2.6.11"
},
"dependencies": {}
"dependencies": {
"throttle-debounce": "^2.1.0"
}
}
13 changes: 12 additions & 1 deletion src/components/useClickAway/useClickAway.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { mount } from '@src/helpers/test'
import { ref } from '@src/api'
import { useClickAway } from '@src/vue-use-kit'

afterEach(() => {
jest.clearAllMocks()
})

const testComponent = () => ({
template: `
<div id="outside-el">
Expand Down Expand Up @@ -29,10 +33,17 @@ describe('useClickAway', () => {
const wrapper = mount(testComponent())
await wrapper.vm.$nextTick()
expect(addEventListenerSpy).toHaveBeenCalled()
expect(addEventListenerSpy).toBeCalledWith(
'mousedown',
expect.any(Function)
)

// Destroy instance to check if the remove event listener is being called
wrapper.destroy()
expect(removeEventListenerSpy).toHaveBeenCalled()
addEventListenerSpy.mockClear()
expect(removeEventListenerSpy).toBeCalledWith(
'mousedown',
expect.any(Function)
)
})
})
4 changes: 4 additions & 0 deletions src/components/useFullscreen/useFullscreen.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { useFullscreen } from '@src/vue-use-kit'
// This feature is difficult to test therefore
// I've only written a simple test

afterEach(() => {
jest.clearAllMocks()
})

const testComponent = () => ({
template: `
<div id="divRef" ref="divRef">
Expand Down
12 changes: 12 additions & 0 deletions src/components/useHover/useHover.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { mount } from '@src/helpers/test'
import { ref } from '@src/api'
import { useHover } from '@src/vue-use-kit'

afterEach(() => {
jest.clearAllMocks()
})

const testComponent = () => ({
template: `
<div id="isHovered" v-if="isHovered"></div>
Expand All @@ -22,9 +26,17 @@ describe('useHover', () => {
)
const wrapper = mount(testComponent())
expect(addEventListenerSpy).toHaveBeenCalled()
expect(addEventListenerSpy).toBeCalledWith(
'mouseenter',
expect.any(Function)
)
expect(removeEventListenerSpy).not.toHaveBeenCalled()
wrapper.destroy()
expect(removeEventListenerSpy).toHaveBeenCalled()
expect(removeEventListenerSpy).toBeCalledWith(
'mouseenter',
expect.any(Function)
)
})

it('should return isHovered false by default', () => {
Expand Down
1 change: 1 addition & 0 deletions src/components/useIdle/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './useIdle'
67 changes: 67 additions & 0 deletions src/components/useIdle/stories/UseIdleDemo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<template>
<table class="table is-fullwidth">
<thead>
<tr>
<th>Prop</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td>isIdle</td>
<td class="idle" :class="{ '-is-idle': isIdle }">{{ isIdle }}</td>
</tr>
<tr>
<td colspan="2">
<button
class="button is-primary"
@click="startTracking"
v-if="!isTracking"
>
Start tracking idle status
</button>
<button class="button is-danger" @click="stopTracking" v-else>
Stop tracking idle status
</button>
</td>
</tr>
</tbody>
</table>
</template>

<script lang="ts">
import Vue from 'vue'
import { ref } from '@src/api'
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 }
}
})
</script>

<style scoped>
.idle {
color: #fff;
background: #981105;
}

.idle.-is-idle {
color: #fff;
background: #209817;
}
</style>
65 changes: 65 additions & 0 deletions src/components/useIdle/stories/useIdle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# useIdle

Vue function that tracks whether user is being inactive.

## Reference

```typescript
useIdle(
ms?: number,
events?: string[],
runOnMount?: boolean
): {
isIdle: Ref<boolean>;
start: () => void;
stop: () => void;
}
```

### Parameters

- `ms: number` milliseconds to wait before deciding whether the user is being idle
- `events: string[]` list of events to track the user for
- `runOnMount: boolean` whether to start tracking idle state on mount, `true` by default

### Returns

- `isIdle: Ref<boolean>` it is `true` when the user is idle, `false` otherwise
- `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

## Usage

```html
<template>
<div>
<p>isIdle: {{ isIdle }}</p>
<button @click="startTracking" v-if="!isTracking">Start tracking</button>
<button @click="stopTracking" v-else>Stop tracking</button>
</div>
</template>

<script lang="ts">
import Vue from 'vue'
import { useIdle } from '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 }
}
})
</script>
```
28 changes: 28 additions & 0 deletions src/components/useIdle/stories/useIdle.story.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { storiesOf } from '@storybook/vue'
import path from 'path'
import StoryTitle from '@src/helpers/StoryTitle.vue'
import UseIdleDemo from './UseIdleDemo.vue'

const functionName = 'useIdle'
const functionPath = path.resolve(__dirname, '..')
const notes = require(`./${functionName}.md`).default

const basicDemo = () => ({
components: { StoryTitle, demo: UseIdleDemo },
template: `
<div class="container">
<story-title
function-path="${functionPath}"
source-name="${functionName}"
demo-name="UseIdleDemo.vue"
>
<template v-slot:title></template>
<template v-slot:intro></template>
</story-title>
<demo />
</div>`
})

storiesOf('sensors|useIdle', module)
.addParameters({ notes })
.add('Demo', basicDemo)
Loading