File Explorer
Reads a repository like a code editor does. Folders come from the paths, files open in tabs with syntax colors, and one search box finds file names and the lines that mention a word.
Installation
pnpm dlx shadcn@latest add https://sprout.tulipfarm.ai/r/file-explorer.jsonCopies File Explorer and the components it uses (Search Field and Spinner) into your app, installs the packages, and adds the styles. Then import it from @/components/ui/file-explorer. Needs Tailwind CSS 4 and a components.json from shadcn init.
Install the following dependencies:
pnpm add clsx tailwind-merge @hugeicons/core-free-icons @hugeicons/react shikiAdd Sprout base once per app: the
cn()helper and the design tokens.lib/sprout/classes.ts- Design tokens, for your global CSS
Add the components it uses: Search Field and Spinner.
Copy and paste the following code into your project.
'use client'; import { useDeferredValue, useEffect, useId, useMemo, useRef, useState, type CSSProperties, type HTMLAttributes, type KeyboardEvent, type ReactNode, } from 'react'; import { ChevronRightIcon } from '@/lib/sprout/icons/chevron-right-icon'; import { CloseIcon } from '@/lib/sprout/icons/close-icon'; import { DocumentTextIcon } from '@/lib/sprout/icons/document-text-icon'; import { FolderIcon } from '@/lib/sprout/icons/folder-icon'; import { FolderOpenIcon } from '@/lib/sprout/icons/folder-open-icon'; import { MinusSquareIcon } from '@/lib/sprout/icons/minus-square-icon'; import { cn } from '@/lib/sprout/classes'; import { tokenClass, useCodeTokens } from '@/lib/sprout/code-tokens'; import { codeLanguageNames, type CodeLanguage } from '@/lib/sprout/languages'; import { languageForFilename } from '@/lib/sprout/syntax'; import { SearchField } from '@/components/ui/search-field'; import { StatePanel } from '@/lib/sprout/state-panel-core'; export interface FileExplorerTextFile { path: string; content: string; } export interface FileExplorerUnavailableFile { path: string; unavailable: string; } export type FileExplorerFile = FileExplorerTextFile | FileExplorerUnavailableFile; export interface FileExplorerProps extends Omit<HTMLAttributes<HTMLDivElement>, 'children'> { files: FileExplorerFile[]; label?: string | undefined; defaultPath?: string | undefined; } type Node = | { kind: 'folder'; path: string; name: string; children: Node[] } | { kind: 'file'; path: string; name: string; file: FileExplorerFile }; type Row = { node: Node; depth: number; size: number; position: number }; type Range = [start: number, end: number]; type Hit = { line: number; text: string; ranges: Range[] }; export const fileExplorerMaxMatches = 500; const nameOf = (path: string) => path.slice(path.lastIndexOf('/') + 1); const folderOf = (path: string) => path.slice(0, Math.max(0, path.lastIndexOf('/'))); const order = (a: Node, b: Node) => a.kind === b.kind ? a.name.localeCompare(b.name, undefined, { numeric: true }) : a.kind === 'folder' ? -1 : 1; function ancestors(path: string) { const parts = path.split('/').slice(0, -1); return parts.map((_, index) => parts.slice(0, index + 1).join('/')); } function buildTree(files: FileExplorerFile[]) { const root: Node[] = []; const folders = new Map<string, Node[]>([['', root]]); for (const file of files) { for (const folder of ancestors(file.path)) { if (folders.has(folder)) continue; const children: Node[] = []; folders.get(folderOf(folder))!.push({ kind: 'folder', path: folder, name: nameOf(folder), children, }); folders.set(folder, children); } folders.get(folderOf(file.path))!.push({ kind: 'file', path: file.path, name: nameOf(file.path), file }); } for (const children of folders.values()) children.sort(order); return root; } function visibleRows(nodes: Node[], expanded: Set<string>, depth = 0): Row[] { return nodes.flatMap((node, index) => [ { node, depth, size: nodes.length, position: index + 1 }, ...(node.kind === 'folder' && expanded.has(node.path) ? visibleRows(node.children, expanded, depth + 1) : []), ]); } function rangesIn(text: string, needle: string): Range[] { const haystack = text.toLowerCase(); const ranges: Range[] = []; for (let at = haystack.indexOf(needle); at >= 0; at = haystack.indexOf(needle, at + needle.length)) ranges.push([at, at + needle.length]); return ranges; } const excerptLead = 32; function searchFiles(files: FileExplorerFile[], query: string) { const needle = query.toLowerCase(); const names = files.filter(file => file.path.toLowerCase().includes(needle)); const hits: { file: FileExplorerTextFile; lines: Hit[] }[] = []; let total = 0; for (const file of files) { if (!('content' in file) || total >= fileExplorerMaxMatches) continue; const lines: Hit[] = []; file.content.split('\n').forEach((raw, index) => { if (total >= fileExplorerMaxMatches) return; const ranges = rangesIn(raw, needle); if (!ranges.length) return; const indent = raw.length - raw.trimStart().length; const cut = Math.max(indent, ranges[0]![0] - excerptLead); const prefix = cut > indent ? '…' : ''; const shift = cut - prefix.length; lines.push({ line: index + 1, text: prefix + raw.slice(cut).trimEnd(), ranges: ranges.map(([start, end]) => [start - shift, end - shift]), }); total += 1; }); if (lines.length) hits.push({ file, lines }); } return { names, hits, total, capped: total >= fileExplorerMaxMatches }; } function marked(pieces: { content: string; className?: string | undefined }[], ranges: Range[]) { const out: ReactNode[] = []; let offset = 0; pieces.forEach((piece, index) => { const end = offset + piece.content.length; const cuts = new Set([offset, end]); for (const [start, stop] of ranges) for (const cut of [start, stop]) if (cut > offset && cut < end) cuts.add(cut); const sorted = [...cuts].sort((a, b) => a - b); for (let at = 0; at < sorted.length - 1; at += 1) { const from = sorted[at]!; const text = piece.content.slice(from - offset, sorted[at + 1]! - offset); const span = ( <span key={`${index}-${from}`} className={piece.className}> {text} </span> ); out.push( ranges.some(([start, stop]) => from >= start && from < stop) ? ( <mark key={`${index}-${from}`} className="sprout-file-explorer-mark"> {span} </mark> ) : ( span ), ); } offset = end; }); return out; } type Tone = 'info' | 'success' | 'warning' | 'danger' | 'accent' | 'muted'; const glyphs: Partial<Record<CodeLanguage, [label: string, tone: Tone]>> = { typescript: ['TS', 'info'], tsx: ['TSX', 'info'], javascript: ['JS', 'warning'], jsx: ['JSX', 'warning'], json: ['{ }', 'warning'], jsonc: ['{ }', 'warning'], json5: ['{ }', 'warning'], markdown: ['MD', 'accent'], mdx: ['MDX', 'accent'], python: ['PY', 'info'], yaml: ['YML', 'danger'], toml: ['TML', 'danger'], css: ['CSS', 'accent'], scss: ['SCS', 'accent'], html: ['< >', 'danger'], xml: ['< >', 'warning'], shellscript: ['SH', 'success'], dotenv: ['ENV', 'success'], sql: ['SQL', 'accent'], rust: ['RS', 'danger'], go: ['GO', 'info'], ruby: ['RB', 'danger'], }; function FileGlyph({ name }: { name: string }) { const language = languageForFilename(name); const extension = name.includes('.') ? (name.split('.').at(-1) ?? '') : ''; const [label, tone] = (language && glyphs[language]) ?? [extension.slice(0, 3).toUpperCase(), 'muted' as const]; if (!label) return <DocumentTextIcon size="md" aria-hidden="true" className="sprout-file-explorer-glyph" />; return ( <svg viewBox="0 0 16 16" aria-hidden="true" focusable="false" data-tone={tone} className="sprout-file-explorer-glyph" > <rect x="0.5" y="1.5" width="15" height="13" rx="3" className="sprout-file-explorer-glyph-tile" /> <text x="8" y="10.8" textAnchor="middle" fontSize={label.length > 2 ? 6 : 7.4} fontWeight="700" className="sprout-file-explorer-glyph-label" > {label} </text> </svg> ); } function CodeView({ file, query, line }: { file: FileExplorerTextFile; query: string; line: number | null }) { const language = languageForFilename(file.path); const tokens = useCodeTokens(file.content, language); const needle = query.toLowerCase(); const current = useRef<HTMLDivElement>(null); useEffect(() => { current.current?.scrollIntoView({ block: 'center' }); }, [line, file.path]); const lines = useMemo<{ content: string; className?: string | undefined }[][]>( () => tokens?.map(row => row.map(token => ({ content: token.content, className: tokenClass(token), })), ) ?? file.content.split('\n').map(content => [{ content }]), [tokens, file.content], ); return ( <div className="sprout-file-explorer-code" tabIndex={0} role="region" aria-label={file.path} data-highlighted={tokens ? '' : undefined} > <pre className="sprout-file-explorer-pre"> <code> {lines.map((pieces, index) => { const text = pieces.map(piece => piece.content).join(''); return ( <div key={index} ref={index + 1 === line ? current : undefined} data-current={index + 1 === line ? '' : undefined} className="sprout-file-explorer-line" > <span aria-hidden="true" className="sprout-file-explorer-line-number"> {index + 1} </span> <span className="sprout-file-explorer-line-text"> {needle ? marked(pieces, rangesIn(text, needle)) : pieces.map((piece, at) => ( <span key={at} className={piece.className}> {piece.content} </span> ))} {'\n'} </span> </div> ); })} </code> </pre> </div> ); } export function FileExplorer({ files, label = 'Files', defaultPath, className, ...props }: FileExplorerProps) { const tree = useMemo(() => buildTree(files), [files]); const byPath = useMemo(() => new Map(files.map(file => [file.path, file])), [files]); const [expanded, setExpanded] = useState(() => new Set(defaultPath ? ancestors(defaultPath) : [])); const [tabs, setTabs] = useState<string[]>(defaultPath && byPath.has(defaultPath) ? [defaultPath] : []); const [active, setActive] = useState<string | null>(tabs[0] ?? null); const [focused, setFocused] = useState<string | null>(active); const [line, setLine] = useState<number | null>(null); const [query, setQuery] = useState(''); const deferred = useDeferredValue(query.trim()); const results = useMemo(() => (deferred ? searchFiles(files, deferred) : null), [files, deferred]); const rows = useMemo(() => visibleRows(tree, expanded), [tree, expanded]); const treeRef = useRef<HTMLDivElement>(null); const id = useId(); const toggle = (path: string, open?: boolean) => setExpanded(current => { const next = new Set(current); if (open ?? !next.has(path)) next.add(path); else next.delete(path); return next; }); const open = (path: string, at: number | null = null) => { setTabs(current => { if (current.includes(path)) return current; const next = [...current]; next.splice(active ? next.indexOf(active) + 1 : next.length, 0, path); return next; }); setActive(path); setFocused(path); setLine(at); setExpanded(current => new Set([...current, ...ancestors(path)])); }; const close = (path: string) => { const index = tabs.indexOf(path); const next = tabs.filter(tab => tab !== path); setTabs(next); if (active === path) { setActive(next[Math.min(index, next.length - 1)] ?? null); setLine(null); } }; const focusRow = (path: string | undefined) => { if (!path) return; setFocused(path); treeRef.current?.querySelector<HTMLElement>(`[data-path="${CSS.escape(path)}"]`)?.focus(); }; const onTreeKeyDown = (event: KeyboardEvent<HTMLDivElement>) => { const index = rows.findIndex(row => row.node.path === focused); const row = rows[index]; if (!row) return; const { node } = row; const isOpen = node.kind === 'folder' && expanded.has(node.path); switch (event.key) { case 'ArrowDown': focusRow(rows[index + 1]?.node.path); break; case 'ArrowUp': focusRow(rows[index - 1]?.node.path); break; case 'Home': focusRow(rows[0]?.node.path); break; case 'End': focusRow(rows.at(-1)?.node.path); break; case 'ArrowRight': if (node.kind !== 'folder') return; if (isOpen) focusRow(rows[index + 1]?.node.path); else toggle(node.path, true); break; case 'ArrowLeft': if (isOpen) toggle(node.path, false); else focusRow(folderOf(node.path) || undefined); break; case 'Enter': case ' ': if (node.kind === 'folder') toggle(node.path); else open(node.path); break; default: return; } event.preventDefault(); }; const activeFile = active ? byPath.get(active) : undefined; const language = activeFile ? languageForFilename(activeFile.path) : null; const rovingPath = focused && rows.some(row => row.node.path === focused) ? focused : rows[0]?.node.path; return ( <div data-slot="file-explorer" {...props} className={cn('sprout-file-explorer', className)}> <aside className="sprout-file-explorer-sidebar" aria-label={label}> <div className="sprout-file-explorer-sidebar-header"> <span className="sprout-file-explorer-title">{label}</span> <button type="button" className="sprout-file-explorer-icon-button" aria-label="Collapse all folders" title="Collapse all folders" disabled={expanded.size === 0} onClick={() => setExpanded(new Set())} > <MinusSquareIcon size="sm" aria-hidden="true" /> </button> </div> <div className="sprout-file-explorer-search"> <SearchField label={`Search ${label.toLowerCase()}`} placeholder="Search names and text" value={query} maxLength={120} onChange={setQuery} className="max-w-none" /> </div> {results ? ( <div className="sprout-file-explorer-results" aria-busy={deferred !== query.trim() || undefined}> <p role="status" className="sprout-file-explorer-summary"> {results.names.length || results.total ? `${results.names.length} ${results.names.length === 1 ? 'file name' : 'file names'}, ${results.capped ? `first ${results.total}` : results.total} ${results.total === 1 ? 'line' : 'lines'}` : `No files or lines match “${deferred}”.`} </p> {results.names.length > 0 && ( <section aria-labelledby={`${id}-names`}> <h3 id={`${id}-names`} className="sprout-file-explorer-group"> File names </h3> <ul className="sprout-file-explorer-list"> {results.names.map(file => ( <li key={file.path}> <button type="button" className="sprout-file-explorer-result" aria-current={file.path === active || undefined} onClick={() => open(file.path)} > <FileGlyph name={file.path} /> <span className="sprout-file-explorer-name"> {marked( [{ content: nameOf(file.path) }], rangesIn(nameOf(file.path), deferred.toLowerCase()), )} </span> <span className="sprout-file-explorer-dir">{folderOf(file.path)}</span> </button> </li> ))} </ul> </section> )} {results.hits.length > 0 && ( <section aria-labelledby={`${id}-lines`}> <h3 id={`${id}-lines`} className="sprout-file-explorer-group"> Text in files </h3> <ul className="sprout-file-explorer-list"> {results.hits.map(({ file, lines }) => ( <li key={file.path}> <button type="button" className="sprout-file-explorer-result" onClick={() => open(file.path, lines[0]!.line)} > <FileGlyph name={file.path} /> <span className="sprout-file-explorer-name">{nameOf(file.path)}</span> <span className="sprout-file-explorer-dir">{folderOf(file.path)}</span> <span className="sprout-file-explorer-count">{lines.length}</span> </button> <ul className="sprout-file-explorer-list"> {lines.map(hit => ( <li key={hit.line}> <button type="button" className="sprout-file-explorer-hit" aria-label={`${file.path}, line ${hit.line}: ${hit.text}`} aria-current={(file.path === active && hit.line === line) || undefined} onClick={() => open(file.path, hit.line)} > <span className="sprout-file-explorer-hit-line">{hit.line}</span> <span className="sprout-file-explorer-hit-text"> {marked([{ content: hit.text }], hit.ranges)} </span> </button> </li> ))} </ul> </li> ))} </ul> </section> )} </div> ) : ( <div ref={treeRef} role="tree" aria-label={label} className="sprout-file-explorer-tree" onKeyDown={onTreeKeyDown} > {rows.length === 0 && <p className="sprout-file-explorer-summary">No files yet.</p>} {rows.map(({ node, depth, size, position }) => { const folder = node.kind === 'folder'; const isOpen = folder && expanded.has(node.path); return ( <div key={node.path} role="treeitem" data-path={node.path} data-kind={node.kind} aria-level={depth + 1} aria-setsize={size} aria-posinset={position} aria-expanded={folder ? isOpen : undefined} aria-selected={folder ? undefined : node.path === active} tabIndex={node.path === rovingPath ? 0 : -1} className="sprout-file-explorer-row" style={{ '--depth': depth } as CSSProperties} onFocus={() => setFocused(node.path)} onClick={() => (folder ? toggle(node.path) : open(node.path))} > {Array.from({ length: depth }, (_, at) => ( <span key={at} aria-hidden="true" className="sprout-file-explorer-guide" /> ))} <span aria-hidden="true" className="sprout-file-explorer-chevron"> {folder && <ChevronRightIcon size="xs" />} </span> {folder ? ( isOpen ? ( <FolderOpenIcon size="md" aria-hidden="true" className="sprout-file-explorer-folder" /> ) : ( <FolderIcon size="md" aria-hidden="true" className="sprout-file-explorer-folder" /> ) ) : ( <FileGlyph name={node.name} /> )} <span className="sprout-file-explorer-name" title={node.path}> {node.name} </span> </div> ); })} </div> )} </aside> <section className="sprout-file-explorer-editor" aria-label="Editor"> {tabs.length > 0 && ( <div className="sprout-file-explorer-tabs" aria-label="Open files"> {tabs.map(path => ( <div key={path} className="sprout-file-explorer-tab" data-active={path === active ? '' : undefined}> <button type="button" className="sprout-file-explorer-tab-open" aria-current={path === active || undefined} title={path} onClick={() => { setActive(path); setLine(null); }} onAuxClick={event => { if (event.button === 1) close(path); }} > <FileGlyph name={path} /> {nameOf(path)} </button> <button type="button" className="sprout-file-explorer-tab-close" aria-label={`Close ${nameOf(path)}`} onClick={() => close(path)} > <CloseIcon size="xs" aria-hidden="true" /> </button> </div> ))} </div> )} {activeFile ? ( <> <nav className="sprout-file-explorer-crumbs" aria-label="File path"> <ol> {activeFile.path.split('/').map((part, index, parts) => ( <li key={index} aria-current={index === parts.length - 1 ? 'page' : undefined}> {index > 0 && ( <ChevronRightIcon size="xs" aria-hidden="true" className="sprout-file-explorer-crumb-separator" /> )} {part} </li> ))} </ol> </nav> <div className="sprout-file-explorer-body"> {'content' in activeFile ? ( activeFile.content === '' ? ( <p className="sprout-file-explorer-summary">This file is empty.</p> ) : ( <CodeView key={activeFile.path} file={activeFile} query={results ? deferred : ''} line={line} /> ) ) : ( <StatePanel kind="unavailable" heading="h3" title="File cannot be displayed" description={activeFile.unavailable} /> )} </div> <div className="sprout-file-explorer-status"> <span>{language ? codeLanguageNames[language] : 'Plain text'}</span> {'content' in activeFile && <span>{activeFile.content.split('\n').length.toLocaleString()} lines</span>} {line !== null && <span>Line {line}</span>} <span className="sprout-file-explorer-status-end">Read only</span> </div> </> ) : ( <StatePanel kind="empty" heading="h3" title="Open a file" description="Choose a file in the tree, or search names and text." icon={DocumentTextIcon} /> )} </section> </div> ); }And the files it imports:
lib/sprout/icons/check-icon.tsxlib/sprout/icons/chevron-right-icon.tsxlib/sprout/icons/close-icon.tsxlib/sprout/icons/create-icon.tsxlib/sprout/icons/document-text-icon.tsxlib/sprout/icons/folder-open-icon.tsxlib/sprout/icons/folder-icon.tsxlib/sprout/icons/inbox-icon.tsxlib/sprout/icons/minus-square-icon.tsxlib/sprout/icons/warning-icon.tsxlib/sprout/code-tokens.tslib/sprout/languages.tslib/sprout/state-panel-core.tsxlib/sprout/syntax.ts
Add the styles to your global CSS, after
@import "tailwindcss".@layer components { .sprout-file-explorer { --sprout-file-explorer-row: 26px; --sprout-file-explorer-indent: 12px; --sprout-file-explorer-mono: ui-monospace, SFMono-Regular, Menlo, monospace; grid-template-columns: var(--sprout-file-explorer-sidebar, 16rem) minmax(0, 1fr); box-sizing: border-box; border: 1px solid var(--sprout-line); border-radius: var(--sprout-radius-panel); background: var(--sprout-surface); min-width: 0; height: 100%; min-height: 0; color: var(--sprout-text); font-family: var(--sprout-font); font-size: var(--sprout-text-sm); display: grid; overflow: hidden; } .sprout-file-explorer ::selection { background: color-mix(in oklab, var(--sprout-info) 22%, transparent); } .sprout-file-explorer-sidebar { border-inline-end: 1px solid var(--sprout-line); background: var(--sprout-canvas); flex-direction: column; min-width: 0; min-height: 0; display: flex; } .sprout-file-explorer-sidebar-header { flex-shrink: 0; justify-content: space-between; align-items: center; gap: 8px; min-height: 40px; padding: 0 6px 0 12px; display: flex; } .sprout-file-explorer-title { white-space: nowrap; text-overflow: ellipsis; min-width: 0; font-weight: 600; overflow: hidden; } .sprout-file-explorer-icon-button { border-radius: var(--sprout-radius-control); width: 28px; height: 28px; color: var(--sprout-muted); cursor: pointer; background: none; border: 0; place-items: center; padding: 0; display: inline-grid; } @media (hover: hover) { .sprout-file-explorer-icon-button:hover:not(:disabled) { background: var(--sprout-hover); color: var(--sprout-text); } .sprout-file-explorer-row:hover { background: var(--sprout-hover); } .sprout-file-explorer-tree:hover .sprout-file-explorer-guide { background-image: linear-gradient(var(--sprout-input), var(--sprout-input)); } .sprout-file-explorer-result:hover, .sprout-file-explorer-hit:hover { background: var(--sprout-hover); } .sprout-file-explorer-tab:hover .sprout-file-explorer-tab-close { opacity: 1; } .sprout-file-explorer-tab-close:hover { background: var(--sprout-hover); color: var(--sprout-text); } } .sprout-file-explorer-icon-button:disabled { opacity: .4; cursor: default; } .sprout-file-explorer-search { flex-shrink: 0; padding: 0 8px 8px; } .sprout-file-explorer-tree, .sprout-file-explorer-results { scrollbar-width: thin; scrollbar-color: var(--sprout-input) transparent; flex: 1; min-height: 0; padding: 0 4px 8px; overflow: auto; } .sprout-file-explorer-tree:focus-visible { outline: none; } .sprout-file-explorer-row { box-sizing: border-box; height: var(--sprout-file-explorer-row); border-radius: var(--sprout-radius-control); color: var(--sprout-text); white-space: nowrap; cursor: pointer; user-select: none; align-items: center; gap: 6px; padding-inline: 4px 8px; display: flex; } .sprout-file-explorer-row[aria-selected="true"] { background: var(--sprout-active); font-weight: 500; } .sprout-file-explorer-row:focus-visible { outline: var(--sprout-focus-width) solid var(--sprout-focus-ring); outline-offset: -2px; } .sprout-file-explorer-guide { width: var(--sprout-file-explorer-indent); background: linear-gradient(var(--sprout-line), var(--sprout-line)) 5px 0 / 1px 100% no-repeat; flex: none; align-self: stretch; margin-inline-end: -6px; } .sprout-file-explorer-chevron { width: 12px; color: var(--sprout-muted); flex: none; place-items: center; display: inline-grid; } .sprout-file-explorer-row[aria-expanded="true"] .sprout-file-explorer-chevron { transform: rotate(90deg); } .sprout-file-explorer-folder { color: var(--sprout-file-explorer-folder, var(--sprout-muted)); flex-shrink: 0; } .sprout-file-explorer-row[aria-expanded="true"] .sprout-file-explorer-folder { color: var(--sprout-file-explorer-folder-open, var(--sprout-text)); } .sprout-file-explorer-name { text-overflow: ellipsis; min-width: 0; overflow: hidden; } .sprout-file-explorer-glyph { width: 16px; height: 16px; color: var(--sprout-muted); --tone: var(--sprout-muted); flex-shrink: 0; } .sprout-file-explorer-glyph[data-tone="info"] { --tone: var(--sprout-syntax-keyword, var(--sprout-info)); } .sprout-file-explorer-glyph[data-tone="success"] { --tone: var(--sprout-syntax-string, var(--sprout-success)); } .sprout-file-explorer-glyph[data-tone="warning"] { --tone: var(--sprout-syntax-constant, var(--sprout-warning)); } .sprout-file-explorer-glyph[data-tone="danger"] { --tone: var(--sprout-danger); } .sprout-file-explorer-glyph[data-tone="accent"] { --tone: var(--sprout-syntax-function, color-mix(in oklab, var(--sprout-info) 55%, var(--sprout-danger))); } .sprout-file-explorer-glyph-tile { fill: color-mix(in oklab, var(--tone) 14%, transparent); stroke: color-mix(in oklab, var(--tone) 30%, transparent); stroke-width: 1px; } .sprout-file-explorer-glyph-label { fill: var(--tone); font-family: var(--sprout-font); letter-spacing: -.02em; } .sprout-file-explorer-summary { color: var(--sprout-muted); font-size: var(--sprout-text-xs); margin: 0; padding: 6px 8px; line-height: 1rem; } .sprout-file-explorer-group { color: var(--sprout-muted); font-size: var(--sprout-text-xs); margin: 10px 0 2px; padding: 0 8px; font-weight: 500; line-height: 1rem; } .sprout-file-explorer-list { margin: 0; padding: 0; list-style: none; } .sprout-file-explorer-result, .sprout-file-explorer-hit { box-sizing: border-box; width: 100%; min-height: var(--sprout-file-explorer-row); border-radius: var(--sprout-radius-control); color: var(--sprout-text); font: inherit; text-align: start; white-space: nowrap; cursor: pointer; background: none; border: 0; align-items: center; gap: 6px; padding: 0 8px; display: flex; } .sprout-file-explorer-result[aria-current="true"], .sprout-file-explorer-hit[aria-current="true"] { background: var(--sprout-active); } .sprout-file-explorer-result:focus-visible, .sprout-file-explorer-hit:focus-visible { outline: var(--sprout-focus-width) solid var(--sprout-focus-ring); outline-offset: -2px; } .sprout-file-explorer-result .sprout-file-explorer-name { flex-shrink: 0; max-width: 70%; } .sprout-file-explorer-dir { min-width: 0; color: var(--sprout-muted); font-size: var(--sprout-text-xs); text-overflow: ellipsis; flex: 1; overflow: hidden; } .sprout-file-explorer-count { background: var(--sprout-secondary); min-width: 18px; color: var(--sprout-muted); font-size: var(--sprout-text-xs); font-variant-numeric: tabular-nums; text-align: center; border-radius: 999px; padding: 0 5px; line-height: 16px; } .sprout-file-explorer-hit { font-family: var(--sprout-file-explorer-mono); font-size: var(--sprout-text-xs); padding-inline-start: 30px; } .sprout-file-explorer-hit-line { min-width: 2ch; color: var(--sprout-muted); font-variant-numeric: tabular-nums; text-align: end; flex-shrink: 0; } .sprout-file-explorer-hit-text { text-overflow: ellipsis; min-width: 0; overflow: hidden; } .sprout-file-explorer-mark { background: color-mix(in oklab, var(--sprout-warning) 26%, transparent); box-shadow: 0 0 0 1px color-mix(in oklab, var(--sprout-warning) 40%, transparent); color: inherit; border-radius: 2px; } .sprout-file-explorer-editor { flex-direction: column; min-width: 0; min-height: 0; display: flex; } .sprout-file-explorer-tabs { border-bottom: 1px solid var(--sprout-line); background: var(--sprout-canvas); scrollbar-width: none; flex-shrink: 0; display: flex; overflow-x: auto; } .sprout-file-explorer-tab { border-inline-end: 1px solid var(--sprout-line); color: var(--sprout-muted); flex-shrink: 0; align-items: center; display: flex; position: relative; } .sprout-file-explorer-tab[data-active] { background: var(--sprout-surface); color: var(--sprout-text); } .sprout-file-explorer-tab[data-active]:after { background: var(--sprout-surface); content: ""; height: 1px; position: absolute; inset: auto 0 -1px; } .sprout-file-explorer-tab[data-active]:before { background: var(--sprout-primary); content: ""; height: 2px; position: absolute; inset: 0 0 auto; } .sprout-file-explorer-tab-open { height: 36px; color: inherit; font: inherit; white-space: nowrap; cursor: pointer; background: none; border: 0; align-items: center; gap: 6px; padding: 0 4px 0 12px; display: flex; } .sprout-file-explorer-tab-close { width: 20px; height: 20px; color: var(--sprout-muted); cursor: pointer; opacity: 0; background: none; border: 0; border-radius: 4px; place-items: center; margin-inline-end: 6px; padding: 0; display: inline-grid; } .sprout-file-explorer-tab:is([data-active], :focus-within) .sprout-file-explorer-tab-close { opacity: 1; } .sprout-file-explorer-tab-open:focus-visible, .sprout-file-explorer-tab-close:focus-visible, .sprout-file-explorer-icon-button:focus-visible { outline: var(--sprout-focus-width) solid var(--sprout-focus-ring); outline-offset: -2px; } .sprout-file-explorer-crumbs { color: var(--sprout-muted); font-size: var(--sprout-text-xs); flex-shrink: 0; padding: 6px 16px; line-height: 1rem; } .sprout-file-explorer-crumbs ol { flex-wrap: wrap; align-items: center; gap: 2px 0; margin: 0; padding: 0; list-style: none; display: flex; } .sprout-file-explorer-crumbs li { align-items: center; display: inline-flex; } .sprout-file-explorer-crumbs li[aria-current="page"] { color: var(--sprout-text); } .sprout-file-explorer-crumb-separator { opacity: .6; margin: 0 4px; } .sprout-file-explorer-body { flex-direction: column; flex: 1; min-height: 0; display: flex; } .sprout-file-explorer-code { scrollbar-width: thin; scrollbar-color: var(--sprout-input) transparent; flex: 1; min-height: 0; padding-bottom: 16px; overflow: auto; } .sprout-file-explorer-code:focus-visible { outline: var(--sprout-focus-width) solid var(--sprout-focus-ring); outline-offset: -2px; } .sprout-file-explorer-pre { width: max-content; min-width: 100%; font-family: var(--sprout-file-explorer-mono); tab-size: 2; margin: 0; font-size: 13px; line-height: 1.25rem; } .sprout-file-explorer-pre code { font: inherit; display: block; } .sprout-file-explorer-line { display: flex; } .sprout-file-explorer-line[data-current] { background: color-mix(in oklab, var(--sprout-warning) 10%, transparent); box-shadow: inset 2px 0 var(--sprout-warning); } .sprout-file-explorer-line-number { box-sizing: border-box; background: var(--sprout-surface); width: 4.5ch; color: color-mix(in oklab, var(--sprout-muted) 70%, transparent); text-align: end; user-select: none; flex-shrink: 0; margin-inline-end: 16px; padding-inline-end: 8px; position: sticky; left: 0; } .sprout-file-explorer-line[data-current] .sprout-file-explorer-line-number { background: color-mix(in oklab, var(--sprout-warning) 10%, var(--sprout-surface)); color: var(--sprout-text); } .sprout-file-explorer-line-text { white-space: pre; padding-inline-end: 24px; } .sprout-file-explorer-status { border-top: 1px solid var(--sprout-line); min-height: 28px; color: var(--sprout-muted); font-size: var(--sprout-text-xs); font-variant-numeric: tabular-nums; flex-wrap: wrap; flex-shrink: 0; align-items: center; gap: 4px 16px; padding: 0 16px; display: flex; } .sprout-file-explorer-status-end { margin-inline-start: auto; } .sprout-file-explorer-editor > .sprout-state-panel, .sprout-file-explorer-body > .sprout-state-panel { margin: auto; } @media (width <= 40rem) { .sprout-file-explorer { grid-template-rows: minmax(0, 2fr) minmax(0, 3fr); grid-template-columns: minmax(0, 1fr); } .sprout-file-explorer-sidebar { border-inline-end: 0; border-bottom: 1px solid var(--sprout-line); } } @media (pointer: coarse) { .sprout-file-explorer { --sprout-file-explorer-row: 40px; } .sprout-file-explorer-tab-close { opacity: 1; width: 32px; height: 32px; } .sprout-code-block-copy { min-height: 44px; } } @media (prefers-reduced-motion: no-preference) { .sprout-file-explorer-chevron { transition: transform var(--sprout-motion-fast) var(--sprout-motion-ease); } .sprout-file-explorer-line[data-current] { animation: sprout-file-explorer-flash .9s var(--sprout-motion-ease); } .sprout-code-block-error { animation: sprout-code-block-in var(--sprout-motion-normal) var(--sprout-motion-ease); } } @keyframes sprout-file-explorer-flash { from { background: color-mix(in oklab, var(--sprout-warning) 32%, transparent); } } .sprout-code-block { box-sizing: border-box; min-width: 0; max-width: 100%; color: var(--sprout-text); font-family: var(--sprout-font); font-size: var(--sprout-text-sm); flex-direction: column; display: flex; overflow: hidden; } .sprout-code-block[data-variant="inline"] { border: 1px solid var(--sprout-line); border-radius: var(--sprout-radius-panel); background: color-mix(in oklab, var(--sprout-hover) 40%, var(--sprout-surface)); } .sprout-code-block[data-variant="file"] { height: 100%; min-height: 0; } .sprout-code-block[data-variant="preview"] { max-height: 100%; font-size: var(--sprout-text-xs); mask-image: linear-gradient(oklch(0% 0 0) 75%, #0000); } .sprout-code-block[data-variant="preview"] .sprout-code-block-viewport { font-size: var(--sprout-text-xs); padding: 12px; line-height: 1.125rem; overflow: hidden; } .sprout-code-block-header { box-sizing: border-box; border-bottom: 1px solid var(--sprout-line); flex-shrink: 0; justify-content: space-between; align-items: center; gap: 12px; min-height: 40px; padding: 4px 6px 4px 12px; display: flex; } .sprout-code-block-label { min-width: 0; color: var(--sprout-muted); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: var(--sprout-text-xs); white-space: nowrap; text-overflow: ellipsis; line-height: 1rem; overflow: hidden; } .sprout-code-block-icon { display: inline-flex; } .sprout-code-block-copy:disabled { cursor: progress; } .sprout-code-block-viewport { box-sizing: border-box; min-width: 0; font-size: var(--sprout-text-sm); padding: 16px; line-height: 1.25rem; display: flex; overflow: auto; } .sprout-code-block[data-variant="inline"] .sprout-code-block-viewport { max-height: 24rem; } .sprout-code-block[data-variant="file"] .sprout-code-block-viewport { flex: 1; min-height: 0; } .sprout-code-block-viewport:focus-visible { outline: var(--sprout-focus-width) solid var(--sprout-focus-ring); outline-offset: -1px; } .sprout-code-block-gutter, .sprout-code-block-pre { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: inherit; line-height: inherit; tab-size: 2; margin: 0; } .sprout-code-block-gutter { border-right: 1px solid var(--sprout-line); color: var(--sprout-muted); text-align: right; user-select: none; flex-shrink: 0; margin-right: 16px; padding-right: 12px; } .sprout-code-block-pre { background: none; flex: 1; min-width: 0; } .sprout-code-block-pre code { font: inherit; } .sprout-code-block[data-wrap] .sprout-code-block-pre { white-space: pre-wrap; overflow-wrap: anywhere; } .sprout-syntax-keyword { color: var(--sprout-syntax-keyword, var(--sprout-info)); } .sprout-syntax-string, .sprout-syntax-string-expression { color: var(--sprout-syntax-string, var(--sprout-success)); } .sprout-syntax-constant { color: var(--sprout-syntax-constant, var(--sprout-warning)); } .sprout-syntax-function { color: var(--sprout-syntax-function, color-mix(in oklab, var(--sprout-info) 55%, var(--sprout-danger))); } .sprout-syntax-parameter { color: var(--sprout-syntax-parameter, color-mix(in oklab, var(--sprout-danger) 65%, var(--sprout-warning))); } .sprout-syntax-comment { color: var(--sprout-syntax-comment, var(--sprout-muted)); } .sprout-syntax-punctuation { color: var(--sprout-syntax-punctuation, color-mix(in oklab, var(--sprout-text) 70%, var(--sprout-muted))); } .sprout-syntax-link { color: var(--sprout-syntax-link, var(--sprout-info)); } .sprout-syntax-heading { color: var(--sprout-syntax-heading, var(--sprout-info)); } .sprout-syntax-inserted { color: var(--sprout-syntax-inserted, var(--sprout-success)); } .sprout-syntax-deleted { color: var(--sprout-syntax-deleted, var(--sprout-danger)); } .sprout-syntax-italic { font-style: italic; } .sprout-syntax-bold { font-weight: 600; } .sprout-syntax-underline { text-underline-offset: 2px; text-decoration: underline; } .sprout-code-block-error { border-top: 1px solid var(--sprout-line); color: var(--sprout-danger); font-size: var(--sprout-text-sm); flex-shrink: 0; margin: 0; padding: 8px 12px; line-height: 1.25rem; } .sprout-code-block-sr-only { clip-path: inset(50%); white-space: nowrap; border: 0; width: 1px; height: 1px; margin: -1px; padding: 0; position: absolute; overflow: hidden; } @keyframes sprout-code-block-in { from { opacity: 0; } } .sprout-state-panel { font-family: var(--sprout-font); color: var(--sprout-text); overflow-wrap: anywhere; animation: sprout-state-panel-in var(--sprout-motion-normal, .18s) var(--sprout-motion-ease, ease) both; } .sprout-state-panel[data-layout="centered"] { text-align: center; flex-direction: column; align-items: center; gap: 1.5rem; width: 100%; max-width: 28rem; margin-inline: auto; padding: 3rem 1.5rem; display: flex; } .sprout-state-panel-media { border: 1px solid var(--sprout-line); background: var(--sprout-surface); width: 2.5rem; height: 2.5rem; color: var(--sprout-text); box-shadow: var(--sprout-shadow-control); border-radius: .5rem; flex-shrink: 0; justify-content: center; align-items: center; display: flex; } .sprout-state-panel-media > svg { flex-shrink: 0; } .sprout-state-panel-danger { color: var(--sprout-danger); } .sprout-state-panel-text { gap: .5rem; width: 100%; display: grid; } .sprout-state-panel-title { font-size: var(--sprout-text-base); letter-spacing: normal; color: var(--sprout-text); margin: 0; font-weight: 500; line-height: 1.5; } .sprout-state-panel-description { font-size: var(--sprout-text-sm); color: var(--sprout-muted); margin: 0; line-height: 1.5; } .sprout-state-panel-actions { width: 100%; font-size: var(--sprout-text-sm); color: var(--sprout-muted); flex-direction: column; align-items: center; gap: .75rem; display: flex; } .sprout-state-panel-actions:empty { display: none; } .sprout-state-panel[data-layout="sample"] { width: 100%; container-type: inline-size; } .sprout-state-panel-card { border: 1px solid var(--sprout-line); border-radius: var(--sprout-radius-panel); background: var(--sprout-surface); box-shadow: var(--sprout-shadow-control); display: grid; overflow: hidden; } .sprout-state-panel-copy { flex-direction: column; justify-content: center; align-items: flex-start; gap: 1.5rem; min-width: 0; padding: 1.5rem; display: flex; } [data-layout="sample"] .sprout-state-panel-text { max-width: 28rem; } [data-layout="sample"] .sprout-state-panel-title { font-size: 1.25rem; line-height: 1.4; } [data-layout="sample"] .sprout-state-panel-description { font-size: var(--sprout-text-base); } [data-layout="sample"] .sprout-state-panel-actions { align-items: stretch; gap: .5rem; } .sprout-state-panel-sample { border-top: 1px solid var(--sprout-line); background: var(--sprout-canvas); min-height: 15rem; display: none; position: relative; overflow: hidden; } .sprout-state-panel-sample-window { border-top: 1px solid var(--sprout-line); border-left: 1px solid var(--sprout-line); border-top-left-radius: var(--sprout-radius-panel); background: var(--sprout-surface); box-shadow: var(--sprout-shadow-panel); position: absolute; inset: 1.5rem 0 0 1.5rem; overflow: hidden; mask-image: linear-gradient(oklch(0% 0 0) 60%, #0000 100%); } @container (width >= 24rem) { [data-layout="sample"] .sprout-state-panel-actions { flex-flow: wrap; align-items: center; width: auto; } } @container (width >= 28rem) { .sprout-state-panel-sample { display: block; } } @container (width >= 32rem) { .sprout-state-panel-copy { padding: 2.5rem; } } @container (width >= 48rem) { .sprout-state-panel-card { grid-template-columns: minmax(0, 5fr) minmax(0, 6fr); } .sprout-state-panel-copy { padding-block: 3.5rem; } .sprout-state-panel-sample { border-top: 0; border-left: 1px solid var(--sprout-line); min-height: 22rem; } } @media (prefers-reduced-motion: reduce) { .sprout-state-panel { animation-name: sprout-state-panel-fade; } } @keyframes sprout-state-panel-fade { from { opacity: 0; } } @keyframes sprout-state-panel-in { from { opacity: 0; transform: translateY(4px); } } }Import it from
@/components/ui/file-explorer. Update the paths if your@/alias points elsewhere.
pnpm add @tulipfarm/sprout@import "tailwindcss";
@import "@tulipfarm/sprout/styles.css";The package keeps the source and ships fixes with each release. Import from @tulipfarm/sprout/file-explorer.
import { defineTool } from "@tulipfarm/agents";
import { z } from "zod";
/** Reads the latest sensor values for one bed. */
export const readBed = defineTool({
name: "read_bed",
description: "Latest soil moisture, temperature, and light for a bed.",
input: z.object({ bed: z.string().regex(/^[A-H]\d{1,2}$/) }),
async run({ bed }, { sensors }) {
const reading = await sensors.latest(bed);
if (!reading) return { status: "missing", bed } as const;
return {
status: "ok",
bed,
moisture: Math.round(reading.moisture * 100),
celsius: reading.celsius,
lux: reading.lux,
} as const;
},
});
export const tools = [readBed];
Usage
import { FileExplorer } from "@tulipfarm/sprout/file-explorer";
<div className="h-[36rem]">
<FileExplorer
label="Soul"
files={[
{ path: "SOUL.md", content: soulMd },
{ path: "agents/scout/tools.ts", content: toolsTs },
{ path: "assets/map.png", unavailable: "Images cannot be displayed here." },
]}
defaultPath="SOUL.md"
/>
</div>Pass a flat list of files. Each path uses / between folders. A file has content, or unavailable with the reason it has no text. The explorer fills its parent, so give the parent a height.
Search
Search ignores case. It lists matching file names, then every matching line grouped by file, with the line number. Pick a line to open the file there. The line is marked and every match in the file is highlighted. Search stops after 500 lines.
Accessibility
The tree is a tree with one tab stop. Up and Down move, Right opens a folder, Left closes it or goes to the parent, Home and End jump, and Enter opens a file. The search summary is a status message. Each open tab has a Close button named after the file; middle-click also closes a tab. The file text is a focusable region named after its path.