File tree Expand file tree Collapse file tree 3 files changed +38
-3
lines changed
packages/core/useElementHover Expand file tree Collapse file tree 3 files changed +38
-3
lines changed Original file line number Diff line number Diff line change 1
1
<script setup lang="ts">
2
2
import { ref } from ' vue'
3
3
import { useElementHover } from ' @vueuse/core'
4
+ import { vElementHover } from ' ./directive'
4
5
5
6
const el = ref <HTMLButtonElement >()
7
+ const isDirectiveHovered = ref (false )
6
8
const isHovered = useElementHover (el , { delayEnter: 200 , delayLeave: 600 })
9
+ function onHover(hovered : boolean ) {
10
+ isDirectiveHovered .value = hovered
11
+ }
7
12
</script >
8
13
9
14
<template >
10
15
<button ref =" el" >
11
16
<span >{{ isHovered ? 'Thank you!' : 'Hover me' }}</span >
12
17
</button >
18
+ <button v-element-hover =" [onHover, { delayEnter: 200, delayLeave: 600 }]" >
19
+ <span >{{ isDirectiveHovered ? 'Thank you!' : 'Hover me' }}</span >
20
+ </button >
13
21
</template >
Original file line number Diff line number Diff line change 1
1
import { watch } from 'vue-demi'
2
2
import { directiveHooks } from '@vueuse/shared'
3
3
import type { ObjectDirective } from 'vue-demi'
4
+ import type { UseElementHoverOptions } from '.'
4
5
import { useElementHover } from '.'
5
6
6
7
type BindingValueFunction = ( state : boolean ) => void
7
8
8
9
export const vElementHover : ObjectDirective <
9
10
HTMLElement ,
10
- BindingValueFunction
11
+ BindingValueFunction | [ handler : BindingValueFunction , options : UseElementHoverOptions ]
11
12
> = {
12
13
[ directiveHooks . mounted ] ( el , binding ) {
13
- if ( typeof binding . value === 'function' ) {
14
+ const value = binding . value
15
+ if ( typeof value === 'function' ) {
14
16
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 ) )
16
23
}
17
24
} ,
18
25
}
Original file line number Diff line number Diff line change @@ -42,3 +42,23 @@ function onHover(state: boolean) {
42
42
</button>
43
43
</template>
44
44
```
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
+ ```
You can’t perform that action at this time.
0 commit comments