Skip to main content

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.

@heroicons/react
reveal-panel.tsxtsx
"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

Speed0.45
Blur reveal (px)8

Installation

npx shadcn@latest add @kinetic/code-block

Usage

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.

PropTypeDefaultDescription
code / language / filename / syntaxHighlight / wrapLinesstring / string / string / boolean / boolean"" / undefined / undefined / true / falseSource string, language label, file path shown in the header, syntax highlighting toggle, and soft line-wrap state.
showLineNumbers / highlightLinesboolean / readonly number[]true / undefinedLine-number gutter toggle and 1-based highlighted lines.
animated / speed / blurPx / copyResetMsboolean / number / number / numbertrue / 0.45 / 8 / 1800Controls 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.

PropTypeDefaultDescription
code / resetMs / onCopy / onCopyError (CodeBlockCopyButton)string / number / (code: string) => void / (error: unknown) => voidinherited / 1800Optional text override, reset interval, and clipboard callbacks.
filename / onDownload (CodeBlockDownloadButton)string / (filename: string) => voidinheritedCustom download filename override and download callback.

CodeBlockContent

Syntax-highlighted, line-staggered code viewport with horizontal scroll edge fade and optional overflow disclosure.

PropTypeDefaultDescription
maxCollapsedLines / expanded / animated / speed / blurPxnumber / boolean / boolean / number / numberinheritedContent-level overrides for line cap, disclosure state, and reveal motion.

Notes

Pass code and language without children for a ready-to-use header and copy button, or compose CodeBlockHeader and CodeBlockContent manually for custom toolbars.