Skip to content

useMouse

Category
Export Size
825 B
Last Changed
yesterday

Reactive mouse position

Demo

Basic Usage

x: 0
y: 0
sourceType: null

Extractor Usage

x: 0
y: 0
sourceType: null

Basic Usage

ts
import { 
useMouse
} from '@vueuse/core'
const {
x
,
y
,
sourceType
} =
useMouse
()

Touch is enabled by default. To only detect mouse changes, set touch to false. The dragover event is used to track mouse position while dragging.

ts
const { 
x
,
y
} =
useMouse
({
touch
: false })

Custom Extractor

It's also possible to provide a custom extractor function to get the position from the event.

ts
import type { 
UseMouseEventExtractor
} from '@vueuse/core'
import {
useMouse
,
useParentElement
} from '@vueuse/core'
const
parentEl
=
useParentElement
()
const
extractor
:
UseMouseEventExtractor
=
event
=> (
event
instanceof
Touch
? null : [
event
.
offsetX
,
event
.
offsetY
]
) const {
x
,
y
,
sourceType
} =
useMouse
({
target
:
parentEl
,
type
:
extractor
})
js
import { useMouse, useParentElement } from '@vueuse/core'
const parentEl = useParentElement()
const extractor = (event) =>
  event instanceof Touch ? null : [event.offsetX, event.offsetY]
const { x, y, sourceType } = useMouse({ target: parentEl, type: extractor })

Component Usage

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

vue
<template>
  <UseMouse v-slot="{ x, y }">
    x: {{ x }}
    y: {{ y }}
  </UseMouse>
</template>

Type Declarations

Show Type Declarations
typescript
export type UseMouseCoordType = "page" | "client" | "screen" | "movement"
export type UseMouseSourceType = "mouse" | "touch" | null
export type UseMouseEventExtractor = (
  event: MouseEvent | Touch,
) => [x: number, y: number] | null | undefined
export interface UseMouseOptions
  extends ConfigurableWindow,
    ConfigurableEventFilter {
  /**
   * Mouse position based by page, client, screen, or relative to previous position
   *
   * @default 'page'
   */
  type?: UseMouseCoordType | UseMouseEventExtractor
  /**
   * Listen events on `target` element
   *
   * @default 'Window'
   */
  target?: MaybeRefOrGetter<Window | EventTarget | null | undefined>
  /**
   * Listen to `touchmove` events
   *
   * @default true
   */
  touch?: boolean
  /**
   * Listen to `scroll` events on window, only effective on type `page`
   *
   * @default true
   */
  scroll?: boolean
  /**
   * Reset to initial value when `touchend` event fired
   *
   * @default false
   */
  resetOnTouchEnds?: boolean
  /**
   * Initial values
   */
  initialValue?: Position
}
/**
 * Reactive mouse position.
 *
 * @see https://vueuse.org/useMouse
 * @param options
 */
export declare function useMouse(options?: UseMouseOptions): {
  x: Ref<number, number>
  y: Ref<number, number>
  sourceType: Ref<UseMouseSourceType, UseMouseSourceType>
}
export type UseMouseReturn = ReturnType<typeof useMouse>

Source

SourceDemoDocs

Contributors

Anthony Fu
青椒肉丝
Jelf
Anthony Fu
Eduardo Luiz Schilling
karukenert
Cats Juice
丶远方
RAX7
François Lévesque
vaakian X
Sergey Danilchenko
lzdFeiFei
Scott Bedard
Marshall Thompson
Shinigami
wheat
Alex Kozack
Antério Vieira

Changelog

v12.0.0-beta.1 on 11/21/2024
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v11.3.0 on 11/21/2024
cceeb - fix: correct spelling error (#4348)
e8d11 - fix: record the previous scroll value (#4244)
v10.4.0 on 8/25/2023
c2f64 - fix: position won't be changed on page scroll when type is page, closes #2922 (#3244)
v10.1.0 on 4/22/2023
4bb5b - feat: support custom event extractor (#2991)
v10.0.0-beta.4 on 4/13/2023
39d18 - feat: support type: screen (#2467)
v9.11.0 on 1/17/2023
d5321 - fix(components): mark defineComponent as pure (#2623)
v9.10.0 on 1/3/2023
0c39d - feat: support for movement detection (#2589)

Released under the MIT License.