Read-only source over a collapsible card, highlighted through the shared Shiki highlighter and falling back to plain mono while the grammar loads.
1
2
3import type { ReactNode } from 'react';
import {Installation#
pnpm dlx shadcn@latest add @zui/code-cardUsage#
import {
CodeCard,
CodeCardActions,
CodeCardCode,
CodeCardContent,
CodeCardCopy,
CodeCardLanguage,
CodeCardLineNumbers,
} from '@/components/data-display/code-card';The root holds the code; its parts show it. Compose a header from CollapsibleCard parts, then the body:
<CodeCard code={source} language="ts">
<CollapsibleCardHeader>
<CollapsibleCardTitle>
<CodeCardLanguage />
</CollapsibleCardTitle>
<CollapsibleCardActions>
<CodeCardCopy />
<CollapsibleCardTrigger />
</CollapsibleCardActions>
</CollapsibleCardHeader>
<CodeCardContent>
<CodeCardLineNumbers />
<CodeCardCode />
</CodeCardContent>
</CodeCard>A block with no header floats its copy action over the code instead, inside CodeCardActions placed before
CodeCardContent: <CodeCardActions><CodeCardCopy variant="secondary" /></CodeCardActions>. Use it for a
self-contained, read-only block; for already-tokenized source with no card chrome, compose HighlightedCode
directly.
Composition#
CodeCard
|-- CollapsibleCardHeader
| |-- CollapsibleCardTitle
| | `-- CodeCardLanguage
| `-- CollapsibleCardActions
| |-- CodeCardCopy
| `-- CollapsibleCardTrigger
`-- CodeCardContent
|-- CodeCardLineNumbers
`-- CodeCardCodeAPI reference#
CodeCard#
Renders CollapsibleCard and takes every prop it takes, plus:
| Prop | Type | Default |
|---|---|---|
code | string | required |
language | string | - |
lines | HighlightLine[] | null | - |
It defaults variant to muted. It sets data-slot="code-card" and data-language to its language.
CodeCardActions#
Renders a div and takes every prop a div takes. It floats over the code's top-right corner, shown while the
block is hovered or one of its children holds keyboard focus; use it for a block with no header. It sets
data-slot="code-card-actions".
CodeCardContent#
Renders CollapsibleCardContent and takes every prop it takes. Its viewport scrolls the code both ways and
carries data-slot="code-card-viewport". It sets data-slot="code-card-content".
CodeCardLineNumbers#
Renders a span and takes every prop a span takes. It numbers every line of the block's code, at least three
digits wide, hidden from assistive technology and left out of a copy. It sets data-slot="code-card-line-numbers"
and aria-hidden.
CodeCardCode#
Renders HighlightedCode and takes every prop it takes except lines and children, which it supplies from the
block's code and highlighted lines. It sets data-slot="code-card-code".
CodeCardLanguage#
Renders a span and takes every prop a span takes. It shows the block's language as an icon and display name,
"Plain text" when it has none; children replace the name. It sets data-slot="code-card-language".
CodeCardCopy#
Renders CopyButton and takes every prop it takes except value, which it supplies as the block's code. label
defaults to Copy code and size to icon. It sets data-slot="code-card-copy".