|
| 1 | +--- |
| 2 | +title: Code Block |
| 3 | +description: |
| 4 | +icon: lucide:code |
| 5 | +--- |
| 6 | + |
| 7 | +The `CodeBlock` component provides syntax highlighting and copy to clipboard functionality for code blocks. It uses Shiki for syntax highlighting and includes automatic light/dark theme switching. |
| 8 | + |
| 9 | +## Install using CLI |
| 10 | + |
| 11 | +:::tabs{variant="card"} |
| 12 | + ::div{label="ai-elements-vue"} |
| 13 | + ```sh |
| 14 | + npx ai-elements-vue@latest add code-block |
| 15 | + ``` |
| 16 | + :: |
| 17 | + ::div{label="shadcn-vue"} |
| 18 | + |
| 19 | + ```sh |
| 20 | + npx shadcn-vue@latest add https://registry.ai-elements-vue.com/code-block.json |
| 21 | + ``` |
| 22 | + :: |
| 23 | +::: |
| 24 | + |
| 25 | +## Install Manually |
| 26 | + |
| 27 | +Copy and paste the following files into the same folder. |
| 28 | + |
| 29 | +:::code-group |
| 30 | + ```vue [CodeBlock.vue] |
| 31 | + <script setup lang="ts"> |
| 32 | + import type { BuiltinTheme, BundledLanguage } from 'shiki' |
| 33 | + import { transformerCopyButton } from '@selemondev/shiki-transformer-copy-button' |
| 34 | + import CodeBlock from 'shiki-block-vue' |
| 35 | +
|
| 36 | + interface Props { |
| 37 | + code: string |
| 38 | + lang: BundledLanguage |
| 39 | + } |
| 40 | +
|
| 41 | + const props = withDefaults(defineProps<Props>(), {}) |
| 42 | +
|
| 43 | + const theme: { light: BuiltinTheme, dark: BuiltinTheme } = { |
| 44 | + light: 'vitesse-light', |
| 45 | + dark: 'vitesse-dark', |
| 46 | + } |
| 47 | + </script> |
| 48 | +
|
| 49 | + <template> |
| 50 | + <CodeBlock |
| 51 | + :lang="props.lang" |
| 52 | + :code="props.code" |
| 53 | + :theme="theme" |
| 54 | + class="overflow-hidden" |
| 55 | + :transformers="[ |
| 56 | + transformerCopyButton({ |
| 57 | + duration: 3000, |
| 58 | + display: 'ready', |
| 59 | + successIcon: `data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' stroke='rgba(128,128,128,1)' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' viewBox='0 0 24 24'%3E%3Crect width='8' height='4' x='8' y='2' rx='1' ry='1'/%3E%3Cpath d='M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2'/%3E%3Cpath d='m9 14 2 2 4-4'/%3E%3C/svg%3E`, |
| 60 | + copyIcon: `data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' stroke='rgba(128,128,128,1)' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' viewBox='0 0 24 24'%3E%3Crect width='8' height='4' x='8' y='2' rx='1' ry='1'/%3E%3Cpath d='M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2'/%3E%3Cpath d='m9 14 2 2 4-4'/%3E%3C/svg%3E`, |
| 61 | + }), |
| 62 | + ]" |
| 63 | + /> |
| 64 | + </template> |
| 65 | +
|
| 66 | + <style scoped> |
| 67 | + /* Dark Mode */ |
| 68 | + @media (prefers-color-scheme: dark) { |
| 69 | + :deep(.shiki), |
| 70 | + :deep(.shiki span) { |
| 71 | + color: var(--shiki-dark) !important; |
| 72 | + background-color: var(--shiki-dark-bg) !important; |
| 73 | + /* Optional, if you also want font styles */ |
| 74 | + font-style: var(--shiki-dark-font-style) !important; |
| 75 | + font-weight: var(--shiki-dark-font-weight) !important; |
| 76 | + text-decoration: var(--shiki-dark-text-decoration) !important; |
| 77 | + } |
| 78 | + } |
| 79 | +
|
| 80 | + html.dark :deep(.shiki), |
| 81 | + html.dark :deep(.shiki span) { |
| 82 | + color: var(--shiki-dark) !important; |
| 83 | + background-color: var(--shiki-dark-bg) !important; |
| 84 | + font-style: var(--shiki-dark-font-style) !important; |
| 85 | + font-weight: var(--shiki-dark-font-weight) !important; |
| 86 | + text-decoration: var(--shiki-dark-text-decoration) !important; |
| 87 | + } |
| 88 | +
|
| 89 | + :deep(.shiki--code--block) { |
| 90 | + width: 100%; |
| 91 | + } |
| 92 | +
|
| 93 | + :deep(pre) { |
| 94 | + z-index: 1; |
| 95 | + padding: 24px; |
| 96 | + border-radius: 10px; |
| 97 | + overflow-x: auto; |
| 98 | + -ms-overflow-style: none; |
| 99 | + scrollbar-width: none; |
| 100 | + position: relative; |
| 101 | + background-color: #F9F9F9 !important; |
| 102 | + } |
| 103 | +
|
| 104 | + :deep(code) { |
| 105 | + display: block; |
| 106 | + line-height: 1.7; |
| 107 | + font-size: 15px; |
| 108 | + } |
| 109 | + </style> |
| 110 | + ``` |
| 111 | + |
| 112 | + ```ts [index.ts] |
| 113 | + export { default as CodeBlock } from './CodeBlock.vue' |
| 114 | + ``` |
| 115 | +::: |
| 116 | + |
| 117 | +## Usage |
| 118 | + |
| 119 | +```vue |
| 120 | +<script setup lang="ts"> |
| 121 | +import { CodeBlock } from '@/components/ai-elements/code-block' |
| 122 | +
|
| 123 | +const code = `<template> |
| 124 | + <div> |
| 125 | + <h1>Hello, {{ name }}!</h1> |
| 126 | + <p>This is an example Vue component.</p> |
| 127 | + </div> |
| 128 | +</template> |
| 129 | +
|
| 130 | +<script setup lang="ts"> |
| 131 | +interface Props { |
| 132 | + name: string |
| 133 | +} |
| 134 | +
|
| 135 | +defineProps<Props>() |
| 136 | +<\/script>` |
| 137 | +</script> |
| 138 | +
|
| 139 | +<template> |
| 140 | + <CodeBlock :code="code" lang="vue" /> |
| 141 | +</template> |
| 142 | +``` |
| 143 | + |
| 144 | +## Features |
| 145 | + |
| 146 | +- Syntax highlighting with Shiki |
| 147 | +- Copy to clipboard functionality |
| 148 | +- Automatic light/dark theme switching |
| 149 | +- Support for all Shiki bundled languages |
| 150 | +- Customizable themes (vitesse-light/vitesse-dark) |
| 151 | +- Responsive design with horizontal scrolling |
| 152 | +- Clean, modern styling |
| 153 | +- Accessible design |
| 154 | + |
| 155 | +## Props |
| 156 | + |
| 157 | +### `<CodeBlock />` |
| 158 | + |
| 159 | +:::field-group |
| 160 | + ::field{name="code" type="string" required} |
| 161 | + The code content to display. |
| 162 | + :: |
| 163 | + |
| 164 | + ::field{name="lang" type="BundledLanguage" required} |
| 165 | + The programming language for syntax highlighting. Supports all Shiki bundled languages. |
| 166 | + :: |
| 167 | +::: |
0 commit comments