Search docs

Tree View

A hierarchical list with expandable branches, selection, and keyboard navigation.

Tree View follows the Tree View WAI-ARIA design pattern.

  • src
    • index.ts
    • utils.ts
  • package.json
  • README.md
import { component$, useStyles$ } from '@builder.io/qwik';
import { TreeView } from '@leve-ui/tree-view';
import { LuChevronRight, LuFile, LuFolder } from '@qwikest/icons/lucide';
import styles from './default.css?inline';

export default component$(() => {
  useStyles$(styles);

  return (
    <TreeView.Root
      aria-label="Project files"
      defaultExpandedValue={['src']}
      defaultValue={['src/index.ts']}
      class="tree-view-default"
    >
      <TreeView.Item value="src" branch class="tree-view-default-item">
        <TreeView.ItemTrigger class="tree-view-default-trigger">
          <TreeView.ItemIndicator class="tree-view-default-indicator">
            <LuChevronRight class="tree-view-default-chevron" aria-hidden="true" />
          </TreeView.ItemIndicator>
          <LuFolder class="tree-view-default-icon" aria-hidden="true" />
          <span>src</span>
        </TreeView.ItemTrigger>
        <TreeView.Group class="tree-view-default-group">
          <TreeView.Item value="src/components" branch class="tree-view-default-item">
            <TreeView.ItemTrigger class="tree-view-default-trigger">
              <TreeView.ItemIndicator class="tree-view-default-indicator">
                <LuChevronRight class="tree-view-default-chevron" aria-hidden="true" />
              </TreeView.ItemIndicator>
              <LuFolder class="tree-view-default-icon" aria-hidden="true" />
              <span>components</span>
            </TreeView.ItemTrigger>
            <TreeView.Group class="tree-view-default-group">
              <TreeView.Item value="src/components/button.tsx" class="tree-view-default-item">
                <TreeView.ItemTrigger class="tree-view-default-trigger">
                  <LuFile class="tree-view-default-icon" aria-hidden="true" />
                  <span>button.tsx</span>
                </TreeView.ItemTrigger>
              </TreeView.Item>
              <TreeView.Item value="src/components/dialog.tsx" class="tree-view-default-item">
                <TreeView.ItemTrigger class="tree-view-default-trigger">
                  <LuFile class="tree-view-default-icon" aria-hidden="true" />
                  <span>dialog.tsx</span>
                </TreeView.ItemTrigger>
              </TreeView.Item>
            </TreeView.Group>
          </TreeView.Item>
          <TreeView.Item value="src/index.ts" class="tree-view-default-item">
            <TreeView.ItemTrigger class="tree-view-default-trigger">
              <LuFile class="tree-view-default-icon" aria-hidden="true" />
              <span>index.ts</span>
            </TreeView.ItemTrigger>
          </TreeView.Item>
          <TreeView.Item value="src/utils.ts" class="tree-view-default-item">
            <TreeView.ItemTrigger class="tree-view-default-trigger">
              <LuFile class="tree-view-default-icon" aria-hidden="true" />
              <span>utils.ts</span>
            </TreeView.ItemTrigger>
          </TreeView.Item>
        </TreeView.Group>
      </TreeView.Item>

      <TreeView.Item value="docs" branch class="tree-view-default-item">
        <TreeView.ItemTrigger class="tree-view-default-trigger">
          <TreeView.ItemIndicator class="tree-view-default-indicator">
            <LuChevronRight class="tree-view-default-chevron" aria-hidden="true" />
          </TreeView.ItemIndicator>
          <LuFolder class="tree-view-default-icon" aria-hidden="true" />
          <span>docs</span>
        </TreeView.ItemTrigger>
        <TreeView.Group class="tree-view-default-group">
          <TreeView.Item value="docs/getting-started.md" class="tree-view-default-item">
            <TreeView.ItemTrigger class="tree-view-default-trigger">
              <LuFile class="tree-view-default-icon" aria-hidden="true" />
              <span>getting-started.md</span>
            </TreeView.ItemTrigger>
          </TreeView.Item>
          <TreeView.Item value="docs/guide.md" class="tree-view-default-item">
            <TreeView.ItemTrigger class="tree-view-default-trigger">
              <LuFile class="tree-view-default-icon" aria-hidden="true" />
              <span>guide.md</span>
            </TreeView.ItemTrigger>
          </TreeView.Item>
        </TreeView.Group>
      </TreeView.Item>

      <TreeView.Item value="tests" branch class="tree-view-default-item">
        <TreeView.ItemTrigger class="tree-view-default-trigger">
          <TreeView.ItemIndicator class="tree-view-default-indicator">
            <LuChevronRight class="tree-view-default-chevron" aria-hidden="true" />
          </TreeView.ItemIndicator>
          <LuFolder class="tree-view-default-icon" aria-hidden="true" />
          <span>tests</span>
        </TreeView.ItemTrigger>
        <TreeView.Group class="tree-view-default-group">
          <TreeView.Item value="tests/app.test.ts" class="tree-view-default-item">
            <TreeView.ItemTrigger class="tree-view-default-trigger">
              <LuFile class="tree-view-default-icon" aria-hidden="true" />
              <span>app.test.ts</span>
            </TreeView.ItemTrigger>
          </TreeView.Item>
        </TreeView.Group>
      </TreeView.Item>

      <TreeView.Item value="package.json" class="tree-view-default-item">
        <TreeView.ItemTrigger class="tree-view-default-trigger">
          <LuFile class="tree-view-default-icon" aria-hidden="true" />
          <span>package.json</span>
        </TreeView.ItemTrigger>
      </TreeView.Item>
      <TreeView.Item value="README.md" class="tree-view-default-item">
        <TreeView.ItemTrigger class="tree-view-default-trigger">
          <LuFile class="tree-view-default-icon" aria-hidden="true" />
          <span>README.md</span>
        </TreeView.ItemTrigger>
      </TreeView.Item>
    </TreeView.Root>
  );
});

Installation#

Install the individual package, or use @leve-ui/core to get every component at once:

pnpm add @leve-ui/tree-view

Anatomy#

import { TreeView } from '@leve-ui/tree-view'; // or '@leve-ui/core'

<TreeView.Root aria-label="Files">
  <TreeView.Item value="src" branch>
    <TreeView.ItemTrigger>
      <TreeView.ItemIndicator />
      src
    </TreeView.ItemTrigger>
    <TreeView.Group>
      <TreeView.Item value="src/index.ts">
        <TreeView.ItemTrigger>index.ts</TreeView.ItemTrigger>
      </TreeView.Item>
    </TreeView.Group>
  </TreeView.Item>
</TreeView.Root>
PartElementDescription
TreeView.Root<ul>The tree container (role="tree"). Holds selection, expansion, and focus state.
TreeView.Item<li>A tree node (role="treeitem"). Set branch when it contains a nested group.
TreeView.ItemTrigger<div>Clickable row that focuses the item, selects it, and toggles branch expansion.
TreeView.ItemIndicator<span>Decorative indicator (aria-hidden="true") such as an expand chevron or checkmark.
TreeView.Group<ul>Nested group of child items (role="group"). Hidden while its parent branch is collapsed.

Set branch on any TreeView.Item that has a child TreeView.Group, so aria-expanded is emitted accurately during SSR and omitted on leaf items.

Examples#

Multiple#

Pass multiple on TreeView.Root to allow selecting multiple items at once and set aria-multiselectable="true".

  • Fruits
    • Apple
    • Banana
  • Vegetables
    • Carrot
    • Spinach

Selected: apple, carrot

Controlled#

Use bind:value and bind:expandedValue to read and update selection and branch expansion from outside.

  • src
    • index.ts
    • app.tsx

Selected: src/index.ts

Expanded: src

Disabled#

disabled on TreeView.Item disables that item (and any items nested inside it), and disabled on TreeView.Root disables the whole tree. Arrow keys and typeahead skip disabled items.

  • pnpm-lock.yaml
  • src
    • secret.ts
    • main.ts
  • README.md
  • archived.txt
  • config.json

API Reference#

Root#

PropDefaultType
multiplefalseboolean

Whether multiple tree items can be selected at once.

defaultValue[]string[]

The values of the items selected initially (uncontrolled).

bind:value–Signal<string[]>

A signal with the values of the selected items (controlled).

onValueChange$–QRL<(value: string[]) => void>

Called when the selected items change.

defaultExpandedValue[]string[]

The values of the branch items expanded initially (uncontrolled).

bind:expandedValue–Signal<string[]>

A signal with the values of the expanded branch items (controlled).

onExpandedValueChange$–QRL<(value: string[]) => void>

Called when the expanded branch items change.

disabledfalseboolean

Disables all items in the tree view.

Also accepts all <ul> attributes.

Data attributeValue
data-leve-tree-viewAlways present
data-disabledPresent when disabled

Item#

PropDefaultType
value (required)–string

A unique value that identifies this tree item.

branchfalseboolean

Whether this item is a branch node that can be expanded or collapsed. Set true on items that contain a nested TreeView.Group.

disabledfalseboolean

Disables this item and any nested items inside it.

textValue–string

The text used by typeahead when the item's text content is not suitable.

level–number

Overrides aria-level for this item. Derived from nesting depth (1 at the root level) by default.

setsize–number

Sets aria-setsize for this item when rendering a flat or virtualized structure.

posinset–number

Sets aria-posinset for this item when rendering a flat or virtualized structure.

Also accepts all <li> attributes.

Data attributeValue
data-leve-tree-view-itemAlways present
data-valueThe item's value
data-state"open" | "closed" on branches, "checked" | "unchecked" on leaves
data-selectedPresent when selected
data-branchPresent when branch is true
data-disabledPresent when the item, an ancestor item, or the Root is disabled
data-depth0-based nesting depth (0 at the root level)
data-text-valueThe textValue, when set

ItemTrigger#

No props of its own. Accepts all <div> attributes.

Data attributeValue
data-leve-tree-view-item-triggerAlways present
data-state"open" | "closed" on branches, "checked" | "unchecked" on leaves
data-selectedPresent when the enclosing item is selected
data-branchPresent when the enclosing item is a branch
data-disabledPresent when disabled
data-depth0-based nesting depth of the enclosing item

ItemIndicator#

No props of its own. Accepts all <span> attributes.

Data attributeValue
data-leve-tree-view-item-indicatorAlways present
data-state"open" | "closed" on branches, "checked" | "unchecked" on leaves
data-selectedPresent when the enclosing item is selected
data-branchPresent when the enclosing item is a branch
data-disabledPresent when disabled

Group#

No props of its own. Accepts all <ul> attributes.

Data attributeValue
data-leve-tree-view-groupAlways present
data-state"open" | "closed"
data-disabledPresent when its parent item or the Root is disabled
data-depthNesting depth of the items inside the group (1 for the first nested group)

The group has the hidden attribute while its parent branch is collapsed.

Accessibility#

Complies with the Tree View WAI-ARIA design pattern.

  • Name the tree with aria-label or aria-labelledby on TreeView.Root (role="tree"). When multiple is true, aria-multiselectable="true" is added.
  • Each TreeView.Item renders role="treeitem" with aria-selected, aria-level (computed from nesting depth or level), and roving tabIndex (0 on the active tab stop, -1 on all other items).
  • Branch items (branch={true}) output aria-expanded ("true" or "false"). Leaf items omit aria-expanded.
  • TreeView.ItemIndicator has aria-hidden="true" so decorative chevrons and checkmarks do not clutter screen reader output or typeahead matching.
  • Disabled items set aria-disabled="true" and tabIndex={-1}, and are skipped by navigation and typeahead.

Keyboard Interactions#

KeyDescription
Tab / Shift+TabMoves focus into the tree (to the active tab stop) or out of the tree.
ArrowDown / ArrowUpMoves focus to the next or previous visible enabled item without wrapping.
ArrowRightOn a closed branch, expands it. On an open branch, moves focus to its first enabled child item.
ArrowLeftOn an open branch, collapses it. On a closed branch or leaf, moves focus to its parent item.
Home / EndMoves focus to the first or last visible enabled item.
Enter / SpaceSelects the focused item (or toggles selection when multiple is true).
*Expands all closed enabled sibling branches at the same level without moving focus.
Printable charactersMoves focus to the next visible enabled item whose label starts with the typed characters.