Tree view
Nested folders with full keyboard support.
Installation
Run the CLI
Run
pnpm dlx asta-ui@latest initonce, then this. The file lands incomponents/astabyte/and the colour tokens go into your CSS.$ pnpm dlx asta-ui@latest add tree-viewOr install the package
Don’t want the file? Import from
asta-ui/tree-viewand update withpnpm update asta-ui. Add the stylesheet once. More.$ pnpm add asta-uiimport { TreeView } from "asta-ui/tree-view";Or copy it by hand
Copy this file. Keep the paths, or fix the
@/imports. Then add the tokens fromglobals.css."use client"; import { useEffect, useMemo, useRef, useState, type KeyboardEvent, type ReactNode } from "react"; const cx = (...c: (string | false | null | undefined)[]) => c.filter(Boolean).join(" "); const KEYFRAMES = `@keyframes asta-tree-in { from { opacity: 0; transform: translateY(-4px) } }`; export interface TreeViewNode { id: string; label: string; /** Replaces the default folder or file icon. */ icon?: ReactNode; /** Muted text on the right, e.g. a size or a count. */ meta?: string; /** Present (even empty) = a folder. */ children?: TreeViewNode[]; disabled?: boolean; } export interface TreeViewProps { nodes: TreeViewNode[]; selected?: string | null; defaultSelected?: string | null; onSelect?: (id: string, node: TreeViewNode) => void; expanded?: string[]; defaultExpanded?: string[]; onExpandedChange?: (ids: string[]) => void; /** Accessible name for the tree. */ label?: string; className?: string; } interface Row { node: TreeViewNode; level: number; parent: string | null; } const isFolder = (n: TreeViewNode) => n.children !== undefined; function FolderIcon({ open }: { open: boolean }) { return ( <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.25" strokeLinecap="round" strokeLinejoin="round" aria-hidden> {open ? <path d="M4 18V6.5A1.5 1.5 0 0 1 5.5 5h3.75l2 2.5H17A1.5 1.5 0 0 1 18.5 9v1.5M4 18l2.4-6.4a1.5 1.5 0 0 1 1.4-1.1h12a1 1 0 0 1 .95 1.3L18.8 18a1.5 1.5 0 0 1-1.4 1H5a1 1 0 0 1-1-1Z" /> : <path d="M3.5 7A1.5 1.5 0 0 1 5 5.5h4.5l2 2.5H19a1.5 1.5 0 0 1 1.5 1.5v8A1.5 1.5 0 0 1 19 19H5a1.5 1.5 0 0 1-1.5-1.5Z" />} </svg> ); } function FileIcon() { return ( <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.25" strokeLinecap="round" strokeLinejoin="round" aria-hidden> <path d="M6 3.5h8l4.5 4.5v12.5H6Z" /> <path d="M14 3.5V8h4.5" /> </svg> ); } /** * Nested folders and files. Click a row to select it, or a folder to open it too. The chevron turns and the * children fade in below. Full WAI-ARIA tree keys: Up/Down, Right to open or step in, Left to close or step * out, Home/End, `*` to open siblings, Enter/Space to select, and type-ahead on labels. */ export function TreeView({ nodes, selected, defaultSelected = null, onSelect, expanded, defaultExpanded = [], onExpandedChange, label, className }: TreeViewProps) { const [innerSel, setInnerSel] = useState<string | null>(defaultSelected); const [innerExp, setInnerExp] = useState<string[]>(defaultExpanded); const sel = selected !== undefined ? selected : innerSel; const exp = useMemo(() => new Set(expanded ?? innerExp), [expanded, innerExp]); // Folders opened by the user: only their children animate in, not the first render. const [opened, setOpened] = useState<Set<string>>(() => new Set()); const [focusId, setFocusId] = useState<string | null>(null); const refs = useRef(new Map<string, HTMLLIElement>()); const tree = useRef<HTMLUListElement>(null); const typed = useRef({ text: "", at: 0 }); // Every node by id, with its parent and depth. const index = useMemo(() => { const m = new Map<string, Row>(); const walk = (list: TreeViewNode[], level: number, parent: string | null) => { for (const n of list) { m.set(n.id, { node: n, level, parent }); if (n.children) walk(n.children, level + 1, n.id); } }; walk(nodes, 1, null); return m; }, [nodes]); // What's on screen, top to bottom. const visible = useMemo(() => { const out: Row[] = []; const walk = (list: TreeViewNode[], level: number, parent: string | null) => { for (const n of list) { out.push({ node: n, level, parent }); if (n.children && exp.has(n.id)) walk(n.children, level + 1, n.id); } }; walk(nodes, 1, null); return out; }, [nodes, exp]); // The one row in the tab order: the focused one if still on screen, else the selection, else the first row. const visibleIds = new Set(visible.map((r) => r.node.id)); const tabId = focusId && visibleIds.has(focusId) ? focusId : sel && visibleIds.has(sel) ? sel : (visible[0]?.node.id ?? null); const setExp = (next: Set<string>) => { const ids = [...next]; if (expanded === undefined) setInnerExp(ids); onExpandedChange?.(ids); }; const isAncestor = (id: string, of: string | null) => { for (let p = of ? index.get(of)?.parent : null; p; p = index.get(p)?.parent) if (p === id) return true; return false; }; const toggle = (id: string, open = !exp.has(id)) => { if (open === exp.has(id)) return; const next = new Set(exp); if (open) { next.add(id); setOpened((o) => (o.has(id) ? o : new Set(o).add(id))); } else { next.delete(id); // Closing a folder that hides the focused row moves focus up to the folder. if (isAncestor(id, focusId)) setFocusId(id); } setExp(next); }; const choose = (n: TreeViewNode) => { if (n.disabled) return; if (selected === undefined) setInnerSel(n.id); onSelect?.(n.id, n); }; const focus = (id: string | undefined | null) => { if (!id) return; setFocusId(id); refs.current.get(id)?.focus(); }; // Keep DOM focus on the focused row when it moves while the tree has focus (e.g. after collapsing a parent). useEffect(() => { if (!focusId || !tree.current?.contains(document.activeElement)) return; const el = refs.current.get(focusId); if (el && document.activeElement !== el) el.focus(); }, [focusId, visible]); const onKey = (e: KeyboardEvent<HTMLUListElement>) => { const i = visible.findIndex((r) => r.node.id === tabId); const row = visible[i]; if (!row) return; const n = row.node; let handled = true; switch (e.key) { case "ArrowDown": focus(visible[i + 1]?.node.id); break; case "ArrowUp": focus(visible[i - 1]?.node.id); break; case "Home": focus(visible[0]?.node.id); break; case "End": focus(visible[visible.length - 1]?.node.id); break; case "ArrowRight": if (isFolder(n) && !n.disabled) { if (!exp.has(n.id)) toggle(n.id, true); else if (n.children!.length) focus(n.children![0].id); } break; case "ArrowLeft": if (isFolder(n) && exp.has(n.id)) toggle(n.id, false); else focus(row.parent); break; case "Enter": choose(n); if (isFolder(n) && !n.disabled) toggle(n.id); break; case " ": choose(n); break; case "*": { const siblings = row.parent ? index.get(row.parent)!.node.children! : nodes; const next = new Set(exp); const fresh: string[] = []; for (const s of siblings) if (isFolder(s) && !s.disabled && !next.has(s.id)) { next.add(s.id); fresh.push(s.id); } if (fresh.length) { setOpened((o) => new Set([...o, ...fresh])); setExp(next); } break; } default: handled = false; if (e.key.length === 1 && !e.ctrlKey && !e.metaKey && !e.altKey) { const t = typed.current; t.text = e.timeStamp - t.at > 600 ? e.key.toLowerCase() : t.text + e.key.toLowerCase(); t.at = e.timeStamp; const start = t.text.length === 1 ? i + 1 : i; const order = [...visible.slice(start), ...visible.slice(0, start)]; const hit = order.find((r) => r.node.label.toLowerCase().startsWith(t.text)); if (hit) { focus(hit.node.id); handled = true; } } } if (handled) e.preventDefault(); }; const renderList = (list: TreeViewNode[], level: number, parent: string | null): ReactNode => list.map((n, k) => { const folder = isFolder(n); const open = folder && exp.has(n.id); const isSel = sel === n.id; const animate = parent !== null && opened.has(parent); return ( <li key={n.id} ref={(el) => { if (el) refs.current.set(n.id, el); else refs.current.delete(n.id); }} role="treeitem" tabIndex={tabId === n.id ? 0 : -1} aria-level={level} aria-setsize={list.length} aria-posinset={k + 1} aria-expanded={folder ? open : undefined} aria-selected={isSel} aria-disabled={n.disabled || undefined} onFocus={(e) => { if (e.target === e.currentTarget) setFocusId(n.id); }} className={cx( "outline-none [&:focus-visible>div:first-child]:ring-2 [&:focus-visible>div:first-child]:ring-inset [&:focus-visible>div:first-child]:ring-brand/50", animate && "animate-[asta-tree-in_.32s_cubic-bezier(.16,1,.3,1)_both] motion-reduce:animate-none", )} style={animate ? { animationDelay: `${Math.min(k, 8) * 24}ms` } : undefined} > <div data-selected={isSel || undefined} onClick={() => { if (n.disabled) return; focus(n.id); choose(n); if (folder) toggle(n.id); }} style={{ paddingLeft: 8 + (level - 1) * 18 }} className={cx( "group/row flex h-8 select-none items-center gap-2 rounded-lg pr-2.5 text-[13px] transition-colors duration-150", n.disabled ? "cursor-not-allowed text-mute opacity-50" : "cursor-pointer", isSel ? "bg-paper-2 text-ink" : !n.disabled && "text-ink-2 hover:bg-[color-mix(in_srgb,var(--color-paper-2)_60%,transparent)] hover:text-ink", )} > <span aria-hidden onClick={(e) => { // The chevron toggles without selecting. if (!folder || n.disabled) return; e.stopPropagation(); focus(n.id); toggle(n.id); }} className="grid h-4 w-4 shrink-0 place-items-center text-mute" > {folder && ( <svg viewBox="0 0 24 24" className={cx("h-3 w-3 transition-transform duration-300 ease-[cubic-bezier(.16,1,.3,1)] motion-reduce:transition-none", open && "rotate-90")} fill="none" stroke="currentColor" strokeWidth="2.7" strokeLinecap="round" strokeLinejoin="round"> <path d="m9.5 6 6 6-6 6" /> </svg> )} </span> <span className={cx("inline-flex h-4 w-4 shrink-0 transition-colors [&_svg]:h-4 [&_svg]:w-4", isSel ? "text-brand" : "text-mute group-hover/row:text-ink-2")}>{n.icon ?? (folder ? <FolderIcon open={open} /> : <FileIcon />)}</span> <span className="flex-1 truncate">{n.label}</span> {n.meta && <span className="shrink-0 text-[11.5px] tabular-nums text-mute">{n.meta}</span>} </div> {open && n.children!.length > 0 && ( <ul role="group" className="flex flex-col gap-px pt-px"> {renderList(n.children!, level + 1, n.id)} </ul> )} </li> ); }); return ( <ul ref={tree} role="tree" aria-label={label} onKeyDown={onKey} className={cx("flex flex-col gap-px", className)}> <style href="asta-tree-view" precedence="asta"> {KEYFRAMES} </style> {renderList(nodes, 1, null)} </ul> ); } export default TreeView;Or ask your AI agent
Paste this into Claude Code, Cursor, Codex, v0 or Lovable. Fill in the last line. More in Prompts.
Add the Astabyte UI Tree view component to my project. Nested folders with full keyboard support. It includes: expand/collapse, selection, arrow keys, Home/End, type-ahead, meta, disabled. Install it: ```sh pnpm dlx asta-ui@latest add tree-view --radix ``` Or with the shadcn CLI: ```sh pnpm dlx shadcn@latest add https://ui.astabyte.com/r/tree-view.json ``` If the project isn't set up yet, run `pnpm dlx asta-ui@latest init --radix` first: it runs `shadcn init` when needed and adds the @astabyte registry (Next.js, Vite, React Router, Astro, TanStack Start; Tailwind CSS v4). Import it like this: ```tsx import { TreeView, type TreeViewNode } from "@/components/astabyte/tree-view"; ``` Usage example and props: https://ui.astabyte.com/tree-view Full catalogue for context: https://ui.astabyte.com/llms.txt Rules: - Keep the colour tokens (paper, paper-2, ink, ink-2, mute, brand). Don't restyle with hard-coded colours; rebrand by changing the token values in the CSS. - Keep the motion as it is: transform and opacity only, and the prefers-reduced-motion fallback. - Keep keyboard, focus and ARIA behaviour. - Use fake data and fake brand names in examples. Use it to: [describe where it goes and what it should do]Or use plain HTML + CSS
Not on React? Use it from Django, Rails, Laravel, Go
html/template, htmx or a static page. Loadasta.cssandasta.jsonce (thehead.htmltab), then paste the markup. Theui.astabyte.com/cdnlink is temporary: pin a version from jsDelivr for production. More in HTML + CSS.<!-- Needs asta.js for expand/collapse, selection and keyboard. Lazy children (vendor) need an htmx endpoint. --> <div style="max-width:320px"><ul role="tree" class="asta-tree-view" data-asta-tree data-tab="src/app/page" aria-label="Files"><input type="hidden" name="file" value="src/app/page" data-asta-tree-value> <li role="treeitem" class="asta-tree-item" data-id="src" tabindex="-1" aria-level="1" aria-setsize="4" aria-posinset="1" aria-expanded="true" aria-selected="false" style="--i:0"><div class="asta-tree-row" data-asta-tree-row data-open><span class="asta-tree-chevron" data-asta-tree-chevron aria-hidden="true"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="m6 4 4 4-4 4"/></svg></span><span class="asta-tree-icon"><svg class="asta-tree-closed" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round" aria-hidden="true"><path d="M2 4.5a1 1 0 0 1 1-1h3L7.5 5H13a1 1 0 0 1 1 1v6a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1v-7.5Z"/></svg><svg class="asta-tree-opened" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round" aria-hidden="true"><path d="M2 12.5v-8a1 1 0 0 1 1-1h3l1.5 1.5H12a1 1 0 0 1 1 1V7M2 12.5 3.8 8a1 1 0 0 1 .9-.6h9.1a.6.6 0 0 1 .6.8l-1.6 4.3h-11Z"/></svg></span><span class="asta-tree-label">src</span></div><ul role="group" class="asta-tree-group"><li role="treeitem" class="asta-tree-item" data-id="src/app" tabindex="-1" aria-level="2" aria-setsize="3" aria-posinset="1" aria-expanded="true" aria-selected="false" style="--i:0"><div class="asta-tree-row" data-asta-tree-row data-open><span class="asta-tree-chevron" data-asta-tree-chevron aria-hidden="true"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="m6 4 4 4-4 4"/></svg></span><span class="asta-tree-icon"><svg class="asta-tree-closed" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round" aria-hidden="true"><path d="M2 4.5a1 1 0 0 1 1-1h3L7.5 5H13a1 1 0 0 1 1 1v6a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1v-7.5Z"/></svg><svg class="asta-tree-opened" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round" aria-hidden="true"><path d="M2 12.5v-8a1 1 0 0 1 1-1h3l1.5 1.5H12a1 1 0 0 1 1 1V7M2 12.5 3.8 8a1 1 0 0 1 .9-.6h9.1a.6.6 0 0 1 .6.8l-1.6 4.3h-11Z"/></svg></span><span class="asta-tree-label">app</span></div><ul role="group" class="asta-tree-group"><li role="treeitem" class="asta-tree-item" data-id="src/app/page" tabindex="0" aria-level="3" aria-setsize="2" aria-posinset="1" aria-selected="true" style="--i:0"><div class="asta-tree-row" data-asta-tree-row data-selected><span class="asta-tree-chevron" data-asta-tree-chevron aria-hidden="true"></span><span class="asta-tree-icon"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round" aria-hidden="true"><path d="M4 2.5h5L12.5 6v7.5H4zM9 2.5V6h3.5"/></svg></span><span class="asta-tree-label">page.templ</span><span class="asta-tree-meta">2 KB</span></div></li><li role="treeitem" class="asta-tree-item" data-id="src/app/layout" tabindex="-1" aria-level="3" aria-setsize="2" aria-posinset="2" aria-selected="false" style="--i:1"><div class="asta-tree-row" data-asta-tree-row><span class="asta-tree-chevron" data-asta-tree-chevron aria-hidden="true"></span><span class="asta-tree-icon"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round" aria-hidden="true"><path d="M4 2.5h5L12.5 6v7.5H4zM9 2.5V6h3.5"/></svg></span><span class="asta-tree-label">layout.templ</span><span class="asta-tree-meta">1 KB</span></div></li></ul></li><li role="treeitem" class="asta-tree-item" data-id="src/ui" tabindex="-1" aria-level="2" aria-setsize="3" aria-posinset="2" aria-expanded="false" aria-selected="false" style="--i:1"><div class="asta-tree-row" data-asta-tree-row><span class="asta-tree-chevron" data-asta-tree-chevron aria-hidden="true"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="m6 4 4 4-4 4"/></svg></span><span class="asta-tree-icon"><svg class="asta-tree-closed" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round" aria-hidden="true"><path d="M2 4.5a1 1 0 0 1 1-1h3L7.5 5H13a1 1 0 0 1 1 1v6a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1v-7.5Z"/></svg><svg class="asta-tree-opened" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round" aria-hidden="true"><path d="M2 12.5v-8a1 1 0 0 1 1-1h3l1.5 1.5H12a1 1 0 0 1 1 1V7M2 12.5 3.8 8a1 1 0 0 1 .9-.6h9.1a.6.6 0 0 1 .6.8l-1.6 4.3h-11Z"/></svg></span><span class="asta-tree-label">ui</span></div><ul role="group" class="asta-tree-group" hidden><li role="treeitem" class="asta-tree-item" data-id="src/ui/button" tabindex="-1" aria-level="3" aria-setsize="2" aria-posinset="1" aria-selected="false" style="--i:0"><div class="asta-tree-row" data-asta-tree-row><span class="asta-tree-chevron" data-asta-tree-chevron aria-hidden="true"></span><span class="asta-tree-icon"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round" aria-hidden="true"><path d="M4 2.5h5L12.5 6v7.5H4zM9 2.5V6h3.5"/></svg></span><span class="asta-tree-label">button.templ</span></div></li><li role="treeitem" class="asta-tree-item" data-id="src/ui/table" tabindex="-1" aria-level="3" aria-setsize="2" aria-posinset="2" aria-selected="false" style="--i:1"><div class="asta-tree-row" data-asta-tree-row><span class="asta-tree-chevron" data-asta-tree-chevron aria-hidden="true"></span><span class="asta-tree-icon"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round" aria-hidden="true"><path d="M4 2.5h5L12.5 6v7.5H4zM9 2.5V6h3.5"/></svg></span><span class="asta-tree-label">table.templ</span></div></li></ul></li><li role="treeitem" class="asta-tree-item" data-id="src/main" tabindex="-1" aria-level="2" aria-setsize="3" aria-posinset="3" aria-selected="false" style="--i:2"><div class="asta-tree-row" data-asta-tree-row><span class="asta-tree-chevron" data-asta-tree-chevron aria-hidden="true"></span><span class="asta-tree-icon"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round" aria-hidden="true"><path d="M4 2.5h5L12.5 6v7.5H4zM9 2.5V6h3.5"/></svg></span><span class="asta-tree-label">main.go</span><span class="asta-tree-meta">4 KB</span></div></li></ul></li><li role="treeitem" class="asta-tree-item" data-id="vendor" tabindex="-1" aria-level="1" aria-setsize="4" aria-posinset="2" aria-expanded="false" aria-selected="false" style="--i:1"><div class="asta-tree-row" data-asta-tree-row><span class="asta-tree-chevron" data-asta-tree-chevron aria-hidden="true"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="m6 4 4 4-4 4"/></svg></span><span class="asta-tree-icon"><svg class="asta-tree-closed" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round" aria-hidden="true"><path d="M2 4.5a1 1 0 0 1 1-1h3L7.5 5H13a1 1 0 0 1 1 1v6a1 1 0 0 1-1 1H3a1 1 0 0 1-1-1v-7.5Z"/></svg><svg class="asta-tree-opened" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round" aria-hidden="true"><path d="M2 12.5v-8a1 1 0 0 1 1-1h3l1.5 1.5H12a1 1 0 0 1 1 1V7M2 12.5 3.8 8a1 1 0 0 1 .9-.6h9.1a.6.6 0 0 1 .6.8l-1.6 4.3h-11Z"/></svg></span><span class="asta-tree-label">vendor</span></div><ul role="group" class="asta-tree-group" hidden hx-get="/tree/vendor" hx-trigger="asta:expand once" hx-swap="innerHTML" data-asta-tree-lazy><li role="none" class="asta-tree-loading" aria-level="2"><svg viewBox="0 0 16 16" fill="none" aria-hidden="true"><circle cx="8" cy="8" r="6" stroke="currentColor" stroke-opacity="0.25" stroke-width="2"/><path d="M14 8a6 6 0 0 0-6-6" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg> Loading…</li></ul></li><li role="treeitem" class="asta-tree-item" data-id="readme" tabindex="-1" aria-level="1" aria-setsize="4" aria-posinset="3" aria-selected="false" style="--i:2"><div class="asta-tree-row" data-asta-tree-row><span class="asta-tree-chevron" data-asta-tree-chevron aria-hidden="true"></span><span class="asta-tree-icon"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round" aria-hidden="true"><path d="M4 2.5h5L12.5 6v7.5H4zM9 2.5V6h3.5"/></svg></span><span class="asta-tree-label">README.md</span></div></li><li role="treeitem" class="asta-tree-item" data-id="secrets" tabindex="-1" aria-level="1" aria-setsize="4" aria-posinset="4" aria-selected="false" aria-disabled="true" style="--i:3"><div class="asta-tree-row" data-asta-tree-row><span class="asta-tree-chevron" data-asta-tree-chevron aria-hidden="true"></span><span class="asta-tree-icon"><svg viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linejoin="round" aria-hidden="true"><path d="M4 2.5h5L12.5 6v7.5H4zM9 2.5V6h3.5"/></svg></span><span class="asta-tree-label">.env</span></div></li> </ul></div>
Usage
The demo above, as code.
"use client";
import { useState } from "react";
import { TreeView, type TreeViewNode } from "@/components/astabyte/tree-view";
const FILES: TreeViewNode[] = [
{
id: "src",
label: "src",
children: [
{
id: "src/app",
label: "app",
children: [
{ id: "src/app/layout.tsx", label: "layout.tsx", meta: "1.2 KB" },
{ id: "src/app/page.tsx", label: "page.tsx", meta: "3.8 KB" },
{ id: "src/app/globals.css", label: "globals.css", meta: "2.1 KB" },
],
},
{
id: "src/components",
label: "components",
children: [
{ id: "src/components/button.tsx", label: "button.tsx", meta: "2.6 KB" },
{ id: "src/components/card.tsx", label: "card.tsx", meta: "1.4 KB" },
{ id: "src/components/dialog.tsx", label: "dialog.tsx", meta: "4.9 KB" },
],
},
{ id: "src/lib", label: "lib", children: [{ id: "src/lib/utils.ts", label: "utils.ts", meta: "0.6 KB" }] },
],
},
{ id: "public", label: "public", children: [{ id: "public/logo.svg", label: "logo.svg", meta: "0.9 KB" }] },
{ id: "drafts", label: "drafts", children: [] },
{ id: "package.json", label: "package.json", meta: "1.1 KB" },
{ id: "README.md", label: "README.md", meta: "2.3 KB" },
{ id: ".env.local", label: ".env.local", disabled: true },
];
export default function TreeViewDemo() {
const [sel, setSel] = useState<string | null>("src/app/page.tsx");
const parts = sel?.split("/") ?? [];
return (
<div className="flex min-h-[460px] w-full items-center justify-center p-6 sm:p-10">
<div className="flex w-full max-w-md flex-col gap-3">
<div className="rounded-xl bg-paper p-2 shadow-[inset_0_0_0_1px_color-mix(in_srgb,var(--color-ink)_10%,transparent)]">
<p className="px-2.5 pb-2 pt-1.5 text-[11.5px] font-medium text-mute">Explorer</p>
<TreeView nodes={FILES} label="Project files" selected={sel} onSelect={(id) => setSel(id)} defaultExpanded={["src", "src/app"]} />
</div>
<p className="flex min-h-5 flex-wrap items-center justify-center gap-1 text-[12.5px] text-mute" aria-live="polite">
{parts.map((p, i) => (
<span key={i} className="inline-flex items-center gap-1">
{i > 0 && <span aria-hidden>/</span>}
<span className={i === parts.length - 1 ? "text-ink-2" : undefined}>{p}</span>
</span>
))}
</p>
</div>
</div>
);
}