DocsComponentsTemplatesThemesGitHub

TreeView

Hierarchical list (files, folders, categories) with expand/collapse and selection; arrow keys open and close.

Code

import * as React from 'react';
import { TreeView } from '@fruit-labs/design';

export default function Demo() {
  return (
    <div
      style={{
        maxWidth: 320,
      }}
    >
      <TreeView
        label="Files"
        defaultExpanded={['src', 'comp']}
        defaultSelected="btn"
        items={[
          {
            id: 'src',
            label: 'src',
            children: [
              {
                id: 'comp',
                label: 'components',
                meta: '3',
                children: [
                  {
                    id: 'btn',
                    label: 'Button.tsx',
                  },
                  {
                    id: 'tbl',
                    label: 'Table.tsx',
                  },
                  {
                    id: 'mdl',
                    label: 'Modal.tsx',
                  },
                ],
              },
              {
                id: 'app',
                label: 'App.tsx',
              },
            ],
          },
          {
            id: 'pkg',
            label: 'README.md',
          },
        ]}
      />
    </div>
  );
}

Install

From npm:

npm install @fruit-labs/design

import { TreeView } from '@fruit-labs/design';

Or copy the TypeScript source into your project:

npx shadcn@latest add https://design.fruitlabs.online/r/tree-view.json

Guidelines

  • Keep depth ≤ 4; show counts in meta.

Props

PropTypeRequired
itemsTreeNode[]Yes
expandedstring[]No
defaultExpandedstring[]No
onExpandedChange(ids: string[]) => voidNo
selectedstringNo
defaultSelectedstringNo
onSelect(id: string) => voidNo
labelstringNo
classNamestringNo

Edit this example on GitHub