Skip to content

useMousePressed

Category
Export Size
837 B
Last Changed
6 months ago

Reactive mouse pressing state. Triggered by mousedown touchstart on target element and released by mouseup mouseleave touchend touchcancel on window.

Demo

pressed: false
sourceType: null
Tracking on

Basic Usage

ts
import { 
useMousePressed
} from '@vueuse/core'
const {
pressed
} =
useMousePressed
()

Touching is enabled by default. To make it only detects mouse changes, set touch to false

ts
const { 
pressed
} =
useMousePressed
({
touch
: false })

To only capture mousedown and touchstart on specific element, you can specify target by passing a ref of the element.

vue
<script setup lang="ts">
import { 
useTemplateRef
} from 'vue'
const
el
=
useTemplateRef
<HTMLDivElement>('el')
const {
pressed
} =
useMousePressed
({
target
:
el
})
</script> <template> <
div
ref
="
el
">
Only clicking on this element will trigger the update. </
div
>
</template>

Component Usage

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

vue
<template>
  <UseMousePressed v-slot="{ 
pressed
}">
Is Pressed: {{
pressed
}}
</UseMousePressed> </template>

Type Declarations

Show Type Declarations
ts
export interface MousePressedOptions extends ConfigurableWindow {
  /**
   * Listen to `touchstart` `touchend` events
   *
   * @default true
   */
  
touch
?: boolean
/** * Listen to `dragstart` `drop` and `dragend` events * * @default true */
drag
?: boolean
/** * Add event listerners with the `capture` option set to `true` * (see [MDN](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#capture)) * * @default false */
capture
?: boolean
/** * Initial values * * @default false */
initialValue
?: boolean
/** * Element target to be capture the click */
target
?:
MaybeComputedElementRef
/** * Callback to be called when the mouse is pressed * * @param event */
onPressed
?: (
event
: MouseEvent | TouchEvent | DragEvent) => void
/** * Callback to be called when the mouse is released * * @param event */
onReleased
?: (
event
: MouseEvent | TouchEvent | DragEvent) => void
} /** * Reactive mouse pressing state. * * @see https://vueuse.org/useMousePressed * @param options */ export declare function
useMousePressed
(
options
?: MousePressedOptions): {
pressed
:
ShallowRef
<boolean, boolean>
sourceType
:
ShallowRef
<
UseMouseSourceType
,
UseMouseSourceType
>
} export type
UseMousePressedReturn
=
ReturnType
<typeof
useMousePressed
>

Source

SourceDemoDocs

Contributors

Anthony Fu
IlyaL
wheat
丶远方
SerKo
IlyaL
Robin
Fernando Fernández
Robin
Anthony Fu
Meet you
Chris-Robin Ennen
Jonas Schade
RAX7
ByMykel
vaakian X
MinatoHikari
Marshall Thompson
Shinigami
Alex Kozack

Changelog

v12.4.0 on
dd316 - feat: use passive event handlers everywhere is possible (#4477)
v12.3.0 on
a123a - feat: add onPressed and onReleased as options (#4425)
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.7.0 on
17f97 - fix: change type of element parameter to MaybeComputedElementRef (#3566)
v10.5.0 on
d5c81 - feat: add capture option (#3392)
v10.1.0 on
4bb5b - feat(useMouse): support custom event extractor (#2991)

Released under the MIT License.

FREE WEEKEND
48 Hours of Unlimited Official Vue.js Certification Training
Reserve Your Spot
23-24 August