CodeBlock
CodeBlock shows code on its own surface, one CodeBlockLine per line. It does not colour the code. To colour it, highlight the code with Shiki and codeBlockTheme, then put the tokens of each line in a CodeBlockLine. A line that is too wide scrolls sideways.
Installation
Run npx nxds-cli@latest init first if nxds isn’t set up in your app yet. See Install.
Usage
Composition
Examples
Line numbers
Set number on each line to show it in the gutter. The copy button copies the code without the numbers.
Marked lines
Set highlighted to draw the eye to a line, and status to mark a result. Give the result in text as well.
Line 5 passed. Line 6 failed. Line 7 is running.
Diff lines
Set diff to added or removed to show a change. The copy button copies the new code, without the removed lines.
In card
Use the flush variant inside a Card. The card draws the border and the corners.
Switch.stories.tsx
This example also needs: Card.
Max height
Add a nx:max-h-* class to CodeBlockContent to scroll long code inside a fixed height.
Props
CodeBlock
| Prop | Type | Default | Description |
|---|---|---|---|
variant | "default" | "flush" | null | "default" | No description |
CodeBlockContent
| Prop | Type | Default | Description |
|---|---|---|---|
language | string | No default documented | Language of the code, such as |
CodeBlockCopyButton
CodeBlockCopyButton adds no props of its own; it accepts the props of the element or primitive it wraps.
CodeBlockLine
| Prop | Type | Default | Description |
|---|---|---|---|
diff | "added" | "removed" | No default documented | Marks the line as added or removed in a diff. The line gets a green or red tint and a |
highlighted | boolean | false | Shades the line to draw the eye to it, such as the line a step runs. |
number | number | No default documented | Line number to show in the gutter. Leave it out to show no number. |
status | "error" | "success" | No default documented | Colours the line's edge to mark a result. Give the result in text as well, because some readers can't see the colour. |
CodeBlock shows code on its own surface, one CodeBlockLine per line. It does not colour the code. To colour it, highlight the code with Shiki and codeBlockTheme, then put the tokens of each line in a CodeBlockLine. A line that is too wide scrolls sideways.
Installation
Install these components first:
Copy and paste the following code into your project.
components/code-block/CodeBlock.vue components/code-block/CodeBlockContent.vue components/code-block/CodeBlockCopyButton.vue components/code-block/CodeBlockLine.vue components/code-block/code-block-theme.ts components/code-block/index.ts Keep each file at the path shown above it.
Paths are relative to
packages/vue/src/. Keep the same folders under your@/root, and the imports between files work without edits.
Usage
Examples
Line numbers
Set number on each line to show it in the gutter. The copy button copies the code without the numbers.
Marked lines
Set highlighted to draw the eye to a line, and status to mark a result. Give the result in text as well.
Diff lines
Set diff to added or removed to show a change. The copy button copies the new code, without the removed lines.
In card
Use the flush variant inside a Card. The card draws the border and the corners.
This example also needs: Card.
Max height
Add a nx:max-h-* class to CodeBlockContent to scroll long code inside a fixed height.
Props
CodeBlock
| Prop | Type | Default | Description |
|---|---|---|---|
variant | "default" | "flush" | null | "default" | No description |
CodeBlockContent
| Prop | Type | Default | Description |
|---|---|---|---|
language | string | No default documented | Language of the code, such as |
CodeBlockCopyButton
CodeBlockCopyButton adds no props of its own; it accepts the props of the element or primitive it wraps.
CodeBlockLine
| Prop | Type | Default | Description |
|---|---|---|---|
diff | "added" | "removed" | No default documented | Marks the line as added or removed in a diff. The line gets a green or red tint and a |
highlighted | boolean | false | Shades the line to draw the eye to it, such as the line a step runs. |
number | number | No default documented | Line number to show in the gutter. Leave it out to show no number. |
status | "success" | "error" | No default documented | Colours the line's edge to mark a result. Give the result in text as well, because some readers can't see the colour. |