Code Block for Astro
Composable Astro code blocks with copy controls, line numbers, diff states, and syntax highlighting.
Installation
bunx --bun shadcn@latest add @gnostic/code-blocknpx shadcn@latest add @gnostic/code-blockpnpm dlx shadcn@latest add @gnostic/code-blockInstall component packages
bun add shikinpm install shikipnpm add shikiCopy the files
src/components/ui/code-block/code-block-actions.astrosrc/components/ui/code-block/code-block-content.astrosrc/components/ui/code-block/code-block-copy-button.astrosrc/components/ui/code-block/code-block-footer.astrosrc/components/ui/code-block/code-block-header.astrosrc/components/ui/code-block/code-block-line.astrosrc/components/ui/code-block/code-block-title.astrosrc/components/ui/code-block/code-block-variants.tssrc/components/ui/code-block/code-block.astrosrc/components/ui/code-block/highlight-code.tssrc/components/ui/code-block/index.tssrc/components/ui/code-block/types.ts
Usage
---
import {
CodeBlock,
CodeBlockHeader,
CodeBlockTitle,
CodeBlockActions,
CodeBlockCopyButton,
CodeBlockContent,
} from "@/components/ui/code-block"
const output = "bun run build"
---
<CodeBlock>
<CodeBlockHeader>
<CodeBlockTitle>Terminal</CodeBlockTitle>
<CodeBlockActions>
<CodeBlockCopyButton value={output} />
</CodeBlockActions>
</CodeBlockHeader>
<CodeBlockContent code={output} />
</CodeBlock>
Composition
CodeBlock
├── CodeBlockHeader
│ ├── CodeBlockTitle
│ └── CodeBlockActions
│ └── CodeBlockCopyButton
├── CodeBlockActions (floating, without a header)
├── CodeBlockContent
│ └── CodeBlockLine
└── CodeBlockFooter
Pass plain text through code or the default slot. Use CodeBlockLine parts
in the slot for individual line states. Header, title, actions, and footer
accept HTML attributes and default slots. Wrap the title in an anchor to link
to a file. Place each block in a TabsContent to show multiple files.
Tabs
Show several files with one CodeBlock in each TabsContent. Each panel has its
own title, highlighted source, line numbers, and copy button. This ports the
shadcn tabs example
to Astro. The React and API code shown in the panels is sample text.
Floating actions
Put actions directly inside the root to show them on hover or keyboard focus. They stay visible on touch screens.
Line numbers and syntax highlighting
Set lineNumbers and use startLine for a sample from the middle of a file.
The line number column grows to fit its content. Use language with code
for server-side syntax highlighting. Shiki does not enter the browser bundle.
Diff and highlighted lines
Use added, removed, or highlighted on each CodeBlockLine. Diff markers
use a symbol and a color. Line numbers and markers are not part of the copied
source. Empty lines retain their height and selectable newline.
For highlighted lines, import highlightCode from
@/components/ui/code-block/highlight-code in Astro frontmatter. It returns
an array of escaped HTML strings. Put each result in a CodeBlockLine with
<Fragment set:html={line} />.
Wrap, small size, and errors
Use wrap to wrap long lines, size="sm" for compact output, and
variant="destructive" for errors. A caller-owned button can change the
content’s data-wrap attribute. Keep its aria-pressed state in sync.
Height and footer
Limit the content height with a max-h-* class. Use the footer for a Show More
button. Keep aria-expanded in sync and point aria-controls at the content.
Terminal colors
Use language="ansi" to show terminal colors. Each line starts with fresh
ANSI state, as in the shadcn component.
Inside Typeset
Typeset leaves elements with data-slot and their contents unchanged. A direct
child component gets the same space above it as a paragraph. Code Block needs
no data-not-typeset attribute. Plain pre and code elements keep their text
styles.
Astro port notes
This component follows the shadcn Code Block composition. Coss does not supply this component. The shared Gnostic recipe uses the same layout with semantic success and destructive colors. Shadcn presets use their upstream Code Block recipes. Gnostic, Soft, and Glow use the shared Gnostic recipe with their own color, font, and radius tokens.
Use Astro class, slots, and scripts. There are no React hooks or render props.
Rendering and highlighting run on the server. Incremental client highlighting
and Streamdown integration are not included. Native scrolling is used; the
upstream scroll-fade utilities are not included.
Supported languages are Astro, Bash, CSS, diff, HTML, JavaScript, JSON, JSX,
Markdown, Python, SQL, TSX, TypeScript, YAML, and ANSI. Common short names such
as ts, js, sh, and yml work. Unknown languages render as plain text.
One final newline is omitted when text is split into lines. The copy button
always copies its exact value, including final newlines.
Accessibility
Code always reads left to right. The header follows the page direction.
Content has tabindex="0" so keyboard users can scroll it. Set
tabindex={-1} to remove this tab stop. Line numbers use CSS counters with
empty alternative text where the browser supports it.
The copy button uses the shared Copy Button controller.
It has an accessible label and a polite status message. It reports
copy failure if the Clipboard API is unavailable or permission is denied.
Clipboard access requires a secure context, such as HTTPS or localhost.
Native button attributes, including disabled, pass through.
API Reference
CodeBlockProps
variant?'default' | 'destructive'
The tone of the block.
'default' | 'destructive''default'size?'default' | 'sm'
The spacing and text size.
'default' | 'sm''default'CodeBlockContentProps
code?string
Plain source text. Takes precedence over the default slot.
stringlanguage?string
Highlight `code` on the server with Shiki. Unknown languages stay plain.
stringwrap?boolean
Wrap long lines.
booleanfalselineNumbers?boolean
Show line numbers outside the selectable text.
booleanfalsestartLine?number
The first line number.
number1CodeBlockLineProps
variant?'default' | 'added' | 'removed' | 'highlighted'
The state of the line.
'default' | 'added' | 'removed' | 'highlighted''default'CodeBlockCopyButtonProps
Accepts the shared Button variant and size props. Defaults are ghost and
icon-xs. Use the default slot to replace the copy and check icons.
valuestring
The exact text to copy, without line numbers or diff markers.
stringRead the installed source for the complete prop types.
Theme and layout
Code Block uses the project preset for its frame and controls. Set a maximum content height for long examples, and choose wrapping or scrolling to suit the code. Keep copy controls reachable at narrow widths. Use Tabs for related code samples and Typeset for the surrounding guide.