vael-ui v0.5.7

Used with a component · SwipeToReveal

useSwipeReveal

The drag engine behind <SwipeToReveal>. You drag a row's content sideways to uncover an actions panel on its leading or trailing edge. A press must move 8 px before it counts as a drag, and a mostly vertical move scrolls the list instead. On release, the row opens or closes based on how fast and how far you dragged. The component adds the action panels, their measured widths, focus handling and the change event.

Install

import { useSwipeReveal } from 'vael-ui'

Examples

<script setup lang="ts">
import { ref } from 'vue'
import { useSwipeReveal } from 'vael-ui'

const open = ref(false)
const { offset, onContentPointerdown, onContentClick } = useSwipeReveal(open, {
  leadingWidth: 0,
  trailingWidth: 96,
})
</script>

<template>
  <div class="row">
    <div class="row-actions"><button>Delete</button></div>
    <div
      class="row-content"
      :style="{ transform: `translateX(${offset}px)` }"
      @pointerdown="onContentPointerdown"
      @click.capture="onContentClick"
    >
      Invoice #1042
    </div>
  </div>
</template>

<style>
.row { position: relative; overflow: hidden; }
.row-actions { position: absolute; inset-block: 0; inset-inline-end: 0; inline-size: 96px; }
/* pan-y keeps vertical list scrolling native while the row takes horizontal drags. */
.row-content { position: relative; touch-action: pan-y; }
</style>

Parameters

NameTypeDescription
openRef<boolean>Whether the row is open (first argument). It is written on every settled release, reveal() and close(). Setting it yourself opens or closes the row at its default side.
leadingMaybeRefOrGetter<boolean>Whether a leading actions panel exists to reveal. Default false.
trailingMaybeRefOrGetter<boolean>Whether a trailing actions panel exists to reveal. Default true.
leadingWidthMaybeRefOrGetter<number>The leading panel's width in px, its fully open position. A getter that reads the panel's offsetWidth keeps it measured. Required.
trailingWidthMaybeRefOrGetter<number>The trailing panel's width in px. Required.
disabledMaybeRefOrGetter<boolean>Ignores new presses while true. Default false.
onCommit(openSide: 'leading' | 'trailing' | null) => voidFires once per settled interaction with the side left open, or null when the row closed. reveal() and close() fire it too.

Returns

NameTypeDescription
offsetRef<number>The live offset in px for translateX(): positive shows the leading panel, negative the trailing one, 0 is closed. Past either end it stretches with rubber-band resistance.
openSideRef<'leading' | 'trailing' | null>The side that is open or settling open, or null when closed.
onContentPointerdown(event: PointerEvent) => voidBind it on the content. Only the primary button starts tracking.
onContentClick(event: MouseEvent) => voidBind it as @click.capture on the content. It swallows the click that follows a drag, and a tap on an open row closes it instead of reaching the row's own click.
reveal(side?: 'leading' | 'trailing') => voidOpens one side. With a single panel you can leave out side; with both, the default is trailing. It does nothing for a side that has no panel.
close() => voidCloses the row.
isDraggingRef<boolean>true once a press passes the 8 px drag threshold, until release. A plain tap never sets it.