Code Block
interlace-ui/code-blockFreeComponent
Fenced code block with a title bar (title left, language tag + copy button right). The copy action shells out to `navigator.clipboard.writeText` and flips an inline "Copied!" affordance for 1.5s — the only piece of state this primitive owns. Everything…
Install it
npx shadcn@latest add https://ds.interlace.tools/r/code-block.jsonSource
1'use client';23import * as React from 'react';45// @interlace/code-block v1.1.0 — Interlace design system.6// Docs, props and live preview: https://ds.interlace.tools/c/code-block7// What changed since: https://ds.interlace.tools/c/code-block#history8// Generated banner — keep it, the upgrade diff reads this version.910/**11 * @interlace/ui — CodeBlock12 *13 * Fenced code block with a title bar (title left, language tag + copy button14 * right). The copy action shells out to `navigator.clipboard.writeText` and15 * flips an inline "Copied!" affordance for 1.5s — the only piece of state16 * this primitive owns. Everything else is structural: a `<figure>` wrapping17 * a `<pre><code class="language-{lang}">`, ready for any downstream syntax18 * highlighter (Shiki, Prism, hand-rolled — we don't care).19 *20 * The header is omitted entirely when neither `title` nor `language` are21 * provided AND copy is disabled, so the primitive degrades to a clean22 * `<figure><pre><code/></pre></figure>` for inline snippets.23 *24 * ## Anatomy25 *26 * CodeBlock (figure — data-min-viewport=320)27 * ├─ figcaption (header bar; rendered only when needed)28 * │ ├─ {title} (left)29 * │ ├─ {language tag} (right)30 * │ └─ <button> "Copy" / "Copied!" (right; client-only)31 * └─ <pre>32 * └─ <code class="language-{lang}">{children}</code>33 *34 * ## MIN_VIEWPORT — 32035 *36 * Code blocks are the load-bearing surface of a docs site and MUST work on37 * a 320 CSS-px phone. We never wrap content (would shred indentation) — we38 * `overflow-x-auto` instead, so a narrow viewport gets a horizontally39 * scrollable block rather than mangled syntax.40 *41 * | Rule | Concept | Where in this file |42 * | ---- | -------------------------------- | ----------------------------------------------------------- |43 * | R4 | Extends native el | `React.ComponentProps<'figure'> & CodeBlockProps` |44 * | R6 | data-slot per part | code-block / code-block-header / code-block-title / code-block-language / code-block-copy / code-block-pre / code-block-code |45 * | R7 | className merged + ...rest | `cn(BASE, className)` + `{...props}` |46 * | R8 | No isXxx; props are scalars | `title?` / `language?` only — no boolean variants |47// … truncated
More from interlace-ui
All 140 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| A11y Startera11y-starter | interlace-ui | Components | Free | no deps | |
| Accordionaccordion | interlace-ui | Components | Free | no deps | |
| Alertalert | interlace-ui | Components | Free | no deps | |
| Alert Dialogalert-dialog | interlace-ui | Components | Free | no deps | |
| Animated Gradient Textanimated-gradient-text | interlace-ui | Components | Free | no deps | |
| Animated Grid Patternanimated-grid-pattern | interlace-ui | Components | Free | no deps | |
| Animated Listanimated-list | interlace-ui | Components | Free | no deps | |
| Article Cardarticle-card | interlace-ui | Components | Free | no deps |
