# Snippet

A command or one line of code to copy, with package manager tabs that every snippet on the page follows.

Example: Snippet. Pick a package manager in either snippet and both follow. Preview: https://chatcn.dev/preview/snippet-demo

## Installation

```bash
npx shadcn@latest add https://chatcn.dev/r/snippet.json
```

## Usage

```tsx
import { Snippet, packageManagerTabs } from "@/components/ui/chatcn/snippet"

<Snippet tabs={packageManagerTabs("ai @ai-sdk/react zod")} />
<Snippet code="npx shadcn@latest add @chatcn/tool-call" />
```

With tabs it holds the same command for several package managers; the choice is remembered under `storageKey` and every snippet with that key follows it, on this page and the next. Long lines scroll sideways inside the snippet with a soft edge rather than wrapping, so what you copy is what you see. `packageManagerTabs` builds the pnpm, npm, yarn and bun versions of an install. Replaces AI Elements' Snippet.

## Examples

### States

Example: States. One line, a long line that scrolls, and no prompt glyph. Preview: https://chatcn.dev/preview/snippet-states

## Props

| Prop | Type | Notes |
| --- | --- | --- |
| `code` | `string` | One line |
| `tabs` | `SnippetTab[]` | `{ label, code }` |
| `prefix` | `ReactNode` | Default `$`; `null` for none |
| `storageKey` | `string | null` | Where the tab choice is kept. `null` turns it off |
| `copyLabel`, `onCopyCode` | `string`, `(code) => void` |  |

## Keyboard

| Key | Action |
| --- | --- |
| Left, Right on the tabs | Switch package manager |

## Accessibility

- Tabs follow the tabs pattern with one tab stop and arrow keys.
- The prompt glyph is hidden from screen readers and from the copied text.

Source: https://chatcn.dev/docs/components/snippet