Skip to content

onLongPress

Category
Export Size
1.02 kB
Last Changed
4 days ago

Listen for a long press on an element. Returns a stop function.

Demo

Long Pressed: false

Clicked: false

Usage

vue
<script setup lang="ts">
import { 
onLongPress
} from '@vueuse/core'
import {
shallowRef
,
useTemplateRef
} from 'vue'
const
htmlRefHook
=
useTemplateRef
('htmlRefHook')
const
longPressedHook
=
shallowRef
(false)
function
onLongPressCallbackHook
(
e
: PointerEvent) {
longPressedHook
.
value
= true
} function
resetHook
() {
longPressedHook
.
value
= false
}
onLongPress
(
htmlRefHook
,
onLongPressCallbackHook
,
{
modifiers
: {
prevent
: true
} } ) </script> <template> <
p
>Long Pressed: {{
longPressedHook
}}</
p
>
<
button
ref
="
htmlRefHook
"
class
="ml-2 button small">
Press long </
button
>
<
button
class
="ml-2 button small" @
click
="
resetHook
">
Reset </
button
>
</template>

Custom Delay

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

ts
import { 
onLongPress
} from '@vueuse/core'
// Fixed delay
onLongPress
(target, handler, {
delay
: 1000 })
// Dynamic delay based on event
onLongPress
(target, handler, {
delay
:
ev
=>
ev
.
pointerType
=== 'touch' ? 800 : 500,
})

Distance Threshold

The long press will be canceled if the pointer moves more than the threshold (default: 10 pixels). Set to false to disable movement detection.

ts
import { 
onLongPress
} from '@vueuse/core'
// Custom threshold
onLongPress
(target, handler, {
distanceThreshold
: 20 })
// Disable movement detection
onLongPress
(target, handler, {
distanceThreshold
: false })

On Mouse Up Callback

You can provide an onMouseUp callback to be notified when the pointer is released.

ts
import { 
onLongPress
} from '@vueuse/core'
onLongPress
(target, handler, {
onMouseUp
(
duration
,
distance
,
isLongPress
) {
console
.
log
(`Held for ${
duration
}ms, moved ${
distance
}px, long press: ${
isLongPress
}`)
}, })

Modifiers

The following modifiers are available:

ModifierDescription
stopCalls event.stopPropagation()
onceRemoves event listener after first trigger
preventCalls event.preventDefault()
captureUses capture mode for event listener
selfOnly trigger if target is the element itself
ts
onLongPress(target, handler, {
  
modifiers
: {
prevent
: true,
stop
: true,
}, })

Component Usage

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

vue
<script setup lang="ts">
import { 
OnLongPress
} from '@vueuse/components'
import {
shallowRef
} from 'vue'
const
longPressedComponent
=
shallowRef
(false)
function
onLongPressCallbackComponent
(
e
: PointerEvent) {
longPressedComponent
.
value
= true
} function
resetComponent
() {
longPressedComponent
.
value
= false
} </script> <template> <
p
>Long Pressed: {{
longPressedComponent
}}</
p
>
<
OnLongPress
as
="button"
class
="ml-2 button small"
@
trigger
="
onLongPressCallbackComponent
"
> Press long </OnLongPress> <
button
class
="ml-2 button small" @
click
="
resetComponent
">
Reset </
button
>
</template>

Directive Usage

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

vue
<script setup lang="ts">
import { 
vOnLongPress
} from '@vueuse/components'
import {
shallowRef
} from 'vue'
const
longPressedDirective
=
shallowRef
(false)
function
onLongPressCallbackDirective
(
e
: PointerEvent) {
longPressedDirective
.
value
= true
} function
resetDirective
() {
longPressedDirective
.
value
= false
} </script> <template> <
p
>Long Pressed: {{
longPressedDirective
}}</
p
>
<
button
v-on-long-pr
ess
.
prevent
="
onLongPressCallbackDirective
"
class
="ml-2 button small"
> Press long </
button
>
<
button
v-on-long-pr
ess="
[
onLongPressCallbackDirective
, {
delay
: 1000,
modifiers
: {
stop
: true } }]
"
class
="ml-2 button small"
> Press long (with options) </
button
>
<
button
class
="ml-2 button small" @
click
="
resetDirective
">
Reset </
button
>
</template>

Type Declarations

Show Type Declarations
ts
export interface OnLongPressOptions {
  /**
   * Time in ms till `longpress` gets called
   *
   * @default 500
   */
  
delay
?: number | ((
ev
: PointerEvent) => number)
modifiers
?: OnLongPressModifiers
/** * Allowance of moving distance in pixels, * The action will get canceled When moving too far from the pointerdown position. * @default 10 */
distanceThreshold
?: number | false
/** * Function called when the ref element is released. * @param duration how long the element was pressed in ms * @param distance distance from the pointerdown position * @param isLongPress whether the action was a long press or not */
onMouseUp
?: (
duration
: number,
distance
: number,
isLongPress
: boolean) => void
} export interface OnLongPressModifiers {
stop
?: boolean
once
?: boolean
prevent
?: boolean
capture
?: boolean
self
?: boolean
} export type
OnLongPressReturn
= () => void
/** @deprecated use {@link OnLongPressReturn} instead */ export type
UseOnLongPressReturn
=
OnLongPressReturn
export declare function
onLongPress
(
target
:
MaybeElementRef
,
handler
: (
evt
: PointerEvent) => void,
options
?: OnLongPressOptions,
):
OnLongPressReturn

Source

SourceDemoDocs

Contributors

Anthony Fu
Anthony Fu
Vida Xie
webfansplz
IlyaL
NoiseFan
James Garbutt
Rainbow
SerKo
Robin
IlyaL
OrbisK
Neil Richter
GojkoGalonja
Doctor Wu
donaldkicksyourass
huiliangShen
Lee Crosby
丶远方
vaakian X
sun0day
Yugang Cao
Mikhailov Nikita
三咲智子
wheat
AllenYu
余小磊

Changelog

v14.0.0-alpha.2 on
e5f74 - feat!: deprecate alias exports in favor of original function names (#5009)
v14.0.0-alpha.0 on
8c521 - feat(components)!: refactor components and make them consistent (#4912)
415f3 - feat: allow function as value in delay (#4979)
v13.4.0 on
319d8 - feat(shared): Introduce TimerHandle for setTimeout type (#4801)
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.10.0 on
7346a - feat: options.onMouseUp callback (#3791)
v10.7.0 on
0e04a - feat: add distanceThreshold option (#3578)
v10.6.0 on
8eb0b - feat(onLongClick): return stop function (#3506)

Released under the MIT License.

FREE Weekend
NOW LIVE: Unlimited access to Mid-Level JavaScript Certification training
Start Now
41
hours
:
55
minutes
:
22
seconds
: