AI-generated code shown with syntax highlighting and a copy action, ready to use — here featured in a reply with the high-emphasis Brand style.
Loading demo: ai-code-block-real-world
npx sdaia-ui@latest add ai-code-block
import { AiCodeBlock } from '@/components/ui/AiCodeBlock';1<AiCodeBlock2fileName="response.ts"3code={`const res = await fetch(url, { method: "POST" });4return res.json();`}5collapsible6defaultCollapsed7collapsedVisibleLines={2}8/>
The full code shown with syntax highlighting and a copy action.
Loading demo: ai-code-block-default
A long snippet collapsed to a compact summary with a “Show N more lines” expander.
Loading demo: ai-code-block-collapsed
Prop | Type | Default | Required |
|---|---|---|---|
variant | 'neutral' | 'brand' | 'outline' | 'neutral' | optional |
fileName | ReactNode | — | optional |
code | string | — | optional |
lines | ReactNode[] | — | optional |
showCopy | boolean | true | optional |
copyLabel | string | 'Copy code' | optional |
copiedLabel | string | 'Copied' | optional |
onCopy | (code: string) => void | Promise<void> | — | optional |
collapsible | boolean | false | optional |
collapsed | boolean | — | optional |
defaultCollapsed | boolean | false | optional |
onCollapsedChange | (collapsed: boolean) => void | — | optional |
collapsedVisibleLines | number | 2 | optional |
hiddenLineCount | number | — | optional |
showMoreLabel | ReactNode | ((n: number) => ReactNode) | (n) => `Show ${n} more lines` | optional |
labels | Partial<AiCodeBlockLabels> | — | optional |
dir | 'ltr' | 'rtl' | — | optional |
as | ElementType | 'div' | optional |
How this component behaves at the three system breakpoints. Spacing and type tokens switch automatically at 768px and 1280px.
| Screen | Behavior |
|---|---|
| Desktop (1280px and up) | Fills the width of its container. Code lines never wrap; long lines scroll sideways inside the block. |
| Tablet (768–1279px) | Same layout as desktop, with the tablet token values. |
| Phone (under 768px) | Same: the block fills the screen and long lines scroll sideways. Code text stays at 13px. Long file names are truncated. |