Code Frame.
Editor chrome: a file bar with an unsaved tag and a toolbar, a line-numbered code area and an optional status bar. Static for docs and previews, or a frame to mount a real editor in.
-- Strip mine for diamonds at y=-59: three 32-block branches,-- a torch every 8 blocks, home to unload when full.local BRANCHES, LENGTH, SPACING = 3, 32, 3local TORCH_EVERY = 8 td.setHome()td.mine.stairs{ toY = -59, returnHome = false } td.run("Diamond strip mine", {}, function(check) for b = 1, BRANCHES do for step = 1, LENGTH do check() assert(td.digMove("forward")) td.clear("up") if step % TORCH_EVERY == 0 then td.placeTorch("left") end end print(("Branch %d done"):format(b)) endend)import { FlaskConicalIcon, PlayIcon, SaveIcon } from "lucide-react";
import { Button } from "@/components/ui/button";
import {
CodeFrame,
CodeFrameBody,
CodeFrameFooter,
CodeFrameHeader,
CodeFrameTag,
CodeFrameTitle,
CodeFrameToolbar,
} from "@/components/ui/code-frame";
const source = `-- Strip mine for diamonds at y=-59: three 32-block branches,
-- a torch every 8 blocks, home to unload when full.
local BRANCHES, LENGTH, SPACING = 3, 32, 3
local TORCH_EVERY = 8
td.setHome()
td.mine.stairs{ toY = -59, returnHome = false }
td.run("Diamond strip mine", {}, function(check)
for b = 1, BRANCHES do
for step = 1, LENGTH do
check()
assert(td.digMove("forward"))
td.clear("up")
if step % TORCH_EVERY == 0 then
td.placeTorch("left")
end
end
print(("Branch %d done"):format(b))
end
end)`;
export default function CodeFrameDemo() {
return (
<CodeFrame className="h-[460px] w-full max-w-3xl">
<CodeFrameHeader>
<CodeFrameTitle>strip_mine.lua</CodeFrameTitle>
<CodeFrameTag>Unsaved</CodeFrameTag>
<CodeFrameToolbar aria-label="File actions">
<Button size="sm" variant="ghost">
<SaveIcon /> Save
</Button>
<Button size="sm" variant="secondary">
<FlaskConicalIcon /> Dry run
</Button>
<Button size="sm">
<PlayIcon /> Run on Testy
</Button>
</CodeFrameToolbar>
</CodeFrameHeader>
<CodeFrameBody code={source} highlightLines={[13, 14]} aria-label="strip_mine.lua source" />
<CodeFrameFooter>
<span>Lua</span>
<span>23 lines</span>
<span className="ml-auto text-success">Testy idle at home</span>
</CodeFrameFooter>
</CodeFrame>
);
}Installation#
With the CLI
$npx lanterncn add code-frameThis also installs the Lantern theme and any Lantern UI components it depends on. The shadcn CLI works directly too: npx shadcn@latest add httptim/lantern-ui/code-frame. See Installation if your project is not set up yet.
Manually
Install the dependencies
npm install class-variance-authorityCopy the source into your project
components/ui/code-frame.tsximport * as React from "react"; import { cva, type VariantProps } from "class-variance-authority"; import { cn } from "@/lib/utils"; /** * Editor chrome: a file bar with a toolbar, a line-numbered code area and an optional status bar. * The body renders static code for docs and previews; mount a real editor as its children instead. */ function CodeFrame({ className, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="code-frame" className={cn("flex min-h-0 min-w-0 flex-col overflow-hidden rounded-lg border bg-[#0e1311]", className)} {...props} /> ); } /** The file bar. Wraps the toolbar onto its own line when space runs out. */ function CodeFrameHeader({ className, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="code-frame-header" className={cn( "flex min-h-12 shrink-0 flex-wrap items-center gap-x-2.5 gap-y-2 border-b bg-card px-3 py-2 sm:px-4", className, )} {...props} /> ); } function CodeFrameTitle({ className, ...props }: React.ComponentProps<"span">) { return ( <span data-slot="code-frame-title" className={cn("min-w-0 truncate font-mono text-[13px] text-foreground", className)} {...props} /> ); } const codeFrameTagVariants = cva("shrink-0 font-mono text-[10px] leading-none tracking-[0.14em] uppercase", { variants: { tone: { warning: "text-warning", primary: "text-primary", success: "text-success", muted: "text-muted-foreground", }, }, defaultVariants: { tone: "warning" }, }); /** A mono status word next to the file name, such as Unsaved or New. */ function CodeFrameTag({ className, tone, ...props }: React.ComponentProps<"span"> & VariantProps<typeof codeFrameTagVariants>) { return <span data-slot="code-frame-tag" className={cn(codeFrameTagVariants({ tone }), className)} {...props} />; } /** Buttons on the right of the file bar. */ function CodeFrameToolbar({ className, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="code-frame-toolbar" role="toolbar" className={cn("ml-auto flex flex-wrap items-center gap-2", className)} {...props} /> ); } const commentPattern = /^\s*(--|\/\/|#)/; /** * The code area. Pass `code` for static, line-numbered text (comment lines are dimmed), or * children to mount your own editor so it fills the frame. */ function CodeFrameBody({ className, code, highlightLines, startLine = 1, children, ...props }: React.ComponentProps<"div"> & { code?: string; /** 1-based line numbers to mark with an orange bar. */ highlightLines?: number[]; startLine?: number; }) { const lines = code?.replace(/\n$/, "").split("\n"); const marked = new Set(highlightLines ?? []); return ( <div data-slot="code-frame-body" tabIndex={lines ? 0 : undefined} className={cn( "relative min-h-0 flex-1 overflow-auto font-mono text-[13px] leading-[1.75] outline-none focus-visible:ring-2 focus-visible:ring-ring/40 focus-visible:ring-inset", lines && "py-3.5", className, )} {...props} > {lines ? ( <pre className="m-0 min-w-fit font-[inherit]"> <code className="grid"> {lines.map((line, i) => { const n = i + startLine; return ( <span key={i} data-highlighted={marked.has(n) || undefined} className="flex border-l-2 border-transparent pr-4 data-[highlighted]:border-primary data-[highlighted]:bg-primary/8" > <span aria-hidden="true" className="w-12 shrink-0 pr-4 text-right text-input select-none sm:w-14" > {n} </span> <span className={cn("whitespace-pre", commentPattern.test(line) ? "text-muted-foreground" : "text-foreground")}> {line || " "} </span> </span> ); })} </code> </pre> ) : ( children )} </div> ); } /** Optional status bar under the code. */ function CodeFrameFooter({ className, ...props }: React.ComponentProps<"div">) { return ( <div data-slot="code-frame-footer" className={cn( "flex min-h-9 shrink-0 items-center gap-3 border-t bg-card px-3 font-mono text-[11px] text-muted-foreground sm:px-4", className, )} {...props} /> ); } export { CodeFrame, CodeFrameHeader, CodeFrameTitle, CodeFrameTag, CodeFrameToolbar, CodeFrameBody, CodeFrameFooter, codeFrameTagVariants, };Update the import paths to match your project
The source imports
cnfrom@/lib/utils.
Usage#
import {
CodeFrame,
CodeFrameHeader,
CodeFrameTitle,
CodeFrameTag,
CodeFrameToolbar,
CodeFrameBody,
CodeFrameFooter,
} from "@/components/ui/code-frame";<CodeFrame className="h-[460px]">
<CodeFrameHeader>
<CodeFrameTitle>strip_mine.lua</CodeFrameTitle>
<CodeFrameTag>Unsaved</CodeFrameTag>
<CodeFrameToolbar aria-label="File actions">
<Button size="sm">Run on Testy</Button>
</CodeFrameToolbar>
</CodeFrameHeader>
<CodeFrameBody code={source} />
</CodeFrame>CodeFrameBody with code renders static, line-numbered text and dims comment lines (--, // and #). It scrolls both ways inside the frame. To edit, leave out code and put your editor (CodeMirror, Monaco, a textarea) inside CodeFrameBody; it fills the remaining height.
CodeFrameTag takes tone warning (default), primary, success or muted. Use Code Block instead when you only need to show a snippet with a copy button.