|
| 1 | +--- |
| 2 | +title: Code Blocks |
| 3 | +description: Display inline code and code blocks |
| 4 | +--- |
| 5 | + |
| 6 | +## Basic |
| 7 | + |
| 8 | +### Inline Code |
| 9 | + |
| 10 | +To display a word or phrase as code, enclose it in backticks (\`). |
| 11 | + |
| 12 | +::code-preview{class="[&>div]:*:my-0"} |
| 13 | + |
| 14 | +`inline code` |
| 15 | + |
| 16 | +#code |
| 17 | + |
| 18 | +```mdc |
| 19 | +`inline code` |
| 20 | +``` |
| 21 | + |
| 22 | +:: |
| 23 | + |
| 24 | +### Code Blocks |
| 25 | + |
| 26 | +To display a block of code, with syntax highlighting, use three backticks (\`\`\`). |
| 27 | + |
| 28 | +::code-preview{class="[&>div]:*:my-0 [&>div]:*:w-full"} |
| 29 | + |
| 30 | +```ts |
| 31 | +export default defineNuxtConfig({ |
| 32 | + modules: ['@nuxt/ui-pro'] |
| 33 | +}) |
| 34 | +``` |
| 35 | + |
| 36 | +#code |
| 37 | + |
| 38 | +````mdc |
| 39 | +```ts |
| 40 | +export default defineNuxtConfig({ |
| 41 | + modules: ['@nuxt/ui-pro'] |
| 42 | +}) |
| 43 | +``` |
| 44 | +```` |
| 45 | + |
| 46 | +:: |
| 47 | + |
| 48 | +When writing a code-block, you can specify a filename that will be displayed on top of the code block. An icon will be automatically displayed based on the extension or the name. |
| 49 | + |
| 50 | +::code-preview{class="[&>div]:*:my-0 [&>div]:*:w-full"} |
| 51 | + |
| 52 | +```ts [nuxt.config.ts] |
| 53 | +export default defineNuxtConfig({ |
| 54 | + modules: ['@nuxt/ui-pro'] |
| 55 | +}) |
| 56 | +``` |
| 57 | + |
| 58 | +#code |
| 59 | + |
| 60 | +````mdc |
| 61 | +```ts [nuxt.config.ts] |
| 62 | +export default defineNuxtConfig({ |
| 63 | + modules: ['@nuxt/ui-pro'] |
| 64 | +}) |
| 65 | +``` |
| 66 | +```` |
| 67 | + |
| 68 | +:: |
| 69 | + |
| 70 | +::tip |
| 71 | +Some icons are already defined by default, but you can add more in your `app.config.ts` through the `uiPro.prose.codeIcon` key: |
| 72 | + |
| 73 | +```ts [app.config.ts] |
| 74 | +export default defineAppConfig({ |
| 75 | + uiPro: { |
| 76 | + prose: { |
| 77 | + codeIcon: { |
| 78 | + terminal: 'i-ph-terminal-window-duotone' |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | +}) |
| 83 | +``` |
| 84 | + |
| 85 | +:: |
| 86 | + |
| 87 | +Every code-block has a built-in copy button that will copy the code to your clipboard. |
| 88 | + |
| 89 | +::tip |
| 90 | +You can change the icon in your `app.config.ts` through the `ui.icons.copy` and `ui.icons.copyCheck` keys: |
| 91 | + |
| 92 | +```ts [app.config.ts] |
| 93 | +export default defineAppConfig({ |
| 94 | + ui: { |
| 95 | + icons: { |
| 96 | + copy: 'i-lucide-copy', |
| 97 | + copyCheck: 'i-lucide-copy-check' |
| 98 | + } |
| 99 | + } |
| 100 | +}) |
| 101 | +``` |
| 102 | + |
| 103 | +:: |
| 104 | + |
| 105 | +## Advanced |
| 106 | + |
| 107 | +### Code Group |
| 108 | + |
| 109 | +Wrap your code-blocks around a `code-group` component to group them together in a tabbed interface. |
| 110 | + |
| 111 | +::code-preview{class="[&>div]:*:my-0 [&>div]:*:w-full"} |
| 112 | + |
| 113 | +:::code-group{class="w-full"} |
| 114 | + |
| 115 | +```bash [pnpm] |
| 116 | +pnpm add @nuxt/ui-pro@next |
| 117 | +``` |
| 118 | + |
| 119 | +```bash [yarn] |
| 120 | +yarn add @nuxt/ui-pro@next |
| 121 | +``` |
| 122 | + |
| 123 | +```bash [npm] |
| 124 | +npm install @nuxt/ui-pro@next |
| 125 | +``` |
| 126 | + |
| 127 | +```bash [bun] |
| 128 | +bun add @nuxt/ui-pro@next |
| 129 | +``` |
| 130 | + |
| 131 | +::: |
| 132 | + |
| 133 | +#code |
| 134 | + |
| 135 | +````mdc |
| 136 | +:::code-group |
| 137 | +
|
| 138 | +```bash [pnpm] |
| 139 | +pnpm add @nuxt/ui-pro@next |
| 140 | +``` |
| 141 | +
|
| 142 | +```bash [yarn] |
| 143 | +yarn add @nuxt/ui-pro@next |
| 144 | +``` |
| 145 | +
|
| 146 | +```bash [npm] |
| 147 | +npm install @nuxt/ui-pro@next |
| 148 | +``` |
| 149 | +
|
| 150 | +```bash [bun] |
| 151 | +bun add @nuxt/ui-pro@next |
| 152 | +``` |
| 153 | +
|
| 154 | +:: |
| 155 | +```` |
| 156 | + |
| 157 | +:: |
| 158 | + |
| 159 | +::note{to="#pre"} |
| 160 | +Like the `ProsePre` component, the `CodeGroup` handles filenames, icons and copy button. |
| 161 | +:: |
| 162 | + |
| 163 | +### Code Tree |
| 164 | + |
| 165 | +Wrap your code-blocks with a `code-tree` component in any particular order to display a tree view of your files. |
| 166 | + |
| 167 | +::code-preview{class="[&>div]:*:my-0 [&>div]:*:w-full"} |
| 168 | + |
| 169 | +::code-tree{defaultValue="app/app.config.ts"} |
| 170 | + |
| 171 | +```ts [nuxt.config.ts] |
| 172 | +export default defineNuxtConfig({ |
| 173 | + modules: ['@nuxt/ui-pro'], |
| 174 | + |
| 175 | + future: { |
| 176 | + compatibilityVersion: 4 |
| 177 | + }, |
| 178 | + |
| 179 | + css: ['~/assets/css/main.css'] |
| 180 | +}) |
| 181 | + |
| 182 | +``` |
| 183 | + |
| 184 | +```css [app/assets/css/main.css] |
| 185 | +@import "tailwindcss"; |
| 186 | +@import "@nuxt/ui-pro"; |
| 187 | +``` |
| 188 | + |
| 189 | +```ts [app/app.config.ts] |
| 190 | +export default defineAppConfig({ |
| 191 | + ui: { |
| 192 | + colors: { |
| 193 | + primary: 'sky', |
| 194 | + colors: 'slate' |
| 195 | + } |
| 196 | + } |
| 197 | +}) |
| 198 | +``` |
| 199 | + |
| 200 | +```vue [app/app.vue] |
| 201 | +<template> |
| 202 | + <UApp> |
| 203 | + <NuxtPage /> |
| 204 | + </UApp> |
| 205 | +</template> |
| 206 | +``` |
| 207 | + |
| 208 | +```json [package.json] |
| 209 | +{ |
| 210 | + "name": "nuxt-app", |
| 211 | + "private": true, |
| 212 | + "type": "module", |
| 213 | + "scripts": { |
| 214 | + "build": "nuxt build", |
| 215 | + "dev": "nuxt dev", |
| 216 | + "generate": "nuxt generate", |
| 217 | + "preview": "nuxt preview", |
| 218 | + "postinstall": "nuxt prepare", |
| 219 | + "lint": "eslint .", |
| 220 | + "lint:fix": "eslint --fix ." |
| 221 | + }, |
| 222 | + "dependencies": { |
| 223 | + "@iconify-json/lucide": "^1.2.18", |
| 224 | + "@nuxt/ui-pro": "3.0.0-alpha.10", |
| 225 | + "nuxt": "^3.15.1" |
| 226 | + }, |
| 227 | + "devDependencies": { |
| 228 | + "eslint": "9.20.1", |
| 229 | + "typescript": "^5.7.2", |
| 230 | + "vue-tsc": "^2.2.0" |
| 231 | + } |
| 232 | +} |
| 233 | +``` |
| 234 | + |
| 235 | +```json [tsconfig.json] |
| 236 | +{ |
| 237 | + "extends": "./.nuxt/tsconfig.json" |
| 238 | +} |
| 239 | +``` |
| 240 | + |
| 241 | +````md [README.md] |
| 242 | +# Nuxt 3 Minimal Starter |
| 243 | + |
| 244 | +Look at the [Nuxt 3 documentation](https://nuxt.com/docs/getting-started/introduction) to learn more. |
| 245 | + |
| 246 | +## Setup |
| 247 | + |
| 248 | +Make sure to install the dependencies: |
| 249 | + |
| 250 | +```bash |
| 251 | +# npm |
| 252 | +npm install |
| 253 | + |
| 254 | +# pnpm |
| 255 | +pnpm install |
| 256 | + |
| 257 | +# yarn |
| 258 | +yarn install |
| 259 | + |
| 260 | +# bun |
| 261 | +bun install |
| 262 | +``` |
| 263 | + |
| 264 | +## Development Server |
| 265 | + |
| 266 | +Start the development server on `http://localhost:3000`: |
| 267 | + |
| 268 | +```bash |
| 269 | +# npm |
| 270 | +npm run dev |
| 271 | + |
| 272 | +# pnpm |
| 273 | +pnpm run dev |
| 274 | + |
| 275 | +# yarn |
| 276 | +yarn dev |
| 277 | + |
| 278 | +# bun |
| 279 | +bun run dev |
| 280 | +``` |
| 281 | + |
| 282 | +## Production |
| 283 | + |
| 284 | +Build the application for production: |
| 285 | + |
| 286 | +```bash |
| 287 | +# npm |
| 288 | +npm run build |
| 289 | + |
| 290 | +# pnpm |
| 291 | +pnpm run build |
| 292 | + |
| 293 | +# yarn |
| 294 | +yarn build |
| 295 | + |
| 296 | +# bun |
| 297 | +bun run build |
| 298 | +``` |
| 299 | + |
| 300 | +Locally preview production build: |
| 301 | + |
| 302 | +```bash |
| 303 | +# npm |
| 304 | +npm run preview |
| 305 | + |
| 306 | +# pnpm |
| 307 | +pnpm run preview |
| 308 | + |
| 309 | +# yarn |
| 310 | +yarn preview |
| 311 | + |
| 312 | +# bun |
| 313 | +bun run preview |
| 314 | +``` |
| 315 | + |
| 316 | +Check out the [deployment documentation](https://nuxt.com/docs/getting-started/deployment) for more information. |
| 317 | +```` |
| 318 | + |
| 319 | +:: |
| 320 | + |
| 321 | +:: |
| 322 | + |
| 323 | +::note{to="https://ui3.nuxt.dev/getting-started/typography#codetree" target="_blank"} |
| 324 | +Learn more about the `code-tree` component. |
| 325 | +:: |
0 commit comments