aiellie

Search

Find a component, template or page

GitHub
Components

Code Workspace

A read-only editor for a set of files: a resizable file tree, tabs, a quick file picker, and search and replace across names and contents.

shop6
import type { CartItem } from "./cart"
​
const TAX_RATE = 0.2
​
// Prices stay in cents until they're shown, so they never drift.
export function totalPrice(items: CartItem[]) {
const subtotal = items.reduce(
(sum, item) => sum + item.price * item.quantity,
0
)
return Math.round(subtotal * (1 + TAX_RATE))
}
​
export function formatPrice(cents: number) {
return new Intl.NumberFormat("en-GB", {
style: "currency",
currency: "GBP",
}).format(cents / 100)
}
Open full screen
fig. 01 · open it full screen from the corner

Installation

npx shadcn@latest add "@aiellie/code-workspace"

shadcn needs the @aiellie registry in components.json first. npx aiellie add code-workspace adds it on its first run, then installs the same way.

Code Workspace lays out a set of files the way an editor does: a tree down the side, the open files as tabs, and the chosen file highlighted with numbered lines. It is for reading code an agent wrote or is about to change, without the weight of a real editor.

Search looks through file names, file contents or both, with match case, whole word and regular expressions. Replace previews the result in every file it touches and undoes in one press. It never writes anything back, so a workspace is always safe to hand to a reader.

Getting started

  1. Give it the files

    Each file is a path and its content. Folders come from the paths, so there is nothing else to describe. The language is read off the extension unless you set language.

    components/project-files.tsx
    "use client"
    
    import {
      CodeWorkspace,
      CodeWorkspacePanel,
      CodeWorkspaceSidebar,
      type CodeWorkspaceFile,
    } from "@/components/aiellie/code-workspace"
    
    const FILES: CodeWorkspaceFile[] = [
      { path: "app/page.tsx", content: "export default function Page() {}" },
      { path: "lib/price.ts", content: "export const TAX_RATE = 0.2" },
    ]
    
    export function ProjectFiles() {
      return (
        <CodeWorkspace files={FILES} defaultFile="lib/price.ts">
          <CodeWorkspaceSidebar title="shop" />
          <CodeWorkspacePanel />
        </CodeWorkspace>
      )
    }
  2. Size it

    The workspace is 384px tall unless you give it a height. The tree and the code scroll inside it, so it needs a definite one, like h-96 or h-full in a sized parent.

Anatomy

<div data-slot="code-workspace">
  <div data-slot="code-workspace-sidebar">
    {/* title and count, or the search */}
    <div data-slot="file-tree" />
  </div>
  {/* resize handle */}
  <div data-slot="code-workspace-panel">
    {/* tabs, the open-a-file picker, actions */}
    <div data-slot="code-workspace-code" />
  </div>
</div>

The sidebar draws its own resize handle after itself, so a workspace is always two children: the sidebar, then the panel. Drag the handle to give the tree more room.

The tree is FileTree, so it walks with the arrow keys and opens the folders around whichever file you choose, from a tab, a search or the picker. A tab closes with its cross or a middle click, and the last one stays open.

The line a text result points at is marked and centred in the pane. Line numbers stay put while a long line scrolls sideways, and they are left out when you copy.

Examples

An action in the tab strip

actions sits at the end of the tab strip. A component inside it can read the workspace with useCodeWorkspace, here to copy the file that's open.

Files3
import { greet } from "./greet"
​
console.log(greet("world"))

Search text from the start

Search opens on file names. Pass defaultScopes to look through contents as well, or only. With both on, files and lines are listed under their own labels.

components/project-files.tsx
<CodeWorkspaceSidebar defaultScopes={["names", "contents"]} />

API reference

CodeWorkspace

PropTypeDefaultDescription
filesCodeWorkspaceFile[]required
defaultFilestringthe first fileThe file open to begin with.
onFileChange(path: string) => voidCalled when another file is chosen.

All other div props are forwarded.

CodeWorkspaceFile

FieldTypeDefaultDescription
pathstringrequiredSplit on / into the tree.
contentstringrequired
languagestringthe extensionA Shiki language id, like tsx or json.

CodeWorkspaceSidebar

PropTypeDefaultDescription
titleReactNode"Files"Shown above the tree.
defaultScopes("names" | "contents")[]["names"]What search looks through when it first opens.
defaultSizestring"16rem"A length or a percentage.
minSizestring"11rem"
maxSizestring"50%"

All other div props are forwarded.

CodeWorkspacePanel

PropTypeDefaultDescription
actionsReactNodeSet at the end of the tab strip.

All other div props are forwarded.

useCodeWorkspace

FieldTypeDescription
activeFilestring | nullThe file being shown.
openFilesstring[]The files open as tabs, in order.
selectFile(path: string, line?: number) => voidOpens a file, and marks a line if given one.
closeFile(path: string) => void
contentOf(path: string) => stringWhat a file shows, with any replacement applied.
replacedRecord<string, string>The files showing a replacement, by path.
revertReplacements() => void