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 11<script setup lang="ts">
22import { ref } from ' vue'
33import { useElementHover } from ' @vueuse/core'
4+ import { vElementHover } from ' ./directive'
45
56const el = ref <HTMLButtonElement >()
7+ const isDirectiveHovered = ref (false )
68const 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 >
Original file line number Diff line number Diff line change 11import { watch } from 'vue-demi'
22import { directiveHooks } from '@vueuse/shared'
33import type { ObjectDirective } from 'vue-demi'
4+ import type { UseElementHoverOptions } from '.'
45import { useElementHover } from '.'
56
67type BindingValueFunction = ( state : boolean ) => void
78
89export 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}
Original file line number Diff line number Diff 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+ ```
You can’t perform that action at this time.
0 commit comments