On this page

Code block

Live examples of Code block, with the exact Vue file that renders each one.

Every example is a complete Vue file. Copy it into an app that has the registry installed, or open it in StackBlitz. Tab into a block to scroll it with the arrow keys.

Highlighted, with a title

A webhook payload with line numbers and a copy button in the header. Highlighting loads in the browser; until then the plain lines show.

order-created.json
{  "event": "order.created",  "orderId": "SO-10482",  "total": 1249000,  "currency": "VND",  "paid": true,  "items": [    { "sku": "TSHIRT-BLK-M", "quantity": 2 }  ]}
CodeBlockHighlighted.vue
<script setup lang="ts">
import { FileJson } from '@lucide/vue'
import {
  CodeBlock,
  CodeBlockActions,
  CodeBlockContent,
  CodeBlockCopyButton,
  CodeBlockHeader,
  CodeBlockLine,
  CodeBlockTitle,
  CodeBlockToken,
  useHighlightedCode,
} from '@/components/ui/code-block'

// The payload an online store sends when an order is created. Colours come from the editor.syntax roles.
const code = `{
  "event": "order.created",
  "orderId": "SO-10482",
  "total": 1249000,
  "currency": "VND",
  "paid": true,
  "items": [
    { "sku": "TSHIRT-BLK-M", "quantity": 2 }
  ]
}`
const lines = useHighlightedCode(code, 'json')
</script>

<template>
  <CodeBlock class="max-w-xl">
    <CodeBlockHeader>
      <CodeBlockTitle><FileJson />order-created.json</CodeBlockTitle>
      <CodeBlockActions>
        <CodeBlockCopyButton :value="code" label="Copy payload" />
      </CodeBlockActions>
    </CodeBlockHeader>
    <CodeBlockContent line-numbers>
      <CodeBlockLine v-for="(line, i) in lines" :key="i">
        <CodeBlockToken v-for="(token, j) in line" :key="j" :token="token" />
      </CodeBlockLine>
    </CodeBlockContent>
  </CodeBlock>
</template>

Changes to a mapping

Added and removed lines, each with its sign. The copy button floats in the corner and shows on hover or focus.

mapping: order-to-sales-order  customer: billing.email  customer: customer.id  warehouse: WH-HCM  tax: line.tax_amount  tax: line.tax_lines[0].amount
CodeBlockDiff.vue
<script setup lang="ts">
import {
  CodeBlock,
  CodeBlockActions,
  CodeBlockContent,
  CodeBlockCopyButton,
  CodeBlockLine,
} from '@/components/ui/code-block'

// A change to a field mapping between the online store and the ERP. Each line is added, removed or unchanged;
// the + and − signs come with the line variant. The copy button floats in the corner and shows on hover or focus.
const lines = [
  { text: 'mapping: order-to-sales-order', variant: 'default' },
  { text: '  customer: billing.email', variant: 'removed' },
  { text: '  customer: customer.id', variant: 'added' },
  { text: '  warehouse: WH-HCM', variant: 'default' },
  { text: '  tax: line.tax_amount', variant: 'removed' },
  { text: '  tax: line.tax_lines[0].amount', variant: 'added' },
] as const
const after = lines.filter(l => l.variant !== 'removed').map(l => l.text).join('\n')
</script>

<template>
  <CodeBlock class="max-w-xl">
    <CodeBlockActions>
      <CodeBlockCopyButton :value="after" label="Copy new mapping" />
    </CodeBlockActions>
    <CodeBlockContent line-numbers>
      <CodeBlockLine v-for="(line, i) in lines" :key="i" :variant="line.variant">
        {{ line.text }}
      </CodeBlockLine>
    </CodeBlockContent>
  </CodeBlock>
</template>

An error response

The destructive variant, small, with wrapped lines and a footer. The title says what failed.

Push to ERP failed for 2 orders
POST /api/sales-orders 422 Unprocessable Entity
SO-10482: customer "C-2291" does not exist in company "Main"
SO-10487: item "TSHIRT-BLK-XXL" is blocked for sale in warehouse "WH-HCM"
Run 4821 · 12 Nov, 09:12
CodeBlockError.vue
<script setup lang="ts">
import { CircleAlert } from '@lucide/vue'
import {
  CodeBlock,
  CodeBlockContent,
  CodeBlockFooter,
  CodeBlockHeader,
  CodeBlockTitle,
} from '@/components/ui/code-block'

// The ERP's answer to a failed push, shown as it came back. size="sm" fits it into a side panel;
// wrap keeps long lines on screen. The title says it failed in words, not only in colour.
const log = `POST /api/sales-orders 422 Unprocessable Entity
SO-10482: customer "C-2291" does not exist in company "Main"
SO-10487: item "TSHIRT-BLK-XXL" is blocked for sale in warehouse "WH-HCM"`
</script>

<template>
  <CodeBlock variant="destructive" size="sm" class="max-w-md">
    <CodeBlockHeader>
      <CodeBlockTitle><CircleAlert />Push to ERP failed for 2 orders</CodeBlockTitle>
    </CodeBlockHeader>
    <CodeBlockContent wrap>{{ log }}</CodeBlockContent>
    <CodeBlockFooter>Run 4821 · 12 Nov, 09:12</CodeBlockFooter>
  </CodeBlock>
</template>