Data

Code Block

Syntax-highlighted code with a copy button and optional line numbers. Switch between multiple files via a header tab bar or a dropdown (each with optional file-type icons), or drop the header entirely. Powered by Shiki.

Preview
01 Installation

Install

Pull Code Block from the barrel for everyday use, or the granular path when you want a tighter bundle.

tsxBarrel1 line
import { CodeBlock } from "@brika/clay";
tsxGranular1 line
import { CodeBlock } from "@brika/clay/components/code-block";
02 Usage

A minimal example

Drop this into a page. Native HTML attributes pass through to the underlying primitive.

tsxCode Block.tsx40 lines
'use client';

import {
  CodeBlock,
  CodeBlockActions,
  CodeBlockContent,
  CodeBlockCopyButton,
  CodeBlockHeader,
  CodeBlockInfo,
} from '@brika/clay/components/code-block';

const SAMPLE = `import { Button } from "@brika/clay/components/button";

export default function Save() {
  return <Button>Save changes</Button>;
}`;

/** Syntax-highlighted code with a filename header and a copy-to-clipboard button. */
export default function CodeBlockDefaultDemo() {
  return (
    <CodeBlock className="w-full max-w-lg">
      <CodeBlockHeader>
        <CodeBlockInfo>
          {({ filename, language }) => (
            <span className="flex min-w-0 items-center gap-2 font-mono text-muted-foreground text-xs">
              <span className="truncate">{filename}</span>
              <span className="text-[0.625rem] uppercase tracking-wider">{language}</span>
            </span>
          )}
        </CodeBlockInfo>
        <CodeBlockActions>
          <CodeBlockCopyButton />
        </CodeBlockActions>
      </CodeBlockHeader>
      <CodeBlockContent language="tsx" filename="save.tsx" showLineNumbers={false}>
        {SAMPLE}
      </CodeBlockContent>
    </CodeBlock>
  );
}
03 Examples

Code Block, every way

Multiple Files

Switch between several files with a header tab bar; each tab can carry a file-type icon.

No Header

Omit the header entirely and float the copy button over the code.

Select Files

A dropdown scales better than tabs when there are many files; each item can carry a file-type icon.

04 Accessibility

Accessibility

  • Code blocks are non-interactive regions; Tab moves through the copy button, not character by character.
  • Copy button carries aria-label="Copy code" and should announce success state via a live region.
  • Multi-file blocks follow the WAI-ARIA tabs pattern (role="tablist"/tab/tabpanel); Left/Right arrows switch files.
  • Syntax highlighting is visual only; AT reads the raw code text without colour cues.
05 Tokens 17 theme-overridable

Theme tokens

Every CSS variable Code Block reads, with its default and the dotted path you'd write in a ThemeConfig JSON to override it. Set any of these in your theme to retune Code Block without touching component code.

Code Block tokens

17 tokens
ColorFill, border, and text colors.9
  • --code-block-bgcolorvar(--muted)
    components.codeBlock.bg

    Code block background.

  • --code-block-border-colorcolorvar(--border)
    components.codeBlock.borderColor

    Border color of the code-block surface and the header divider.

  • --code-block-subtle-bgcolorcolor-mix(in oklch, var(--muted) 30%, transparent)
    components.codeBlock.subtleBg

    Background of the `subtle` variant, a faded muted tint that drops the surrounding chrome.

  • --code-block-header-bgcolorcolor-mix(in oklch, var(--muted) 60%, transparent)
    components.codeBlock.headerBg

    Background color of the code-block header strip (filename + actions row).

  • --code-block-gutter-bgcolorcolor-mix(in oklch, var(--muted) 40%, transparent)
    components.codeBlock.gutterBg

    Background color of the line-number gutter on the left edge.

  • --code-block-gutter-bordercolorcolor-mix(in oklch, var(--border) 60%, transparent)
    components.codeBlock.gutterBorder

    Color of the divider between the line-number gutter and the code body.

  • --code-block-gutter-labelcolorvar(--muted-foreground)
    components.codeBlock.gutterLabel

    Text color of the line numbers in the gutter.

  • --code-block-tab-labelcolorvar(--muted-foreground)
    components.codeBlock.tabLabel

    Text color of an inactive file tab in the header (multi-file blocks).

  • --code-block-tab-active-labelcolorvar(--foreground)
    components.codeBlock.tabActiveLabel

    Text color of the active file tab in the header (multi-file blocks).

GeometrySizes, lengths, and corner radii.1
  • --code-block-radiusradiusvar(--radius-control)
    components.codeBlock.radius

    Code block corner radius.

TypographyTypeface, size, weight, spacing.6
  • --code-block-font-familyfont-familyvar(--font-mono)
    components.codeBlock.fontFamily

    Typeface for code-block.

  • --code-block-font-sizefont-sizevar(--text-body-md)
    components.codeBlock.fontSize

    Font size for code-block.

  • --code-block-font-weightfont-weight500
    components.codeBlock.fontWeight

    Font weight for code-block.

  • --code-block-line-heightline-height1.25
    components.codeBlock.lineHeight

    Line height for code-block.

  • --code-block-letter-spacingletter-spacing0
    components.codeBlock.letterSpacing

    Letter spacing for code-block. Useful for caps labels.

  • --code-block-text-transformtext-transformnone
    components.codeBlock.textTransform

    Text transform for code-block (`uppercase`, `lowercase`, `capitalize`, `none`).

ElevationDrop shadow and depth.1
  • --code-block-backdrop-blurblur0px
    components.codeBlock.backdropBlur

    Backdrop blur applied to the code-block surface. Set non-zero for a frosted-glass treatment.

Override in a theme

Authoring a theme is plain JSON. Drop overrides under components.codeBlock, the names are camelCase versions of the variable suffix.

jsonmy-theme.json12 lines
{
  "id": "my-theme",
  "name": "My Theme",
  "description": "...",
  "accentSwatches": ["#000"],

  "components": {
    "codeBlock": {
      "radius": "var(--radius-control)"
    }
  }
}

Then apply with applyTheme(myTheme) (see the theming guide), scope it to a subtree with themeToCssVars(myTheme, mode), or consume any token directly in your own JSX with the matching Tailwind utility (e.g. bg-code-block-…) — see the chips on each token row.

06 API reference

API reference

Props specific to Code Block. Native HTML attributes pass through to the underlying primitive, see the component source on GitHub for the full type signature.

  • string
    Active file value on first render (uncontrolled). Required when using CodeBlockTabs.
  • (value: string) => void
    Fires with the newly selected file value when the active tab changes.
  • string
    Active file value for multi-file blocks (controlled). Pair with onValueChange.
  • VariantProps<typeof codeBlockVariants>['variant']
    Visual treatment; "subtle" drops the surrounding chrome.