---
title: "File Tree"
description: "Explore collapsible folders and inspect a selected file."
---

# File Tree

Explore collapsible folders and inspect a selected file.

## Usage

Expand folders and select a file to inspect its path.

## Example

```contract
use FontWeights from "@rightui/ui"
use uiFontFamily, ControlStyle as Control from "@rightui/ui"

component TreeFile
  props
    path: string
    name: string
    selected: string
    choose: action
  inject
    uiWeights: FontWeights
    uiFont: string
  state hovered = false
  action hover(inside: bool)
    hovered = inside
  view
    button class=Control font-weight=uiWeights.medium hover=hover testId=("ai-file-tree-" + path) press=choose(path) aria-current=(selected == path ? "true" : "false") display="flex" align-items="center" gap="ui.space-8" width="100%" height="ui.space-32" min-height="ui.space-32" padding="ui.space-4 ui.space-12" border="0" border-radius="ui.radius-sm" background-color=(selected == path or hovered ? "ui.muted" : "transparent") color="ui.foreground" font-size="ui.text-sm" text-align="left"
      svg width="ui.space-16" height="ui.space-16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="ui.icon-stroke"
        path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8zM14 2v6h6"
      text name font-family=uiFontFamily(uiFont) data-rightui-font="body"

component Demo
  props
    width: number
    reduced: bool
  inject
    uiWeights: FontWeights
    uiFont: string
  state appOpen = true
  state componentsOpen = true
  state uiOpen = true
  state selected = "components/ui/button.tsx"
  action appToggle
    appOpen = not appOpen
  action componentsToggle
    componentsOpen = not componentsOpen
  action uiToggle
    uiOpen = not uiOpen
  action choose(path: string)
    selected = path
  view
    column width="100%" max-width="20rem" padding="ui.space-24 ui.space-0" font-size="ui.text-sm"
      button id="ai-file-tree-app" testId="ai-file-tree-app" class=Control aria-label="app" aria-controls="ai-file-tree-app-files" aria-expanded=appOpen press=appToggle display="flex" align-items="center" width="100%" height="ui.space-32" min-height="ui.space-32" padding="ui.space-4 ui.space-12" border="0" background-color="transparent" color="ui.foreground" font-size="ui.text-sm" font-weight=uiWeights.regular text-align="left"
        :hover
          background-color="ui.muted"
        row gap="ui.space-8" align-items="center"
          svg width="ui.space-16" height="ui.space-16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="ui.icon-stroke" rotate=(appOpen ? "90deg" : "0deg") aria-hidden=true
            path d="m9 6 6 6-6 6"
          svg width="ui.space-16" height="ui.space-16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="ui.icon-stroke" aria-hidden=true
            path d="M20 20H4a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h5l2 2h9a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2Z"
          text "app" font-family=uiFontFamily(uiFont) data-rightui-font="body"
      if appOpen
        column id="ai-file-tree-app-files" padding-left="ui.space-16"
          TreeFile(path="app/layout.tsx", name="layout.tsx", selected=selected, choose=choose)
          TreeFile(path="app/page.tsx", name="page.tsx", selected=selected, choose=choose)
      button id="ai-file-tree-components" testId="ai-file-tree-components" class=Control aria-label="components" aria-controls="ai-file-tree-component-files" aria-expanded=componentsOpen press=componentsToggle display="flex" align-items="center" width="100%" height="ui.space-32" min-height="ui.space-32" padding="ui.space-4 ui.space-12" border="0" background-color="transparent" color="ui.foreground" font-size="ui.text-sm" font-weight=uiWeights.regular text-align="left"
        :hover
          background-color="ui.muted"
        row gap="ui.space-8" align-items="center"
          svg width="ui.space-16" height="ui.space-16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="ui.icon-stroke" rotate=(componentsOpen ? "90deg" : "0deg") aria-hidden=true
            path d="m9 6 6 6-6 6"
          svg width="ui.space-16" height="ui.space-16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="ui.icon-stroke" aria-hidden=true
            path d="M20 20H4a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h5l2 2h9a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2Z"
          text "components" font-family=uiFontFamily(uiFont) data-rightui-font="body"
      if componentsOpen
        column id="ai-file-tree-component-files" padding-left="ui.space-16"
          button id="ai-file-tree-ui" testId="ai-file-tree-ui" class=Control aria-label="ui" aria-controls="ai-file-tree-ui-files" aria-expanded=uiOpen press=uiToggle display="flex" align-items="center" width="100%" height="ui.space-32" min-height="ui.space-32" padding="ui.space-4 ui.space-12" border="0" background-color="transparent" color="ui.foreground" font-size="ui.text-sm" font-weight=uiWeights.regular text-align="left"
            :hover
              background-color="ui.muted"
            row gap="ui.space-8" align-items="center"
              svg width="ui.space-16" height="ui.space-16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="ui.icon-stroke" rotate=(uiOpen ? "90deg" : "0deg") aria-hidden=true
                path d="m9 6 6 6-6 6"
              svg width="ui.space-16" height="ui.space-16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="ui.icon-stroke" aria-hidden=true
                path d="M20 20H4a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h5l2 2h9a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2Z"
              text "ui" font-family=uiFontFamily(uiFont) data-rightui-font="body"
          if uiOpen
            column id="ai-file-tree-ui-files" padding-left="ui.space-16"
              TreeFile(path="components/ui/button.tsx", name="button.tsx", selected=selected, choose=choose)
          TreeFile(path="components/login-form.tsx", name="login-form.tsx", selected=selected, choose=choose)

```

## Keyboard

Tab to controls; Enter or Space to activate buttons.

## Demo runtime

Timed examples use an injected clock. Add these members to your root component. Replace the scripted state with events from your TypeScript or Rust data source when connecting a model.

```contract
use defaultFontWeights from "@rightui/ui"
  state demoTime = 0
  state demoUntil = 0
  provide
    uiFont = "theme"
    uiWeights = defaultFontWeights()
    rightuiDemoTime = demoTime
    rightuiRunDemo = runDemo
  action runDemo(duration: number)
    demoUntil = max(demoUntil, demoTime + duration)
  action tickDemo
    demoTime = min(demoUntil, demoTime + 32)
  task demoClock when demoTime < demoUntil
    every(32, tickDemo)
```

## Source

File: patterns/ai-elements/file-tree.contract

```contract
use FontWeights from "@rightui/ui"
use uiFontFamily, ControlStyle as Control from "@rightui/ui"

component TreeFile
  props
    path: string
    name: string
    selected: string
    choose: action
  inject
    uiWeights: FontWeights
    uiFont: string
  state hovered = false
  action hover(inside: bool)
    hovered = inside
  view
    button class=Control font-weight=uiWeights.medium hover=hover testId=("ai-file-tree-" + path) press=choose(path) aria-current=(selected == path ? "true" : "false") display="flex" align-items="center" gap="ui.space-8" width="100%" height="ui.space-32" min-height="ui.space-32" padding="ui.space-4 ui.space-12" border="0" border-radius="ui.radius-sm" background-color=(selected == path or hovered ? "ui.muted" : "transparent") color="ui.foreground" font-size="ui.text-sm" text-align="left"
      svg width="ui.space-16" height="ui.space-16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="ui.icon-stroke"
        path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8zM14 2v6h6"
      text name font-family=uiFontFamily(uiFont) data-rightui-font="body"

component Demo
  props
    width: number
    reduced: bool
  inject
    uiWeights: FontWeights
    uiFont: string
  state appOpen = true
  state componentsOpen = true
  state uiOpen = true
  state selected = "components/ui/button.tsx"
  action appToggle
    appOpen = not appOpen
  action componentsToggle
    componentsOpen = not componentsOpen
  action uiToggle
    uiOpen = not uiOpen
  action choose(path: string)
    selected = path
  view
    column width="100%" max-width="20rem" padding="ui.space-24 ui.space-0" font-size="ui.text-sm"
      button id="ai-file-tree-app" testId="ai-file-tree-app" class=Control aria-label="app" aria-controls="ai-file-tree-app-files" aria-expanded=appOpen press=appToggle display="flex" align-items="center" width="100%" height="ui.space-32" min-height="ui.space-32" padding="ui.space-4 ui.space-12" border="0" background-color="transparent" color="ui.foreground" font-size="ui.text-sm" font-weight=uiWeights.regular text-align="left"
        :hover
          background-color="ui.muted"
        row gap="ui.space-8" align-items="center"
          svg width="ui.space-16" height="ui.space-16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="ui.icon-stroke" rotate=(appOpen ? "90deg" : "0deg") aria-hidden=true
            path d="m9 6 6 6-6 6"
          svg width="ui.space-16" height="ui.space-16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="ui.icon-stroke" aria-hidden=true
            path d="M20 20H4a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h5l2 2h9a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2Z"
          text "app" font-family=uiFontFamily(uiFont) data-rightui-font="body"
      if appOpen
        column id="ai-file-tree-app-files" padding-left="ui.space-16"
          TreeFile(path="app/layout.tsx", name="layout.tsx", selected=selected, choose=choose)
          TreeFile(path="app/page.tsx", name="page.tsx", selected=selected, choose=choose)
      button id="ai-file-tree-components" testId="ai-file-tree-components" class=Control aria-label="components" aria-controls="ai-file-tree-component-files" aria-expanded=componentsOpen press=componentsToggle display="flex" align-items="center" width="100%" height="ui.space-32" min-height="ui.space-32" padding="ui.space-4 ui.space-12" border="0" background-color="transparent" color="ui.foreground" font-size="ui.text-sm" font-weight=uiWeights.regular text-align="left"
        :hover
          background-color="ui.muted"
        row gap="ui.space-8" align-items="center"
          svg width="ui.space-16" height="ui.space-16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="ui.icon-stroke" rotate=(componentsOpen ? "90deg" : "0deg") aria-hidden=true
            path d="m9 6 6 6-6 6"
          svg width="ui.space-16" height="ui.space-16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="ui.icon-stroke" aria-hidden=true
            path d="M20 20H4a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h5l2 2h9a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2Z"
          text "components" font-family=uiFontFamily(uiFont) data-rightui-font="body"
      if componentsOpen
        column id="ai-file-tree-component-files" padding-left="ui.space-16"
          button id="ai-file-tree-ui" testId="ai-file-tree-ui" class=Control aria-label="ui" aria-controls="ai-file-tree-ui-files" aria-expanded=uiOpen press=uiToggle display="flex" align-items="center" width="100%" height="ui.space-32" min-height="ui.space-32" padding="ui.space-4 ui.space-12" border="0" background-color="transparent" color="ui.foreground" font-size="ui.text-sm" font-weight=uiWeights.regular text-align="left"
            :hover
              background-color="ui.muted"
            row gap="ui.space-8" align-items="center"
              svg width="ui.space-16" height="ui.space-16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="ui.icon-stroke" rotate=(uiOpen ? "90deg" : "0deg") aria-hidden=true
                path d="m9 6 6 6-6 6"
              svg width="ui.space-16" height="ui.space-16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="ui.icon-stroke" aria-hidden=true
                path d="M20 20H4a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h5l2 2h9a2 2 0 0 1 2 2v10a2 2 0 0 1-2 2Z"
              text "ui" font-family=uiFontFamily(uiFont) data-rightui-font="body"
          if uiOpen
            column id="ai-file-tree-ui-files" padding-left="ui.space-16"
              TreeFile(path="components/ui/button.tsx", name="button.tsx", selected=selected, choose=choose)
          TreeFile(path="components/login-form.tsx", name="login-form.tsx", selected=selected, choose=choose)

```
