TreeView

Lockness UI Component

VIEW

DOCUMENTATION

TreeView

Documentation for the TreeView component.

Installation

bash
deno run -A jsr:@lockness/ui add treeview

Usage

tsx
import { TreeView, TreeViewItem } from '@lockness/ui/components'

<TreeView>
  <TreeViewItem label="Documents" hasChildren defaultExpanded>
    <TreeViewItem label="Work" hasChildren>
      <TreeViewItem label="Report.pdf" />
      <TreeViewItem label="Presentation.pptx" />
    </TreeViewItem>
    <TreeViewItem label="Personal" hasChildren>
      <TreeViewItem label="Resume.pdf" />
    </TreeViewItem>
  </TreeViewItem>
  <TreeViewItem label="README.md" />
</TreeView>

Props

TreeView

PropTypeDefaultDescription
childrenunknown-Tree items (TreeViewItem components)
itemsTreeViewDataItem[]-Data-driven tree structure (alternative to children)
variant'interactive' | 'text''interactive'Display variant: 'interactive' for collapsible tree, 'text' for ASCII tree
rootLabelstring-Root label for text variant
classstring-Additional CSS class names

TreeViewItem

PropTypeDefaultDescription
labelstringrequiredItem label text
idstring-Unique identifier for this item
hasChildrenboolean-Whether item has children (is a branch vs leaf)
defaultExpandedboolean-Whether item is initially expanded
selectableboolean-Whether item is selectable
defaultSelectedboolean-Whether item is initially selected
iconunknown-Optional icon element
childrenunknown-Nested tree items
classstring-Additional CSS class names
onClick(event: Event) => void-Click handler for item selection

TreeViewDataItem

PropTypeDefaultDescription
idstringrequiredUnique identifier
labelstringrequiredDisplay label
iconunknown-Optional icon element
childrenTreeViewDataItem[]-Nested children items
defaultExpandedboolean-Whether item is initially expanded
selectableboolean-Whether item is selectable
defaultSelectedboolean-Whether item is initially selected

Theming

The TreeView component can be customized using CSS variables. This allows you to change the appearance of tree structures globally or override specific instances.

Available CSS Variables

VariableDefaultDescription
--treeview-item-padding-x0.5remHorizontal padding for tree items
--treeview-item-padding-y0.375remVertical padding for tree items
--treeview-item-border-radiusvar(--radius)Border radius for tree items
--treeview-item-background-hovervar(--accent)Background color on item hover
--treeview-item-foreground-hovervar(--accent-foreground)Text color on item hover
--treeview-icon-size1remSize of expand/collapse icons
--treeview-icon-transition-duration200msDuration of icon rotation animation

Theming Examples

Global Customization

Customize all tree views by setting CSS variables in your theme:

css
/* app/view/assets/app.css */
@theme {
    /* More spacious items */
    --treeview-item-padding-x: 0.75rem;
    --treeview-item-padding-y: 0.5rem;

    /* More rounded items */
    --treeview-item-border-radius: 0.5rem;

    /* Larger icons */
    --treeview-icon-size: 1.25rem;

    /* Faster animation */
    --treeview-icon-transition-duration: 150ms;
}

Local Overrides

Override CSS variables for specific tree view instances:

tsx
<div style="--treeview-item-padding-y: 0.25rem;">
    <TreeView>
        {/* Compact tree with less indentation */}
    </TreeView>
</div>

<div style="--treeview-item-padding-x: 1rem; --treeview-item-border-radius: 0.75rem;">
    <TreeView>
        {/* Tree with more padding and rounded items */}
    </TreeView>
</div>

Component-Specific Theming

Create themed sections with different tree view styles:

tsx
<div class="file-browser" style={{
    '--treeview-item-padding-x': '0.5rem',
    '--treeview-item-padding-y': '0.25rem',
    '--treeview-item-border-radius': '0.25rem',
    '--treeview-icon-size': '0.875rem'
}}>
    <TreeView>
        {/* Compact file browser tree */}
        <TreeViewItem label="src" hasChildren defaultExpanded>
            <TreeViewItem label="components" hasChildren>
                <TreeViewItem label="Button.tsx" />
            </TreeViewItem>
        </TreeViewItem>
    </TreeView>
</div>

<div class="navigation-tree" style={{
    '--treeview-item-padding-x': '1rem',
    '--treeview-item-padding-y': '0.5rem',
    '--treeview-item-border-radius': '0.5rem',
    '--treeview-icon-size': '1.25rem',
    '--treeview-icon-transition-duration': '250ms'
}}>
    <TreeView>
        {/* Spacious navigation tree with smooth animations */}
        <TreeViewItem label="Documentation" hasChildren>
            <TreeViewItem label="Getting Started" />
            <TreeViewItem label="Components" hasChildren>
                <TreeViewItem label="Button" />
                <TreeViewItem label="Input" />
            </TreeViewItem>
        </TreeViewItem>
    </TreeView>
</div>

BASIC TREE

  • Documents
    • Work
      • Report.pdf
      • Presentation.pptx
  • README.md
tsx
import { TreeView, TreeViewItem } from '@lockness/ui/components'

<TreeView>
  <TreeViewItem label="Parent" hasChildren defaultExpanded>
    <TreeViewItem label="Child 1" />
    <TreeViewItem label="Child 2" hasChildren>
      <TreeViewItem label="Grandchild" />
    </TreeViewItem>
  </TreeViewItem>
  <TreeViewItem label="Another Parent" />
</TreeView>

FILE SYSTEM TREE (DATA-DRIVEN)

  • src
    • components
      • Button.tsx
      • Card.tsx
      • TreeView.tsx
  • package.json
tsx
import { TreeView } from '@lockness/ui/components'

const data = [
  {
    id: '1',
    label: 'Parent',
    defaultExpanded: true,
    children: [
      { id: '1-1', label: 'Child 1' },
      {
        id: '1-2',
        label: 'Child 2',
        children: [
          { id: '1-2-1', label: 'Grandchild' }
        ]
      }
    ]
  },
  { id: '2', label: 'Another Parent' }
]

<TreeView items={data} />

TEXT VARIANT (ASCII TREE)

Static ASCII representation, perfect for documentation or code examples.

my-app/
├── app/
│   ├── controller/
│   ├── model/
│   ├── view/
│   │   ├── components/
│   │   ├── layouts/
│   │   └── pages/
│   ├── kernel.tsx
│   └── routes.ts
├── public/
│   ├── css/
│   └── img/
├── main.ts
└── deno.json
tsx
import { TreeView } from '@lockness/ui/components'

const projectStructure = [
    { id: 'app', label: 'app/', children: [
        { id: 'controller', label: 'controller/' },
        { id: 'model', label: 'model/' },
        { id: 'view', label: 'view/', children: [
            { id: 'components', label: 'components/' },
            { id: 'layouts', label: 'layouts/' },
        ]},
    ]},
    { id: 'main', label: 'main.ts' },
]

<TreeView
    items={projectStructure}
    variant="text"
    rootLabel="my-app/"
/>

CUSTOM ICONS

  • Folders
    • public
  • package.json
tsx
import { TreeView, TreeViewItem } from '@lockness/ui/components'

const FolderIcon = () => (
  <svg>...</svg>
)

<TreeView>
  <TreeViewItem
    label="Folder"
    icon={<FolderIcon />}
    hasChildren
  >
    <TreeViewItem label="File.txt" />
  </TreeViewItem>
</TreeView>

SELECTABLE ITEMS

  • Navigation
    • Home
    • About
    • Contact
tsx
import { TreeView, TreeViewItem } from '@lockness/ui/components'

<TreeView>
  <TreeViewItem
    label="Home"
    selectable
    defaultSelected
  />
  <TreeViewItem label="About" selectable />
  <TreeViewItem label="Contact" selectable />
</TreeView>

KEYBOARD NAVIGATION

  • Expand branch / Move to first child
  • Collapse branch / Move to parent
  • Move to next item
  • Move to previous item
  • Enter / Space Select item (if selectable)