Skip to content
2k
On This Page

File Tree

The files the agent changed, as a folder tree.

// Inline SVG icons from Lucide (https://lucide.dev), ISC License, Copyright (c) Lucide Icons and Contributors.
import { For, createSignal, createMemo } from "solid-js"

import { cn } from "~/lib/utils"
import { Button } from "~/components/ui/button"
import {
  Collapsible,
  CollapsibleContent,
  CollapsibleTrigger
} from "~/components/ui/collapsible"

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"
      }
    ]
  }
]

function FileTreeNode(componentProps: {
  node: FileNode
  selected: string
  onSelect: (path: string) => void
}) {
  const content = createMemo(() => {
    const node = componentProps.node
    switch (node.type) {
      case "folder":
        return (
          <Collapsible defaultOpen class="group/folder">
            <CollapsibleTrigger
              render={(triggerProps) => (
                <Button
                  {...triggerProps}
                  variant="ghost"
                  size="sm"
                  class="group/folder-trigger w-full justify-start font-normal aria-expanded:bg-transparent aria-expanded:hover:bg-muted dark:aria-expanded:hover:bg-muted/50"
                >
                  <svg
                    width="24"
                    height="24"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                    stroke-linecap="round"
                    stroke-linejoin="round"
                    aria-hidden="true"
                    class="transition-transform group-aria-expanded/folder-trigger:rotate-90"
                  >
                    <path d="m9 18 6-6-6-6" />
                  </svg>
                  <svg
                    width="24"
                    height="24"
                    viewBox="0 0 24 24"
                    fill="none"
                    stroke="currentColor"
                    stroke-width="2"
                    stroke-linecap="round"
                    stroke-linejoin="round"
                    aria-hidden="true"
                  >
                    <path d="M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z" />
                  </svg>
                  {node.name}
                </Button>
              )}
            />
            <CollapsibleContent>
              <div class="ml-4 flex flex-col">
                {
                  <For each={node.children}>
                    {(child) => (
                      <FileTreeNode
                        node={child}
                        selected={componentProps.selected}
                        onSelect={componentProps.onSelect}
                      />
                    )}
                  </For>
                }
              </div>
            </CollapsibleContent>
          </Collapsible>
        )
      case "file":
        return (
          <Button
            variant="ghost"
            size="sm"
            class={cn("w-full justify-start font-normal", {
              "bg-accent text-accent-foreground":
                componentProps.selected === node.path
            })}
            aria-current={
              componentProps.selected === node.path ? "true" : undefined
            }
            onClick={() => componentProps.onSelect(node.path)}
          >
            <svg
              width="24"
              height="24"
              viewBox="0 0 24 24"
              fill="none"
              stroke="currentColor"
              stroke-width="2"
              stroke-linecap="round"
              stroke-linejoin="round"
              aria-hidden="true"
            >
              <path d="M6 22a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h8a2.4 2.4 0 0 1 1.704.706l3.588 3.588A2.4 2.4 0 0 1 20 8v12a2 2 0 0 1-2 2z" />
              <path d="M14 2v5a1 1 0 0 0 1 1h5" />
            </svg>
            {node.name}
          </Button>
        )
      default: {
        const unreachable: never = node
        return unreachable
      }
    }
  })
  return <>{content()}</>
}

export default function FileTreeDemo() {
  const [selected, setSelected] = createSignal("components/ui/button.tsx")

  return (
    <div class="flex w-full max-w-xs flex-col py-6 text-sm">
      {
        <For each={files}>
          {(node) => (
            <FileTreeNode
              node={node}
              selected={selected()}
              onSelect={setSelected}
            />
          )}
        </For>
      }
    </div>
  )
}

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 solidui-cli@latest add collapsible button

Install the required Solid UI dependencies:

pnpm dlx solidui-cli@latest add collapsible button

Copy and paste the following code into your project.

components/ui/collapsible.tsx
import { type ComponentProps } from "@solidjs/web"
import {
  Collapsible as SdkCollapsible,
  CollapsibleTrigger as SdkCollapsibleTrigger,
  CollapsiblePanel as SdkCollapsiblePanel
} from "@ui-sdk/solid/collapsible"
import { omit } from "solid-js"
type CollapsibleProps = Omit<ComponentProps<typeof SdkCollapsible>, "class"> & {
  class?: string
}
const Collapsible = (props: CollapsibleProps) => (
  <SdkCollapsible {...props} class={props.class} />
)
type CollapsibleTriggerProps = Omit<
  ComponentProps<typeof SdkCollapsibleTrigger>,
  "class"
> & {
  class?: string
}
const CollapsibleTrigger = (props: CollapsibleTriggerProps) => {
  const others = omit(props, "class", "children")
  return (
    <SdkCollapsibleTrigger class={props.class} {...others}>
      {props.children}
    </SdkCollapsibleTrigger>
  )
}
type CollapsibleContentProps = Omit<
  ComponentProps<typeof SdkCollapsiblePanel>,
  "class"
> & {
  class?: string
}
const CollapsibleContent = (props: CollapsibleContentProps) => {
  const others = omit(props, "class", "children")
  return (
    <SdkCollapsiblePanel class={props.class} {...others}>
      {props.children}
    </SdkCollapsiblePanel>
  )
}
export { Collapsible, CollapsibleContent, CollapsibleTrigger }

Update the import paths to match your project setup.

Usage

Render nodes recursively. Nest each folder’s children in its CollapsibleContent.

if (node.type === "folder") {
  return (
    <Collapsible defaultOpen class="group/folder">
      <CollapsibleTrigger
        render={(triggerProps) => (
          <Button
            {...triggerProps}
            variant="ghost"
            size="sm"
            class="w-full justify-start"
          >
            <ChevronRightIcon class="transition-transform group-data-[state=open]/folder:rotate-90" />
            <FolderIcon />
            {node.name}
          </Button>
        )}
      />
    </Collapsible>
  )
}
<Button
  variant="ghost"
  size="sm"
  class="w-full justify-start"
  onClick={() => onSelect(node.path)}
>
  <FileIcon />
  {node.name}
</Button>

See Collapsible.

Migrating from AI SDK Elements

If you added File Tree from AI SDK Elements, your project has a components/ai-elements/file-tree.tsx wrapper. Paste this prompt into your coding agent to rebuild it on Collapsible and Button. Every export keeps its name and props, so the code that uses it keeps working.

Prompt
Migrate components/ai-elements/file-tree.tsx from AI SDK Elements to the Solid UI Collapsible and Button components.
 
Keep every export, with the same name and props, so the code that imports it keeps working. Only change how each part renders.
 
1. Install the components: npx solidui-cli@latest add collapsible button
2. Read https://www.solid-ui.com/docs/ai-elements/file-tree.md and build File Tree the same way.
3. Rebuild each part:
   - FileTreeFolder renders a Collapsible. Its trigger is a CollapsibleTrigger with a Button passed through render with variant="ghost", size="sm" and class="w-full justify-start", and its children go in a CollapsibleContent.
   - FileTreeFile renders a Button with variant="ghost" and size="sm" that calls onSelect with its path. If a file has FileTreeActions, render them next to the Button, not inside it.
   - Keep FileTree with its expanded and selection state, and keep FileTreeIcon, FileTreeName and FileTreeActions as they are.
4. Remove the imports the file no longer uses.
5. Run the type checker and fix any errors.