Data Tree

new

A tree drawn from data, with rows from each node's label and icon, loading, empty and error states, and virtualization by one prop.

Source
Demo
  • src
  • App.vue
  • main.ts
  • package.json
  • README.md

No file open

Open Props in the corner of the example to try size and disabled.

Installation

Usage

<script setup lang="ts">
import { DataTree } from '@/components/ui/data-tree'

const files = [
  { id: 'src', label: 'src', children: [{ id: 'src/App.vue', label: 'App.vue' }] },
  { id: 'package.json', label: 'package.json' },
]

const opened = shallowRef()
</script>

<template>
  <DataTree v-model="opened" :items="files" aria-label="Files" />
</template>

DataTree is to Tree what DataTable is to Table. It takes Tree's props and draws the rows itself: a chevron, a checkbox with checkbox, the node's icon and its label. It also draws the states a tree of fetched data goes through. For rows of another shape, use the item slot; to wrap each row in something else, such as a context menu, use Tree.

v-model and v-model:expanded work as on Tree, and they survive a state: a tree that empties and fills again keeps what was open and selected.

Examples

Loading, empty and error

The order is error, then loading, then empty, then the tree. With no items, loading shows skeleton rows; with items, the tree stays and the root turns aria-busy. With no items and not loading, the empty message shows. error shows ahead of everything: pass the error itself, and the error slot gets it as { error }, the place for an ErrorState. A state sits in the tree's frame, next to the tree rather than inside it.

States
  • Inbox
  • Archive

Virtualization

virtualize renders only the rows near the view. Give DataTree a height; the tree scrolls inside the frame. Pass { overscan, estimateSize, textContent } to tune the TreeVirtualizer.

Virtualized data tree

10,100 nodes, about 20 rendered. Try End, or type a name.

Custom rows

The item slot replaces a row's content and gets its state. Build it from TreeItemToggle, TreeItemIcon, TreeItemLabel and TreeItemCheckbox.

With custom rows
  • Inbox12
  • Sent
  • Projects3
  • kappa-ui3
  • Website
  • Archive

API Reference

Data attributes

The tree inside carries the Tree slots and attributes.

data-slotAttributesDescription
data-treedata-size, data-variantThe root and the frame, aria-busy while loading.
data-tree-loading—The loading state.
data-tree-empty—The empty state.
data-tree-error—The error state.

Accessibility

  • A tree may only hold rows, so a state replaces the tree instead of going inside it.
  • aria-label, aria-labelledby and a field's label reach the element with the tree role.
  • The default error message is an alert; the skeleton rows are hidden from screen readers.

Changelog

  1. registry 0.21.0

    b073de0 — New DataTree: a tree drawn from data over the Tree parts, as DataTable is over Table. It takes Tree's props, draws each row from the node's icon and label (with a checkbox in checkbox mode, or your own through the item slot), and shows loading (skeleton rows), empty and error states in its frame through loading, empty and error and their slots, error first. virtualize renders only the rows near the view. v-model and v-model:expanded survive a state.