With Title
The title sits in a header inside the block, beside the copy action.
<CodeBlock title="Installation" code="npm install @usefragments/ui" language="bash" />Displays code with syntax highlighting, copying, and optional line numbers, diffs or folding.
<CodeBlock code={`import { Button } from '@usefragments/ui';
function App() {
return <Button>Click me</Button>;
}`} language="tsx" />pnpm add @usefragments/uiThe npm package stays managed by your package manager. Import styles once from @usefragments/ui/styles. See the installation guide for framework and theme setup.
shiki — Optional — provides syntax highlighting; without it code renders unhighlighted.pnpm add shikiImport CodeBlock once, then compose its parts with dot notation.
CodeBlock.TabbedThe title sits in a header inside the block, beside the copy action.
<CodeBlock title="Installation" code="npm install @usefragments/ui" language="bash" />Mono 11 for dense panels and side columns.
<CodeBlock size="sm" language="bash" title="Run in CI" code="npx @usefragments/cli check --ci" />Turn these on once a snippet is long enough to point at.
<CodeBlock code={`const greeting = "Hello";
const name = "World";
console.log(\`\${greeting}, \${name}!\`);`} language="typescript" showLineNumbers />Numbering starts where the excerpt actually starts in the file.
<CodeBlock
code={` return (
<button onClick={() => setCount(c => c + 1)}>
Count: {count}
</button>
);Marks the lines the prose is talking about.
<CodeBlock code={`import { useState } from 'react';
function Counter() {
const [count, setCount] = useState(0);
return (Added and removed lines, side by side with the rest.
<CodeBlock
code={`import { useState } from 'react';
import { useCallback } from 'react';
function Counter() {
const [count, setCount] = useState(0);Long lines break instead of scrolling sideways.
<CodeBlock
code={`const longString = "This is a very long string that would normally cause horizontal scrolling, but with word wrap enabled it will break to the next line instead.";`}
language="typescript"
wordWrap
/>Caps the height so one snippet cannot own the page.
<CodeBlock
code={`// This code block has a maximum height
function processItems(items: string[]) {
const results = [];
for (const item of items) {
if (item.startsWith('_')) {Shows the opening lines and hides the rest behind a toggle.
<CodeBlock
code={`import React, { useState, useEffect } from 'react';
interface User {
id: string;
name: string;Config files highlight the same way source does.
<CodeBlock title="package.json" code={`{
"name": "my-app",
"dependencies": {
"@usefragments/ui": "^0.3.0"
}
}`} language="json" />Drop the copy button when the snippet is not meant to be reused.
<CodeBlock code="const simple = true;" language="typescript" showCopy={false} />Prop | Type | Default | Description |
|---|---|---|---|
addedLines | (string | number)[] | undefined | Not set | Lines marked as added in diff view (e.g., [2, "4-6"]) |
codeRequired | string | Not set | The code string to display |
collapsedLines | number | undefined | 5 | Number of lines to show when collapsed |
collapsible | boolean | undefined | false | Allow collapsing/expanding the code block |
defaultCollapsed | boolean | undefined | false | Initial collapsed state (only applies when collapsible is true) |
highlightLines | (string | number)[] | undefined | Not set | Lines to highlight (e.g., [1, 3, "5-7"]) |
language | CodeBlockLanguage | undefinedmdhtmlrubytextgotsxtypescripttsjavascriptjsjsxbashshellcssscsssassjsonxmlmarkdownyamlymlpythonpyrustjavakotlinswiftccppcsharpphpsqlgraphqldiffplaintext | tsx | Programming language for syntax highlighting |
maxHeight | number | undefined | Not set | Maximum height in pixels (enables scrolling) |
onCopy | (() => void) | undefined | Not set | Callback fired when the copy button is clicked and copy succeeds |
removedLines | (string | number)[] | undefined | Not set | Lines marked as removed in diff view (e.g., [1, 3]) |
showCopy | boolean | undefined | true | Show the copy action. It stays visible and says Copied or Couldn't copy for two seconds |
showLineNumbers | boolean | undefined | false | Whether to display line numbers |
size | CodeBlockSize | undefinedsmmd | md | Code type step: md is mono 12 for documents and replies, sm is mono 11 for dense panels |
startLineNumber | number | undefined | 1 | Starting line number (useful for code excerpts) |
Tabbed.className | string | undefined | Not set | Additional class name |
Tabbed.collapsedLines | number | undefined | Not set | Lines shown while folded. |
Tabbed.collapsible | boolean | undefined | Not set | Fold long code to `collapsedLines` with a "Show N more lines" bar |
Tabbed.defaultCollapsed | boolean | undefined | Not set | Start folded (only with `collapsible`) |
Tabbed.defaultTab | string | undefined | Not set | Default selected tab (by tab value, or label when value is omitted) |
Tabbed.maxHeight | number | undefined | Not set | Maximum height in pixels; the code scrolls past it |
Tabbed.onCopy | ((tabLabel: string) => void) | undefined | Not set | Called after a tab's code reaches the clipboard. Receives the tab label. |
Tabbed.onValueChange | ((value: string) => void) | undefined | Not set | Called when the selected tab changes |
Tabbed.showCopy | boolean | undefined | Not set | Show the copy action. |
Tabbed.showLineNumbers | boolean | undefined | Not set | Show line numbers |
Tabbed.size | CodeBlockSize | undefinedsmmd | Not set | Code type step. |
Tabbed.tabsRequired | CodeBlockTab[] | Not set | Array of code tabs |
Tabbed.value | string | undefined | Not set | Controlled selected tab value |
Tabbed.wordWrap | boolean | undefined | Not set | Wrap long lines instead of scrolling sideways |
title | React.ReactNode | Not set | A file name or label, shown in a header inside the block beside the copy action |
wordWrap | boolean | undefined | false | Enable word wrapping for long lines |
style | CSSProperties | undefined | Not set | — |