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.

Install using CLI

AI Elements Vue
shadcn-vue CLI
npx ai-elements-vue@latest add snippet

Install Manually

Copy and paste the following files into the same folder.

Snippet.vue
SnippetAddon.vue
SnippetText.vue
SnippetInput.vue
SnippetCopyButton.vue
context.ts
index.ts
<script setup lang="ts">
import type { HTMLAttributes } from 'vue'
import { InputGroup } from '@repo/shadcn-vue/components/ui/input-group'
import { cn } from '@repo/shadcn-vue/lib/utils'
import { computed, provide } from 'vue'
import { SnippetKey } from './context'

type InputGroupProps = InstanceType<typeof InputGroup>['$props']

interface Props extends /* @vue-ignore */ InputGroupProps {
  code: string
  class?: HTMLAttributes['class']
}

const props = defineProps<Props>()

provide(SnippetKey, {
  code: computed(() => props.code),
})
</script>

<template>
  <InputGroup
    :class="cn('font-mono', props.class)"
    v-bind="$attrs"
  >
    <slot />
  </InputGroup>
</template>

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

Props

<Snippet />

coderequiredstring
The code content to display.
[...props]InputGroupProps
Additional props forwarded to the underlying shadcn-vue/ui InputGroup component.

<SnippetAddon />

[...props]InputGroupAddonProps
Additional props forwarded to the underlying shadcn-vue/ui InputGroupAddon component.

<SnippetText />

[...props]InputGroupTextProps
Additional props forwarded to the underlying shadcn-vue/ui InputGroupText component.

<SnippetInput />

[...props]InputGroupInputProps
Additional props forwarded to the underlying shadcn-vue/ui InputGroupInput component. Value and readOnly are set automatically.

<SnippetCopyButton />

timeoutnumber
How long to show the copied state (ms).
@copy() => void
Callback fired after a successful copy.
@error(error: Error) => void
Callback fired if copying fails.
[...props]InputGroupButtonProps
Additional props forwarded to the underlying shadcn-vue/ui InputGroupButton component.