Skip to content

v-pan

Pan/drag gesture detection.

Since: 1.5.0

Usage

Basic

vue
<template>
  <div v-pan="handlePan">Swipe me</div>
</template>

<script setup>
function handlePan(e) {
  console.log('Direction:', e.direction)
  console.log('Distance:', e.distance)
}
</script>

With Options

vue
<template>
  <div v-pan="{
    onPan: handlePan,
    direction: 'horizontal',
    threshold: 20
  }">
    Horizontal only
  </div>
</template>

API

Options

OptionTypeDefaultDescription
onPan(event: PanEvent) => void-Pan event callback
direction'horizontal' | 'vertical' | 'all''all'Allowed directions
thresholdnumber10Minimum distance to trigger
disabledbooleanfalseDisable pan detection

Pan Event

ts
interface PanEvent {
  /** Direction: 'left' | 'right' | 'up' | 'down' */
  direction: string
  /** Distance from start point */
  distance: number
  /** Delta X from last position */
  deltaX: number
  /** Delta Y from last position */
  deltaY: number
  /** Total X offset */
  offsetX: number
  /** Total Y offset */
  offsetY: number
  /** Whether pan is in progress */
  isPanning: boolean
  /** Original event */
  event: MouseEvent | TouchEvent
}

Examples

Horizontal Slider

vue
<template>
  <div v-pan="{ onPan: handlePan, direction: 'horizontal' }" class="slider">
    <div :style="{ transform: `translateX(${offsetX}px)` }">
      Slider content
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const offsetX = ref(0)

function handlePan(e) {
  offsetX.value += e.deltaX
}
</script>

Pull to Reveal

vue
<template>
  <div v-pan="{ onPan: handlePan, direction: 'vertical' }">
    Pull down to reveal
  </div>
</template>

Composable API

For programmatic use, you can use the usePan composable:

typescript
import { usePan } from 'directix'

const { isPanning, deltaX, deltaY, direction, bind } = usePan({
  direction: 'horizontal',
  onPan: handlePan
})

// Bind to element
onMounted(() => bind(elementRef.value))

UsePanReturn

PropertyTypeDescription
isPanningRef<boolean>Whether pan is in progress
deltaXRef<number>Current delta X
deltaYRef<number>Current delta Y
directionRef<string>Current direction
bind(element: HTMLElement) => () => voidBind to element

Code Generator

Quick Code Generator
<template>
  <div v-pan>
    <!-- Pan/drag gesture detection. directive -->
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'

// Configure your options here
const options = {}
</script>

Released under the MIT License.