Overview
A code block is a nds-code wrapper around a <code> element. A lang-* class on the <code> names the language, and the action bar nds-code-action holds the copy button. A block with several files uses tabs. Inline code is a <code class="nds-inline-code"> in a sentence.
Pick another component when:
- the text is a value to copy, such as a reference number or a link: Copy
- the text is long prose that needs a “Show more” button, not code: Expandable Content
Markup
Variants
The language of every structure comes from the lang-* class on its <code>. The API lists every language class.
| Group | Option | Markup | On element | Use |
|---|---|---|---|---|
| Structure | Block (default) | — | — | One file of code. nds-expandable clamps a long block and adds a “Show more” button |
| Structure | Tabs | canon #code-tabs |
— | Several files or languages for one example, one tab each. Each panel has its own action bar |
| Structure | Markdown | canon #code-markdown |
— | Markdown or a prompt for an AI agent (lang-markdown, lang-prompt). The lines wrap and have no line numbers |
| Structure | Inline | canon #code-inline |
— | A short code reference in a sentence: a class name, a method, a value |
| Tag strip | Tag strip (hint: More tags beside the language tag, such as a version) | canon #code-tags |
.nds-code.nds-expandable (start) |
Extra tags beside the language tag, such as a version |
| Tag strip | Tag strip (hint: More tags beside the language tag, such as a version) | canon #code-tags-md |
div.nds-code:not(.nds-expandable):not(.nds-tabs) (start) |
The same, on a Markdown block. The strip takes the block’s lang-* class |
Built-in Features
-
Syntax Highlighting
HTML, CSS, JavaScript, Markdown and AI agent prompts get syntax colors. In an HTML block, the body of a
<style>or<script>gets CSS or JavaScript colors. -
Color Swatches
A CSS value that paints a color shows a small swatch before it: a hex, an
rgb(), or avar()that resolves to a color. The swatch follows dark mode, and copy and text selection skip it. -
Line Numbers
Every code block gets line numbers, with no class, except a Markdown or prompt block. The CSS reserves their column before the script loads, so the code does not move when the numbers appear.
-
Highlight on Approach
The script colors each block as it comes near the screen, not all blocks at page load. A block in a hidden tab is colored when its tab opens.
-
Language Tag
The script adds a tag with the language name to each block, from its
lang-*class. It also names languages that get no colors, such aslang-bashorlang-json. In tabs, each panel gets its own tag. -
Language Detection
A block with no
lang-*class gets its language from its content: a leading<means HTML, a JavaScript keyword means JavaScript, and a CSS rule means CSS. A class always wins over the guess. -
Copy Button
A
nds-copybutton in the action bar copies the source text of its block, or of its own tab panel. The button shows a short confirmation after a copy. The script addsnds-icon-only nds-mdto the button, unless it has a visible label, so the markup needs neither class. -
Keyboard Scroll
A block that scrolls gets
tabindex="0", so a keyboard user can tab to it and scroll it with the arrow keys. -
Wrapped Prose
Markdown and prompt lines wrap at the width of body text, so they need no side scroll. A Markdown table row does not wrap. The copied text keeps the original lines.
-
Inline Code
The CSS colors inline code from its
lang-*class, with no script. In a table, inline code does not break at its hyphens. -
Syntax Colors
Six
--syntax-*properties set the syntax colors for a page or one block. The default colors are designed to meet WCAG AA contrast in light and dark mode.
Best Practices
- Put a
lang-*class on every<code>. The guess from the content knows only HTML, CSS and JavaScript. - Escape the HTML in a code block: write
<for<and&for&. The browser otherwise reads the sample as real markup. - Start the code lines at the left edge, not indented to match the HTML around them. The block shows every space.
- Give the copy button an
aria-label, such as “Copy code”. The button shows only an icon. - Use tabs only for files that belong to one example. Keep the tab and panel ids unique on the page.
- Use
lang-markdownorlang-promptfor text that people read or paste whole. Use a code language for code that people cite by line. - Keep inline code short: a name, a value or one call. Put a longer sample in a code block.
- Do not expect a component inside a
<code>element to work. The scripts skip it, so a sample stays static.
API
Language Classes
Set one on the <code> element. The language-* form of each class works too (language-html).
| Class | Language | Effect |
|---|---|---|
lang-html |
HTML | Tags, attributes and values get colors. The body of a <style> or <script> gets CSS or JavaScript colors |
lang-css |
CSS | Selectors, properties and values get colors. SCSS syntax ($vars, @mixin, // comments) gets no colors of its own |
lang-js, lang-javascript |
JavaScript | Keywords, strings, numbers and comments get colors |
lang-markdown, lang-md |
Markdown | Headings, list and quote markers, links, bold and inline code get colors. A fenced block with html, css or js gets that language’s colors, and any other fenced block shows as plain code |
lang-prompt |
AI agent prompt | Paths, file names, UPPER_CASE placeholders and markers, and quoted phrases get colors, so the reader checks them before sending |
any other lang-* |
its name | The tag shows the name, such as lang-bash or lang-json. The code shows as plain text |
| none | a guess | The script guesses HTML, CSS or JavaScript from the content |
Other Classes
| Class | Element | Effect |
|---|---|---|
nds-code-action |
div in .nds-code, or in each tab panel |
The action bar in the top corner. It holds the copy button |
nds-code-tags |
span in .nds-code |
A strip of tags in the top corner, beside the language tag. It takes the block’s lang-* class, so it clears the line numbers the same way |
nds-code-lang |
.nds-tag in .nds-code-tags |
The language tag. An authored one stops the script from adding its own |
nds-inline-code |
code in text |
Inline code. With lang-js it takes the keyword color, and with any other class the attribute color |
CSS Custom Properties
Set these on the block, on a parent, or on :root. The --syntax-* properties also color inline code. One value covers light and dark mode.
| Property | Default | Controls |
|---|---|---|
--syntax-tag |
--colors-red-600 (dark: --colors-red-400) |
HTML tags and CSS selectors |
--syntax-attr |
--colors-yellow-700 (dark: --colors-yellow-400) |
Attribute names and numbers |
--syntax-string |
--colors-green-700 (dark: --colors-green-400) |
Attribute values, strings and template strings |
--syntax-property |
--colors-blue-700 (dark: --colors-blue-400) |
CSS property names |
--syntax-keyword |
--colors-tertiary-500 (dark: --colors-tertiary-300) |
Keywords, literals and built-in globals |
--syntax-comment |
--colors-neutral-500 (dark: --colors-neutral-400) |
Comments |
--code-max-height |
60svh |
Height of the code area. A longer block scrolls inside it |
--code-bg |
var(--background-stripe) |
Background of a Markdown or prompt block |
Tokens
It reads these shared tokens. A change to one also changes other components: see Tokens.
Source: the var() reads in _sass/components/_code.scss.
| Token | Preview | Value |
|---|---|---|
--background-stripeRead by background | var(--colors-neutral-50)Dark mode var(--colors-neutral-850) | |
--border-neutral-secondaryRead by border, background, box-shadow | var(--colors-neutral-200)Dark mode var(--colors-neutral-700) |
JavaScript
The NDS.Code script loads after the page shows. Until it loads, each call loads it and returns a promise of the result, so await a call whose result you use.
The script colors every code block on the page by itself. Call it again for a code block that you add or change later.
| Method | Effect |
|---|---|
NDS.Code.init() |
Colors every .nds-code code on the page that is not colored yet, as it nears the screen. Call it after you add code blocks |
NDS.Code.highlight(codeEl) |
Colors one <code> again from its text. Call it after you change the text. It is safe to call more than once |
NDS.Code.detectLanguage(codeEl, source) |
Returns the language the block gets: html, css, javascript, markdown or prompt. Returns null for a lang-* class with no colors |
The full API is in the banner of _js/nds-code.js.