Code Block
A syntax-highlighted code viewer with file header metadata, staggered blur-fade line reveals, line wrapping, download action, and stroke-drawn copy button.
"use client";import { useAnimatedSize } from "@/registry/ui/lib/use-animated-size";import { SPRING } from "@/registry/ui/lib/spring";export function RevealPanel({ open, children }: { open: boolean; children: React.ReactNode }) { const { outerRef, innerRef } = useAnimatedSize({ open, axis: "height", spring: SPRING.collapse, speed: 0.45, }); return ( <div ref={outerRef} className="overflow-clip"> <div ref={innerRef}>{children}</div> </div> );}Customize
Installation
npx shadcn@latest add @kinetic/code-blockUsage
Compose CodeBlock, CodeBlockHeader, CodeBlockTitle, CodeBlockActions, CodeBlockWrapButton, CodeBlockDownloadButton, CodeBlockCopyButton, and CodeBlockContent, or pass code, filename, and language directly to CodeBlock for the default header and action toolbar.
import {
CodeBlock,
CodeBlockActions,
CodeBlockContent,
CodeBlockCopyButton,
CodeBlockDownloadButton,
CodeBlockHeader,
CodeBlockTitle,
CodeBlockWrapButton,
} from "@/components/kinetic/ui/code-block";
const SAMPLE_CODE = `import { runSpring, SPRING } from "@/components/kinetic/ui/lib/spring";
export function animateHeight(node: HTMLElement, target: number) {
return runSpring({
state: { value: node.offsetHeight, velocity: 0 },
target,
config: SPRING.collapse,
onUpdate: ({ value }) => {
node.style.height = \`\${value}px\`;
},
});
}`;
export function SpringSnippet() {
return (
<CodeBlock
code={SAMPLE_CODE}
filename="src/lib/spring-height.ts"
language="ts"
showLineNumbers
highlightLines={[3, 7]}
>
<CodeBlockHeader>
<CodeBlockTitle />
<CodeBlockActions>
<CodeBlockWrapButton />
<CodeBlockDownloadButton />
<CodeBlockCopyButton />
</CodeBlockActions>
</CodeBlockHeader>
<CodeBlockContent />
</CodeBlock>
);
}Syntax highlighting, line highlights, and wrapping
Set syntaxHighlight (true by default) to colorize keywords, strings, comments, numbers, and types with semantic tokens, highlightLines to accent specific 1-based lines, showLineNumbers to toggle the tabular-nums gutter, and wrapLines or CodeBlockWrapButton to toggle soft wrapping. For long snippets, maxCollapsedLines can still cap visible rows with a spring height reveal.
"use client";
import { useState } from "react";
import { CodeBlock } from "@/components/kinetic/ui/code-block";
interface CollapsibleSnippetProps {
readonly filename: string;
readonly source: string;
readonly focusedLines?: readonly number[];
}
export function CollapsibleSnippet({
filename,
source,
focusedLines = [2, 4],
}: CollapsibleSnippetProps) {
const [expanded, setExpanded] = useState(false);
return (
<CodeBlock
code={source}
language={filename}
highlightLines={focusedLines}
maxCollapsedLines={5}
expanded={expanded}
onExpandedChange={setExpanded}
/>
);
}Motion preferences
CodeBlock defaults to speed={0.45} and blurPx={8}. Initial lines and newly expanded overflow lines enter via staggerReveal, overflow height transitions run through useAnimatedSize, and CodeBlockCopyButton confirms copies with directionalSwap, popIn, and a drawStroke checkmark. Reduced motion reveals lines and copy state immediately without blur or motion delays.
API Reference
CodeBlock
Syntax-highlighted code viewer coordinating file header metadata, line stagger reveals, line wrapping, download actions, and shared motion preferences.
| Prop | Type | Default | Description |
|---|---|---|---|
code / language / filename / syntaxHighlight / wrapLines | string / string / string / boolean / boolean | "" / undefined / undefined / true / false | Source string, language label, file path shown in the header, syntax highlighting toggle, and soft line-wrap state. |
showLineNumbers / highlightLines | boolean / readonly number[] | true / undefined | Line-number gutter toggle and 1-based highlighted lines. |
animated / speed / blurPx / copyResetMs | boolean / number / number / number | true / 0.45 / 8 / 1800 | Controls motion activation, speed scale, initial reveal blur (px), and copy confirmation timeout. |
CodeBlockHeader / CodeBlockTitle / CodeBlockActions / CodeBlockWrapButton / CodeBlockDownloadButton / CodeBlockCopyButton
Header bar displaying filename metadata with line-wrap, file download, and directionalSwap + drawStroke clipboard copy actions.
| Prop | Type | Default | Description |
|---|---|---|---|
code / resetMs / onCopy / onCopyError (CodeBlockCopyButton) | string / number / (code: string) => void / (error: unknown) => void | inherited / 1800 | Optional text override, reset interval, and clipboard callbacks. |
filename / onDownload (CodeBlockDownloadButton) | string / (filename: string) => void | inherited | Custom download filename override and download callback. |
CodeBlockContent
Syntax-highlighted, line-staggered code viewport with horizontal scroll edge fade and optional overflow disclosure.
| Prop | Type | Default | Description |
|---|---|---|---|
maxCollapsedLines / expanded / animated / speed / blurPx | number / boolean / boolean / number / number | inherited | Content-level overrides for line cap, disclosure state, and reveal motion. |