Skip to content

Code Block

Free

Shows code with syntax colors, a Copy button, and line wrap. It can also number and mark lines, show a diff, hold file tabs, and show install commands.

  • Base UI
  • Motion
  • React 19
  • Tailwind CSS 4

Added Nov 8, 2026 · MIT + Commons Clause

Playground

Loading the preview

Props

Put a header and the content in the root.

example-code.tsx

CodeBlock

<figure>
PropTypeDefaultDescription
palette"color""ink""theme"—
variant"framed""plain"—
wrapboolean—Wrap long lines (controlled). The Wrap toggle sets it.
defaultWrapbooleanfalseWrap long lines at first (uncontrolled). Default false: code scrolls sideways.
onWrapChange—
streamingbooleanfalseThe code still arrives: an AI answer or a tool writes it now. The content shows plain text with fresh ink on the newest characters, Copy waits, and CodeBlockStatus says "Writing" with the line count. When it turns false, the syntax colors fade in once and the status says "Wrote N lines". Default false.
interrupted"cut-off""stopped"—The stream ended before the code was whole: stopped (a person stopped it) or cut-off (the reply ended inside the block, a destructive edge). A note under the code says so.
...props<figure> attributes—Every other attribute goes to the <figure>, with a ref.

CodeBlockHeader

<div>
PropTypeDefaultDescription
...props<div> attributes—Every other attribute goes to the <div>, with a ref.

CodeBlockTitle

<div>
PropTypeDefaultDescription
...props<div> attributes—Every other attribute goes to the <div>, with a ref.

CodeBlockLanguage

<span>
PropTypeDefaultDescription
langstring—The language: its short label comes from languageLabel (tsx → "TSX").
...props<span> attributes—Every other attribute goes to the <span>, with a ref.

CodeBlockCopyButton

<button>
PropTypeDefaultDescription
aria-labelstring—Defines a string value that labels the current element. The name of the button. An icon-only button defaults to "Copy"; one with text uses its text.
classNamestring—
busyboolean—The button waits for the app (busy words): a spinner, aria-busy, and no clicks. Focus stays on the button. The style hook stays data-loading.
spinnerPlacement"center""start"—center (default): the spinner covers the label. start: it takes the leading icon slot. The icon sizes always use center, because their one icon is the label.
timeoutnumber—How long the result (check or X) shows, in ms. Default 2000.
onCopy—Runs after the clipboard has the text, with the text.
onCopyError—Runs when the copy fails: no Clipboard API, the browser refused, or getValue failed.
glass"clear""dark""light"—
rounded"default""full"—
size"default""icon""icon-lg""icon-sm""icon-xs""lg""sm""xl""xs"—
variant"dashed""default""destructive""destructive-outline""ghost""link""media""outline""secondary"—
From Base UIButtonBase UI docs
focusableWhenDisabledbooleanfalseWhether the button should be focusable when disabled.
render—Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.
style—Style applied to the element, or a function that returns a style object based on the component's state.
...props<button> attributes—Every other attribute goes to the <button>, with a ref.

CodeBlockWrapToggle

<button>
PropTypeDefaultDescription
classNamestring—
From Base UIToggleBase UI docs
disabledbooleanfalseWhether the component should ignore user interaction.
valuestring—A unique string that identifies the toggle when used inside a toggle group.
render—Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.
...props<button> attributes—Every other attribute goes to the <button>, with a ref.

CodeBlockActions

<div>
PropTypeDefaultDescription
...props<div> attributes—Every other attribute goes to the <div>, with a ref.

CodeBlockFooter

<div>
PropTypeDefaultDescription
...props<div> attributes—Every other attribute goes to the <div>, with a ref.

CodeBlockContent

<pre>
PropTypeDefaultDescription
aria-labelstring—Defines a string value that labels the current element. The name of the code region for screen readers. Default: "<Language> code".
langstring—The language: a name or alias that highlighter knows (ts, bash, md). Default: plain text.
codeRequiredstring—The source code. Copy copies it as is. While the block streams, the code so far.
lineNumbersbooleantrueShow line numbers. Default true.
startLinenumber1The number of the first line, for a part of a file. Default 1.
numbers—Own line numbers, one entry per line, in place of startLine: one column, or two for a diff ([old, new]; undefined leaves a cell empty: an added line has no old number). Shown even with lineNumbers={false}.
marks—A mark for a line, by its shown number: { 4: "highlight", 7: "inserted" }.
focusreadonly number[]—Lines in focus, by their shown number: a tint band and a 2px edge at the start. The other lines keep full contrast.
wordsreadonly string[]—Word marks: each match of each string.
ranges—Word marks by character range in code: [start, end], end not included.
promptstring—A prompt before each line ($), not selectable and not copied.
maxLinesnumber—Fold code longer than this many lines in a Spoiler: the first lines under a fade, and "Show all N lines" centered under the code.
expandedboolean—Shows every line when maxLines applies (controlled).
defaultExpandedbooleanfalseStart expanded when maxLines applies (uncontrolled).
onExpandedChange—Runs when the fold trigger shows or hides the lines past maxLines.
labelsCodeBlockContentLabels—The words of the fold trigger (expand, collapse) and of the stream notes.
reveal"off""paragraph""word""word"The pace of streamed code (#/lib/stream-pace): word (default) shows bursts at an even rate on the frame clock, off shows the code as it comes. A part that paces its text already (Streaming Markdown) passes off.
classNamestring—Classes of the scroll box around the <pre>: set a height here (max-h-96).
...props<pre> attributes—Every other attribute goes to the <pre>, with a ref.

CodeBlockLine

<span>
PropTypeDefaultDescription
mark"deleted""error""highlight""inserted""warning"—The mark of the line: its tint, and for inserted and deleted an <ins> or <del> element, so a screen reader can say the change.
numbernumber—The shown line number (data-line).
focusedbooleanfalseA line in focus: the default tint band and a 2px edge at the start.
lastbooleanfalseThe last line of a stream (data-last).
...props<span> attributes—Every other attribute goes to the <span>.

CodeBlockStatus

<span>
PropTypeDefaultDescription
childrenReactNode—A mark before "Writing" while the code arrives: the Agent Presence orb with the writing verb in the AI items (<AiAgentPresence verb="writing" size={14} aria-hidden />).
labelsCodeBlockStatusLabels—
...props<span> attributes—Every other attribute goes to the <span>, with a ref.

CodeBlockTabs

<div>
PropTypeDefaultDescription
classNamestring—
syncKeystring—Share the open tab with every block that has the same key, and keep it for the next visit: "package-manager", "framework". Works with string tab values. A block that has no tab for the shared choice keeps its own tab.
From Base UITabs.RootBase UI docs
valueany—The value of the currently active Tab. Use when the component is controlled. When the value is null, no Tab will be active.
defaultValueany0The default value. Use when the component is not controlled. When the value is null, no Tab will be active.
orientation"horizontal""vertical"'horizontal'The component orientation (layout flow direction).
...props<div> attributes—Every other attribute goes to the <div>, with a ref.

CodeBlockTabsList

<div>
PropTypeDefaultDescription
classNamestring—
rounded"default""full"—
size"default""lg""sm"—
From Base UITabs.ListBase UI docs
activateOnFocusbooleanfalseWhether to automatically change the active tab on arrow key focus. Otherwise, tabs will be activated using <kbd>Enter</kbd> or <kbd>Space</kbd> key press.
loopFocusbooleantrueWhether to loop keyboard focus back to the first item when the end of the list is reached while using the arrow keys.
render—Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.
...props<div> attributes—Every other attribute goes to the <div>, with a ref.

CodeBlockTab

<button>
PropTypeDefaultDescription
classNamestring—
From Base UITabs.TabBase UI docs
valueRequiredany—The value of the Tab.
disabledboolean—Whether the Tab is disabled. If a first Tab on a <Tabs.List> is disabled, it won't initially be selected. Instead, the next enabled Tab will be selected. However, it does not work like this during server-side rendering, as it is not known during pre-rendering which Tabs are disabled. To work around it, ensure that defaultValue or value on <Tabs.Root> is set to an enabled Tab's value.
render—Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.
...props<button> attributes—Every other attribute goes to the <button>, with a ref.

CodeBlockTabsPanel

<div>
PropTypeDefaultDescription
classNamestring—
gap"default""none"—The space between the list and the panel: default (12px) or none, for a panel that sits flush under a list in a head with its own edge (the JSX Preview stage).
From Base UITabs.PanelBase UI docs
valueRequiredany—The value of the TabPanel. It will be shown when the Tab with the corresponding value is active.
keepMountedbooleanfalseWhether to keep the HTML element in the DOM while the panel is hidden.
render—Allows you to replace the component's HTML element with a different tag, or compose it with another component. Accepts a ReactElement or a function that returns the element to render.
...props<div> attributes—Every other attribute goes to the <div>, with a ref.

CodeBlockInstall

<div>
PropTypeDefaultDescription
wrapboolean—Wrap long lines (controlled). The Wrap toggle sets it.
defaultWrapboolean—Wrap long lines at first (uncontrolled). Default false: code scrolls sideways.
onWrapChange—
streamingboolean—The code still arrives: an AI answer or a tool writes it now. The content shows plain text with fresh ink on the newest characters, Copy waits, and CodeBlockStatus says "Writing" with the line count. When it turns false, the syntax colors fade in once and the status says "Wrote N lines". Default false.
interrupted"cut-off""stopped"—The stream ended before the code was whole: stopped (a person stopped it) or cut-off (the reply ended inside the block, a destructive edge). A note under the code says so.
palette"color""ink""theme"—
variant"framed""plain"—
commandRequiredstring—One npm command: npm install @tanstack/highlight, npx shadcn add @gelatin-ui/code-block.
managersPACKAGE_MANAGERSThe tabs, in order. Default npm, pnpm, yarn, bun.
syncKeystring"package-manager"The shared choice (see CodeBlockTabs). Default "package-manager".
promptstring"$"The prompt before the command. Default $.
labelstring"Package manager"The name of the tab list. Default "Package manager".
marks—A mark before the name of each tab, in place of the built-in one (the npm, pnpm, Yarn, or Bun logo at 14px). The name always shows with it.
markTone"color""mono""color"The built-in marks: color (default, the brand colors) or mono (the tab text color).
labelsPartial<{ copy: string; }>—The words (CODE_BLOCK_INSTALL_LABELS). Keep the object stable.
onCopyCommand—Called after Copy wrote the command to the clipboard, with the text it copied: the command of the open tab (pnpm dlx shadcn add …). A failed copy does not call it. The root's own onCopy stays the DOM clipboard event.
...props<div> attributes—Every other attribute goes to the <div>, with a ref.
  • Use CodeBlockTabs for two or more files.
  • Use CodeBlockInstall for one command in four package managers.
  • Use maxLines for a long sample in an article.
  • For code in a sentence, use Code.
TabMoves focus to the code region when it scrolls, then to Copy and Wrap.
Left ArroworRight ArrowScrolls the focused code sideways.

The root is a figure.

A code region that scrolls takes the focus, so the keys can scroll it.

The ` prompt of a terminal is not selected and not copied.

  1. AddedCode Block, with Code, Copy Button, Direction, Rolling Number, Scroll Area, Spoiler, Tabs, Toggle.
Open the release