A code block highlighted with Shiki that can type its code out, character by character.
Installation
The code is highlighted with Shiki, a peer dependency of this primitive, loaded on demand:
Usage
<script setup lang="ts">
import { CodeBlock } from '@animate-ui/vue/primitives/code-block';
</script>
<template>
<CodeBlock
code="console.log('Hello, world!');"
lang="javascript"
writing
:duration="2000"
/>
</template>The block renders Shiki's <pre> inside a div. Style it through that div ([&_pre]:…, [&_.line]:…); data-writing and data-done let you show a cursor while it types.
Ready-made block
A styled block around the primitive, with a cursor while it types and a copy button, is a file you add to your project and own:
<script setup lang="ts">
import CodeBlock from '@/components/animate-ui/components/code-block/CodeBlock.vue';
</script>
<template>
<CodeBlock
code="console.log('Hello, world!');"
lang="javascript"
writing
:duration="2000"
/>
</template>It takes every prop of the primitive, plus:
| Prop | Type | Default |
|---|---|---|
cursor? | boolean | true |
copy-button? | boolean | true |
scroll-class? | string | - |
Animation
- With
writing, the code appears one character at a time, evenly overduration(afterdelay), and the block andscroll-container(its parent element by default) scroll smoothly to follow the last line. - With
in-view, writing starts when the block scrolls into view. - Under reduced motion, the whole code is shown at once (
doneis still emitted).
API Reference
CodeBlock
| Prop | Type | Default |
|---|---|---|
code | string | - |
lang | string | - |
theme? | "light" | "dark" | "light" |
themes? | { light: string; dark: string } | { light: "github-light", dark: "github-dark" } |
writing? | boolean | false |
duration? | number | 5000 |
delay? | number | 0 |
scroll-container? | HTMLElement | null | - |
in-view? | boolean | false |
in-view-margin? | string | "0px" |
in-view-once? | boolean | true |
...props? | HTMLAttributes | - |
It emits done once the whole code is shown, and write ({ index, length, done }) on each written character, and once done.