Code Block.
Code with its language, one click from the clipboard.
A fenced code block with a language label and a copy button. Response uses it for every ``` fence. Pass highlighted markup as children to add syntax colors; the copy action still copies the original code.
Make it yours
Preparing the example…
Composition
AnatomyLong lines scroll inside the block, which is focusable for that reason. The copy result is announced politely.
CodeBlock (figure)
├── figcaption — language + IconButton copy
├── pre tabIndex=0 > code
└── span[role=status] — Copied / Copy failedA place to start
UsageImport the stylesheet once at your application root. This complete example reflects the properties selected above.
Loading example…
Details that make a difference
Examples & statesThe finer details
API referenceGenerated from Dixy’s local TypeScript interfaces. Native HTML attributes and refs are forwarded. The table covers Dixy’s own options.
CodeBlock
| Prop | Type / description | Default |
|---|---|---|
code* | stringThe source text. Copied verbatim. | — |
language | stringLanguage name shown in the header, e.g. "tsx". | — |
children | ReactNodePre-highlighted markup to show instead of plain text, e.g. from Shiki. The copy action still copies `code`. | — |
copyLabel | stringAccessible name of the copy button. | Copy code |
copiedLabel | stringAnnounced after a successful copy. | Copied |
failedLabel | stringAnnounced when the clipboard is unavailable. | Copy failed |
onCopy | ((code: string) => void)Called with the code after the clipboard write succeeds. | — |
Built from the same foundations
Styling tokensChange Theme settings above to compare appearance, accent, neutral, radius, spacing density, motion and direction. Override component aliases on a Theme root so custom properties follow portaled content too. The wrapper merges className, style, refs and render composition with its owning primitive.
| Token | Purpose |
|---|---|
--dixy-font-code-sm | The component-specific starting point. |
--dixy-font-label-md | Control label font and line height; body roles remain readable in text inputs. |
--dixy-color-background-selected | Selected surfaces use semantic color across appearances. |
--dixy-color-border-focus | Focus indication remains visible independently of shadows. |
--dixy-spacing-inset-sm | Density-aware component padding. |
--dixy-motion-transition-feedback | Short state feedback, reduced by the inherited motion setting. |
Consider everyone
AccessibilityThe copy button has a constant name and the result is announced through a status region. The scrollable pre is named after its language.
Target: WCAG 2.2 AA. Automated tests cover selected behavior; manual screen-reader, contrast, and zoom validation remain part of the release checklist.
Build it. Then understand it.
Keep the source of truth separate from its presentation: highlighted markup is for eyes, the code string is for the clipboard.
--dixy-font-code-sm