vael-ui v0.5.7

Used with a component · BottomSheet

useSheetDrag

The drag and snap engine behind <BottomSheet>. The panel stays as tall as its area and moves only with translateY, so a drag never resizes it. A release settles on a snap point, steps one point after a short, quick flick, or calls onDismiss. It judges the speed over the last 80 ms, so a slow drag that ends in a flick counts as a flick. A drag on the content moves the sheet only when the content is scrolled to the top and the drag goes down. <BottomSheet> adds the overlay, the focus trap, enter and exit, the close flow and nested sheet detection.

Install

import { useSheetDrag } from 'vael-ui'

Examples

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

const open = shallowRef(true)
const panel = useTemplateRef<HTMLElement>('panel')
const handle = useTemplateRef<HTMLElement>('handle')
const content = useTemplateRef<HTMLElement>('content')

const { activeSnap, isDragging } = useSheetDrag(open, {
  panelEl: panel,
  handleEl: handle,
  contentEl: content,
  snapPoints: [
    { id: 'peek', height: 0.35 },
    { id: 'full', height: 0.9 },
  ],
  initialSnap: 'peek',
  onDismiss: () => (open.value = false),
})
</script>

<template>
  <div
    v-if="open"
    ref="panel"
    class="sheet"
    :data-snap="activeSnap"
    :data-dragging="isDragging || undefined"
  >
    <div ref="handle" class="handle" />
    <div ref="content" class="content">Drag the handle, or flick down to dismiss.</div>
  </div>
</template>

<style scoped>
/* As tall as the largest snap point: the composable only moves it with translateY. */
.sheet {
  position: fixed;
  inset: auto 0 0;
  height: 90dvh;
  border-radius: 16px 16px 0 0;
  background: var(--ui-surface);
}
.handle {
  height: 24px;
  cursor: grab;
  touch-action: none;
}
</style>

Parameters

NameTypeDescription
activeRef<boolean>Whether the sheet is open (first argument). Turning it true places the panel at initialSnap at once, so the entrance is yours to animate. Turning it false leaves the panel where it is, so your exit slides it out from there.
panelElRef<HTMLElement | null>The element that moves. The composable sets its transform, transition and block-size inline styles. It also sets --sheet-visible-height to the height on screen, for capping a scroll body inside.
handleElRef<HTMLElement | null>A press here always starts a drag.
contentElRef<HTMLElement | null>The scrolling body. A press here becomes a sheet drag only when the body is at scroll top and the move is downward and mostly vertical. A mostly horizontal move is left to nested gestures, such as a swipe-to-reveal row.
snapPointsMaybeRefOrGetter<SheetSnapPoint[]>The heights the sheet rests at: { id, height } entries from smallest to largest. height is a fraction of the area: 0.6 shows 60% of it. Required.
initialSnapMaybeRefOrGetter<string | undefined>The id of the snap point to open at. Default: the first snap point.
boundsMaybeRefOrGetter<HTMLElement | null | undefined>An element the sheet fills instead of the viewport. Its resizes refit the panel, the same way viewport and on-screen keyboard changes do.
dismissibleMaybeRefOrGetter<boolean>Lets a drag past the smallest snap point dismiss the sheet. false returns it to that point instead. Default true.
onDismiss(velocity: number) => voidFires when a release should dismiss the sheet, with the panel left where the drag ended. Run your close flow here. Required. velocity is the release speed in px/ms, positive toward closed.
onProgress(progress: number | null, timing?: { duration: number; easing: string }) => voidReports how open the sheet is while dragging: 1 at the lowest snap point or above, 0 closed. Once it's let go without closing, null with the timing it settles on (unset means the drawer's). Use it to move another element with the sheet, such as the page behind it.

Returns

NameTypeDescription
activeSnapRef<string | null>The id of the snap point the sheet rests at. Set it to move the sheet there.
isDraggingRef<boolean>true while the sheet follows a pointer.
reset() => voidPuts the panel at initialSnap at once. active turning true already calls it. The entrance is yours to animate, such as with useMotion; <BottomSheet> plays its own.
settle() => voidMoves the panel back to the snap point it rests on. <BottomSheet> calls it when a before-close guard refuses a drag dismiss, so the sheet springs back from where it was let go.
setReceded(receded: boolean) => voidScales the panel down and shifts it up 16 px from its current snap point while another sheet stacks on top. <BottomSheet> calls it on the parent sheet when a nested one opens.