Skip to content
WebJs
Menu

The files the agent changed, as a folder tree.

app
components
ui
import { html, nothing } from '@webjsdev/core';
import { cn } from '#lib/utils/cn.ts';
import { buttonClass } from '#components/ui/button.ts';
import { collapsibleClass, collapsibleContentClass } from '#components/ui/collapsible.ts';
import { ChevronRightIcon, FileIcon, FolderIcon } from '#lib/ui/lucide.ts';

type FileNode =
  | { type: 'file'; name: string; path: string }
  | { type: 'folder'; name: string; path: string; children: FileNode[] };

const files: FileNode[] = [
  {
    type: 'folder',
    name: 'app',
    path: 'app',
    children: [
      { type: 'file', name: 'layout.tsx', path: 'app/layout.tsx' },
      { type: 'file', name: 'page.tsx', path: 'app/page.tsx' },
    ],
  },
  {
    type: 'folder',
    name: 'components',
    path: 'components',
    children: [
      {
        type: 'folder',
        name: 'ui',
        path: 'components/ui',
        children: [
          { type: 'file', name: 'button.tsx', path: 'components/ui/button.tsx' },
        ],
      },
      { type: 'file', name: 'login-form.tsx', path: 'components/login-form.tsx' },
    ],
  },
];

const selected = 'components/ui/button.tsx';

// A folder is a native <details>, open to start with. The chevron turns only
// for its OWN folder: [[open]>summary>&] matches the summary of an open
// details, where group-open would also match every folder nested inside an
// open one. The selected file is the one the reader picked. Wiring the
// selection to a click is the reader's.
function FileTreeNode(node: FileNode): unknown {
  if (node.type === 'folder') {
    return html`
      <details data-slot="collapsible" open class=${collapsibleClass()}>
        <summary data-slot="collapsible-trigger" data-variant="ghost" data-size="sm" class=${cn(buttonClass({ variant: 'ghost', size: 'sm' }), 'w-full cursor-pointer list-none justify-start font-normal [&::-webkit-details-marker]:hidden')}>
          ${ChevronRightIcon('transition-transform [[open]>summary>&]:rotate-90')}
          ${FolderIcon()}
          ${node.name}
        </summary>
        <div data-slot="collapsible-content" class=${collapsibleContentClass()}>
          <div class="ml-4 flex flex-col">
            ${node.children.map((child) => FileTreeNode(child))}
          </div>
        </div>
      </details>
    `;
  }
  const current = selected === node.path;
  return html`
    <button type="button" aria-current=${current ? 'true' : nothing} data-slot="button" data-variant="ghost" data-size="sm" class=${cn(buttonClass({ variant: 'ghost', size: 'sm' }), 'w-full justify-start font-normal', current && 'bg-accent text-accent-foreground')}>
      ${FileIcon()}
      ${node.name}
    </button>
  `;
}

export default function FileTreeDemo() {
  return html`
    <div class="flex w-full max-w-xs flex-col py-6 text-sm">
      ${files.map((node) => FileTreeNode(node))}
    </div>
  `;
}
View Code

File Tree is the files the agent changed, shown as folders you can open. A folder is a Collapsible. A file is a Button. For a single file the agent read or edited, use a Tool line instead.

Installation

File Tree is built with the Collapsible and Button components.

pnpm dlx @webjsdev/ui add collapsible button

Usage

Render nodes recursively. Nest each folder's children in its collapsible content. The summary takes the button classes itself, since a button nested inside a <summary> would split the control from its label.

if (node.type === 'folder') {
  return html`
    <details data-slot="collapsible" open class=${collapsibleClass()}>
      <summary data-slot="collapsible-trigger" class=${cn(buttonClass({ variant: 'ghost', size: 'sm' }), 'w-full list-none justify-start [&::-webkit-details-marker]:hidden')}>
        ${ChevronRightIcon('transition-transform [[open]>summary>&]:rotate-90')}
        ${FolderIcon()}
        ${node.name}
      </summary>
      <div data-slot="collapsible-content" class=${collapsibleContentClass()}>
        <div class="ml-4 flex flex-col">
          ${node.children.map((child) => fileTreeNode(child))}
        </div>
      </div>
    </details>
  `;
}
<button type="button" data-slot="button" data-variant="ghost" data-size="sm" class=${cn(buttonClass({ variant: 'ghost', size: 'sm' }), 'w-full justify-start')} @click=${() => this.select(node.path)}>
  ${FileIcon()}
  ${node.name}
</button>

See Collapsible.

Migrating from AI SDK Elements

If you used File Tree from AI SDK Elements, its wrapper lives in components/ai-elements/file-tree.tsx. In a WebJs app it becomes this pattern, built from WebJs UI components. Each export of the wrapper maps onto a part:

AI SDK ElementsWebJs UI
FileTreeFolderA <details> with collapsibleClass(). Its <summary> takes buttonClass({ variant: 'ghost', size: 'sm' }) plus w-full justify-start, and its children go in the collapsible content.
FileTreeFileA button with buttonClass({ variant: 'ghost', size: 'sm' }) whose click handler selects its path. If a file has actions, render them next to the button, not inside it.
FileTreeThe container. Keep the expanded and selected paths in your component, as the open attribute and aria-current.
FileTreeIcon, FileTreeName, FileTreeActionsThe icon, the name and the actions inside or beside the button, as they are.