Pull to Refresh
Preview
Activity
Pull down to check for updates
Motion review approved
4mThe pull request is ready to merge.
New component feedback
18mThe spring feels much closer to native now.
Registry checks passed
31mAll component files resolved successfully.
Preview deployed
1hThe latest build is ready to inspect.
Docs comment resolved
2hThe usage example now covers async refreshes.
<script setup lang="ts">
import { PullToRefresh } from 'beui-vue'
import { computed, ref, defineComponent, h, type PropType } from 'vue'
/** Inline Lucide glyphs for the layout demos (generated from lucide-react, ISC). Inner markup of 24x24 stroked icons. */
const ICONS: Record<string, string> = {
"ArrowRight": "<path d=\"M5 12h14\"></path><path d=\"m12 5 7 7-7 7\"></path>",
"ArrowUp": "<path d=\"m5 12 7-7 7 7\"></path><path d=\"M12 19V5\"></path>",
"ArrowUpRight": "<path d=\"M7 7h10v10\"></path><path d=\"M7 17 17 7\"></path>",
"BarChart3": "<path d=\"M3 3v16a2 2 0 0 0 2 2h16\"></path><path d=\"M18 17V9\"></path><path d=\"M13 17V5\"></path><path d=\"M8 17v-3\"></path>",
"Bell": "<path d=\"M10.268 21a2 2 0 0 0 3.464 0\"></path><path d=\"M3.262 15.326A1 1 0 0 0 4 17h16a1 1 0 0 0 .74-1.673C19.41 13.956 18 12.499 18 8A6 6 0 0 0 6 8c0 4.499-1.411 5.956-2.738 7.326\"></path>",
"Braces": "<path d=\"M8 3H7a2 2 0 0 0-2 2v5a2 2 0 0 1-2 2 2 2 0 0 1 2 2v5c0 1.1.9 2 2 2h1\"></path><path d=\"M16 21h1a2 2 0 0 0 2-2v-5c0-1.1.9-2 2-2a2 2 0 0 1-2-2V5a2 2 0 0 0-2-2h-1\"></path>",
"Building2": "<path d=\"M10 12h4\"></path><path d=\"M10 8h4\"></path><path d=\"M14 21v-3a2 2 0 0 0-4 0v3\"></path><path d=\"M6 10H4a2 2 0 0 0-2 2v7a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2V9a2 2 0 0 0-2-2h-2\"></path><path d=\"M6 21V5a2 2 0 0 1 2-2h8a2 2 0 0 1 2 2v16\"></path>",
"Calendar": "<path d=\"M8 2v3\"></path><path d=\"M16 2v3\"></path><rect x=\"3\" y=\"3\" width=\"18\" height=\"18\" rx=\"2\"></rect><path d=\"M3 9h18\"></path>",
"CalendarClock": "<path d=\"M16 14v2.2l1.6 1\"></path><path d=\"M16 2v3\"></path><path d=\"M21 7.338V5a2 2 0 00-2-2H5a2 2 0 00-2 2v14a2 2 0 002 2h2.338\"></path><path d=\"M3 9h5.859\"></path><path d=\"M8 2v3\"></path><circle cx=\"16\" cy=\"16\" r=\"6\"></circle>",
"Check": "<path d=\"M20 6 9 17l-5-5\"></path>",
"CheckCircle2": "<circle cx=\"12\" cy=\"12\" r=\"10\"></circle><path d=\"m9 12 2 2 4-4\"></path>",
"ChevronDown": "<path d=\"m6 9 6 6 6-6\"></path>",
"ChevronLeft": "<path d=\"m15 18-6-6 6-6\"></path>",
"ChevronRight": "<path d=\"m9 18 6-6-6-6\"></path>",
"ChevronUp": "<path d=\"m18 15-6-6-6 6\"></path>",
"ChevronsUpDown": "<path d=\"m7 15 5 5 5-5\"></path><path d=\"m7 9 5-5 5 5\"></path>",
"Circle": "<circle cx=\"12\" cy=\"12\" r=\"10\"></circle>",
"CircleUserRound": "<path d=\"M17.925 20.056a6 6 0 0 0-11.851.001\"></path><circle cx=\"12\" cy=\"11\" r=\"4\"></circle><circle cx=\"12\" cy=\"12\" r=\"10\"></circle>",
"Command": "<path d=\"M15 6v12a3 3 0 1 0 3-3H6a3 3 0 1 0 3 3V6a3 3 0 1 0-3 3h12a3 3 0 1 0-3-3\"></path>",
"File": "<path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path>",
"FileCode2": "<path d=\"M4 12.15V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.706.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2h-3.35\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path><path d=\"m5 16-3 3 3 3\"></path><path d=\"m9 22 3-3-3-3\"></path>",
"FileJson2": "<path d=\"M14 22h4a2 2 0 0 0 2-2V8a2.4 2.4 0 0 0-.706-1.706l-3.588-3.588A2.4 2.4 0 0 0 14 2H6a2 2 0 0 0-2 2v6\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path><path d=\"M5 14a1 1 0 0 0-1 1v2a1 1 0 0 1-1 1 1 1 0 0 1 1 1v2a1 1 0 0 0 1 1\"></path><path d=\"M9 22a1 1 0 0 0 1-1v-2a1 1 0 0 1 1-1 1 1 0 0 1-1-1v-2a1 1 0 0 0-1-1\"></path>",
"FileText": "<path d=\"M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z\"></path><path d=\"M14 2v5a1 1 0 0 0 1 1h5\"></path><path d=\"M10 9H8\"></path><path d=\"M16 13H8\"></path><path d=\"M16 17H8\"></path>",
"Folder": "<path d=\"M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z\"></path>",
"FolderKanban": "<path d=\"M4 20h16a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.93a2 2 0 0 1-1.66-.9l-.82-1.2A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13c0 1.1.9 2 2 2Z\"></path><path d=\"M8 10v4\"></path><path d=\"M12 10v2\"></path><path d=\"M16 10v6\"></path>",
"FolderOpen": "<path d=\"m6 14 1.5-2.9A2 2 0 0 1 9.24 10H20a2 2 0 0 1 1.94 2.5l-1.54 6a2 2 0 0 1-1.95 1.5H4a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h3.9a2 2 0 0 1 1.69.9l.81 1.2a2 2 0 0 0 1.67.9H18a2 2 0 0 1 2 2v2\"></path>",
"GitPullRequest": "<circle cx=\"18\" cy=\"18\" r=\"3\"></circle><circle cx=\"6\" cy=\"6\" r=\"3\"></circle><path d=\"M13 6h3a2 2 0 0 1 2 2v7\"></path><line x1=\"6\" x2=\"6\" y1=\"9\" y2=\"21\"></line>",
"GripVertical": "<circle cx=\"9\" cy=\"12\" r=\"1\"></circle><circle cx=\"9\" cy=\"5\" r=\"1\"></circle><circle cx=\"9\" cy=\"19\" r=\"1\"></circle><circle cx=\"15\" cy=\"12\" r=\"1\"></circle><circle cx=\"15\" cy=\"5\" r=\"1\"></circle><circle cx=\"15\" cy=\"19\" r=\"1\"></circle>",
"Heart": "<path d=\"M2 9.5a5.5 5.5 0 0 1 9.591-3.676.56.56 0 0 0 .818 0A5.49 5.49 0 0 1 22 9.5c0 2.29-1.5 4-3 5.5l-5.492 5.313a2 2 0 0 1-3 .019L5 15c-1.5-1.5-3-3.2-3-5.5\"></path>",
"Home": "<path d=\"M15 21v-8a1 1 0 0 0-1-1h-4a1 1 0 0 0-1 1v8\"></path><path d=\"M3 10a2 2 0 0 1 .709-1.528l7-6a2 2 0 0 1 2.582 0l7 6A2 2 0 0 1 21 10v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z\"></path>",
"House": "<path d=\"M15 21v-8a1 1 0 0 0-1-1h-4a1 1 0 0 0-1 1v8\"></path><path d=\"M3 10a2 2 0 0 1 .709-1.528l7-6a2 2 0 0 1 2.582 0l7 6A2 2 0 0 1 21 10v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z\"></path>",
"Inbox": "<polyline points=\"22 12 16 12 14 15 10 15 8 12 2 12\"></polyline><path d=\"M5.45 5.11 2 12v6a2 2 0 0 0 2 2h16a2 2 0 0 0 2-2v-6l-3.45-6.89A2 2 0 0 0 16.76 4H7.24a2 2 0 0 0-1.79 1.11z\"></path>",
"Layers": "<path d=\"M12.83 2.18a2 2 0 0 0-1.66 0L2.6 6.08a1 1 0 0 0 0 1.83l8.58 3.91a2 2 0 0 0 1.66 0l8.58-3.9a1 1 0 0 0 0-1.83z\"></path><path d=\"M2 12a1 1 0 0 0 .58.91l8.6 3.91a2 2 0 0 0 1.65 0l8.58-3.9A1 1 0 0 0 22 12\"></path><path d=\"M2 17a1 1 0 0 0 .58.91l8.6 3.91a2 2 0 0 0 1.65 0l8.58-3.9A1 1 0 0 0 22 17\"></path>",
"LayoutGrid": "<rect width=\"7\" height=\"7\" x=\"3\" y=\"3\" rx=\"1\"></rect><rect width=\"7\" height=\"7\" x=\"14\" y=\"3\" rx=\"1\"></rect><rect width=\"7\" height=\"7\" x=\"14\" y=\"14\" rx=\"1\"></rect><rect width=\"7\" height=\"7\" x=\"3\" y=\"14\" rx=\"1\"></rect>",
"ListTodo": "<path d=\"M13 5h8\"></path><path d=\"M13 12h8\"></path><path d=\"M13 19h8\"></path><path d=\"m3 17 2 2 4-4\"></path><rect x=\"3\" y=\"4\" width=\"6\" height=\"6\" rx=\"1\"></rect>",
"LoaderCircle": "<path d=\"M21 12a9 9 0 1 1-6.219-8.56\"></path>",
"Mail": "<path d=\"m22 7-8.991 5.727a2 2 0 0 1-2.009 0L2 7\"></path><rect x=\"2\" y=\"4\" width=\"20\" height=\"16\" rx=\"2\"></rect>",
"MessageCircle": "<path d=\"M2.992 16.342a2 2 0 0 1 .094 1.167l-1.065 3.29a1 1 0 0 0 1.236 1.168l3.413-.998a2 2 0 0 1 1.099.092 10 10 0 1 0-4.777-4.719\"></path>",
"Minus": "<path d=\"M5 12h14\"></path>",
"Music": "<path d=\"M9 18V5l12-2v13\"></path><circle cx=\"6\" cy=\"18\" r=\"3\"></circle><circle cx=\"18\" cy=\"16\" r=\"3\"></circle>",
"NotebookTabs": "<path d=\"M2 6h4\"></path><path d=\"M2 10h4\"></path><path d=\"M2 14h4\"></path><path d=\"M2 18h4\"></path><rect width=\"16\" height=\"20\" x=\"4\" y=\"2\" rx=\"2\"></rect><path d=\"M15 2v20\"></path><path d=\"M15 7h5\"></path><path d=\"M15 12h5\"></path><path d=\"M15 17h5\"></path>",
"PackageCheck": "<path d=\"M12 22V12\"></path><path d=\"m16 17 2 2 4-4\"></path><path d=\"M21 11.127V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.729l7 4a2 2 0 0 0 2 .001l1.32-.753\"></path><path d=\"M3.29 7 12 12l8.71-5\"></path><path d=\"m7.5 4.27 8.997 5.148\"></path>",
"Palette": "<path d=\"M12 22a1 1 0 0 1 0-20 10 9 0 0 1 10 9 5 5 0 0 1-5 5h-2.25a1.75 1.75 0 0 0-1.4 2.8l.3.4a1.75 1.75 0 0 1-1.4 2.8z\"></path><circle cx=\"13.5\" cy=\"6.5\" r=\".5\" fill=\"currentColor\"></circle><circle cx=\"17.5\" cy=\"10.5\" r=\".5\" fill=\"currentColor\"></circle><circle cx=\"6.5\" cy=\"12.5\" r=\".5\" fill=\"currentColor\"></circle><circle cx=\"8.5\" cy=\"7.5\" r=\".5\" fill=\"currentColor\"></circle>",
"PanelLeft": "<rect width=\"18\" height=\"18\" x=\"3\" y=\"3\" rx=\"2\"></rect><path d=\"M9 3v18\"></path>",
"Plus": "<path d=\"M5 12h14\"></path><path d=\"M12 5v14\"></path>",
"RadioTower": "<path d=\"M4.9 16.1C1 12.2 1 5.8 4.9 1.9\"></path><path d=\"M7.8 4.7a6.14 6.14 0 0 0-.8 7.5\"></path><circle cx=\"12\" cy=\"9\" r=\"2\"></circle><path d=\"M16.2 4.8c2 2 2.26 5.11.8 7.47\"></path><path d=\"M19.1 1.9a9.96 9.96 0 0 1 0 14.1\"></path><path d=\"M9.5 18h5\"></path><path d=\"m8 22 4-11 4 11\"></path>",
"Rocket": "<path d=\"M12 15v5s3.03-.55 4-2c1.08-1.62 0-5 0-5\"></path><path d=\"M4.5 16.5c-1.5 1.26-2 5-2 5s3.74-.5 5-2c.71-.84.7-2.13-.09-2.91a2.18 2.18 0 0 0-2.91-.09\"></path><path d=\"M9 12a22 22 0 0 1 2-3.95A12.88 12.88 0 0 1 22 2c0 2.72-.78 7.5-6 11a22.4 22.4 0 0 1-4 2z\"></path><path d=\"M9 12H4s.55-3.03 2-4c1.62-1.08 5 .05 5 .05\"></path>",
"Search": "<path d=\"m21 21-4.34-4.34\"></path><circle cx=\"11\" cy=\"11\" r=\"8\"></circle>",
"Settings": "<path d=\"M9.671 4.136a2.34 2.34 0 0 1 4.659 0 2.34 2.34 0 0 0 3.319 1.915 2.34 2.34 0 0 1 2.33 4.033 2.34 2.34 0 0 0 0 3.831 2.34 2.34 0 0 1-2.33 4.033 2.34 2.34 0 0 0-3.319 1.915 2.34 2.34 0 0 1-4.659 0 2.34 2.34 0 0 0-3.32-1.915 2.34 2.34 0 0 1-2.33-4.033 2.34 2.34 0 0 0 0-3.831A2.34 2.34 0 0 1 6.35 6.051a2.34 2.34 0 0 0 3.319-1.915\"></path><circle cx=\"12\" cy=\"12\" r=\"3\"></circle>",
"ShieldCheck": "<path d=\"M20 13c0 5-3.5 7.5-7.66 8.95a1 1 0 0 1-.67-.01C7.5 20.5 4 18 4 13V6a1 1 0 0 1 1-1c2 0 4.5-1.2 6.24-2.72a1.17 1.17 0 0 1 1.52 0C14.51 3.81 17 5 19 5a1 1 0 0 1 1 1z\"></path><path d=\"m9 12 2 2 4-4\"></path>",
"Sparkles": "<path d=\"M11.017 2.814a1 1 0 0 1 1.966 0l1.051 5.558a2 2 0 0 0 1.594 1.594l5.558 1.051a1 1 0 0 1 0 1.966l-5.558 1.051a2 2 0 0 0-1.594 1.594l-1.051 5.558a1 1 0 0 1-1.966 0l-1.051-5.558a2 2 0 0 0-1.594-1.594l-5.558-1.051a1 1 0 0 1 0-1.966l5.558-1.051a2 2 0 0 0 1.594-1.594z\"></path><path d=\"M20 2v4\"></path><path d=\"M22 4h-4\"></path><circle cx=\"4\" cy=\"20\" r=\"2\"></circle>",
"Target": "<circle cx=\"12\" cy=\"12\" r=\"10\"></circle><circle cx=\"12\" cy=\"12\" r=\"6\"></circle><circle cx=\"12\" cy=\"12\" r=\"2\"></circle>",
"Undo2": "<path d=\"M9 14 4 9l5-5\"></path><path d=\"M4 9h10.5a5.5 5.5 0 0 1 5.5 5.5a5.5 5.5 0 0 1-5.5 5.5H11\"></path>",
"Workflow": "<rect width=\"8\" height=\"8\" x=\"3\" y=\"3\" rx=\"2\"></rect><path d=\"M7 11v4a2 2 0 0 0 2 2h4\"></path><rect width=\"8\" height=\"8\" x=\"13\" y=\"13\" rx=\"2\"></rect>",
"X": "<path d=\"M18 6 6 18\"></path><path d=\"m6 6 12 12\"></path>"
}
export type LucideName = string
export const LucideIcon = defineComponent({
name: 'LucideIcon',
props: { name: { type: String as PropType<LucideName>, required: true }, size: { type: Number, default: 16 }, strokeWidth: { type: Number, default: 2 } },
setup: props => () => h('svg', {
xmlns: 'http://www.w3.org/2000/svg', width: props.size, height: props.size, viewBox: '0 0 24 24', fill: 'none', stroke: 'currentColor',
'stroke-width': props.strokeWidth, 'stroke-linecap': 'round', 'stroke-linejoin': 'round', 'aria-hidden': 'true', innerHTML: ICONS[props.name] ?? '',
}),
})
const initialUpdates = [
{ id: 1, icon: 'GitPullRequest', title: 'Motion review approved', detail: 'The pull request is ready to merge.', time: '4m' },
{ id: 2, icon: 'MessageCircle', title: 'New component feedback', detail: 'The spring feels much closer to native now.', time: '18m' },
{ id: 3, icon: 'CheckCircle2', title: 'Registry checks passed', detail: 'All component files resolved successfully.', time: '31m' },
{ id: 4, icon: 'Bell', title: 'Preview deployed', detail: 'The latest build is ready to inspect.', time: '1h' },
{ id: 5, icon: 'MessageCircle', title: 'Docs comment resolved', detail: 'The usage example now covers async refreshes.', time: '2h' },
]
const refreshCount = ref(0)
const latestUpdate = ref<number | null>(null)
const updates = computed(() => latestUpdate.value
? [{ id: latestUpdate.value, icon: 'Bell', title: 'You’re all caught up', detail: 'The activity feed was refreshed just now.', time: 'now' }, ...initialUpdates]
: initialUpdates)
async function refresh() {
await new Promise(resolve => setTimeout(resolve, 900))
refreshCount.value += 1
latestUpdate.value = Date.now()
}
</script>
<template>
<div class="stage">
<div class="phone">
<PullToRefresh aria-label="Activity feed" class="feed" :on-refresh="refresh">
<header class="bar">
<div>
<p class="bar-title">Activity</p>
<p class="bar-sub">Pull down to check for updates</p>
</div>
<span class="pill">{{ refreshCount ? `${refreshCount} refreshed` : 'live' }}</span>
</header>
<div class="list">
<article v-for="update in updates" :key="update.id" class="item">
<div class="icon"><LucideIcon :name="update.icon" :size="16" /></div>
<div class="body">
<div class="line">
<p class="title">{{ update.title }}</p>
<span class="time">{{ update.time }}</span>
</div>
<p class="detail">{{ update.detail }}</p>
</div>
</article>
</div>
</PullToRefresh>
</div>
</div>
</template>
<style scoped>
.stage { display: flex; min-height: 34rem; width: 100%; align-items: center; justify-content: center; padding: 1rem; box-sizing: border-box; }
.phone { width: 100%; max-width: 24rem; overflow: hidden; border: 1px solid var(--beui-border); border-radius: 2rem; background: var(--beui-background); box-shadow: 0 25px 50px -12px rgb(0 0 0 / .25); }
.feed { height: 30rem; }
p { margin: 0; }
.bar { position: sticky; top: 0; z-index: 10; display: flex; align-items: center; justify-content: space-between; padding: 1rem 1.25rem; border-bottom: 1px solid var(--beui-border); background: color-mix(in srgb, var(--beui-background) 90%, transparent); backdrop-filter: blur(12px); }
.bar-title { font-weight: 600; font-size: 1rem; line-height: 1.5rem; color: var(--beui-foreground); }
.bar-sub { font-size: .75rem; line-height: 1rem; color: var(--beui-muted-foreground); }
.pill { border-radius: 9999px; background: var(--beui-muted); padding: .25rem .625rem; font-family: var(--font-mono, "Geist Mono Variable", ui-monospace, monospace); font-size: 10px; line-height: 1.5; color: var(--beui-muted-foreground); }
.list { padding: 0 .5rem .75rem; }
.item { display: flex; gap: .75rem; padding: 1rem .75rem; }
.item:not(:last-child) { border-bottom: 1px solid var(--beui-border); }
.icon { display: grid; width: 2.25rem; height: 2.25rem; flex: none; place-items: center; border: 1px solid var(--beui-border); border-radius: .75rem; background: var(--beui-card); color: var(--beui-muted-foreground); box-shadow: 0 1px 3px 0 rgb(0 0 0 / .1), 0 1px 2px -1px rgb(0 0 0 / .1); }
.body { min-width: 0; flex: 1; }
.line { display: flex; align-items: flex-start; justify-content: space-between; gap: .75rem; }
.title { font-size: .875rem; font-weight: 500; line-height: 1.25rem; color: var(--beui-foreground); }
.time { flex: none; font-family: var(--font-mono, "Geist Mono Variable", ui-monospace, monospace); font-size: 10px; line-height: 1.5; color: var(--beui-muted-foreground); }
.detail { margin-top: .25rem; font-size: .75rem; line-height: 1.625; color: var(--beui-muted-foreground); }
</style><script lang="ts">
/** English defaults. Override per instance with `labels`. */
export const pullToRefreshMessages = {
pulling: 'Pull to refresh',
release: 'Release to refresh',
refreshing: 'Refreshing',
label: 'Refreshable content',
}
export type PullToRefreshMessages = typeof pullToRefreshMessages
export type PullToRefreshStatus = 'idle' | 'pulling' | 'ready' | 'refreshing'
</script>
<script setup lang="ts">
import { AnimatePresence, animate, motion, useMotionValue, useTransform } from 'motion-v'
import { useReducedMotion } from '../core/reduced-motion'
import { computed, onBeforeUnmount, onMounted, shallowRef, useTemplateRef, watchEffect } from 'vue'
import { easeInOut, easeOut, panelSpring, swapSpring } from '../core/motion'
const props = withDefaults(defineProps<{
/** Runs after the user pulls beyond the threshold and releases. A prop rather than an event, because the pull waits for the returned promise. */
onRefresh: () => void | Promise<void>
/** Keeps the indicator active while an externally managed refresh runs. */
refreshing?: boolean
disabled?: boolean
/** Resisted pull distance in pixels required to refresh. */
threshold?: number
/** Maximum resisted pull distance in pixels. */
maxPull?: number
/** Content offset in pixels while refreshing. */
holdDistance?: number
/** Text shown while pulling. The `pullingLabel` slot takes over for rich content. */
pullingLabel?: string
/** Text shown once the pull passes the threshold. The `releaseLabel` slot takes over for rich content. */
releaseLabel?: string
/** Text shown while refreshing. The `refreshingLabel` slot takes over for rich content. */
refreshingLabel?: string
ariaLabel?: string
/** Accessible name of the keyboard refresh button. */
label?: string
labels?: Partial<typeof pullToRefreshMessages>
/** Same as `class`. */
className?: string
contentClassName?: string
indicatorClassName?: string
}>(), { refreshing: false, disabled: false, threshold: 76, maxPull: 132, holdDistance: 68 })
const emit = defineEmits<{ error: [reason: unknown] }>()
defineSlots<{
default?(props: { refresh: () => Promise<void>; state: PullToRefreshStatus }): unknown
/** Label while pulling; replaces `pullingLabel`. */
pullingLabel?(): unknown
/** Label once the pull passes the threshold; replaces `releaseLabel`. */
releaseLabel?(): unknown
/** Label while refreshing; replaces `refreshingLabel`. */
refreshingLabel?(): unknown
}>()
const text = computed(() => ({ ...pullToRefreshMessages, ...props.labels }))
type Gesture = { active: boolean; startX: number; startY: number; pointerId: number | null }
const EMPTY_GESTURE: Gesture = { active: false, startX: 0, startY: 0, pointerId: null }
// This character needs a compact repeating rhythm rather than a settling
// spring: a small orbit and blink that read as activity without feeling busy.
const CHARACTER_LOOP = { duration: 0.9, ease: easeInOut, repeat: Number.POSITIVE_INFINITY } as const
const CALM_PULSE = { duration: 1.2, ease: easeInOut, repeat: Number.POSITIVE_INFINITY } as const
const LABEL_SWAP = { duration: 0.16, ease: easeOut } as const
const dragThreshold = computed(() => Math.max(24, props.threshold))
const pullLimit = computed(() => Math.max(props.maxPull, dragThreshold.value + 24))
const restingDistance = computed(() => Math.min(Math.max(0, props.holdDistance), dragThreshold.value))
// The pull resists as it grows, so a long drag never tears the page apart.
const resistedDistance = (distance: number, maxPull: number) => maxPull * (1 - Math.exp(-Math.max(0, distance) / maxPull))
const reduce = useReducedMotion()
const root = useTemplateRef<HTMLElement>('root')
const status = shallowRef<PullToRefreshStatus>('idle')
const internalRefreshing = shallowRef(false)
const isRefreshing = computed(() => props.refreshing || internalRefreshing.value)
const y = useMotionValue(0)
const progress = useTransform(y, [0, dragThreshold.value], [0, 1])
const indicatorOpacity = useTransform(y, [0, 10, dragThreshold.value], [0, 0.45, 1])
const indicatorScale = useTransform(y, [0, dragThreshold.value], [0.86, 1])
const lift = useTransform(progress, [0, 1], [-7, 0])
const tilt = useTransform(progress, [0, 1], [-10, 0])
const stretch = useTransform(progress, [0, 0.55, 1], [0.68, 1.1, 0.92])
// Live props are read by the native listeners installed once on mount.
let disabledNow = props.disabled
let refreshingNow = isRefreshing.value
let liveThreshold = dragThreshold.value
let liveRest = restingDistance.value
let gesture: Gesture = { ...EMPTY_GESTURE }
let animation: { stop: () => void } | undefined
function settle(target: number) {
animation?.stop()
if (reduce.value) {
y.set(target)
return
}
animation = animate(y, target, panelSpring)
}
function updatePull(distance: number) {
if (disabledNow || refreshingNow) return
animation?.stop()
const next = resistedDistance(distance, pullLimit.value)
y.set(next)
status.value = next >= liveThreshold ? 'ready' : 'pulling'
}
async function runRefresh() {
if (disabledNow || refreshingNow) return
internalRefreshing.value = true
status.value = 'refreshing'
settle(liveRest)
try {
await props.onRefresh()
} catch (reason) {
emit('error', reason)
} finally {
internalRefreshing.value = false
// A synchronous refresh can resolve before the prop watcher runs, so release
// here instead of relying only on it.
if (!props.refreshing) {
status.value = 'idle'
settle(0)
}
}
}
function finishPull() {
const shouldRefresh = y.get() >= liveThreshold && !disabledNow && !refreshingNow
gesture = { ...EMPTY_GESTURE }
if (shouldRefresh) {
void runRefresh()
return
}
status.value = 'idle'
settle(0)
}
/* ---- native touch: the only path that can stop the page scroll ----------- */
function onTouchStart(event: TouchEvent) {
const rootNode = root.value
if (!rootNode || event.touches.length !== 1 || rootNode.scrollTop > 0 || disabledNow || refreshingNow) return
const touch = event.touches[0]!
gesture = { active: true, startX: touch.clientX, startY: touch.clientY, pointerId: null }
}
function onTouchMove(event: TouchEvent) {
const rootNode = root.value
const touch = event.touches[0]
if (!gesture.active || !touch || !rootNode) return
const deltaX = touch.clientX - gesture.startX
const deltaY = touch.clientY - gesture.startY
if (rootNode.scrollTop > 0 || deltaY < 0) {
gesture = { ...EMPTY_GESTURE }
return
}
if (Math.abs(deltaX) > deltaY) return
if (event.cancelable) event.preventDefault()
updatePull(deltaY)
}
function onTouchEnd() {
if (gesture.active) finishPull()
}
onMounted(() => {
const rootNode = root.value
if (!rootNode) return
rootNode.addEventListener('touchstart', onTouchStart, { passive: true })
rootNode.addEventListener('touchmove', onTouchMove, { passive: false })
rootNode.addEventListener('touchend', onTouchEnd)
rootNode.addEventListener('touchcancel', onTouchEnd)
})
/* ---- pointer path for mouse and pen ------------------------------------- */
function capturePointer(element: Element, pointerId: number) {
// WebKit throws when the pointer is already gone; touch pointers carry
// implicit capture, so losing it is never fatal.
try { (element as HTMLElement).setPointerCapture(pointerId) } catch {}
}
function startPointerPull(event: PointerEvent) {
// Everything but touch: a finger is driven by the native listeners above,
// which can preventDefault the page scroll. A pen fires no touch events at
// all, so this is its only route.
const target = event.currentTarget as HTMLElement
if (event.pointerType === 'touch' || event.button !== 0 || target.scrollTop > 0 || disabledNow || refreshingNow) return
capturePointer(target, event.pointerId)
gesture = { active: true, startX: event.clientX, startY: event.clientY, pointerId: event.pointerId }
}
function movePointerPull(event: PointerEvent) {
if (!gesture.active || gesture.pointerId !== event.pointerId) return
const deltaX = event.clientX - gesture.startX
const deltaY = event.clientY - gesture.startY
if (deltaY < 0 || Math.abs(deltaX) > deltaY) return
event.preventDefault()
updatePull(deltaY)
}
function endPointerPull(event: PointerEvent) {
if (gesture.pointerId !== event.pointerId) return
finishPull()
}
/* ---- prop mirroring ----------------------------------------------------- */
// Mirrors live props into the plain fields the native touch listeners read, and
// drives the indicator while an externally managed refresh runs.
watchEffect(() => {
disabledNow = props.disabled
refreshingNow = isRefreshing.value
liveThreshold = dragThreshold.value
liveRest = restingDistance.value
if (isRefreshing.value) {
status.value = 'refreshing'
settle(liveRest)
return
}
if (status.value === 'refreshing') {
status.value = 'idle'
settle(0)
}
})
const accessibleLabel = computed(() => props.label ?? props.ariaLabel ?? text.value.label)
const canRefresh = computed(() => !props.disabled && !isRefreshing.value)
function onKeyRefresh() {
if (!canRefresh.value) return
void runRefresh()
}
onBeforeUnmount(() => {
animation?.stop()
const rootNode = root.value
if (!rootNode) return
rootNode.removeEventListener('touchstart', onTouchStart)
rootNode.removeEventListener('touchmove', onTouchMove)
rootNode.removeEventListener('touchend', onTouchEnd)
rootNode.removeEventListener('touchcancel', onTouchEnd)
})
</script>
<template>
<section
ref="root"
class="pull-refresh"
:class="[className, { 'is-dragging': status === 'pulling' || status === 'ready', 'is-off': !canRefresh }]"
:aria-label="props.ariaLabel ?? text.label"
:aria-busy="isRefreshing"
:data-state="status"
:data-disabled="disabled || undefined"
@pointerdown="startPointerPull"
@pointermove="movePointerPull"
@pointerup="endPointerPull"
@pointercancel="endPointerPull">
<motion.div
class="pull-indicator"
:class="indicatorClassName"
aria-live="polite"
aria-atomic="true"
:style="reduce ? { opacity: indicatorOpacity } : { opacity: indicatorOpacity, scale: indicatorScale }">
<!-- The mascot lifts, tilts and stretches with the pull, then orbits while
a refresh runs. -->
<motion.span class="buddy" :style="reduce ? undefined : { y: lift, rotate: tilt, scaleY: stretch }">
<motion.svg
aria-hidden="true"
viewBox="0 0 36 36"
class="buddy-svg"
:animate="status === 'refreshing'
? (reduce ? { opacity: [0.55, 1, 0.55] } : { y: [0, -2, 0], rotate: [-3, 3, -3] })
: (reduce ? { opacity: 1 } : { y: 0, rotate: 0, scale: status === 'ready' ? 1.08 : 1 })"
:transition="status === 'refreshing' ? (reduce ? CALM_PULSE : CHARACTER_LOOP) : swapSpring">
<motion.g
class="buddy-orbit"
:class="{ 'is-on': status === 'ready' || status === 'refreshing' }"
:style="{ transformOrigin: '18px 18px' }"
:animate="status === 'refreshing' && !reduce ? { rotate: [0, 360] } : (reduce ? undefined : { rotate: status === 'ready' ? 0 : -35 })"
:transition="status === 'refreshing' ? (reduce ? CALM_PULSE : CHARACTER_LOOP) : swapSpring">
<path d="M18 2.5a15.5 15.5 0 0 1 12.7 6.6" class="buddy-arc" />
<circle cx="31.3" cy="10.2" r="2.2" class="buddy-ink" />
</motion.g>
<rect x="7" y="7" width="22" height="22" rx="9" class="buddy-ink" />
<motion.g
:style="{ opacity: 1, transformOrigin: '18px 16px' }"
:animate="status === 'refreshing' && !reduce
? { scaleY: [1, 1, 0.15, 1, 1] }
: (reduce ? { opacity: 1 } : { scaleY: status === 'ready' ? 1.18 : 1 })"
:transition="status === 'refreshing' && !reduce ? CHARACTER_LOOP : swapSpring">
<circle cx="14.2" cy="16" r="1.45" class="buddy-eye" />
<circle cx="21.8" cy="16" r="1.45" class="buddy-eye" />
</motion.g>
<path d="M14.5 21h7" class="buddy-mouth is-idle" :class="{ 'is-hidden': status === 'ready' || status === 'refreshing' }" />
<path d="M14 20.5c1 2.4 7 2.4 8 0" class="buddy-mouth" :class="{ 'is-hidden': status !== 'ready' }" />
<circle cx="18" cy="21" r="1.6" class="buddy-eye buddy-nose" :class="{ 'is-hidden': status !== 'refreshing' }" />
</motion.svg>
</motion.span>
<span class="buddy-label">
<AnimatePresence :initial="false" mode="wait">
<motion.span
:key="status"
class="buddy-label-text"
:initial="reduce ? { opacity: 0 } : { opacity: 0, y: 3 }"
:animate="reduce ? { opacity: 1 } : { opacity: 1, y: 0 }"
:exit="reduce ? { opacity: 0 } : { opacity: 0, y: -3 }"
:transition="LABEL_SWAP">
<slot v-if="status === 'refreshing'" name="refreshingLabel">{{ refreshingLabel ?? text.refreshing }}</slot>
<slot v-else-if="status === 'ready'" name="releaseLabel">{{ releaseLabel ?? text.release }}</slot>
<slot v-else name="pullingLabel">{{ pullingLabel ?? text.pulling }}</slot>
</motion.span>
</AnimatePresence>
</span>
</motion.div>
<motion.div class="pull-content" :class="contentClassName" :style="reduce ? undefined : { y }">
<slot :refresh="runRefresh" :state="status" />
</motion.div>
<!-- The pull is a pointer gesture; this is its keyboard path. -->
<button type="button" class="pull-keyboard" :aria-label="accessibleLabel" :disabled="!canRefresh" @click="onKeyRefresh">
{{ accessibleLabel }}
</button>
</section>
</template>
<style scoped>
.pull-refresh {
position: relative;
width: 100%;
overflow-y: auto;
overscroll-behavior: contain;
background: var(--beui-background);
/* `touch-action` stays default: this element is the scroller, and the pull
only takes over once the content is already at the top. */
-webkit-touch-callout: none;
cursor: grab;
}
@media (pointer: coarse) { .pull-refresh { user-select: none; } }
/* Whatever is inside stays selectable with a mouse; only a running pull suppresses
selection, so dragging down cannot highlight the content and drag-scroll the feed. */
.pull-refresh.is-dragging { cursor: grabbing; -webkit-user-select: none; user-select: none; }
.pull-refresh.is-off { cursor: default; }
.pull-indicator {
pointer-events: none;
position: absolute;
inset-inline: 0;
top: 0;
z-index: 20;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: .125rem;
height: 4.25rem;
font-size: 11px;
font-weight: 500;
color: var(--beui-muted-foreground);
background: linear-gradient(to bottom, var(--beui-background), color-mix(in srgb, var(--beui-background) 95%, transparent), transparent);
}
.buddy { display: block; width: 2.25rem; height: 2.25rem; transform-origin: bottom; }
.buddy-svg { display: block; width: 100%; height: 100%; overflow: visible; }
.buddy-orbit { opacity: 0; transition: opacity .15s cubic-bezier(.4, 0, .2, 1); }
.buddy-orbit.is-on { opacity: 1; }
.buddy-arc { fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; color: var(--beui-muted-foreground); }
.buddy-ink { fill: var(--beui-foreground); }
.buddy-eye { fill: var(--beui-background); }
.buddy-mouth { fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; color: var(--beui-background); }
.buddy-mouth, .buddy-nose { transition: opacity .15s cubic-bezier(.4, 0, .2, 1); }
.is-hidden { opacity: 0; }
.buddy-label { position: relative; display: block; height: 1rem; min-width: 6rem; text-align: center; }
.buddy-label-text { position: absolute; inset-inline: 0; white-space: nowrap; }
.pull-content { position: relative; z-index: 10; min-height: 100%; background: inherit; will-change: transform; }
.pull-keyboard {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
border: 0;
padding: 0;
background: transparent;
}
.pull-keyboard:focus-visible {
top: .5rem;
left: .5rem;
width: auto;
height: auto;
overflow: visible;
clip-path: none;
padding: .375rem .75rem;
border: 1px solid var(--beui-border);
border-radius: .5rem;
background: var(--beui-card);
color: var(--beui-foreground);
font: inherit;
font-size: .75rem;
}
</style>Install
Add it with your package manager, or copy the source manually.
Run it in a project set up with shadcn-vue. The source is copied to components/beui and its dependencies are added to your package.json. Read the shadcn-vue guide
$ bunx --bun shadcn-vue@latest add @beui-vue/pull-to-refresh$ bun add beui-vueDownload the release archive to your project root, then install the local file.
$ bun add ./beui-vue-0.1.0.tgzImport the shared styles once in your application.
import PullToRefresh from '@/components/beui/layout/PullToRefresh.vue'
import '@/components/beui/styles.css'API Reference
onRefresh() => void | Promise<void>Runs after the user pulls beyond the threshold and releases. A prop rather than an event, because the pull waits for the returned promise.
—refreshing?booleanKeeps the indicator active while an externally managed refresh runs.
falsedisabled?booleanfalsethreshold?numberResisted pull distance in pixels required to refresh.
76maxPull?numberMaximum resisted pull distance in pixels.
132holdDistance?numberContent offset in pixels while refreshing.
68pullingLabel?stringText shown while pulling. The `pullingLabel` slot takes over for rich content.
—releaseLabel?stringText shown once the pull passes the threshold. The `releaseLabel` slot takes over for rich content.
—refreshingLabel?stringText shown while refreshing. The `refreshingLabel` slot takes over for rich content.
—ariaLabel?string—labels?Partial<typeof pullToRefreshMessages>Override any built-in string.
—className?stringSame as `class`.
—contentClassName?string—indicatorClassName?string—label?stringAccessible name of the keyboard refresh button.
—@error?(reason: unknown) => void—#default?{ refresh: () => Promise<void>; state: PullToRefreshStatus }—#pullingLabel?slotLabel while pulling; replaces `pullingLabel`.
—#releaseLabel?slotLabel once the pull passes the threshold; replaces `releaseLabel`.
—#refreshingLabel?slotLabel while refreshing; replaces `refreshingLabel`.
—Related components
Wheel Picker
iOS-style picker wheel: a 3D drum on native momentum scroll that snaps to the nearest notch, with wheel, drag and keyboard control. Composes side by side for date and time pickers, reduced-motion safe.
Before After Slider
Compare two images with a draggable divider. Drag or click anywhere on the frame, use the arrow keys, and keep vertical page scroll on touch.
Animated Color Selector
Composable color swatches with a spring-gliding selection ring, press feedback, native radio keyboard navigation, and controlled or uncontrolled state.
Updated