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.
Install the following dependencies:
Copy and paste the following code into your project.
/**
* Collapsible: togglable content panel built on native <details>/<summary>.
*
* Tier-1 (no custom element). The browser handles open/close state,
* keyboard activation (Enter / Space on <summary>), focus management,
* and disclosure-widget accessibility, nothing to ship in JS.
*
* shadcn parity:
* Collapsible → <details class=${collapsibleClass()}>
* CollapsibleTrigger → <summary class=${collapsibleTriggerClass()}>
* CollapsibleContent → <div class=${collapsibleContentClass()}>
*
* Initial state: add `open` on <details> to render expanded on first
* paint. Programmatic toggling: `el.open = true | false`. Migrated from
* the prior <ui-collapsible> custom element; the trigger class hides
* the native disclosure marker so callers can render their own chevron.
*
* Design tokens used: --border, --ring, --foreground.
*
* A11y (mostly handled by the native primitives):
* Build it on `<details>` + `<summary>`. The browser gives you the button
* semantics, the expanded state, Enter / Space activation, and focus for free,
* with no ARIA to hand-write. Do not rebuild it from a `<div>` plus
* `aria-expanded`.
* Put the trigger classes on the `<summary>` ITSELF, never on a `<button>` or
* `<div>` nested inside it: nesting an interactive element in a `<summary>`
* splits the focusable element from the labelled one and empties the summary's
* accessible name.
* Give the `<summary>` real text. A summary whose only content is an icon has
* no accessible name, so an icon-only trigger needs an `aria-label` and an
* `aria-hidden="true"` icon.
* Do not put anything essential ONLY inside collapsed content: it is not
* reachable by find-in-page on every engine.
*
* @example
* ```html
* <details class=${collapsibleClass()}>
* <summary class=${collapsibleTriggerClass()}>
* Show details
* <svg class="size-4 transition-transform group-open:rotate-180" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" aria-hidden="true"><path d="m6 9 6 6 6-6" /></svg>
* </summary>
* <div class=${collapsibleContentClass()}>
* Hidden until <summary> is clicked, Enter / Space pressed, or the
* <details> element's `open` property is set via JS.
* </div>
* </details>
* ```
*/
/**
* Root: marks the disclosure widget as a `group` so descendants can use
* Tailwind's `group-open:` variant to react to the `[open]` attribute
* (which `<details>` sets natively). No visual styling of its own.
*/
export const collapsibleClass = (): string => 'group';
/**
* Trigger: hides the native ::marker (and the WebKit -details-marker shim)
* so the disclosure triangle does not appear; callers wrap their own
* chevron icon and rotate it on open via `group-open:rotate-180`.
*
* `disabled: true` returns the visual disabled state. Native <details>
* has no `disabled` attribute, so for full keyboard prevention add the
* standard `inert` attribute on the <details> element. shadcn's React
* `disabled` prop combines both visual and behavior; we split them.
*/
export const collapsibleTriggerClass = (opts: { disabled?: boolean } = {}): string => {
// shadcn's CollapsibleTrigger carries no classes of its own, so a button's
// classes merged onto it keep their size and type. Only the <summary>
// resets stay inline; new-york's row look rides the cn- name.
const base = 'flex w-full items-center justify-between gap-2 rounded-md text-sm font-medium outline-none transition-all focus-visible:ring-2 focus-visible:ring-ring/50 cursor-pointer list-none marker:hidden [&::-webkit-details-marker]:hidden';
if (opts.disabled) return `${base} pointer-events-none cursor-not-allowed opacity-50`;
return base;
};
/**
* Content: <details> already hides children other than <summary> when
* not [open], so this is purely typographic spacing. No display rules.
*/
export const collapsibleContentClass = (): string => 'text-sm';
Update the import paths to match your project setup.
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: