Primitives
Code Block
Filename tab, copy button, line numbers, and line highlighting. Bring your own highlighter (pass pre-highlighted HTML) — none is bundled.
Motioncopy button flip
Install
Install Kineto and follow the theme setup once in your app.
bash
npm install kinetotsx
import { CodeBlock } from "kineto/code-block";Preview
save.tsxtsx
1import { Button } from "kineto";23export function Save() {4 return <Button variant="brand">Save changes</Button>;5}
tsx
"use client";
import * as React from "react";
import { cn } from "@/lib/cn";
import { CopyButton } from "@/components/ui/copy-button";
export interface CodeBlockProps extends React.HTMLAttributes<HTMLDivElement> {
/** The raw source — used for the copy button and the plain-text render. */
code: string;
/** Optional filename shown in a tab above the code. */
filename?: string;
/** Language label (and a hint for your highlighter). */
language?: string;
/** Pre-highlighted HTML (e.g. from Shiki). When set, it's rendered as-is. */
html?: string;
/** Show a line-number gutter (plain render only). */
showLineNumbers?: boolean;
/** 1-based line numbers to emphasize (plain render only). */
highlightLines?: number[];
}
/**
* A complete code block: optional filename tab, a copy button, line numbers, and line highlighting.
* Bring your own syntax highlighter — pass pre-highlighted `html` (Shiki/Prism) and it's rendered
* verbatim; otherwise the raw `code` is rendered line-by-line with the gutter and highlights. No
* highlighter is bundled, so it stays light.
*/
const CodeBlock = React.forwardRef<HTMLDivElement, CodeBlockProps>(
({ className, code, filename, language, html, showLineNumbers = false, highlightLines, ...props }, ref) => {
const highlight = new Set(highlightLines ?? []);
const lines = React.useMemo(() => code.replace(/\n$/, "").split("\n"), [code]);
return (
<div
ref={ref}
className={cn("overflow-hidden rounded-xl border border-border bg-card", className)}
{...props}
>
{/* Header always present — it hosts the copy button (filename/language are optional). */}
<div className="flex items-center justify-between border-b border-border bg-muted/40 pl-4 pr-2">
<div className="flex items-center gap-2 py-2 text-xs text-muted-foreground">
{filename ? <span className="font-mono font-medium text-foreground">{filename}</span> : null}
{language ? (
<span className="rounded bg-muted px-1.5 py-0.5 font-mono uppercase tracking-wide">
{language}
</span>
) : null}
</div>
<CopyButton value={code} />
</div>
<div className="overflow-x-auto text-sm">
{html ? (
<div className="[&_pre]:m-0 [&_pre]:bg-transparent [&_pre]:p-4" dangerouslySetInnerHTML={{ __html: html }} />
) : (
<pre className="py-4 font-mono leading-relaxed">
{lines.map((line, i) => {
const n = i + 1;
const isOn = highlight.has(n);
return (
<div
key={i}
className={cn(
"px-4",
isOn && "bg-brand/10 shadow-[inset_2px_0_0_hsl(var(--brand))]"
)}
>
{showLineNumbers ? (
<span className="mr-4 inline-block w-6 select-none text-right text-muted-foreground/60">
{n}
</span>
) : null}
<span className="text-foreground">{line || " "}</span>
</div>
);
})}
</pre>
)}
</div>
</div>
);
}
);
CodeBlock.displayName = "CodeBlock";
export { CodeBlock };src/components/ui/code-block.tsx
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| code | string | — | Raw source — used for copy and the plain render. |
| filename | string | — | Optional filename shown in a tab. |
| language | string | — | Language label / highlighter hint. |
| html | string | — | Pre-highlighted HTML (e.g. Shiki); rendered as-is when set. |
| showLineNumbers | boolean | false | Show a line-number gutter (plain render). |
| highlightLines | number[] | — | 1-based lines to emphasize (plain render). |