# Snippet

Shows a command or value with a copy button.

[View source and API](https://github.com/tempoxyz/ds/blob/main/src/web/components/Snippet.tsx)

Figma: [Content/Code Snippet](https://www.figma.com/design/yE96kcymnNR88PJp6G1sPs?node-id=5227-3317)

## Overview

Snippet renders a command or value in a `<code>` element, with a copy button in the corner. A click anywhere on the command copies it.

## Examples

### Basic

```tsx
import { Snippet, style } from '@tempoxyz/ds'

export const fill = true
export function Demo() {
  return <Snippet {...styles.snippet()}>pnpm add @tempoxyz/ds</Snippet>
}

namespace styles {
  export const snippet = style({ maxWidth: '28rem !custom' })
}
```

[View example source](https://github.com/tempoxyz/ds/blob/main/docs/src/examples/demos/Snippet/Basic.tsx)

### Wrap

The first word, the program, renders in the primary color. Long commands wrap, and wrapped lines align with the arguments.

```tsx
import { Snippet, style } from '@tempoxyz/ds'

export const fill = true
export function Demo() {
  return (
    <Snippet {...styles.snippet()}>
      claude "Read https://tempo.xyz/SKILL.md and connect to my Tempo wallet"
    </Snippet>
  )
}

namespace styles {
  export const snippet = style({ maxWidth: '24.75rem !custom' })
}
```

[View example source](https://github.com/tempoxyz/ds/blob/main/docs/src/examples/demos/Snippet/Wrap.tsx)

### Prompt

Set `prompt` to a glyph, or to `false` to hide it. It defaults to `$`.

```tsx
import { Snippet, style } from '@tempoxyz/ds'

export const fill = true
export function Demo() {
  return (
    <Snippet prompt={false} {...styles.snippet()}>
      https://ds.tempo.xyz
    </Snippet>
  )
}

namespace styles {
  export const snippet = style({ maxWidth: '28rem !custom' })
}
```

[View example source](https://github.com/tempoxyz/ds/blob/main/docs/src/examples/demos/Snippet/Prompt.tsx)

### Tabs

Pass a [Tabs](/docs/components/tabs) list as `tabs` to switch between commands. Wrap the snippet in `Tabs.Root`, and pass the command of the active tab as `children`.

```tsx
import { Snippet, style, Tabs } from '@tempoxyz/ds'
import { useState } from 'react'

export const fill = true
const commands = {
  bun: 'bun add @tempoxyz/ds zyzz',
  npm: 'npm install @tempoxyz/ds zyzz',
  pnpm: 'pnpm add @tempoxyz/ds zyzz',
} as const

const managers = ['npm', 'pnpm', 'bun'] as const

export function Demo() {
  const [manager, setManager] = useState<(typeof managers)[number]>('npm')

  return (
    <Tabs.Root
      onValueChange={(value) => {
        const entry = managers.find((item) => item === value)
        if (entry) setManager(entry)
      }}
      value={manager}
    >
      <Snippet
        labels={{ copy: 'Copy install command' }}
        tabs={
          <Tabs.List aria-label="Package manager">
            {managers.map((entry) => (
              <Tabs.Tab key={entry} value={entry}>
                {entry}
              </Tabs.Tab>
            ))}
            <Tabs.Indicator />
          </Tabs.List>
        }
        {...styles.snippet()}
      >
        {commands[manager]}
      </Snippet>
    </Tabs.Root>
  )
}

namespace styles {
  export const snippet = style({ maxWidth: '28rem !custom' })
}
```

[View example source](https://github.com/tempoxyz/ds/blob/main/docs/src/examples/demos/Snippet/Tabs.tsx)

## Anatomy

Import the component and assemble its parts.

```tsx
import { Snippet, Tabs } from '@tempoxyz/ds'

<Tabs.Root>
  <Snippet
    tabs={
      <Tabs.List>
        <Tabs.Tab />
        <Tabs.Indicator />
      </Tabs.List>
    }
  />
</Tabs.Root>
```

## Guidelines

### When to Use

* Show a command, such as an install command or a `curl` request.
* Show a value to paste elsewhere, such as a webhook URL or an RPC endpoint, with `prompt={false}`.
* Show one command per tool, such as an agent or a package manager, with `tabs`.

### When Not to Use

* Use [SecretValue](/docs/components/secret-value) for API keys and other secrets, which stay masked.
* Use [Hex](/docs/components/hex) with `copy` for addresses and hashes, which truncates the middle to fit.
* Use [CopyButton](/docs/components/copy-button) to add copying beside your own text.
* Use [Tabs](/docs/components/tabs) with panels to switch content other than commands.

### Copy Exactly What You Show

Snippet copies its `children`, never the prompt.

**Do:** Pass only the command, so the pasted text runs as is.

```tsx
/**
 * Do example for "Copy Exactly What You Show" on the Snippet page.
 * Pass only the command, so the pasted text runs as is.
 * @module
 */
import { Snippet, style } from '@tempoxyz/ds'

/** Renders the static preview, which the guideline card shows inert. */
export function Demo() {
  return <Snippet {...styles.snippet()}>pnpm add @tempoxyz/ds</Snippet>
}

namespace styles {
  export const snippet = style({ width: '100% !custom' })
}
```

**Don't:** Type a leading `$` into the command.

```tsx
/**
 * Don't example for "Copy Exactly What You Show" on the Snippet page.
 * Don't type a leading `$` into the command.
 * @module
 */
import { Snippet, style } from '@tempoxyz/ds'

/** Renders the static preview, which the guideline card shows inert. */
export function Demo() {
  return <Snippet {...styles.snippet()}>$ pnpm add @tempoxyz/ds</Snippet>
}

namespace styles {
  export const snippet = style({ width: '100% !custom' })
}
```

### Show the Prompt Only for Shell Commands

The `$` marks text to run in a terminal.

**Do:** Set `prompt={false}` for a URL or other value.

```tsx
/**
 * Do example for "Show the Prompt Only for Shell Commands" on the Snippet page.
 * Set `prompt={false}` for a URL or other value.
 * @module
 */
import { Snippet, style } from '@tempoxyz/ds'

/** Renders the static preview, which the guideline card shows inert. */
export function Demo() {
  return (
    <Snippet prompt={false} {...styles.snippet()}>
      https://example.com/webhooks/tempo
    </Snippet>
  )
}

namespace styles {
  export const snippet = style({ width: '100% !custom' })
}
```

**Don't:** Show a shell prompt before a value that isn't a command.

```tsx
/**
 * Don't example for "Show the Prompt Only for Shell Commands" on the Snippet page.
 * Don't show a shell prompt before a value that isn't a command.
 * @module
 */
import { Snippet, style } from '@tempoxyz/ds'

/** Renders the static preview, which the guideline card shows inert. */
export function Demo() {
  return <Snippet {...styles.snippet()}>https://example.com/webhooks/tempo</Snippet>
}

namespace styles {
  export const snippet = style({ width: '100% !custom' })
}
```

### Keep One Command per Snippet

Long commands wrap. Use a code block for multi-line scripts and config files.

**Do:** Split a multi-step setup into one snippet per command.

```tsx
/**
 * Do example for "Keep One Command per Snippet" on the Snippet page.
 * Split a multi-step setup into one snippet per command.
 * @module
 */
import { Snippet, style } from '@tempoxyz/ds'

/** Renders the static preview, which the guideline card shows inert. */
export function Demo() {
  return (
    <div {...styles.stack()}>
      <Snippet>pnpm add @tempoxyz/ds</Snippet>
      <Snippet>pnpm build</Snippet>
      <Snippet>pnpm dev</Snippet>
    </div>
  )
}

namespace styles {
  export const stack = style({ display: 'grid', gap: '8', width: '100% !custom' })
}
```

**Don't:** Chain several commands into one snippet.

```tsx
/**
 * Don't example for "Keep One Command per Snippet" on the Snippet page.
 * Don't chain several commands into one snippet.
 * @module
 */
import { Snippet, style } from '@tempoxyz/ds'

/** Renders the static preview, which the guideline card shows inert. */
export function Demo() {
  return <Snippet {...styles.snippet()}>pnpm add @tempoxyz/ds && pnpm build && pnpm dev</Snippet>
}

namespace styles {
  export const snippet = style({ width: '100% !custom' })
}
```

### Switch Alternatives with Tabs

If the command differs per tool, such as a package manager, show one snippet with a tab per tool.

**Do:** Show one command per package manager in one snippet with tabs.

```tsx
/**
 * Do example for "Switch Alternatives with Tabs" on the Snippet page.
 * Show one command per package manager in one snippet with tabs.
 * @module
 */
import { Snippet, style, Tabs } from '@tempoxyz/ds'

/** Renders the static preview, which the guideline card shows inert. */
export function Demo() {
  return (
    <Tabs.Root defaultValue="npm" {...styles.root()}>
      <Snippet
        tabs={
          <Tabs.List aria-label="Package manager">
            <Tabs.Tab value="npm">npm</Tabs.Tab>
            <Tabs.Tab value="pnpm">pnpm</Tabs.Tab>
            <Tabs.Tab value="bun">bun</Tabs.Tab>
            <Tabs.Indicator />
          </Tabs.List>
        }
      >
        npm install @tempoxyz/ds
      </Snippet>
    </Tabs.Root>
  )
}

namespace styles {
  export const root = style({ width: '100% !custom' })
}
```

**Don't:** Stack one snippet per package manager.

```tsx
/**
 * Don't example for "Switch Alternatives with Tabs" on the Snippet page.
 * Don't stack one snippet per package manager.
 * @module
 */
import { Snippet, style } from '@tempoxyz/ds'

/** Renders the static preview, which the guideline card shows inert. */
export function Demo() {
  return (
    <div {...styles.stack()}>
      <Snippet>npm install @tempoxyz/ds</Snippet>
      <Snippet>pnpm add @tempoxyz/ds</Snippet>
      <Snippet>bun add @tempoxyz/ds</Snippet>
    </div>
  )
}

namespace styles {
  export const stack = style({ display: 'grid', gap: '8', width: '100% !custom' })
}
```

## Content

* Write commands that run unchanged. If a value differs per person, read it from an environment variable, such as `$TEMPO_API_KEY`, not a placeholder.
* Never put a real secret in a snippet.
* Introduce each snippet with a sentence that states what it does and where to run it.

## Accessibility

The command renders in a `<code>` element. The copy button follows the [WAI-ARIA button pattern](https://www.w3.org/WAI/ARIA/apg/patterns/button/), and the tabs follow the [WAI-ARIA tabs pattern](https://www.w3.org/WAI/ARIA/apg/patterns/tabs/).

### Semantics

* **Command (code):** Wraps to its width, so the whole command stays visible without scrolling.
* **Prompt (aria-hidden):** Hidden from screen readers, and never selected or copied with the command.
* **Copy Button (button):** Named "Copy" unless you pass `labels.copy`. Reads "Copied" for two seconds, and a hidden live region announces each copy.
* **Tabs (tablist):** Switches the command in place. A click on a tab never copies, and the list needs its own name.

### Keyboard

* **Move Through the Snippet (Tab):** Moves focus to the active tab, if any, then to the copy button.
* **Switch the Command (Arrow keys):** Moves between tabs. Enter or Space shows the command of the focused tab.
* **Copy the Command (Enter or Space):** Copies the command from the copy button. Focus stays on the button.

A click anywhere on the command also copies, unless your `onClick` calls `preventDefault()`. The button is the keyboard path.

### Your Responsibilities

* **Label Each Snippet (labels.copy):** If a page shows several snippets, put a visible heading before each, and name each copy button, such as "Copy install command".
* **Name the Tabs (SC 4.1.2):** Name the tab list, such as "Package manager", and pass the command of the active tab as the children.

## Related

[CopyButton](/docs/components/copy-button)

Use for copying beside your own layout.

[SecretValue](/docs/components/secret-value)

Use for secrets.

[Hex](/docs/components/hex)

Use for addresses and hashes.

[Kbd](/docs/components/kbd)

Use for keyboard shortcuts.

[Tabs](/docs/components/tabs)

Use for switching between related views.
