vael-ui v0.5.7

Gestures

Layout

Sortable

A list you reorder by dragging or from the keyboard. Use it for any order people set by hand, in one list or across several.

Install

import { Sortable } from 'vael-ui'

Playground

  • Apple
  • Banana
  • Cherry
  • Date
  • Elderberry
Press Space or Enter to start reordering. Use the arrow keys to move, Space to drop, Escape to cancel.

Placement, behaviour and animation

Usage
<script setup lang="ts">
import { Sortable } from 'vael-ui'

const items = [
  { label: 'Apple', value: 'apple' },
  { label: 'Banana', value: 'banana' },
  { label: 'Cherry', value: 'cherry' },
]
</script>

<template>
  <Sortable :items="items" />
</template>

Props

NameTypeDefaultDescription
itemKeykeyof T'value'Property that holds each item's stable identity. value matches MenuItemData, SelectItemData and TreeNode.
labelKeykeyof T'label'Property to announce, and to render when you don't pass an #item slot.
axis'x' | 'y''y''y' reorders a column of rows; 'x' reorders a row of items. Arrow keys follow the axis.
canDrop((details: SortableDropDetails) => boolean)Runs repeatedly while you drag; return false to mark the target invalid. Keep it cheap.
beforeDrop((details: SortableDropDetails) => boolean | Promise<boolean>)Async check at drop time. Return false, or a promise of false, to cancel. Pairs with confirmAction().result.
autoScrollbooleantrueScrolls the list, any scrollable ancestor, or the page while a drag nears its edge.
restrictToListbooleanfalseFor lists in a group: keeps the dragged row inside the part of the list it's over that's on screen, so it never hangs past a fixed-height column. Leave it off for lists that grow with their rows.
disabledbooleanfalseTurns off dragging, so rows stay static.
virtualizenumber | booleanfalseFor long lists: only rows near the screen render their #item content; the others keep a row of their measured size, so dragging, autoscroll and moving between grouped lists work as before. The page or any scrolling box around the list can scroll it. true, or the size in px to hold for a row not seen yet (its height, or its width with axis="x").
handle'button' | 'item''button'What you drag by. 'button' is the grip at the start of each row. 'item' is the whole row, as a board's cards: a press that moves drags it, one that doesn't is a click on what's inside. From the keyboard, Space picks a row up and Enter opens it (a click on its content). Rows then take a touch hold (touchDragDelay, 150 ms unset) so the list still scrolls.
variant'card' | 'plain''card''card' draws each row as a card: a border, padding and a background. 'plain' draws no box, so rows look like your content; a held row lifts with a background and a shadow.
motionSortableMotiontrueHow it moves: its appearance, row by row ({ initial: { item: { opacity: 0, y: 8 } }, stagger: 0.04 }), and its states ({ states: { dragging: { root: { scale: '1.01' } } } }). false skips the built-in springs, so rows snap to their new slots. Use it when you drive the motion yourself.
groupSortableGroupHandleHandle from useSortableGroup(). Lists that share it, <Sortable> or useSortable(), let items cross between them.
groupIdstring | numberThis list's identity within group. Unset, Sortable assigns one.
previewMode'element' | 'clone''element'What follows the pointer. 'element' moves the real item; 'clone' floats a copy from the start of the drag, for content that can't leave its layout. The original is the placeholder: it moves to where the row will land and carries data-sortable-source (hidden by default; style it to show it dimmed or outlined).
touchDragDelaynumber0Milliseconds a touch must hold a row still before a drag starts. Unset, drags start at once. Set it only when #item or #handle content is also tappable; the built-in handle never needs it.
uiPartial<Record<'root' | 'item' | 'handle', UiPartValue>>Class and style overrides for each part.
itemsT[][]The list, in order. Sortable assigns a new array when a drop commits, so bind it with v-model:items.

Events

NameTypeDescription
drop-error(error: unknown, details: SortableDropDetails)Fires when beforeDrop throws or rejects, after Sortable reverts the move.
reorder(value: string | number, to: DropPosition)Fires after items changes order. Persist optimistically here; on failure, restore your own snapshot of items.
drag-start(value: string | number)Fires when a row is picked up, by pointer or keyboard.
drag-end(value: string | number)Fires when the row is dropped or the drag is cancelled, before its rows settle.
update:items(value: T[])Fires when items changes (v-model:items).

Slots

NameTypeDescription
item{ item: T; index: number; grabbed: boolean; }Row content. Unset, the row shows the item's labelKey value.
handle{ item: T; }Replaces the default drag handle.

Exposed

NameTypeDescription
elHTMLElement | nullRoot element.
isGrabbedbooleanWhether an item is held, by pointer or keyboard.
isSettlingbooleanWhether rows are still gliding into place after a drop or cancel. The root also carries data-settling then. A list that clips its overflow only while something is held would cut a row off mid-glide, so keep the clipping off while this is true too.
isValidDropbooleanfalse while hovering a target that canDrop rejected.
isPendingbooleanWhether an async beforeDrop is still deciding.
isForeignDropTargetbooleangroup only: whether a drag from another list is hovering this one.
activeValuestring | number | nullKey of the item being dragged, or null.
partsRecord<string, HTMLElement | HTMLElement[] | null>Its elements by part name, for your own animation; a repeated part is an array.

Motion New

Sortable: its appearance, row by row, and states.

<Sortable
  :motion="{ states: { dragging: { item: { scale: 1.04 } } } }"
/>
NameTypeDescription
initialexitrootitemSets where each part comes in from and leaves to: any CSS property, plus x, y and blur. A plain number is px, or degrees for rotate.
statesdraggingsettlingSets values per part that hold while a state is on. An array is a set of keyframes that plays once as the state turns on, such as a shake on invalid.
enterleave(parts, context) => animationAnimates the parts with GSAP, motion or element.animate(). Return the animation and the component waits for it, and stops it if interrupted.
springdurationeasing{ damping, response } | number | stringTimes all of it: a spring, or a duration in ms with an easing. Never both.
falsebooleanTurns its motion off, for an animation of your own on its exposed elements.

Accessibility

KeyAction
EnterSpacePicks up the item from its focused handle, or drops the held item.
ArrowUpArrowDownMoves the held item one position, when axis is y.
ArrowLeftArrowRightMoves the held item one position, when axis is x.
ArrowLeftArrowRightMoves the held item to the end of the previous or next list in a group, when axis is y (ArrowUp / ArrowDown when it is x).
EscapeCancels the move and returns the item to where it started, during a keyboard move or a drag.
TabShiftTabCancels a keyboard move as focus leaves the handle.
  • Each item's handle is a <button> named by labelKey, with aria-roledescription="sortable item", and aria-pressed while its item is grabbed. Its aria-describedby points at hidden instructions for the keys. Escape cancels a grab or drag without closing a Dialog or Drawer around the list.
  • An always-mounted role="status" region with aria-live="polite" announces each grab, move, drop and cancel with the position, such as “Position 2 of 5”.
  • After a keyboard drop, focus stays on the moved item’s handle. Focus leaving the handle during a keyboard move cancels it. With disabled, the handles are disabled buttons.
  • Right to left, ArrowLeft and ArrowRight swap.