Code Block
Provides syntax highlighting, line numbers, and copy to clipboard functionality for code blocks.
The CodeBlock component provides syntax highlighting, line numbers, and copy to clipboard functionality for code blocks. It's fully composable, allowing you to customize the header, actions, and content.
Loading preview…
Installation
npm i zjkm-ui-ai-elementsimport { CodeBlockContainer, CodeBlockHeader, CodeBlockTitle, CodeBlockFilename, CodeBlockActions, CodeBlockContent, CodeBlock, CodeBlockCopyButton, CodeBlockLanguageSelector, CodeBlockLanguageSelectorTrigger, CodeBlockLanguageSelectorValue, CodeBlockLanguageSelectorContent, CodeBlockLanguageSelectorItem,} from "zjkm-ui-ai-elements/code-block";First time? Add the package's styles: see Setup.
Usage
The CodeBlock is fully composable. Here's a basic example:
import {
CodeBlock,
CodeBlockActions,
CodeBlockCopyButton,
CodeBlockFilename,
CodeBlockHeader,
CodeBlockTitle,
} from "zjkm-ui-ai-elements/code-block";
import { FileIcon } from "lucide-react";
export const Example = () => (
<CodeBlock code={code} language="typescript">
<CodeBlockHeader>
<CodeBlockTitle>
<FileIcon size={14} />
<CodeBlockFilename>example.ts</CodeBlockFilename>
</CodeBlockTitle>
<CodeBlockActions>
<CodeBlockCopyButton />
</CodeBlockActions>
</CodeBlockHeader>
</CodeBlock>
);Features
- Syntax highlighting with Shiki
- Line numbers (optional)
- Copy to clipboard functionality
- Automatic light/dark theme switching via CSS variables
- Language selector for multi-language examples
- Fully composable architecture
- Accessible design
Examples
Dark Mode
To use the CodeBlock component in dark mode, wrap it in a div with the dark class.
Loading preview…
Language Selector
Add a language selector to switch between different code implementations:
Loading preview…
Props
<CodeBlock />
Prop
Type
<CodeBlockHeader />
Container for the header row. Uses flexbox with justify-between.
Prop
Type
<CodeBlockTitle />
Left-aligned container for icon and filename. Uses flexbox with gap-2.
Prop
Type
<CodeBlockFilename />
Displays the filename in monospace font.
Prop
Type
<CodeBlockActions />
Right-aligned container for action buttons. Uses flexbox with gap-2.
Prop
Type
<CodeBlockCopyButton />
Prop
Type
<CodeBlockLanguageSelector />
Wrapper for the language selector. Extends shadcn/ui Select.
Prop
Type
<CodeBlockLanguageSelectorTrigger />
Trigger button for the language selector dropdown. Pre-styled for code block header.
<CodeBlockLanguageSelectorValue />
Displays the selected language value.
<CodeBlockLanguageSelectorContent />
Dropdown content container. Defaults to align="end".
<CodeBlockLanguageSelectorItem />
Individual language option in the dropdown.
Prop
Type
<CodeBlockContainer />
Low-level container component with performance optimizations (contentVisibility). Used internally by CodeBlock.
<CodeBlockContent />
Low-level component that handles syntax highlighting. Used internally by CodeBlock, but can be used directly for custom layouts.
Prop
Type