useElementHover ​
Reactive element's hover state.
Demo ​
Usage ​
vue
<script setup>
import { useElementHover } from '@vueuse/core'
const myHoverableElement = ref()
const isHovered = useElementHover(myHoverableElement)
</script>
<template>
<button ref="myHoverableElement">
{{ isHovered }}
</button>
</template>
Directive Usage ​
This function also provides a directive version via the
@vueuse/components
package. Learn more about the usage.
vue
<script setup lang="ts">
import { vElementHover } from '@vueuse/components'
import { ref } from 'vue'
const isHovered = ref(false)
function onHover(state: boolean) {
isHovered.value = state
}
</script>
<template>
<button v-element-hover="onHover">
{{ isHovered ? 'Thank you!' : 'Hover me' }}
</button>
</template>
You can also provide hover options:
vue
<script setup lang="ts">
import { vElementHover } from '@vueuse/components'
import { ref } from 'vue'
const isHovered = ref(false)
function onHover(hovered: boolean) {
isHovered.value = hovered
}
</script>
<template>
<button v-element-hover="[onHover, { delayEnter: 1000 }]">
<span>{{ isHovered ? 'Thank you!' : 'Hover me' }}</span>
</button>
</template>
Type Declarations ​
typescript
export interface UseElementHoverOptions extends ConfigurableWindow {
delayEnter?: number
delayLeave?: number
}
export declare function useElementHover(
el: MaybeRefOrGetter<EventTarget | null | undefined>,
options?: UseElementHoverOptions,
): Ref<boolean>
Source ​
Contributors ​
Anthony Fu
Anthony Fu
webfansplz
JoaquÃn Sánchez
Jelf
geekreal
cx33
Joe Maylor
Changelog ​
v12.0.0-beta.1
on 11/21/2024v11.0.0-beta.2
on 7/17/2024v10.0.0-beta.4
on 4/13/20234d757
- feat(types)!: rename MaybeComputedRef
to MaybeRefOrGetter
v10.0.0-beta.0
on 3/14/2023v9.13.0
on 2/18/2023