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/onLongPress.md
1---2category: Sensors3---45# onLongPress67Listen for a long press on an element. Returns a stop function.89## Usage1011```vue12<script setup lang="ts">13import { onLongPress } from '@vueuse/core'14import { shallowRef, useTemplateRef } from 'vue'1516const htmlRefHook = useTemplateRef('htmlRefHook')17const longPressedHook = shallowRef(false)1819function onLongPressCallbackHook(e: PointerEvent) {20longPressedHook.value = true21}22function resetHook() {23longPressedHook.value = false24}2526onLongPress(27htmlRefHook,28onLongPressCallbackHook,29{30modifiers: {31prevent: true32}33}34)35</script>3637<template>38<p>Long Pressed: {{ longPressedHook }}</p>3940<button ref="htmlRefHook" class="ml-2 button small">41Press long42</button>4344<button class="ml-2 button small" @click="resetHook">45Reset46</button>47</template>48```4950### Custom Delay5152By default, the handler fires after 500ms. You can customize this with the `delay` option. It can be a number or a function that receives the `PointerEvent`.5354```ts55import { onLongPress } from '@vueuse/core'5657// Fixed delay58onLongPress(target, handler, { delay: 1000 })5960// Dynamic delay based on event61onLongPress(target, handler, {62delay: ev => ev.pointerType === 'touch' ? 800 : 500,63})64```6566### Distance Threshold6768The long press will be canceled if the pointer moves more than the threshold (default: 10 pixels). Set to `false` to disable movement detection.6970```ts71import { onLongPress } from '@vueuse/core'7273// Custom threshold74onLongPress(target, handler, { distanceThreshold: 20 })7576// Disable movement detection77onLongPress(target, handler, { distanceThreshold: false })78```7980### On Mouse Up Callback8182You can provide an `onMouseUp` callback to be notified when the pointer is released.8384```ts85import { onLongPress } from '@vueuse/core'8687onLongPress(target, handler, {88onMouseUp(duration, distance, isLongPress) {89console.log(`Held for ${duration}ms, moved ${distance}px, long press: ${isLongPress}`)90},91})92```9394### Modifiers9596The following modifiers are available:9798| Modifier | Description |99| --------- | -------------------------------------------- |100| `stop` | Calls `event.stopPropagation()` |101| `once` | Removes event listener after first trigger |102| `prevent` | Calls `event.preventDefault()` |103| `capture` | Uses capture mode for event listener |104| `self` | Only trigger if target is the element itself |105106```ts107onLongPress(target, handler, {108modifiers: {109prevent: true,110stop: true,111},112})113```114115## Component Usage116117```vue118<script setup lang="ts">119import { OnLongPress } from '@vueuse/components'120import { shallowRef } from 'vue'121122const longPressedComponent = shallowRef(false)123124function onLongPressCallbackComponent(e: PointerEvent) {125longPressedComponent.value = true126}127function resetComponent() {128longPressedComponent.value = false129}130</script>131132<template>133<p>Long Pressed: {{ longPressedComponent }}</p>134135<OnLongPress136as="button"137class="ml-2 button small"138@trigger="onLongPressCallbackComponent"139>140Press long141</OnLongPress>142143<button class="ml-2 button small" @click="resetComponent">144Reset145</button>146</template>147```148149## Directive Usage150151```vue152<script setup lang="ts">153import { vOnLongPress } from '@vueuse/components'154import { shallowRef } from 'vue'155156const longPressedDirective = shallowRef(false)157158function onLongPressCallbackDirective(e: PointerEvent) {159longPressedDirective.value = true160}161function resetDirective() {162longPressedDirective.value = false163}164</script>165166<template>167<p>Long Pressed: {{ longPressedDirective }}</p>168169<button170v-on-long-press.prevent="onLongPressCallbackDirective"171class="ml-2 button small"172>173Press long174</button>175176<button177v-on-long-press="[onLongPressCallbackDirective, { delay: 1000, modifiers: { stop: true } }]"178class="ml-2 button small"179>180Press long (with options)181</button>182183<button class="ml-2 button small" @click="resetDirective">184Reset185</button>186</template>187```188189## Type Declarations190191```ts192export interface OnLongPressOptions {193/**194* Time in ms till `longpress` gets called195*196* @default 500197*/198delay?: number | ((ev: PointerEvent) => number)199modifiers?: OnLongPressModifiers200/**201* Allowance of moving distance in pixels,202* The action will get canceled When moving too far from the pointerdown position.203* @default 10204*/205distanceThreshold?: number | false206/**207* Function called when the ref element is released.208* @param duration how long the element was pressed in ms209* @param distance distance from the pointerdown position210* @param isLongPress whether the action was a long press or not211*/212onMouseUp?: (duration: number, distance: number, isLongPress: boolean) => void213}214export interface OnLongPressModifiers {215stop?: boolean216once?: boolean217prevent?: boolean218capture?: boolean219self?: boolean220}221export type OnLongPressReturn = () => void222/** @deprecated use {@link OnLongPressReturn} instead */223export type UseOnLongPressReturn = OnLongPressReturn224export declare function onLongPress(225target: MaybeElementRef,226handler: (evt: PointerEvent) => void,227options?: OnLongPressOptions,228): OnLongPressReturn229```230