Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cf439dd

Browse files
userquinantfu
andauthored
feat(useElementHover): add options to the directive (#3897)
Co-authored-by: Anthony Fu <[email protected]>
1 parent 8a023fe commit cf439dd

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
<script setup lang="ts">
22
import { ref } from 'vue'
33
import { useElementHover } from '@vueuse/core'
4+
import { vElementHover } from './directive'
45
56
const el = ref<HTMLButtonElement>()
7+
const isDirectiveHovered = ref(false)
68
const isHovered = useElementHover(el, { delayEnter: 200, delayLeave: 600 })
9+
function onHover(hovered: boolean) {
10+
isDirectiveHovered.value = hovered
11+
}
712
</script>
813

914
<template>
1015
<button ref="el">
1116
<span>{{ isHovered ? 'Thank you!' : 'Hover me' }}</span>
1217
</button>
18+
<button v-element-hover="[onHover, { delayEnter: 200, delayLeave: 600 }]">
19+
<span>{{ isDirectiveHovered ? 'Thank you!' : 'Hover me' }}</span>
20+
</button>
1321
</template>
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
import { watch } from 'vue-demi'
22
import { directiveHooks } from '@vueuse/shared'
33
import type { ObjectDirective } from 'vue-demi'
4+
import type { UseElementHoverOptions } from '.'
45
import { useElementHover } from '.'
56

67
type BindingValueFunction = (state: boolean) => void
78

89
export const vElementHover: ObjectDirective<
910
HTMLElement,
10-
BindingValueFunction
11+
BindingValueFunction | [handler: BindingValueFunction, options: UseElementHoverOptions]
1112
> = {
1213
[directiveHooks.mounted](el, binding) {
13-
if (typeof binding.value === 'function') {
14+
const value = binding.value
15+
if (typeof value === 'function') {
1416
const isHovered = useElementHover(el)
15-
watch(isHovered, v => binding.value(v))
17+
watch(isHovered, v => value(v))
18+
}
19+
else {
20+
const [handler, options] = value
21+
const isHovered = useElementHover(el, options)
22+
watch(isHovered, v => handler(v))
1623
}
1724
},
1825
}

packages/core/useElementHover/index.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,23 @@ function onHover(state: boolean) {
4242
</button>
4343
</template>
4444
```
45+
46+
You can also provide hover options:
47+
48+
```vue
49+
<script setup lang="ts">
50+
import { ref } from 'vue'
51+
import { vElementHover } from '@vueuse/components'
52+
53+
const isHovered = ref(false)
54+
function onHover(hovered: boolean) {
55+
isHovered.value = hovered
56+
}
57+
</script>
58+
59+
<template>
60+
<button v-element-hover="[onHover, { delayEnter: 1000 }]">
61+
<span>{{ isHovered ? 'Thank you!' : 'Hover me' }}</span>
62+
</button>
63+
</template>
64+
```

0 commit comments

Comments
 (0)