Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useMouseInElement.md
1---2category: Elements3---45# useMouseInElement67Reactive mouse position related to an element89## Usage1011```vue12<script setup lang="ts">13import { useMouseInElement } from '@vueuse/core'14import { useTemplateRef } from 'vue'1516const target = useTemplateRef('target')1718const { x, y, isOutside } = useMouseInElement(target)19</script>2021<template>22<div ref="target">23<h1>Hello world</h1>24</div>25</template>26```2728## Component Usage2930```vue31<template>32<UseMouseInElement v-slot="{ elementX, elementY, isOutside }">33x: {{ elementX }}34y: {{ elementY }}35Is Outside: {{ isOutside }}36</UseMouseInElement>37</template>38```3940## Directive Usage4142```vue43<script setup lang="ts">44import { vMouseInElement } from '@vueuse/components'45import { UseMouseSourceType } from '@vueuse/core'4647interface MouseInElementType {48x: number49y: number50sourceType: UseMouseSourceType51elementX: number52elementY: number53elementPositionX: number54elementPositionY: number55elementHeight: number56elementWidth: number57isOutside: boolean58}5960const options = {61handleOutside: true62}63function onMouseInElement({ x, y, sourceType, elementX, elementY, elementPositionX, elementPositionY, elementHeight, elementWidth, isOutside }: MouseInElementType) {64console.log(x, y, sourceType, elementX, elementY, elementPositionX, elementPositionY, elementHeight, elementWidth, isOutside)65}66</script>6768<template>69<textarea v-mouse-in-element="onMouseInElement" />70<!-- with options -->71<textarea v-mouse-in-element="[onMouseInElement, options]" />72</template>73```7475## Type Declarations7677```ts78export interface MouseInElementOptions extends UseMouseOptions {79/**80* Whether to handle mouse events when the cursor is outside the target element.81* When enabled, mouse position will continue to be tracked even when outside the element bounds.82*83* @default true84*/85handleOutside?: boolean86/**87* Listen to window resize event88*89* @default true90*/91windowScroll?: boolean92/**93* Listen to window scroll event94*95* @default true96*/97windowResize?: boolean98}99export interface UseMouseInElementReturn extends UseMouseReturn {100elementX: ShallowRef<number>101elementY: ShallowRef<number>102elementPositionX: ShallowRef<number>103elementPositionY: ShallowRef<number>104elementHeight: ShallowRef<number>105elementWidth: ShallowRef<number>106isOutside: ShallowRef<boolean>107stop: () => void108}109/**110* Reactive mouse position related to an element.111*112* @see https://vueuse.org/useMouseInElement113* @param target114* @param options115*/116export declare function useMouseInElement(117target?: MaybeElementRef,118options?: MouseInElementOptions,119): {120x: ShallowRef<number>121y: ShallowRef<number>122sourceType: ShallowRef<UseMouseSourceType>123elementX: ShallowRef<number, number>124elementY: ShallowRef<number, number>125elementPositionX: ShallowRef<number, number>126elementPositionY: ShallowRef<number, number>127elementHeight: ShallowRef<number, number>128elementWidth: ShallowRef<number, number>129isOutside: ShallowRef<boolean, boolean>130stop: () => void131}132```133