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'
Files of a project
Open folders and choose a file.
src
db
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
| Tree | Type | Default |
|---|---|---|
itemsrequired | TreeNode[] | |
labelrequired | string | |
defaultExpanded | string[] | |
onSelect | ((id: string) => void) | |
selected | string | null |
| TreeSkeleton | Type | Default |
|---|---|---|
items | number | 5 |