Scroll Animation
Smooth Scroll
SmoothScroll.vue<script lang="ts">
import { useSmoothScroll } from 'beui-vue'
import { 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] ?? '',
}),
})
/** The scroll-to-top control, built on `useSmoothScroll()` like the upstream preview. */
const ScrollTopButton = defineComponent({
name: 'ScrollTopButton',
setup() {
const api = useSmoothScroll()
return () => h('button', { type: 'button', class: 'top', 'aria-label': 'Scroll to top', onClick: () => api?.scrollTo(0) }, [h(LucideIcon, { name: 'ArrowUp', size: 16 })])
},
})
</script>
<script setup lang="ts">
import { SmoothScroll } from 'beui-vue'
// In production <SmoothScroll> wraps the page (root). Here it runs in contained
// mode (root=false) so the box itself smooth-scrolls, and the button uses
// useSmoothScroll() to glide back to the top.
const SECTIONS = Array.from({ length: 16 }, (_, index) => index + 1)
</script>
<template>
<SmoothScroll :root="false" class="box">
<div class="sections">
<div v-for="n in SECTIONS" :key="n" class="section">Section {{ n }}</div>
</div>
<ScrollTopButton />
</SmoothScroll>
</template>
<style scoped>
.box { height: 16rem; width: 100%; max-width: 32rem; box-sizing: border-box; overflow-y: auto; border: 1px solid var(--beui-border); border-radius: 1rem; background: var(--beui-card); scrollbar-width: none; }
.box::-webkit-scrollbar { display: none; }
.sections { padding: 1rem; }
.section:not(:last-child) { margin-bottom: .75rem; }
.section { border-radius: .5rem; background: color-mix(in srgb, var(--beui-muted) 60%, transparent); padding: 1rem .75rem; font-size: .875rem; line-height: 1.25rem; color: var(--beui-muted-foreground); }
.box :deep(.top) { position: sticky; bottom: .75rem; left: calc(100% - 3rem); z-index: 10; display: grid; width: 2.25rem; height: 2.25rem; place-items: center; padding: 0; border: 1px solid var(--beui-border); border-radius: 9999px; background: color-mix(in srgb, var(--beui-background) 80%, transparent); color: var(--beui-foreground); backdrop-filter: blur(8px); cursor: pointer; transition: background-color .15s; }
.box :deep(.top:hover) { background: var(--beui-background); }
</style><script setup lang="ts">
import type Lenis from 'lenis'
import { useMotionValue } from 'motion-v'
import { useReducedMotion } from '../core/reduced-motion'
import { computed, onMounted, provide, shallowRef, useTemplateRef, watchEffect } from 'vue'
import { scrollKey, type SmoothScrollApi } from './scroll-context'
defineOptions({ inheritAttrs: false })
const { root = true, lerp = .1, duration = 1.2, orientation = 'vertical', wheelMultiplier = 1, touch = false } = defineProps<{
/** Drive the page (window) when true, or a contained scroll area when false. */
root?: boolean
/** Smoothing factor; lower is smoother and heavier. */
lerp?: number
/** Wheel / programmatic ease duration in seconds. */
duration?: number
orientation?: 'horizontal' | 'vertical'
/** Wheel scroll speed multiplier. */
wheelMultiplier?: number
/** Smooth touch scrolling. Off by default — native momentum is good on mobile. */
touch?: boolean
}>()
defineSlots<{ default?: () => unknown }>()
const wrapper = useTemplateRef('wrapper')
const content = useTemplateRef('content')
const container = computed(() => !root && wrapper.value || undefined)
const reduce = useReducedMotion()
const progress = useMotionValue(0)
const velocity = useMotionValue(0)
const scrollY = useMotionValue(0)
const lenis = shallowRef<Lenis | null>(null)
let engine: Lenis | undefined
// Lenis' own expo-out curve: the canonical smooth-scroll easing. A (t) => number function,
// not a `core/motion` token, because tokens are bezier control points for the motion lib.
const easeScroll = (t: number) => Math.min(1, 1.001 - 2 ** (-10 * t))
// Lenis loads lazily. A scroll asked for before it is ready waits for it, so the
// engine, created at the old position, cannot snap a native glide back.
let loading = false
let pending: [number | HTMLElement, { offset?: number; duration?: number; immediate?: boolean }] | undefined
const scrollTo: SmoothScrollApi['scrollTo'] = (target, options = {}) => {
const source = container.value ?? window
let element: number | HTMLElement | null
if (typeof target === 'string') {
try { element = (container.value ?? document).querySelector<HTMLElement>(target) } catch { return }
} else element = target
if (element === null) return
if (engine) { engine.scrollTo(element, { ...options, immediate: options.immediate || reduce.value || false }); return }
if (loading) { pending = [element, options]; return }
const horizontal = orientation === 'horizontal'
const current = container.value ? (horizontal ? container.value.scrollLeft : container.value.scrollTop) : (horizontal ? window.scrollX : window.scrollY)
const axis = horizontal ? 'left' : 'top'
const border = container.value ? (horizontal ? container.value.clientLeft : container.value.clientTop) : 0
const position = typeof element === 'number' ? element : element.getBoundingClientRect()[axis] - (container.value?.getBoundingClientRect()[axis] ?? 0) - border + current
source.scrollTo({ [axis]: position + (options.offset ?? 0), behavior: reduce.value || options.immediate ? 'instant' : 'smooth' })
}
provide(scrollKey, { container, lenis, progress, velocity, scrollY, scrollTo })
onMounted(() => watchEffect(cleanup => {
const source = container.value ?? window
const node = content.value
const options = { lerp, duration, orientation, wheelMultiplier, smoothWheel: true, syncTouch: touch, easing: easeScroll, autoRaf: true }
const horizontal = orientation === 'horizontal'
let disposed = false
let previous = performance.now()
function sync() {
const host = container.value ?? document.documentElement
const offset = horizontal ? host.scrollLeft : host.scrollTop
const maximum = horizontal ? host.scrollWidth - host.clientWidth : host.scrollHeight - host.clientHeight
const now = performance.now()
velocity.set((offset - scrollY.get()) * 16 / Math.max(1, now - previous))
previous = now
progress.set(maximum > 0 ? offset / maximum : 0)
scrollY.set(offset)
}
sync()
source.addEventListener('scroll', sync, { passive: true })
if (!reduce.value && (root || node)) {
loading = true
void import('lenis').then(({ default: LenisEngine }) => {
if (disposed) return
engine = new LenisEngine({ ...options, wrapper: source, content: root ? document.documentElement : node! })
lenis.value = engine
engine.on('scroll', instance => { progress.set(instance.progress); scrollY.set(instance.scroll); velocity.set(instance.velocity) })
source.removeEventListener('scroll', sync)
loading = false
const queued = pending
pending = undefined
if (queued) engine.scrollTo(queued[0], { ...queued[1], immediate: queued[1].immediate || reduce.value || false })
}).catch(() => {
// No engine: fall back to the native path, including a scroll that was waiting.
loading = false
const queued = pending
pending = undefined
if (queued) scrollTo(queued[0], queued[1])
})
}
cleanup(() => { disposed = true; loading = false; pending = undefined; source.removeEventListener('scroll', sync); engine?.destroy(); engine = undefined; lenis.value = null })
}))
defineExpose({ progress, velocity, scrollY, scrollTo })
// Root mode drives the page itself, so the template renders no wrapper there: the slot is the page.
</script>
<template>
<slot v-if="root" />
<div v-else ref="wrapper" v-bind="$attrs"><div ref="content"><slot /></div></div>
</template>Install
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/smooth-scroll$ 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 SmoothScroll from '@/components/beui/layout/SmoothScroll.vue'
import '@/components/beui/styles.css'API Reference
root?booleanDrive the page (window) when true, or a contained scroll area when false.
truelerp?numberSmoothing factor; lower is smoother and heavier.
.1duration?numberWheel / programmatic ease duration in seconds.
1.2orientation?"horizontal" | "vertical"verticalwheelMultiplier?numberWheel scroll speed multiplier.
1touch?booleanSmooth touch scrolling. Off by default — native momentum is good on mobile.
falseclass?string—#default?slot—Scroll Progress
ScrollProgress.vue<script setup lang="ts">
import { ScrollProgress } from 'beui-vue'
import { useTemplateRef } from 'vue'
// Real usage: drop <ScrollProgress /> anywhere; it reads the page scroll and pins itself with `fixed`.
// Here it is scoped to a box by passing the scroller as `container` and `:fixed="false"`.
const region = useTemplateRef('region')
</script>
<template>
<div class="progress-frame">
<ScrollProgress :container="region" :fixed="false" :height="3" data-testid="progress-bar" />
<div class="circle"><ScrollProgress :container="region" variant="circle" :size="36" /></div>
<div ref="region" role="region" aria-label="Sections" tabindex="0" class="scroller">
<div class="rows"><div v-for="section in 18" :key="section" class="row">Section {{ section }}</div></div>
</div>
</div>
</template>
<style scoped>
.progress-frame { box-sizing: border-box; position: relative; width: 100%; max-width: 32rem; overflow: hidden; border: 1px solid var(--beui-border); border-radius: 1rem; background: var(--beui-card); }
.scroller { height: 16rem; overflow-y: auto; scrollbar-width: none; }
.scroller::-webkit-scrollbar { display: none; }
.rows { padding: 1rem; }
.circle { position: absolute; right: .75rem; top: .75rem; z-index: 10; padding: .25rem; border-radius: 999px; background: color-mix(in srgb, var(--beui-background) 70%, transparent); -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px); pointer-events: none; }
.circle :deep(svg) { display: block; }
.row:not(:last-child) { margin-bottom: .75rem; }
.row { border-radius: .5rem; padding: 1rem .75rem; background: color-mix(in srgb, var(--beui-muted) 60%, transparent); font-size: .875rem; line-height: 1.25rem; color: var(--beui-muted-foreground); }
</style><script setup lang="ts">
import { motion, useMotionValue, useScroll, useSpring, useTransform, type MotionValue } from 'motion-v'
import { useReducedMotion } from '../core/reduced-motion'
import { computed, toRef, watchEffect } from 'vue'
import { useSmoothScrollProvider } from '../layout/scroll-context'
const { progress, container, spring = true, variant = 'bar', position = 'top', height = 2, fixed = true, size = 40, thickness = 3 } = defineProps<{
progress?: number | MotionValue<number>; container?: HTMLElement | null; spring?: boolean; variant?: 'bar' | 'circle'; position?: 'top' | 'bottom'; height?: number; fixed?: boolean; size?: number; thickness?: number
}>()
const reduce = useReducedMotion()
const provider = useSmoothScrollProvider()
const { scrollYProgress } = useScroll({ container: toRef(() => container ?? provider?.container.value) })
const raw = useMotionValue(0)
const smooth = useSpring(raw, { stiffness: 120, damping: 30, mass: .6 })
watchEffect(cleanup => {
const input = progress ?? (!container && provider ? provider.progress : scrollYProgress)
const update = (next: number) => raw.set(Math.min(1, Math.max(0, next)))
if (typeof input === 'number') update(input)
else { update(input.get()); cleanup(input.on('change', update)) }
})
const value = computed(() => spring && !reduce.value ? smooth : raw)
const radius = computed(() => Math.max(0, (size - thickness) / 2))
const rawOffset = useTransform(raw, progress => 1 - progress)
const smoothOffset = useTransform(smooth, progress => 1 - progress)
</script>
<template>
<svg v-if="variant === 'circle'" aria-hidden="true" focusable="false" :width="size" :height="size" :viewBox="`0 0 ${size} ${size}`" class="progress-circle">
<circle :cx="size / 2" :cy="size / 2" :r="radius" :stroke-width="thickness" opacity=".15" />
<motion.circle :cx="size / 2" :cy="size / 2" :r="radius" :stroke-width="thickness" pathLength="1" stroke-dasharray="1" stroke-linecap="round"
:style="{ strokeDashoffset: spring && !reduce ? smoothOffset : rawOffset }" :transform="`rotate(-90 ${size / 2} ${size / 2})`" />
</svg>
<motion.div v-else aria-hidden="true" class="progress-bar" :style="{ position: fixed ? 'fixed' : 'absolute', [position]: 0, height: `${height}px`, scaleX: value }" />
</template>
<style scoped>
.progress-bar { z-index: 50; left: 0; right: 0; background: var(--beui-foreground); transform-origin: left; pointer-events: none; }
.progress-circle { fill: none; stroke: currentColor; color: var(--beui-foreground); }
</style>Install
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/scroll-progress$ 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 ScrollProgress from '@/components/beui/effects/ScrollProgress.vue'
import '@/components/beui/styles.css'API Reference
variant?"bar" | "circle"barposition?"top" | "bottom"topheight?numberBar thickness in px.
2fixed?booleanPosition the bar with `fixed` (page) or `absolute` (embedded).
trueprogress?number | MotionValue<number>Override the scroll source. Defaults to the page via useSmoothScroll.
—spring?booleanSpring-smooth the value. Disabled automatically under reduced motion.
trueclass?string—size?numberDiameter in px.
40thickness?numberStroke width in px.
3container?HTMLElement | null—Parallax
Parallax.vue<script setup lang="ts">
import { Parallax } from 'beui-vue'
import { useTemplateRef } from 'vue'
// On a real page <Parallax> tracks the viewport. Here it is scoped to the box via the `container` prop.
// Scroll inside the box: the background image drifts against the scroll, the label and avatar drift with it at different speeds.
const region = useTemplateRef('region')
</script>
<template>
<div ref="region" role="region" aria-label="Layered landscape" tabindex="0" class="parallax-frame">
<div class="hint">Scroll down ↓</div>
<div class="scene">
<Parallax class="landscape" :container="region" :speed="-.6"><img class="photo" src="https://picsum.photos/seed/beui-parallax/800/600" width="800" height="600" alt="" /></Parallax>
<Parallax class="label" :container="region" :speed=".5"><span class="pill">Parallax</span></Parallax>
<Parallax class="avatar" :container="region" :speed=".9"><img class="face" src="https://picsum.photos/seed/beui-avatar/120/120" width="48" height="48" alt="" /></Parallax>
</div>
<div class="hint">↑ Scroll up</div>
</div>
</template>
<style scoped>
.parallax-frame { box-sizing: border-box; position: relative; height: 600px; width: 100%; max-width: 42rem; overflow-y: auto; scrollbar-width: none; border: 1px solid var(--beui-border); border-radius: 1rem; background: var(--beui-card); }
.parallax-frame::-webkit-scrollbar { display: none; }
.hint { display: flex; height: 20rem; align-items: center; justify-content: center; font-size: .875rem; line-height: 1.25rem; color: var(--beui-muted-foreground); }
.scene { position: relative; height: 24rem; overflow: hidden; }
.landscape { position: absolute; left: 0; right: 0; top: -25%; height: 150%; }
.photo { display: block; width: 100%; height: 100%; object-fit: cover; }
.label { position: absolute; inset: 0; display: grid; place-items: center; }
.pill { border-radius: 999px; background: color-mix(in srgb, var(--beui-background) 85%, transparent); -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px); padding: .5rem 1.25rem; font-size: 1rem; line-height: 1.5rem; font-weight: 500; color: var(--beui-foreground); }
.avatar { position: absolute; bottom: 1rem; right: 1rem; }
.face { display: block; width: 3rem; height: 3rem; border: 2px solid var(--beui-background); border-radius: 50%; object-fit: cover; box-shadow: 0 10px 15px -3px rgb(0 0 0 / .1), 0 4px 6px -4px rgb(0 0 0 / .1); }
</style><script setup lang="ts">
import { motion, useMotionValue, useScroll, useSpring, useTransform } from 'motion-v'
import { useReducedMotion } from '../core/reduced-motion'
import { toRef, useTemplateRef, watchEffect } from 'vue'
const { speed = .3, axis = 'y', spring = true, container } = defineProps<{
/**
* Drift as a fraction of the element's travel through the viewport.
* Positive moves with the scroll (foreground), negative against it
* (background). ~0.1–0.5 reads best.
*/
speed?: number
axis?: 'x' | 'y'
/** Scroll container element for contained scroll areas. Defaults to the viewport. */
container?: HTMLElement | null
/** Spring-smooth the drift. Disabled automatically under reduced motion. */
spring?: boolean
}>()
defineSlots<{ default?: () => unknown }>()
// The moving element is the root itself, so a `class` lands on the layer that drifts.
const target = useTemplateRef('target')
const reduce = useReducedMotion()
const { scrollYProgress } = useScroll({
target,
container: toRef(() => container ?? undefined),
offset: ['start end', 'end start'],
})
// Progress 0→1 as the element crosses the viewport, mapped to a symmetric drift.
const travel = useMotionValue(speed * 100)
watchEffect(() => travel.set(speed * 100))
const drift = useTransform(() => (1 - 2 * scrollYProgress.get()) * travel.get())
// Soft follow so the drift trails the scroll smoothly; looser than the UI springs on purpose.
const smoothed = useSpring(drift, { stiffness: 120, damping: 30, mass: .6 })
</script>
<template>
<motion.div ref="target" :style="reduce ? {} : { [axis]: spring ? smoothed : drift }"><slot /></motion.div>
</template>Install
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/parallax$ 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 Parallax from '@/components/beui/effects/Parallax.vue'
import '@/components/beui/styles.css'API Reference
speed?numberDrift as a fraction of the element's travel through the viewport. Positive moves with the scroll (foreground), negative against it (background). ~0.1–0.5 reads best.
.3axis?"x" | "y"ycontainer?HTMLElement | nullScroll container element for contained scroll areas. Defaults to the viewport.
—spring?booleanSpring-smooth the drift. Disabled automatically under reduced motion.
trueclass?string—#default?slot—Scroll To
ScrollTo.vue<script setup lang="ts">
import { ScrollTo, SmoothScroll } from 'beui-vue'
import { useId } from 'vue'
// ScrollTo uses the active SmoothScroll provider. Here it is contained (:root="false"); the nav buttons glide the box to each section.
const prefix = useId()
const sections = ['Intro', 'Features', 'Pricing', 'FAQ']
</script>
<template>
<SmoothScroll :root="false" class="scroll-frame" role="region" aria-label="Scroll to section" tabindex="0">
<nav class="nav" aria-label="Sections">
<ScrollTo v-for="section in sections" :key="section" :to="`#${prefix}-${section}`" :offset="-48" class="link">{{ section }}</ScrollTo>
</nav>
<section v-for="section in sections" :id="`${prefix}-${section}`" :key="section" :data-pricing="section === 'Pricing' ? '' : undefined" class="section">{{ section }}</section>
</SmoothScroll>
</template>
<style scoped>
.scroll-frame { box-sizing: border-box; position: relative; width: 100%; max-width: 32rem; height: 20rem; overflow-y: auto; scrollbar-width: none; border: 1px solid var(--beui-border); border-radius: 1rem; background: var(--beui-card); }
.scroll-frame::-webkit-scrollbar { display: none; }
.nav { position: sticky; top: 0; z-index: 10; display: flex; gap: .375rem; padding: .5rem; border-bottom: 1px solid var(--beui-border); background: color-mix(in srgb, var(--beui-background) 80%, transparent); -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px); }
.link { padding: .25rem .75rem; border: 0; border-radius: 999px; background: transparent; font: inherit; font-size: .875rem; line-height: 1.25rem; color: var(--beui-muted-foreground); cursor: pointer; transition: background-color .15s, color .15s; }
.link:hover { background: var(--beui-muted); color: var(--beui-foreground); }
.section { display: flex; height: 16rem; align-items: center; justify-content: center; font-size: 1.125rem; line-height: 1.75rem; font-weight: 500; color: var(--beui-foreground); }
</style><script setup lang="ts">
import { animate } from 'motion-v'
import { useReducedMotion } from '../core/reduced-motion'
import { onBeforeUnmount } from 'vue'
import { useSmoothScrollProvider } from '../layout/scroll-context'
const { to, offset = 0, duration, container } = defineProps<{ to: number | string | HTMLElement; offset?: number; duration?: number; container?: HTMLElement | null }>()
const reduce = useReducedMotion()
const provider = useSmoothScrollProvider()
let cancel: (() => void) | undefined
function navigate() {
cancel?.()
if (provider && !container) { provider.scrollTo(to, { offset, duration, immediate: duration === 0 }); return }
const host = container ?? window
const origin = container?.scrollTop ?? window.scrollY
let target: number
if (typeof to === 'number') target = to
else {
let element: Element | null
if (typeof to === 'string') {
try { element = (container ?? document).querySelector(to) } catch { return }
} else element = to
if (!element) return
const containerTop = container ? container.getBoundingClientRect().top + container.clientTop : 0
target = element.getBoundingClientRect().top - containerTop + origin
}
const top = target + offset
if (!duration || duration <= 0 || reduce.value) {
host.scrollTo({ top, behavior: reduce.value || duration === 0 ? 'instant' : 'smooth' })
return
}
const motion = animate(origin, top, {
duration, ease: time => Math.min(1, 1.001 - 2 ** (-10 * time)),
onUpdate: next => host.scrollTo({ top: next, behavior: 'instant' }),
onComplete: () => stop(),
})
function stop() {
motion.stop()
host.removeEventListener('wheel', stop)
host.removeEventListener('touchstart', stop)
cancel = undefined
}
host.addEventListener('wheel', stop, { passive: true })
host.addEventListener('touchstart', stop, { passive: true })
cancel = stop
}
onBeforeUnmount(() => cancel?.())
</script>
<template><button type="button" @click="navigate"><slot /></button></template>Install
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/scroll-to$ 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 ScrollTo from '@/components/beui/effects/ScrollTo.vue'
import '@/components/beui/styles.css'API Reference
tonumber | string | HTMLElementWhere to scroll: px offset, selector string or element.
—offset?numberExtra px offset from the target (e.g. to clear a sticky header).
0duration?numberOverride the ease duration in seconds.
—class?string—container?HTMLElement | null—Scroll Reveal
ScrollReveal.vue<script setup lang="ts">
import { ScrollReveal } from 'beui-vue'
import { useTemplateRef } from 'vue'
// On a page <ScrollReveal> tracks the viewport. Here `root` points at the box so each card reveals as it scrolls into the contained view.
const region = useTemplateRef('region')
const cards = ['Spring slide', 'Blur in', 'Staggered by delay', 'Reveal once']
</script>
<template>
<div ref="region" class="reveal-frame" role="region" aria-label="Scroll reveals" tabindex="0">
<div class="cards">
<div class="hint">Scroll ↓</div>
<ScrollReveal v-for="(name, index) in cards" :key="name" class="card" :root="region" :once="false" :delay="index * .05">{{ name }}</ScrollReveal>
<div class="hint">End</div>
</div>
</div>
</template>
<style scoped>
.reveal-frame { box-sizing: border-box; height: 20rem; width: 100%; max-width: 32rem; overflow-y: auto; scrollbar-width: none; border-radius: 1rem; border: 1px solid var(--beui-border); background: var(--beui-card); }
.reveal-frame::-webkit-scrollbar { display: none; }
.cards { display: flex; flex-direction: column; gap: 4rem; padding: 1.5rem; }
.hint { text-align: center; font-size: .875rem; line-height: 1.25rem; color: var(--beui-muted-foreground); }
.card { border-radius: .75rem; border: 1px solid var(--beui-border); background: color-mix(in srgb, var(--beui-muted) 50%, transparent); padding: 4rem 1rem; text-align: center; font-size: 1rem; line-height: 1.5rem; font-weight: 500; color: var(--beui-foreground); }
</style><script setup lang="ts">
import { motion, useInView } from 'motion-v'
import { useReducedMotion } from '../core/reduced-motion'
import { computed, useTemplateRef } from 'vue'
import { easeOut } from '../core/motion'
const { y = 16, blur = 8, duration = .6, delay = 0, once = true, amount = .3, root } = defineProps<{
/** Slide distance in px before reveal. */
y?: number
/** Enter blur in px (kept ≤ 10 per motion conventions). */
blur?: number
/** Reveal duration in seconds. */
duration?: number
delay?: number
/** Reveal only once (default) or every time it enters view. */
once?: boolean
/** Portion of the element that must be visible to trigger. */
amount?: 'some' | 'all' | number
/** Scroll root element for contained scroll areas. Defaults to the viewport. */
root?: Element | null
}>()
defineSlots<{ default?: () => unknown }>()
const target = useTemplateRef('target')
const reduce = useReducedMotion()
const inView = useInView(target, computed(() => ({ root: root ?? undefined, once, amount })))
// Reduced motion keeps the opacity fade and drops the movement and blur. The same
// keys stay in both states, so a late preference change never strands an offset.
const hidden = computed(() => reduce.value ? { opacity: 0, y: 0, filter: 'blur(0px)' } : { opacity: 0, y, filter: `blur(${blur}px)` })
const shown = { opacity: 1, y: 0, filter: 'blur(0px)' }
const initial = { opacity: 0, y, filter: `blur(${blur}px)` }
</script>
<template>
<motion.div ref="target" :initial="initial" :animate="inView ? shown : hidden" :transition="{ duration, ease: easeOut, delay }"><slot /></motion.div>
</template>Install
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/scroll-reveal$ 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 ScrollReveal from '@/components/beui/effects/ScrollReveal.vue'
import '@/components/beui/styles.css'API Reference
y?numberSlide distance in px before reveal.
16blur?numberEnter blur in px (kept ≤ 10 per motion conventions).
8duration?numberReveal duration in seconds.
.6delay?number0once?booleanReveal only once (default) or every time it enters view.
trueamount?"some" | "all" | numberPortion of the element that must be visible to trigger.
.3root?Element | nullScroll root element for contained scroll areas. Defaults to the viewport.
—class?string—#default?slot—Related components
Tabs
Pill, segment or underline tabs with a spring layoutId indicator and optional icons.
Shared Layout Background
A pill that glides between hovered items via motion's shared layout, with blur enter/exit.
Preview Rail
Codex app-inspired navigation rail with compact ticks that form a hover pyramid and reveal a floating destination preview.
Updated