Skip to content
On this page

useMouse

Category
Export Size
1.29 kB
Last Changed
2 months ago

Reactive mouse position

Demo

x: 0
y: 0
sourceType: null

Basic Usage

js
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.

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

Component Usage

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

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

Type Declarations

typescript
export interface UseMouseOptions
  extends ConfigurableWindow,
    ConfigurableEventFilter {
  /**
   * Mouse position based by page, client, or relative to previous position
   *
   * @default 'page'
   */
  type?: "page" | "client" | "movement"
  /**
   * 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 type MouseSourceType = "mouse" | "touch" | null
/**
 * Reactive mouse position.
 *
 * @see https://vueuse.org/useMouse
 * @param options
 */
export declare function useMouse(options?: UseMouseOptions): {
  x: Ref<number>
  y: Ref<number>
  sourceType: Ref<MouseSourceType>
}
export type UseMouseReturn = ReturnType<typeof useMouse>
export interface UseMouseOptions
  extends ConfigurableWindow,
    ConfigurableEventFilter {
  /**
   * Mouse position based by page, client, or relative to previous position
   *
   * @default 'page'
   */
  type?: "page" | "client" | "movement"
  /**
   * 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 type MouseSourceType = "mouse" | "touch" | null
/**
 * Reactive mouse position.
 *
 * @see https://vueuse.org/useMouse
 * @param options
 */
export declare function useMouse(options?: UseMouseOptions): {
  x: Ref<number>
  y: Ref<number>
  sourceType: Ref<MouseSourceType>
}
export type UseMouseReturn = ReturnType<typeof useMouse>

Source

SourceDemoDocs

Contributors

Anthony Fu
Jelf
vaakian X
Sergey Danilchenko
lzdFeiFei
Scott Bedard
Marshall Thompson
Shinigami
wheat
Alex Kozack
Antério Vieira

Changelog

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)
v8.9.3 on 7/14/2022
4d2c8 - fix!: rename type MouseOptions to UseMouseOptions (#1877)
v7.7.0 on 2/26/2022
3927d - feat: support eventFilter (#1261)

Released under the MIT License.