Code Block

shadcn-vue's code block, animated. The code types itself out with a cursor at its end, the block follows the last line, and the copy button's icon swaps.

<script setup lang="ts">
import { Download01Icon, FileCodeIcon, TextWrapIcon } from '@hugeicons/core-free-icons'
import { HugeiconsIcon } from '@hugeicons/vue'
import { useCodeWriting } from '@animate-ui/vue/primitives/code-block'
import { ref } from 'vue'
import { Button } from '@/components/ui/button'
import {
  CodeBlock,
  CodeBlockActions,
  CodeBlockContent,
  CodeBlockCopyButton,
  CodeBlockHeader,
  CodeBlockLine,
  CodeBlockTitle,
  CodeBlockToken,
  useHighlightedCode,
} from './code-block'

const props = withDefaults(
  defineProps<{
    delay?: number | string
    duration?: number | string
    writing?: boolean | string
    cursor?: boolean | string
    cadence?: 'natural' | 'even'
  }>(),
  { delay: 0, duration: 6000, writing: true, cursor: true, cadence: 'even' },
)

const code = `import { streamText } from "ai"

export async function POST(request: Request) {
  const { messages } = await request.json()

  const result = streamText({
    model: "anthropic/claude-sonnet-4.5",
    system: "You are a helpful assistant. Answer in one short paragraph.",
    messages,
  })

  return result.toUIMessageStreamResponse()
}`

// The block keeps its full height while the code is written.
const LINES = code.split('\n').length

function download(value: string, filename: string) {
  const url = URL.createObjectURL(new Blob([value], { type: 'text/plain' }))
  const link = document.createElement('a')
  link.href = url
  link.download = filename
  link.click()
  URL.revokeObjectURL(url)
}

const wrap = ref(false)
const written = useCodeWriting(code, () => ({
  writing: String(props.writing) === 'true',
  delay: Number(props.delay),
  duration: Number(props.duration),
  cadence: props.cadence,
  inView: true,
}))
const lines = useHighlightedCode(written.code, 'ts')
</script>

<template>
  <div class="w-full max-w-lg">
    <CodeBlock :ref="written.ref">
      <CodeBlockHeader>
        <CodeBlockTitle>
          <HugeiconsIcon :icon="FileCodeIcon" :stroke-width="2" />
          app/api/chat/route.ts
        </CodeBlockTitle>
        <CodeBlockActions>
          <Button
            variant="ghost"
            size="icon-xs"
            aria-label="Wrap lines"
            :aria-pressed="wrap"
            class="aria-pressed:text-foreground"
            @click="wrap = !wrap"
          >
            <HugeiconsIcon :icon="TextWrapIcon" :stroke-width="2" />
          </Button>
          <Button
            variant="ghost"
            size="icon-xs"
            aria-label="Download"
            @click="download(code, 'route.ts')"
          >
            <HugeiconsIcon :icon="Download01Icon" :stroke-width="2" />
          </Button>
          <CodeBlockCopyButton :value="code" />
        </CodeBlockActions>
      </CodeBlockHeader>
      <CodeBlockContent
        :wrap="wrap"
        line-numbers
        :writing="written.writing.value"
        :cursor="String(cursor) === 'true'"
        :style="{ minHeight: `calc(${LINES}lh + 1.5rem)` }"
      >
        <CodeBlockLine
          v-for="(line, index) in lines"
          :key="index"
          :variant="index >= 5 && index <= 9 ? 'highlighted' : 'default'"
        >
          <CodeBlockToken v-for="(token, i) in line" :key="i" :token="token" />
        </CodeBlockLine>
      </CodeBlockContent>
    </CodeBlock>
  </div>
</template>

Installation

Install the packages

npm install @animate-ui/vue motion-v
pnpm add @animate-ui/vue motion-v
yarn add @animate-ui/vue motion-v
bun add @animate-ui/vue motion-v

Add the shadcn-vue component

npx shadcn-vue@latest add code-block
pnpm dlx shadcn-vue@latest add code-block
yarn dlx shadcn-vue@latest add code-block
bun x shadcn-vue@latest add code-block

Animate it

The CLI finds your shadcn-vue components through components.json and makes the edits the Manual tab lists for you.

npx @animate-ui/cli@latest animate code-block
pnpm dlx @animate-ui/cli@latest animate code-block
yarn dlx @animate-ui/cli@latest animate code-block
bun x @animate-ui/cli@latest animate code-block

shadcn-vue's Code Block isn't built on a primitive it could swap: animate-ui animate code-block rewrites it in place instead. In CodeBlockContent.vue the outer Primitive becomes the code-block primitive's Content, and CodeBlockCopyButton.vue is built on the Copy Button. A file you changed is left as it is, and the CLI points here: the Manual tab lists the edits.

Usage

<script setup lang="ts">
import { useCodeWriting } from '@animate-ui/vue/primitives/code-block';
import {
  CodeBlock,
  CodeBlockActions,
  CodeBlockContent,
  CodeBlockCopyButton,
  CodeBlockHeader,
  CodeBlockLine,
  CodeBlockTitle,
  CodeBlockToken,
  useHighlightedCode,
} from '@/components/ui/code-block';

const props = defineProps<{ code: string }>();

const written = useCodeWriting(
  () => props.code,
  () => ({
    duration: 4000,
    inView: true,
  }),
);
const lines = useHighlightedCode(written.code, 'ts');
</script>

<template>
  <CodeBlock :ref="written.ref">
    <CodeBlockHeader>
      <CodeBlockTitle>route.ts</CodeBlockTitle>
      <CodeBlockActions>
        <CodeBlockCopyButton :value="code" />
      </CodeBlockActions>
    </CodeBlockHeader>
    <CodeBlockContent :writing="written.writing.value" line-numbers>
      <CodeBlockLine v-for="(line, index) in lines" :key="index">
        <CodeBlockToken v-for="(token, i) in line" :key="i" :token="token" />
      </CodeBlockLine>
    </CodeBlockContent>
  </CodeBlock>
</template>

Everything else is shadcn-vue's: the composition, plain text content, line numbers, wrapping, diff and highlighted lines, the small size and the destructive variant. Without useCodeWriting, the block shows its code at once.

useCodeWriting types the code out and returns what is written so far: highlight that, and give writing to CodeBlockContent for the cursor and the scroll. Its ref goes on the element (or component) whose entering the viewport starts the writing (with inView). useHighlightedCode (shadcn-vue's) returns the tokens of each line, rendered with CodeBlockToken. The copy button copies the whole value, even while the code is being written.

Animation

  • With useCodeWriting, the code appears one character at a time, evenly over duration (after delay). With cadence: 'natural', it types like someone typing instead: quicker inside a word, pausing after punctuation and at the end of a line, the next line's indentation coming at once. Each new line is highlighted as it comes: useHighlightedCode tokenizes only the lines that are new.
  • While it writes, CodeBlockContent shows a cursor (|) at the end of the last line (:cursor="false" hides it) and scrolls smoothly to follow the last line.
  • The last characters written fade in behind the cursor, on CodeBlockLines (plain text content gets the cursor only): transparent right behind it, opaque again 4 characters back. When the writing pauses (450ms without a new character) or moves to a new line, the fade slides off the end in 250ms (ease-out cubic-bezier(0.23, 1, 0.32, 1)), and the cursor pulses until it goes on.
  • With inView, writing starts when the block scrolls into view.
  • The copy button's icon shrinks into a blur as the check grows out of it when the code is copied, and swaps back after 2 seconds (Copy Button); the button scales to 0.97 while pressed.
  • Wrapping on or off (:wrap), the lines glide to their new place and the block to its new height, in 300ms with the same ease-out.
  • Under reduced motion, the whole code is shown at once (onDone is still called), so there is no cursor or fade; wrapping changes at once; the copy icons crossfade through their blur without scaling, and the button doesn't scale.

API Reference

Every part takes shadcn-vue's props. Animate UI adds, through the primitive's Content:

CodeBlockContent

PropTypeDefault
writing?
booleanfalse
cursor?
booleantrue

useCodeWriting

useCodeWriting(code, options) takes the code (a value, ref or getter) and a getter of options, and returns { ref, element, code, writing, done, isInView }: code is what is written so far (a ref), writing is true until the whole code is shown.

PropTypeDefault
writing?
booleantrue
duration?
number5000
delay?
number0
cadence?
'even' | 'natural''even'
onDone?
() => void-
onWrite?
(info: { index: number; length: number; done: boolean }) => void-
inView?
booleanfalse
inViewMargin?
string"0px"
inViewOnce?
booleantrue

Primitive

@animate-ui/vue/primitives/code-block also exports the CodeBlock primitive: one element that highlights a whole code with Shiki's codeToHtml (a peer dependency, loaded on demand) and types it out.

<CodeBlock code="console.log('Hello, world!');" lang="javascript" writing />

CodeBlock (primitive)

PropTypeDefault
code
string-
lang
string-
theme?
"light" | "dark""light"
themes?
{ light: string; dark: string }{ light: "github-light", dark: "github-dark" }
writing?
booleanfalse
duration?
number5000
delay?
number0
scroll-container?
HTMLElement | null-
in-view?
booleanfalse
in-view-margin?
string"0px"
in-view-once?
booleantrue
...props?
HTMLAttributes-

It emits done once the whole code is shown, and write ({ index, length, done }) on each written character, and once done.

Built by Skyleen. The source code is available on GitHub.

Last updated: 10/27/2026