Skip to content

useMouseInElement

Category
Export Size
1.31 kB
Last Changed
last week

Reactive mouse position related to an element

Demo

Hover me
x: 0
y: 0
sourceType: null
elementX: 0
elementY: 0
elementPositionX: 0
elementPositionY: 0
elementHeight: 0
elementWidth: 0
isOutside: true

Usage

vue
<script setup>
import { useMouseInElement } from '@vueuse/core'
import { ref } from 'vue'

const target = ref(null)

const { x, y, isOutside } = useMouseInElement(target)
</script>

<template>
  <div ref="target">
    <h1>Hello world</h1>
  </div>
</template>

Component Usage

This function also provides a renderless component version via the @vueuse/components package. Learn more about the usage.

vue
<template>
  <UseMouseInElement v-slot="{ elementX, elementY, isOutside }">
    x: {{ elementX }}
    y: {{ elementY }}
    Is Outside: {{ isOutside }}
  </UseMouseInElement>
</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 { vMouseInElement } from '@vueuse/components'
import { UseMouseSourceType } from '@vueuse/core'

interface MouseInElementType {
  x: number
  y: number
  sourceType: UseMouseSourceType
  elementX: number
  elementY: number
  elementPositionX: number
  elementPositionY: number
  elementHeight: number
  elementWidth: number
  isOutside: boolean
}

const options = {
  handleOutside: true
}
function onMouseInElement({ x, y, sourceType, elementX, elementY, elementPositionX, elementPositionY, elementHeight, elementWidth, isOutside }: MouseInElementType) {
  console.log(x, y, sourceType, elementX, elementY, elementPositionX, elementPositionY, elementHeight, elementWidth, isOutside)
}
</script>

<template>
  <textarea v-mouse-in-element="onMouseInElement" />
  <!-- with options -->
  <textarea v-mouse-in-element="[onMouseInElement, options]" />
</template>

Type Declarations

typescript
export interface MouseInElementOptions extends UseMouseOptions {
  handleOutside?: boolean
}
/**
 * Reactive mouse position related to an element.
 *
 * @see https://vueuse.org/useMouseInElement
 * @param target
 * @param options
 */
export declare function useMouseInElement(
  target?: MaybeElementRef,
  options?: MouseInElementOptions,
): {
  x: Ref<number, number>
  y: Ref<number, number>
  sourceType: Ref<UseMouseSourceType, UseMouseSourceType>
  elementX: Ref<number, number>
  elementY: Ref<number, number>
  elementPositionX: Ref<number, number>
  elementPositionY: Ref<number, number>
  elementHeight: Ref<number, number>
  elementWidth: Ref<number, number>
  isOutside: Ref<boolean, boolean>
  stop: () => void
}
export type UseMouseInElementReturn = ReturnType<typeof useMouseInElement>

Source

SourceDemoDocs

Contributors

Anthony Fu
wheat
Fernando Fernández
青椒肉丝
Anthony Fu
Jakub Freisler
Coder Poet
Aymoc
Mikhailov Nikita
vaakian X
Jelf
Ivan Agafonov
Shinigami
Alex Kozack
ctholho
Nurettin Kaya
Antério Vieira

Changelog

v12.4.0 on 1/10/2025
dd316 - feat: use passive event handlers everywhere is possible (#4477)
v12.3.0 on 1/2/2025
34be5 - feat: added directive for vMouseInElement (#4438)
v12.0.0-beta.1 on 11/21/2024
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v11.1.0 on 9/16/2024
4dcc2 - fix: allow el to be instanceof Element (#4189)
v11.0.0-beta.2 on 7/17/2024
efe4d - fix: el.getBoundingClientRect is not a function (#4056)
v10.7.0 on 12/5/2023
fccf2 - feat: upgrade deps (#3614)
v10.6.0 on 11/9/2023
7c88d - fix: optimize outside computation (#3380)
v9.12.0 on 1/29/2023
54f4e - fix: check if mouse out of window (#2458)
v9.11.0 on 1/17/2023
d5321 - fix(components): mark defineComponent as pure (#2623)

Released under the MIT License.