Skip to content
On this page

useMouse

Category
Last Changed
19 days ago

Reactive mouse position

Demo

x: 0
'y': 0
sourceType: null

Basic Usage

import { useMouse } from '@vueuse/core'

const { x, y, sourceType } = useMouse()
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.

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

Component

This function also provides a renderless component version via the @vueuse/components package. Learn more about the usage.
<UseMouse v-slot="{ x, y }">
  x: {{ x }}
  y: {{ y }}
</UseMouse>
<UseMouse v-slot="{ x, y }">
  x: {{ x }}
  y: {{ y }}
</UseMouse>

Type Declarations

export interface MouseOptions
  extends ConfigurableWindow,
    ConfigurableEventFilter {
  /**
   * Mouse position based by page or client
   *
   * @default 'page'
   */
  type?: "page" | "client"
  /**
   * Listen to `touchmove` events
   *
   * @default true
   */
  touch?: boolean
  /**
   * Reset to initial value when `touchend` event fired
   *
   * @default false
   */
  resetOnTouchEnds?: boolean
  /**
   * Initial values
   */
  initialValue?: Position
}
export declare type MouseSourceType = "mouse" | "touch" | null
/**
 * Reactive mouse position.
 *
 * @see https://vueuse.org/useMouse
 * @param options
 */
export declare function useMouse(options?: MouseOptions): {
  x: Ref<number>
  y: Ref<number>
  sourceType: Ref<MouseSourceType>
}
export declare type UseMouseReturn = ReturnType<typeof useMouse>
export interface MouseOptions
  extends ConfigurableWindow,
    ConfigurableEventFilter {
  /**
   * Mouse position based by page or client
   *
   * @default 'page'
   */
  type?: "page" | "client"
  /**
   * Listen to `touchmove` events
   *
   * @default true
   */
  touch?: boolean
  /**
   * Reset to initial value when `touchend` event fired
   *
   * @default false
   */
  resetOnTouchEnds?: boolean
  /**
   * Initial values
   */
  initialValue?: Position
}
export declare type MouseSourceType = "mouse" | "touch" | null
/**
 * Reactive mouse position.
 *
 * @see https://vueuse.org/useMouse
 * @param options
 */
export declare function useMouse(options?: MouseOptions): {
  x: Ref<number>
  y: Ref<number>
  sourceType: Ref<MouseSourceType>
}
export declare type UseMouseReturn = ReturnType<typeof useMouse>

Source

SourceDemoDocs

Contributors

Anthony Fu
lzdFeiFei
Scott Bedard
jelf
Marshall Thompson
Shinigami
wheat
Alex Kozack
Antério Vieira

Changelog

v7.7.0 on 2/26/2022
3927d - feat: support eventFilter (#1261)
v7.2.0 on 12/8/2021
819f4 - feat: mouse position based by option.type (#993)
v6.4.0 on 9/17/2021
9a45b - fix: exports common types
v5.1.0 on 7/5/2021
bd745 - feat: useMouse supports drag and drop (#592)
v4.11.2 on 5/30/2021
68c7d - feat(typedef): add return typedefs (#543) (#544)
v5.0.0-beta.2 on 5/25/2021
5bede - feat: introduce components & directives (#486)
useMouse has loaded