Snippet

Lightweight inline code display for terminal commands and short code references.

The Snippet component provides a lightweight way to display terminal commands and short code snippets with copy functionality. Built on top of InputGroup, it's designed for brief code references in text.

See scripts/snippet.tsx for this example.

Installation

npx ai-elements@latest add snippet

Features

  • Composable architecture with InputGroup
  • Optional prefix text (e.g., $ for terminal commands)
  • Built-in copy button
  • Compact design for chat/markdown

Examples

Without Prefix

See scripts/snippet-plain.tsx for this example.

Props

<Snippet />

PropTypeDefaultDescription
codestringRequiredThe code content to display.
childrenReact.ReactNode-Child elements like SnippetAddon, SnippetInput, etc.
...propsReact.ComponentProps<typeof InputGroup>-Spread to the InputGroup component.

<SnippetAddon />

PropTypeDefaultDescription
...propsReact.ComponentProps<typeof InputGroupAddon>-Spread to the InputGroupAddon component.

<SnippetText />

PropTypeDefaultDescription
...propsReact.ComponentProps<typeof InputGroupText>-Spread to the InputGroupText component.

<SnippetInput />

PropTypeDefaultDescription
...propsOmit<React.ComponentProps<typeof InputGroupInput>,-Spread to the InputGroupInput component. Value and readOnly are set automatically.

<SnippetCopyButton />

PropTypeDefaultDescription
onCopy() => void-Callback fired after a successful copy.
onError(error: Error) => void-Callback fired if copying fails.
timeoutnumber2000How long to show the copied state (ms).
childrenReact.ReactNode-Custom button content.
...propsReact.ComponentProps<typeof InputGroupButton>-Spread to the InputGroupButton component.