Code Block

Styled container for displaying code snippets with header, language badge, and copy button

Code Block

Code blocks provide a styled container for displaying code snippets. They include a header with a language badge and optional title, a copy button, and a content area for the code.

Basic Usage

Wrap a <pre><code> block inside .code-block with a .code-header and .code-content area.

Basic Code Block

html
<button class="btn btn-primary">Click me</button>

With Title

Add a .code-title span before the language badge to label the file or context.

Code Block with Title

App.jsx jsx
export default function App() {
return <h1>Hello World</h1>;
}

With Copy Button

Add a .copy-button inside the header for clipboard functionality.

Code Block with Copy Button

bash
npm install @duskmoon-dev/core

Variants

Compact

Use .code-block-compact for reduced padding, suitable for inline or sidebar usage.

Compact Code Block

css
.btn { display: inline-flex; }

Borderless

Use .code-block-borderless to remove the outer border, useful when nesting inside other containers.

Borderless Code Block

js
const x = 42;

Multiple Languages

Code blocks work with any language label — the badge is purely informational.

Multiple Languages

styles.css css
.card { border-radius: 0.5rem; }
index.ts typescript
interface Props { title: string; }

Best Practices

  • Always include a .code-language badge so users know the syntax
  • Use .code-title for file names or descriptive labels
  • Keep code snippets focused — show only the relevant portion
  • Use the compact variant for short, single-line examples
  • The copy button should use the Clipboard API (navigator.clipboard.writeText())

API Reference

CSS Classes

ClassDescription
.code-blockBase container — border, radius, overflow hidden
.code-headerFlex header row — background, border-bottom
.code-titleBold label (e.g., filename) — pushes siblings right
.code-languageUppercase language badge — muted color
.copy-buttonGhost button — transparent bg, hover state
.copy-iconSVG icon inside copy button
.code-contentCode area — surface background, overflow scroll
.code-block-compactVariant: reduced padding
.code-block-borderlessVariant: no outer border

Structure

<div class="code-block">
  <div class="code-header">
    <span class="code-title">filename.ext</span>   <!-- optional -->
    <span class="code-language">lang</span>
    <button class="copy-button">...</button>        <!-- optional -->
  </div>
  <div class="code-content">
    <pre><code>...</code></pre>
  </div>
</div>
  • Markdown Body — full markdown typography including inline code and fenced blocks
  • Card — general-purpose content container