File Tree

Free

A project's files and folders: a free-set glyph chosen from each name (folders, TypeScript, images, archives, spreadsheets, SQL, lockfiles, `package.json`, Dockerfiles, `.env`), folders sorted first in natural order, git-style badges, expand / collapse, single selection and a `fileOpen` output. It is `sv-tree-view` underneath, so the WAI-ARIA tree keyboard, the single tab stop, type-ahead and the loading / error / empty states are the tree's own.

</>sv-file-tree file treefilesfoldersexplorerprojecttreeiderepositorydeveloper

Complexity tier: Interactive The primitive bar, plus: its async states are declared — none is an answer — it has a keyboard model and restores focus, and any destructive action it offers can be undone.

import { SvFileTreeComponent } from '@styloviz/code-block';

Demo & usage

Basic usage

Usage example — open the gallery for the full interactive demo.

An interactive demo for this component lives in the gallery.

Open live demo

More examples

Files from your API

A node with `children` is a folder; set `kind: 'folder'` on an empty one. Ids must be unique across the whole tree.

example.htmlAngular
files: SvFileNode[] = [
  { id: 'src', name: 'src', expanded: true, children: [
    { id: 'main', name: 'main.ts' },
    { id: 'assets', name: 'assets', kind: 'folder' },
  ] },
  { id: 'pkg', name: 'package.json', badge: 'M' },
];

// <sv-file-tree [nodes]="files" [loading]="loading()" [error]="error()" (retry)="reload()" />

Restore the view

`expandedIds` and `selectedId` are two-way bindable, so a host can persist them.

example.htmlAngular
<sv-file-tree [nodes]="files" [(expandedIds)]="open" [(selectedId)]="current" />

Your own glyphs

Return the name of an icon you registered with `provideSvIcons`.

example.htmlAngular
<sv-file-tree [nodes]="files" [iconFor]="iconFor" />

iconFor = (node: SvFileNode) => node.name.endsWith('.md') ? 'book-open' : svFileIconName(node.name, node.children ? 'folder' : 'file');

When to use

  • The file pane of a code viewer, an IDE-like admin tool or a repository browser.
  • A folder rail in a drive or document manager.
  • Use `sv-tree-view` for hierarchies that are not files — categories, permissions, org charts.

Compare with

  • Tree View / Table — Tree View is the general tree — any hierarchy, an org-chart layout, and in Pro a file explorer with sizes and dates, checkboxes, drag and drop and lazy loading. File Tree is Tree View with files: a glyph chosen from each name, folders sorted first, and a fileOpen output.

Async states

  • loading
  • empty
  • error
  • retry

Item identity & owned state

Restorable state
  • expandedIds

Bind these to put a saved view back — the open sections, the hidden columns, the selected rows — and read their Change events to store what the user does.

Inputs

NameTypeDefaultDescription
nodesreadonly SvFileNode[][]The files and folders, nested through `children`.
ariaLabelstring'Files'What this tree is, e.g. `'Project files'` — names the `role="tree"`.
loadingbooleanfalseTrue while the tree is being fetched. Shows a placeholder instead.
errorSvDataError | string | nullnullLoad failure — shows an error with a retry button.
emptyTextstring'No files'Copy for the empty tree.
selectablebooleantrueAllow selecting a node (click, Enter or Space).
showLinesbooleantrueDraw the indentation guide lines.
sortSvFileTreeSort'folders-first'`folders-first` (A→Z, numbers in numeric order) or `none` to keep the order given.
localestringsvLocale()Locale whose collation orders the names (`sort="folders-first"`).
iconFor((node: SvFileNode) => string) | nullnullChoose the glyph yourself. Return the name of an icon registered with `provideSvIcons` (or one `svFileIconName` returns). `null` uses `svFileIconName`.
expandedIdsSet<string> (two-way)new Set()Expanded folder ids. Two-way bindable, so a host can restore the view.
selectedIdstring | null (two-way)nullThe selected node id. Two-way bindable.

Outputs

EventPayload typeDescription
nodeSelectSvFileNodeA file or folder was selected by click, Enter or Space.
fileOpenSvFileNodeA FILE (not a folder) was activated — open it.
retryvoidThe host should re-run the failed load.
expandedIdsChangeSet<string>Emits the new `expandedIds` whenever it changes — the output half of `[(expandedIds)]`.
selectedIdChangestring | nullEmits the new `selectedId` whenever it changes — the output half of `[(selectedId)]`.

Wire it to your API

This component is headless about transport: it renders what you give it and reports what the user asks for. Nothing below requires @styloviz/data — drive it from whatever you already use.

Template

Bound to the names this component actually exposes.

usage.htmlAngular
<sv-file-tree
  [nodes]="nodes()"
  [loading]="loading()"
  [error]="error()"
  (retry)="load()"
/>

Host class

Drive the three async inputs from your own fetch.

host.component.tsTypeScript
import { signal } from '@angular/core';
import { toSvDataError, type SvDataError } from '@styloviz/core';
import type { SvFileNode } from '@styloviz/code-block';

readonly nodes = signal<SvFileNode[]>([]);
readonly loading = signal(false);
readonly error = signal<SvDataError | null>(null);

async load(): Promise<void> {
  this.loading.set(true);
  this.error.set(null);
  try {
    const res = await fetch('/api/nodes');
    if (!res.ok) throw res;
    this.nodes.set(await res.json());
  } catch (e) {
    this.error.set(toSvDataError(e));
  } finally {
    this.loading.set(false);
  }
}
  • This component does not emit a query, so it renders what you give it. Call `load()` on your own trigger and bind `nodes`, `loading` and `error`.
  • Normalize failures with `toSvDataError()` from `@styloviz/core`. It derives `retryable` from the status, so a 404 does not get a "Try again" button that can only fail again.

Full walkthrough: Connecting to a backend.

Customization & theming

  • Glyph tones come from the `warning` (folders), `primary` (TypeScript), `accent` (media), `info` (data) and `danger` (lockfiles, npm) tokens.
  • `svFileIconName(name, kind)` and `svFileIconClass(icon)` are exported, so a list or a breadcrumb can show the same glyph.
  • `iconTemplate` on `sv-tree-view` (free and Pro) is the hook it uses — available to any tree that wants a glyph per node.
  • Strings (`Files`, `No files`) are `codeBlock` labels in 12 language packs.

Best practices

  • Do: Give it an `ariaLabel` that says which files ("Project files", "Changed files") — two trees on a page are otherwise both just "tree".
  • Do: Set `sort="none"` when the order means something (git status, a manifest), and keep the default to get folders first and natural name order.
  • Don't: Do not put actions in the glyph — it is decorative and hidden from assistive technology; the row is named by the file name.
  • Tip: Use `badge` for a short status — `M`, `A`, a count — and `disabled` for something listed but not openable.

Accessibility

  • One `role="tree"` with `treeitem`s, `aria-level`, `aria-expanded` on folders and `aria-selected` when selectable — the tree's own markup.
  • Keyboard per WAI-ARIA: ↑/↓ move, → opens a folder or steps into it, ← closes it or climbs to the parent, Home/End, type-ahead, Enter/Space select; the tree is one tab stop with a roving tabindex.
  • Glyphs are `aria-hidden`; a folder is announced as expandable, not by its icon.
  • Focus is recovered when the host removes the focused node, and the loading, error (with a retry button) and empty states are announced through the kit's shells.

Feedback on File Tree

Spotted a gap in this page, or something that didn't work as described? Comments are GitHub Discussions in the public styloviz/community repo, so posting needs a GitHub account.

Loads giscus.app and GitHub — nothing is fetched until you click.