Skip to content
GitHub (opens in a new tab)

CodeSnippet

Read-only block of code with a copy button, optional title, language label, tabs, and line numbers. No syntax highlighting.

stableMoleculecontracts/codesnippet.json

Preview

CodeSnippet

Read-only code with a copy button. Optional title, language label, tabs, and line numbers. No syntax highlighting, so it adds no dependency.

Master

Two tabs with line numbers. Variants shows title, wrapping and more.

tsx
import { Button, Card } from "agentic-ds-kit";

export function Welcome({ name }: { name: string }) {
  return (
    <Card title={`Welcome back, ${name}`} description="Pick up where you left off, or start a new project from one of the templates below.">
      <Button appearance="solid" mode="primary">New project</Button>
    </Card>
  );
}

Usage

Copy writes the raw code string, never the line numbers. With tabs, the title only names the code region; the tab labels are the visible header. Language is a label, not highlighting. The code region takes keyboard focus only when it scrolls.

Do's

  • Showing a command, config, or code sample the reader will copy
  • Install or usage snippets in docs, onboarding, or developer settings
  • Several variants of one snippet (npm / pnpm, React / HTML) behind tabs

Don'ts

  • Editable code — use Textarea
  • A short inline token or path inside a sentence — use a <code> element in body text
  • Secrets the user must not see in full (API keys) — mask them before passing code
<CodeSnippet
  tabs={[
    { label: "Welcome.tsx", code: welcome, language: "tsx" },
    { label: "tokens.css", code: tokens, language: "css" },
  ]}
  showLineNumbers
/>

When to use

  • Showing a command, config, or code sample the reader will copy
  • Install or usage snippets in docs, onboarding, or developer settings
  • Several variants of one snippet (npm / pnpm, React / HTML) behind tabs

When not to use

  • Editable code — use Textarea
  • A short inline token or path inside a sentence — use a <code> element in body text
  • Secrets the user must not see in full (API keys) — mask them before passing code

What an agent reads

import { CodeSnippet } from "agentic-ds-kit";
must
  • Code renders in <pre><code>
  • The region is named by title, ariaLabel, the active tab label, or the language ("Code" when none)
  • Two snippets on one page need different names (title or ariaLabel)
  • The region is focusable (tabIndex 0) only when it scrolls, so keyboard users can scroll it
  • Copy button is named by its visible text (Copy, then Copied); a polite live region announces Copied
  • Line numbers are aria-hidden and user-select none; copy writes the raw code string
  • Tabs follow kit Tabs keyboard (arrows, Home, End)
  • built-in labels (code, languageCode, copy, copied) come from KitLabelsProvider; English by default
defaults
  • showLineNumbers false
  • copyable true
  • wrap false
stops if
  • Do not invent a local code block; use kit CodeSnippet
  • Do not add a syntax highlighting dependency
  • Do not use raw hex or a local monospace font; use type.family.code
  • Do not put line numbers into the code string

From contracts/codesnippet.json: must is the a11y requirements, defaults the prop defaults, stops if the doNot rules where the agent asks instead of inventing a cousin.

Props

NameTypeRequiredDefaultNotes
codestringNo—required unless tabs is set; ignored when tabs is set
tabsarrayNo—renders kit Tabs (segmented, sm) in the header; a tab's language wins over language
languagestringNo—label in the header only; no highlighting
titlestringNo—filename or caption in the header; with tabs it only names the code region
ariaLabelstringNo—names the code region when there is no title (title wins). Give each snippet on a page its own name
showLineNumbersbooleanNofalsedecorative (aria-hidden, not selectable, never copied)
copyablebooleanNotruekit Button sm: tertiary in the header, secondary in its own column beside the code when there is no header, so text never scrolls under it. Copy -> Copied for 1.2s
wrapbooleanNofalsefalse scrolls long lines horizontally
maxHeightstring | numberNo—scrolls inside past this height
  • Component
    →

    Textarea

    Multi-line text entry for notes and descriptions.

  • Component
    →

    DoneCheck

    After an agent says it is finished: each agreed "done when" check marked met, not met, unverified or checking, with its proof.

  • Component
    →

    GuardrailLog

    Every time a policy rule stepped in on an agent by itself: blocked, rewritten or flagged, what the agent tried, newest first.

  • Block
    →

    Eval suite

    Test a candidate agent version against the baseline, case by case, before you ship it.