Code Block
Display and edit code with syntax highlighting, formatting, and Mermaid diagrams.
Code Block Default
Supported Features
-
Controlled/Uncontrolled state management
-
Syntax highlighting via Shiki (50+ languages, 50+ themes)
-
Language selection with searchable menu
-
Code formatting via Prettier (JavaScript, TypeScript, JSON, HTML, CSS, SCSS, Less, Markdown, YAML, GraphQL)
-
Code wrapping toggle
-
Copy to clipboard
-
Caption support
-
Mermaid diagram preview
-
Readonly mode
Installation
pnpm add @notion-kit/code-blockAnatomy
<CodeBlock>
<CodeBlockLang />
<CodeBlockToolbar />
<CodeBlockContent />
<CodeBlockCaption />
</CodeBlock>Examples
Uncontrolled
In uncontrolled mode, the component manages its own state internally.
Use defaultValue to set the initial code and language.
Code Block Default
<CodeBlock
defaultValue={{
code: sampleCode,
lang: "typescript",
}}
>
<CodeBlockLang />
<CodeBlockToolbar />
<CodeBlockContent />
<CodeBlockCaption />
</CodeBlock>Controlled
In controlled mode, use value and onChange to manage state externally.
Code Block Controlled
<CodeBlock value={value} onChange={setValue}>
<CodeBlockLang />
<CodeBlockToolbar />
<CodeBlockContent />
<CodeBlockCaption />
</CodeBlock>Readonly
Use the readonly prop to prevent editing code, changing language, and editing the caption.
Code Block Readonly
<CodeBlock
readonly
defaultValue={{
code: sampleCode,
lang: "typescript",
caption: "This code block is readonly",
}}
>
<CodeBlockLang />
<CodeBlockToolbar />
<CodeBlockContent />
<CodeBlockCaption />
</CodeBlock>Mermaid Diagrams
When language is set to mermaid, a live diagram preview is rendered alongside the code editor.
Code Block Mermaid
<CodeBlock defaultValue={{ code: mermaidCode, lang: "mermaid" }}>
<CodeBlockLang />
<CodeBlockToolbar />
<CodeBlockContent />
<CodeBlockCaption />
</CodeBlock>API Reference
CodeBlock
The root component. Accepts all standard <div> props in addition to the following:
Prop
Type
CodeBlockContent
Renders the syntax-highlighted code with an editable textarea overlay.
No additional props beyond standard <div> props.
CodeBlockLang
Renders the language selector button with a searchable dropdown menu.
CodeBlockToolbar
Renders the toolbar with copy-to-clipboard and action menu.
CodeBlockCaption
Renders an editable caption area below the code block. Disabled in readonly mode.
MermaidPreview
Renders a live Mermaid diagram preview. Only renders when the language is mermaid.
type CodeBlockValue
Prop
Type