diff --git a/packages/core/index.ts b/packages/core/index.ts index da2f3ba229ba..2e48c9235283 100644 --- a/packages/core/index.ts +++ b/packages/core/index.ts @@ -24,6 +24,7 @@ export * from './useDisplayMedia' export * from './useDocumentVisibility' export * from './useDraggable' export * from './useElementBounding' +export * from './useElementHover' export * from './useElementSize' export * from './useElementVisibility' export * from './useEventBus' diff --git a/packages/core/useElementHover/demo.vue b/packages/core/useElementHover/demo.vue new file mode 100644 index 000000000000..c1736b218ece --- /dev/null +++ b/packages/core/useElementHover/demo.vue @@ -0,0 +1,13 @@ + + + diff --git a/packages/core/useElementHover/index.md b/packages/core/useElementHover/index.md new file mode 100644 index 000000000000..bdcf09c65ff4 --- /dev/null +++ b/packages/core/useElementHover/index.md @@ -0,0 +1,24 @@ +--- +category: Sensors +--- + +# useElementHover + +Reactive element's hover state. + +## Usage + +```vue + + + +``` diff --git a/packages/core/useElementHover/index.ts b/packages/core/useElementHover/index.ts new file mode 100644 index 000000000000..e4be3461125f --- /dev/null +++ b/packages/core/useElementHover/index.ts @@ -0,0 +1,12 @@ +import { Ref, ref } from 'vue-demi' +import { MaybeRef } from '@vueuse/shared' +import { useEventListener } from '../useEventListener' + +export function useElementHover(el: MaybeRef): Ref { + const isHovered = ref(false) + + useEventListener(el, 'mouseenter', () => isHovered.value = true) + useEventListener(el, 'mouseleave', () => isHovered.value = false) + + return isHovered +} diff --git a/packages/functions.md b/packages/functions.md index 18b857a158c4..8813f1058745 100644 --- a/packages/functions.md +++ b/packages/functions.md @@ -74,6 +74,7 @@ - [`useDocumentVisibility`](https://vueuse.org/core/useDocumentVisibility/) — reactively track [`document.visibilityState`](https://developer.mozilla.org/en-US/docs/Web/API/Document/visibilityState) - [`useDraggable`](https://vueuse.org/core/useDraggable/) — make elements draggable - [`useElementBounding`](https://vueuse.org/core/useElementBounding/) — reactive [bounding box](https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect) of an HTML element + - [`useElementHover`](https://vueuse.org/core/useElementHover/) — reactive element's hover state - [`useElementSize`](https://vueuse.org/core/useElementSize/) — reactive size of an HTML element - [`useElementVisibility`](https://vueuse.org/core/useElementVisibility/) — tracks the visibility of an element within the viewport - [`useGeolocation`](https://vueuse.org/core/useGeolocation/) — reactive [Geolocation API](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API)