Skip to content

Tree

Things inside things: folders and files, schemas and tables. Arrow keys move, Right opens, Left closes, Enter chooses.

import { Tree, TreeSkeleton } from '@sypnia/ui/react'

On this page
  1. Files of a project
  2. Skeleton
  3. When to use it
  4. For everyone
  5. Props

Files of a project

Open folders and choose a file.

src
api.ts
index.ts
package.json
README.md

api.ts

Code
import { useState } from 'react';
import { Tree, type TreeNode } from '@sypnia/ui/react';

const files: TreeNode[] = [
  { id: 'src', label: 'src', children: [
    { id: 'db', label: 'db', children: [{ id: 'db/client.ts', label: 'client.ts' }, { id: 'db/schema.sql', label: 'schema.sql' }] },
    { id: 'api.ts', label: 'api.ts' },
    { id: 'index.ts', label: 'index.ts' },
  ] },
  { id: 'package.json', label: 'package.json', icon: 'json' },
  { id: 'README.md', label: 'README.md' },
];
// Arrow keys move, Right opens a folder, Left closes it, Enter chooses. Choose a file.
export default function Files() {
  const [file, setFile] = useState<string | null>('api.ts');
  return (
    <div className="sy-stack">
      <Tree label="Files of storefront" items={files} defaultExpanded={['src']} selected={file} onSelect={setFile} />
      <p className="sy-machine">{file}</p>
    </div>
  );
}

Skeleton

The real piece and its skeleton: the same box, so nothing moves when the data arrives.

public
orders
customers
Code
import { Tree, TreeSkeleton } from '@sypnia/ui/react';

export default function Skeleton() {
  return (
    <div className="sy-grid" style={{ ['--min' as string]: '14rem' }}>
      <Tree label="Schemas" defaultExpanded={['public']} items={[{ id: 'public', label: 'public', icon: 'folder', children: [{ id: 'orders', label: 'orders', icon: 'table' }, { id: 'customers', label: 'customers', icon: 'table' }] }]} />
      <TreeSkeleton items={3} />
    </div>
  );
}

When to use it

Use it when

  • A hierarchy the person explores: files, schemas, sections.

Do not use it when

  • It has one level: List.
  • It is where the person goes in the app: SideNav.

For everyone

  • It is a tree grid with a name and a single tab stop; each folder says whether it is open.
  • On touch screens rows are 44 px tall.

Props

TreeTypeDefault
itemsrequired TreeNode[]
labelrequired string
defaultExpanded string[]
onSelect ((id: string) => void)
selected string | null
TreeSkeletonTypeDefault
items number 5
  1. Files of a project
  2. Skeleton
  3. When to use it
  4. For everyone
  5. Props

    Search pages, sections and terms