CodeBlock component
The JSX form of a code panel, for the cases where a fence cannot carry the props.
- #authoring
- #code
- #components
<CodeBlock /> renders the same panel a fence does, with the options as props
instead of fence metadata. It exists for the few places a fence cannot reach,
such as code assembled from a variable or a block a component generates.
A fence is the better default. When the props are literals the tag is lowered to a fence before highlighting, so the two forms produce identical output and there is no reason to prefer the verbose one.
Props
<CodeBlock />
code?string
The source to render. A template literal keeps newlines and quotes intact.
language?string
Shiki language id. Unknown ids fall back to plain text rather than failing.
- Default
text
filename?string
Shown in the panel header and used to pick the file icon.
showLineNumbers?boolean
Pass false for a block short enough that a gutter is noise.
- Default
true
highlightLines?number[]
Lines to tint, counted from one. The fence equivalent is a brace range.
The one real limitation
Highlighting happens at compile time. A code prop that can only be known at
runtime cannot be lowered to a fence, so it renders without highlighting. The
panel is correct and copyable, just plain. If the colours matter, the code has
to be literal.
Related entries
Line and token highlighting
Drawing attention to the lines or words that matter inside a block, with ranges and quoted tokens.
Code panels
What every fenced block becomes, which is a titled panel with a file icon, line numbers, soft wrap, copy and collapse.
Terminals and diffs
Shell blocks that read as a session, and diff blocks whose header reports the shape of the change.