vael-ui v0.5.7

Selection

Layout

TreeSelect

A dropdown with a Tree in its panel, for picking one or more nodes from a hierarchy. Use it when a form field takes a value from nested data.

Validating a form? vael-form wires this up with a schema

Install

import { TreeSelect } from 'vael-ui'

Playground

Placement, behaviour and animation

Usage
<script setup lang="ts">
import { ref } from 'vue'
import { TreeSelect } 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)
const open = ref(false)
</script>

<template>
  <TreeSelect
    placeholder="Choose an option"
    aria-label="Project files"
    :items="items"
    v-model="value"
    v-model:open="open"
  />
</template>

Props

NameTypeDefaultDescription
itemsRequiredreadonly T[]Tree data: nodes with value, label and optional children.
placeholderstringText shown in the trigger while nothing is selected.
selectionMode'multiple' | 'single' | 'checkbox''single''single': a click replaces the selection and closes the panel. '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 'checkbox' mode, which puts only leaves in the model.
disabledbooleanfalseDisables the trigger and blocks interaction.
clearablebooleanfalseShows a clear button once something is selected.
invalidbooleanfalseMarks the field invalid. ORed with the nearest Field's error state.
size'sm' | 'md' | 'lg''md'Trigger size.
filterablebooleantrueShows the built-in label search box atop the panel, auto-expanding ancestors of any match. false removes it entirely.
filterPlaceholderstring'Search...'Placeholder for the search box.
emptyTextstring'No results found'Text shown when no nodes match the search or items is empty.
expandOnRowClickbooleanfalseClicking a folder row also toggles its expansion, not only the chevron. The click still selects the row unless selectableFolders is false.
stickyScrollbooleanfalsePins each expanded ancestor's row to the top of the panel while its children scroll past, VS Code-style. Nested rows keep their folder context in view.
scrollFadebooleantrueFades the panel's top and bottom edges while there are more rows past them. With stickyScroll, only the bottom fades, so pinned rows stay solid.
namestringRenders hidden <input>(s) so a plain <form> post carries the selection, repeating name outside 'single' mode.
side'top' | 'right' | 'bottom' | 'left''bottom'Which side of the trigger the panel opens on.
align'start' | 'end' | 'center''start'How the panel aligns against the trigger along that side.
sideOffsetnumber8Gap between the trigger and the panel, in pixels.
alignOffsetnumber0Shifts the panel along the alignment axis, in pixels.
closeOnEscbooleantrueEscape key closes the panel.
closeOnOutsidebooleantrueClicking outside the panel closes it.
beforeCloseBeforeClose<PopoverCloseReason>Runs before the panel closes. Return true to close it, false to keep it open, or a promise of either, like () => confirmAction(…).result. The panel stays open while the promise is pending.
forceMountbooleanfalseKeeps it mounted while closed, toggled with v-show, so its content keeps its state. vael-ui then leaves its motion to you.
teleportTostring | HTMLElement'body'Teleport target: a CSS selector or element.
maxPanelHeightnumber320Caps the panel height in pixels; the tree scrolls past it. The viewport limits it too, so pass Infinity to fill the available space.
panelWidthPanelWidth'trigger'The panel's width. 'trigger' matches the field (at least 18rem, room for nested rows); a width in px or any CSS length ('20rem') sets it.
motionTreeSelectMotiontruefalse skips all built-in motion: the panel opening, rows, the chevron, the list easing to a new height, and the clear button. An object moves the panel your way, like Popover's: values for the panel part, { spring }, or enter / leave hooks for your own library.
uiPartial<Record<'trigger' | 'value' | 'positioner' | 'panel' | 'header' | 'filter' | 'list' | 'node' | 'empty' | 'footer' | 'start' | 'end' | 'clear' | 'chevron', UiPartValue>>Class and style overrides for each part.
modelValuestring | number | (string | number)[] | nullnullSelected value, or an array of values outside 'single' mode.
openbooleanfalseWhether the panel is open.
querystring''Search box text.
nodeT | T[] | nullnullSelected node object(s), shaped like the model. It mirrors the model, so writes to it don't stick.

Events

NameTypeDescription
open-change(value: boolean, details: PopoverOpenChangeDetails)Fires before the panel closes; details.cancel() keeps it open.
select(node: T)Fires when you pick or toggle a node.
change(value: string | number | (string | number)[] | null)Fires when you change the selection, including clear.
expand-change(value: string | number, expanded: boolean)Fires when a node expands or collapses, except during filter-driven auto-expansion.
update:modelValue(value: string | number | (string | number)[] | null)Fires when modelValue changes (v-model).
update:open(value: boolean)Fires when open changes (v-model:open).
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
startanyContent at the start of the trigger, such as an icon, before the value.
endanyContent before the built-in clear button and chevron.
value{ selected: T[]; }Custom trigger content for the selected nodes.
headeranyContent above the filter input (when filterable is on) or the tree.
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; }Custom row content for each node; the built-in row wrapper and behavior stay in place.
emptyanyReplaces emptyText.

Exposed

NameTypeDescription
elHTMLElement | nullRoot element: the trigger frame.
triggerElHTMLElement | nullTrigger element: the frame around the combobox and its clear button. Focusing it focuses the combobox.
panelElHTMLElement | nullPanel element (null while closed).
positionerElHTMLElement | nullPositioning wrapper around the panel (null while closed).
listElHTMLElement | nullThe tree's role="tree" element (null while closed).
placementPlacementResolved placement after flipping, e.g. 'bottom-start'.
positionerStyleRecord<string, string>Inline positioning styles applied to the positioner.
isClosingbooleanTrue while a beforeClose close is pending.
open() => voidOpens the panel (no-op while disabled).
close() => voidCloses the panel, running @open-change and beforeClose first.
cancelClose() => voidCancels a close pending in beforeClose and keeps the panel open.
expandAll() => voidExpands every folder. No-op while the panel is closed.
collapseAll() => voidCollapses every folder. No-op while the panel is closed.
expandNode(value: string | number) => voidExpands the node with this value. No-op while the panel is closed.
collapseNode(value: string | number) => voidCollapses the node with this value. No-op while the panel is closed.
findNode(value: string | number) => TreeNodeFinds the node with this value in items (undefined while closed).
findParent(value: string | number) => TreeNode | nullFinds the parent of the node with this value (null for a root node or while closed).
removeNode(value: string | number) => booleanRemoves the node with this value from items in place; returns whether it was found.

Motion New

TreeSelect's panel: values or hooks for its parts, like Popover's.

<TreeSelect
  :motion="{
    initial: { panel: { opacity: 0, y: 8, scale: 0.98 } },
    spring: { damping: 0.85, response: 0.35 },
  }"
/>
NameTypeDescription
initialexitpositionerpanelSets 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.
morphlinkedexitOrder'trigger' | Record<string, LinkedMotion> | 'reverse'morph: 'trigger' grows it out of the element that opened it. linked moves other elements with it, such as the page receding behind it. exitOrder: 'reverse' closes its children first.
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
ArrowDownArrowUpEnterSpaceOpens the tree from the trigger.
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.
EscapeCloses the panel and returns focus to the trigger.
  • The trigger has role="combobox" with aria-haspopup="tree", aria-expanded and aria-controls pointing at the tree. The tree has the same roles as Tree and takes the trigger’s label.
  • On open, focus moves to the selected row, opening the folders above it, or else to the first row. With filterable, it moves to the search box. A single selection closes the panel; multiple and checkbox keep it open.
  • The clear button is a Tab stop, and clearing puts focus back on the trigger.
  • Inside a Field, the label names the trigger. Without one, pass aria-label or aria-labelledby. Right to left, ArrowLeft and ArrowRight swap.