vael-ui v0.5.7

Selection

Layout

Tree

An indented, collapsible list for nested data, with single or multiple selection. Use it for file explorers and nested navigation.

Rows update from reactive state. In a custom #node slot, read reactive values (props, refs, computeds), not plain variables captured from the parent.

Install

import { Tree } from 'vael-ui'

Playground

package.json
README.md

Placement, behaviour and animation

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

const items = [
  { label: 'src', value: 'src', children: [
    { label: 'App.vue', value: 'src/App.vue' },
    { label: 'main.ts', value: 'src/main.ts' },
  ] },
  { label: 'README.md', value: 'README.md' },
]
const value = ref<string | number | (string | number)[] | null>(null)
</script>

<template>
  <Tree :items="items" v-model="value" />
</template>

Props

NameTypeDefaultDescription
itemsRequiredreadonly T[]Tree data: TreeNode objects (value, label, optional children) or your own extension of them.
selectionMode'multiple' | 'single' | 'checkbox''single''single': a click replaces the selection. 'multiple': a click toggles that node only. 'checkbox': checkboxes with cascading parent/child toggles.
selectableFoldersbooleantruefalse makes folders unselectable, so only leaves can become the value. No effect in selectionMode="checkbox", which only puts leaves in the model.
filterablebooleantrueShows a built-in label search box above the tree and expands the ancestors of any match.
filterPlaceholderstring'Search...'Placeholder and accessible label for the filter box.
emptyTextstring'No results found'Text shown when no rows are visible (empty items or no filter matches).
motionTreeMotiontrueHow it moves: its appearance, row by row ({ initial: { node: { opacity: 0, x: -4 } }, stagger: 0.03 }), over list, node, chevron and label. false skips all built-in motion (row transitions, chevron rotation, cross-folder moves and the list easing to its new height as you search).
forceMountbooleanfalseKeeps collapsed folders' children mounted (v-show plus data-state) so you can animate expand/collapse yourself instead of using the built-in transition.
reorderablebooleanfalseLets you drag rows to reorder and nest them, VS Code style. Drop on a row's middle to move into it, or on an edge to place beside it.
canNestInto((node: T) => boolean)Which rows accept dropped children. Unset, any row that already has children does; pass your own to let empty folders take drops.
reorderSiblingsbooleantruefalse turns off sibling reordering, so dragging only moves rows into a folder. Rows that can't hold children show no indicator.
canDrop((details: SortableDropDetails) => boolean)Structural check that runs as you drag; returning false marks the target invalid.
beforeDrop((details: SortableDropDetails) => boolean | Promise<boolean>)Async check at drop time; return false (or a promise of it) to cancel. Works with confirmAction().result.
autoExpandDelaynumber600How long, in ms, you hover a collapsed row mid-drag before it opens.
previewMode'element' | 'clone''clone'Drag preview: 'clone' shows a floating copy while the real row hides until drop; 'element' moves the real row itself.
touchDragDelaynumber150How long, in ms, a touch pointer must hold a row still before a drag starts. The hold tells a drag apart from a tap to select or expand. Mouse and pen are unaffected.
expandOnRowClickbooleanfalseClicking anywhere on a folder row toggles it, as well as the chevron. The click still selects the folder unless selectableFolders is false.
stickyScrollbooleanfalsePins each expanded ancestor row to the top of the list while its children scroll past, VS Code-style.
scrollFadebooleantrueFades the list's top and bottom edges while there are more rows past them. With stickyScroll, only the bottom fades, so pinned rows stay solid.
idstringId for the role="tree" list element. Auto-generated when omitted.
uiPartial<Record<'list' | 'node' | 'filter' | 'empty' | 'chevron' | 'label' | 'filterIcon', UiPartValue>>Class and style overrides for each part.
modelValuestring | number | (string | number)[] | nullnullSelected value: one value in 'single' mode, an array in 'multiple', leaf values in 'checkbox'.
querystring''Filter box text.
nodeT | T[] | nullnullSelected node object(s), resolved from v-model against items. Read-only in effect: the value model overwrites any writes.

Events

NameTypeDescription
select(node: T)Fires when you select or toggle a node.
change(value: string | number | (string | number)[] | null)Fires when you change the selection, with the new model value.
drop-error(error: unknown, details: SortableDropDetails)Fires when beforeDrop throws or rejects, after the move is reverted.
expand-change(value: string | number, expanded: boolean)Fires when a single node expands or collapses. Filter auto-expansion, expandAll and collapseAll don't fire it.
reorder(value: string | number, to: DropPosition)Fires after items has been reordered in place.
update:modelValue(value: string | number | (string | number)[] | null)Fires when modelValue changes (v-model).
update:query(value: string)Fires when query changes (v-model:query).
update:node(value: T | T[] | null)Fires when node changes (v-model:node).

Slots

NameTypeDescription
node{ node: T; depth: number; expanded: boolean; checked: boolean; indeterminate: boolean; disabled: boolean; toggleExpand: () => void; toggleSelect: () => void; findNode: (value: string | number) => T | undefined; findParent: (value: string | number) => T | null; removeNode: (value: string | number) => boolean; }Row content inside the role="treeitem" wrapper. findNode/findParent/removeNode are findTreeNode/findTreeParent/removeTreeNode bound to this tree's items. Read reactive state here; plain variables won't update the rows.
emptyanyReplaces the empty-state row shown when no rows are visible.

Exposed

NameTypeDescription
elHTMLElement | nullRoot element.
listElHTMLElement | nullThe role="tree" list element.
highlightElHTMLElement | nullThe hover and keyboard highlight, sliding between rows. With motion: false it moves at once, for your own animation.
filterInputRef{ el: HTMLElement | null; inputEl: HTMLInputElement | null; } | nullThe filter box's Input instance (null when filterable is off).
focusFirstRow() => voidFocuses the first visible row.
initRoving() => voidMakes the first selected row, or else the first visible row, the tab stop without focusing it.
focusNode(value: string | number) => voidExpands the folders above the row with this value and focuses it.
expandAll() => voidExpands every folder.
collapseAll() => voidCollapses every folder.
expandNode(value: string | number) => voidExpands the node with this value, e.g. after adding a child to it.
collapseNode(value: string | number) => voidCollapses the node with this value.
findNode(value: string | number) => TFinds a node in items by value.
findParent(value: string | number) => T | nullFinds a node's parent in items by value (null at the root).
removeNode(value: string | number) => booleanRemoves a node from items in place; returns whether one was found.
isReorderingbooleantrue while a row is held, by pointer or keyboard.
cancelReorder() => voidAborts an in-flight reorder and springs every row back into place.
partsRecord<string, HTMLElement | HTMLElement[] | null>Its elements by part name, for your own animation; a repeated part is an array.

Motion New

Tree: its appearance, row by row.

<Tree
  :motion="{ initial: { list: { opacity: 0, y: 4 } } }"
/>
NameTypeDescription
initialexitlistnodechevronlabelSets 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.
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

Follows the Tree View pattern from the WAI-ARIA Authoring Practices Guide.

KeyAction
ArrowDownArrowUpMoves focus to the next or previous visible row.
HomeEndMoves focus to the first or last visible row.
ArrowRightExpands a closed folder, or moves focus to the first child of an open one.
ArrowLeftCollapses an open folder, or moves focus to the parent row.
EnterSpaceSelects or checks the focused row.
A letterMoves focus to the next row whose label starts with the typed letters.
ArrowDownMoves focus from the search box to the first row.
SpacePicks up the focused row when reorderable is set.
ArrowUpArrowDownMoves the picked-up row up or down.
ArrowLeftArrowRightMoves the picked-up row one level out or in.
SpaceEnterDrops the picked-up row.
EscapeCancels the move and puts the row back.
  • role="tree" holds role="treeitem" rows with aria-level, aria-setsize and aria-posinset; folders add aria-expanded. aria-label, aria-labelledby and aria-describedby go on the tree.
  • Rows carry aria-selected, or aria-checked in checkbox mode, with "mixed" for a partly checked folder. aria-multiselectable is set unless the mode is single.
  • One row is in the tab order at a time, starting on the selected row; in checkbox mode the checkboxes stay out of it. The exposed focusNode(value) opens the folders above a row and focuses it. Typing the same letter again cycles through matches.
  • With reorderable, Space picks a row up instead of selecting it; Enter still selects. Each move is announced through a role="status" live region. Right to left, ArrowLeft and ArrowRight swap.