Skip to Content
Liquid Glass UI
Components

CodeBlock

Default Liquid Glass code viewer with SF Mono typography, sticky line numbers, diff highlighting, Xcode-themed syntax highlighting, and one-click copy.

Fully compatible with the standard shadcn/ui JSX API (CodeBlock / CodeBlockHeader / CodeBlockTitle / CodeBlockActions / CodeBlockWrapButton / CodeBlockCopyButton / CodeBlockContent / CodeBlockLine / CodeBlockFooter):

  • Default Liquid Glass: Uses the medium Liquid Glass backdrop reacting to the global glass tint; pass glass={false} for a 100% opaque code surface, and automatically switches to Over-Glass inside glass Cards or Dialogs.
  • Responsive typography: 14px radius and 13px monospace on mobile ↔ 10px radius and 12px monospace on desktop, with size="sm" stepping down one size.
  • Sticky line numbers & diff rows: lineNumbers enables line numbering (startLine sets the offset) pinned to the left during horizontal scrolling; CodeBlockLine supports added, removed, and highlighted tints with a leading accent bar.
  • Xcode syntax highlighting: Pass language (e.g. "tsx", "bash", "swift") to CodeBlockContent for shiki highlighting styled after Xcode's default light/dark themes. Loaded lazily in the browser with pure-JS regex (no WASM) and zero layout shift.
  • Wrap & copy buttons: Place CodeBlockWrapButton and CodeBlockCopyButton inside CodeBlockActions (always visible in CodeBlockHeader, or floating top-right on hover/focus on desktop and persistent on touch devices).
apple-liquid-ui/preview/code-block-demo
MacBook · 16:10

Installation (Shadcn Registry)

npx shadcn@latest add @apple-liquid/code-block

Diff Lines, Terminal & Errors

apple-liquid-ui/preview/code-block-diff
MacBook · 16:10

Usage

import { FileCodeIcon } from 'lucide-react';
import {
  CodeBlock,
  CodeBlockActions,
  CodeBlockContent,
  CodeBlockCopyButton,
  CodeBlockHeader,
  CodeBlockTitle,
  CodeBlockWrapButton,
} from '@/components/ui/code-block';

const code = `npx shadcn@latest add @apple-liquid/button`;

export function Example() {
  return (
    <CodeBlock>
      <CodeBlockHeader>
        <CodeBlockTitle>
          <FileCodeIcon />
          Terminal
        </CodeBlockTitle>
        <CodeBlockActions>
          <CodeBlockWrapButton />
          <CodeBlockCopyButton value={code} />
        </CodeBlockActions>
      </CodeBlockHeader>
      <CodeBlockContent language="bash">{code}</CodeBlockContent>
    </CodeBlock>
  );
}

Props API

CodeBlock

PropTypeDefaultDescription
variant'default' | 'destructive''default'destructive adds red border and text for error logs
size'default' | 'sm''default'Typography and padding density
wrapboolean—Controlled word-wrap state synced with CodeBlockWrapButton
defaultWrapbooleanfalseInitial uncontrolled word-wrap state
onWrapChange(wrap: boolean) => void—Fired when CodeBlockWrapButton toggles wrapping
glassbooleantrueLiquid Glass material reacting to glass tint; false renders a 100% opaque surface. Auto-switches to Over-Glass on glass surfaces
surface'default' | 'over-glass'Auto-detectedManually override the hosting surface context

CodeBlockContent

PropTypeDefaultDescription
lineNumbersbooleanfalseShow sticky line numbers (auto-splits string children by line)
startLinenumber1Starting line number
wrapbooleanfalseSoft-wrap long lines instead of horizontal scrolling
languageBundledLanguage | string—Shiki syntax highlighting with Xcode palette; unknown languages stay plain text

CodeBlockLine

PropTypeDefaultDescription
variant'default' | 'added' | 'removed' | 'highlighted''default'Added, removed, or highlighted diff line style

Syntax highlighting applies to string children only; manually composed CodeBlockLine rows remain plain text. Installing this component also adds shiki.

CodeBlockWrapButton toggles wrapping on its parent CodeBlock, and CodeBlockCopyButton copies the provided value; both forward remaining props to Button (defaulting to variant="ghost", size="icon-xs").

Language & Text

CodeBlock accepts locale and messages, which apply to all of its parts and the overlays it opens and win over the global <LiquidGlassProvider locale messages>. To override the text globally, put it under messages.codeBlock on the provider. See Liquid Glass → Global settings.

PropTypeDescription
localeLiquidGlassLocaleInputUI language of this component (any BCP 47 tag; unsupported ones fall back to English)
messagesPartial<CodeBlockMessages>Text overrides, merged over the built-in text and the global messages.codeBlock
KeyBuilt-in text (English)
copyCopy
copiedCopied
enableWrapEnable word wrap
disableWrapDisable word wrap

On this page