vael-ui v0.5.7

Used with a component · PullToRefresh

usePullToRefresh

The gesture engine and state machine behind <PullToRefresh>. A downward drag at scroll top pulls a distance you render, then a release past threshold runs onRefresh. Its state goes from idle to pulling, ready, loading and done, then back to idle. Touch drives it through a non-passive touchmove, the one way to hold back the browser's own scroll and bounce. A mouse drags it through pointer events. <PullToRefresh> adds the pull zone, the arrow and loader indicator and the #indicator slot.

Install

import { usePullToRefresh } from 'vael-ui'

Examples

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

const scrollEl = useTemplateRef<HTMLElement>('scroller')
const { state, pullDistance } = usePullToRefresh({
  scrollEl,
  onRefresh: () => fetchInbox(),
})
</script>

<template>
  <div ref="scroller" class="inbox">
    <div class="pull-zone" :style="{ blockSize: `${pullDistance}px` }">
      {{ state === 'loading' ? 'Refreshing' : state === 'ready' ? 'Release to refresh' : '' }}
    </div>
    <ul>...</ul>
  </div>
</template>

Parameters

NameTypeDescription
scrollElRef<HTMLElement | null>The scroll container. A pull starts only when it rests at scrollTop 0 and the drag moves down at least 8 px. Scrolling away mid drag ends the pull.
onRefresh() => Promise<void> | voidRuns the refresh, while state is loading. A rejection still settles back to idle and the error is dropped, so handle it inside. Required.
thresholdMaybeRefOrGetter<number | undefined>The pull distance in px at which a release refreshes. A quick flick past half of it refreshes too. Default 60.
maxPullMaybeRefOrGetter<number | undefined>The pull in px past which it meets rubber-band resistance. The zone holds at this height while loading. Default 80.
doneHoldMsMaybeRefOrGetter<number | undefined>How long done shows, in ms, before returning to idle. Default 650.

Returns

NameTypeDescription
stateRef<'idle' | 'pulling' | 'ready' | 'loading' | 'done'>Where the pull is. ready means a release now refreshes.
pullDistanceRef<number>The current pull in px, damped past maxPull. Bind it to the zone height.
progressComputedRef<number>The pull from 0 to 1, relative to threshold, not maxPull. It reaches 1 exactly where ready starts.
refresh() => Promise<void>Runs the same loading and done sequence a release does, from code. It does nothing while a refresh is already running or showing done.