Code Block

Code block with syntax highlighting, a copy button, and collapsible long snippets with configurable expanded height limits.

import { CodeBlock } from "@plexui/vue/components/CodeBlock";

Examples

Overview

Collapsible

Expanded by default

Custom collapsed height

Max expanded height

Without copy button

Supported languages

API

CodeBlock

PropTypeDefaultDescription
default slotstring—Code content (text only; interpolate a string to keep its whitespace)
codestring—Code content, used when the default slot is empty
languagestring—Prism language name
classstring—Additional wrapper class

CollapsibleCodeBlock

PropTypeDefaultDescription
default slot / codestring—Code content
languagestring—Prism language name
default-expandedbooleanfalseStart expanded
collapsed-heightnumber140Height when collapsed (px)
max-expanded-heightnumber—Max height when expanded (px); taller code scrolls
copyablebooleantrueShow copy button
classstring—Additional wrapper class

Composition

CodeBlockBase (the container), CodeBlockBaseCode (the highlighted <pre>, with code, language and code-tag-props for the <code> element) and CodeBlockBaseCopyButton (copy-value, loading, disabled) build custom blocks. They are React's CodeBlockBase, CodeBlockBase.Code and CodeBlockBase.CopyButton as flat exports.

Styling

Code Block reads these component tokens. Override them at any scope to restyle it:

  • --codeblock-background-color: background color (default light-dark(#f9f9f9, #161616))
  • --codeblock-syntax-1: syntax 1 (default light-dark(#c0660d, var(--yellow-100)))
  • --codeblock-syntax-2: syntax 2 (default light-dark(var(--blue-500), var(--blue-200)))
  • --codeblock-syntax-3: syntax 3 (default light-dark(var(--green-600), var(--green-300)))
  • --codeblock-syntax-4: syntax 4 (default var(--pink-500))
  • --codeblock-syntax-5: syntax 5 (default light-dark(var(--purple-500), var(--purple-300)))