Code block
Readable, copyable code samples with Google Sans Code and lightweight syntax colours.
import 'material-next-web-components/code-block.js';On this page
Overview
Use <mnw-code-block> for install commands, configuration and short examples in documentation, release notes or a product's developer page. It shows the language as a label, colours common tokens and, with copy, adds a Copy action that writes the code to the clipboard.
Use something else when the reader must edit code (an editor such as CodeMirror), when exact highlighting matters for a language the built-in tokenizer does not know, or for very long files: the block has no line numbers, folding or search.
Live demo
Copy a snippet: its event shows here.
<mnw-code-block language="bash" copy code="npm install material-next-web-components lit @material/web"></mnw-code-block>
<mnw-code-block language="html" copy>
<template>
<md-filled-button href="./install/">Get started</md-filled-button>
</template>
</mnw-code-block>
<mnw-code-block language="text" code="No copy action: the copy attribute is off."></mnw-code-block>
<p class="demo-log" aria-live="polite">Copy a snippet: its event shows here.</p>import 'material-next-web-components/code-block.js';
export default (root: HTMLElement) => {
const log = root.querySelector('.demo-log')!;
root.addEventListener('mnw-code-copy', (event) => {
const {code} = (event as CustomEvent<{code: string}>).detail;
log.textContent = `mnw-code-copy: ${code.length} characters on the clipboard`;
});
root.addEventListener('mnw-code-copy-error', () => {
log.textContent = 'mnw-code-copy-error: the browser refused the clipboard';
});
};Usage
There are two ways to give it code:
- The
codeattribute or property, for one-liners and for code you build in JavaScript. Newlines are kept; in HTML write them as real line breaks or . - The default slot, for multi-line markup. Wrap it in a
<template>so the browser keeps it as text instead of rendering it. The block reads the slot's text content, so leading indentation is kept as written.
When both are present, code wins.
<mnw-code-block language="ts" copy code="const ready = true;"></mnw-code-block>
<mnw-code-block language="html" copy>
<template><md-filled-button>Install</md-filled-button></template>
</mnw-code-block>language is the label shown in the header and picks the highlighter: html (or markup) colours tags and attributes; every other value, text included, uses one script-style tokenizer that colours comments, strings, JavaScript and TypeScript keywords, capitalised names and numbers.
API
A Material code block with Google Sans Code typography, lightweight syntax colours and an optional copy action.
| Property | Attribute | Type | Default | Description |
|---|---|---|---|---|
language | language | string | 'text' | The programming language label displayed in the block header. |
code | code | string | '' | The code snippet to render. Slotted content is used when omitted. |
copy | copy | boolean | false | Shows the Material copy-to-clipboard action. |
| Event | Description |
|---|---|
mnw-code-copy-error | {code, error} when the Clipboard API is missing or refuses; the block shows "Copy failed". |
mnw-code-copy | {code} after the code is written to the clipboard. |
| Slot | Description |
|---|---|
| Default | Code as text, used when code is empty. Wrap markup in a <template> so the browser does not render it. |
| Custom property | Description |
|---|---|
--md-sys-color-code-background | Code surface. |
--md-sys-color-code-on-background | Code text. |
--md-sys-color-code-comment | Comment tokens. |
--md-sys-color-code-punctuation | Punctuation tokens. |
--md-sys-color-code-tag | Markup tag tokens. |
--md-sys-color-code-string | String tokens. |
--md-sys-color-code-keyword | Keyword tokens. |
--md-sys-color-code-function | Function name tokens. |
--md-sys-color-code-variable | Variable and number tokens. |
The copy attribute is a boolean: present means on. Setting the property to false removes the attribute.
Accessibility
- The code is a
<figure>whose caption is the language label, around a<pre>, so assistive technology reads it as preformatted text. - The Copy action is a Material Web assist chip, reachable with the keyboard. After a copy, "Copied" (or "Copy failed") is announced through a polite live region, and the chip shows "Copied" for 1.4 seconds.
- Syntax colours never carry meaning on their own.
Styling
The block reads the page's Material colour roles (--md-sys-color-*) for its surfaces and the --md-sys-color-code-* properties above for tokens, with light and dark defaults. Load Google Sans Code for the code font; it falls back to the platform's monospace font.
Limitations
- The highlighter is a small built-in tokenizer, not Prism or Shiki. It has no grammar per language, so Bash, CSS or JSON get the script-style colours, and CSS or scripts inside HTML are not coloured.
- Copy needs the Clipboard API, which browsers only allow in secure contexts (HTTPS or
localhost). Listen formnw-code-copy-errorto offer another way. - Long lines scroll horizontally inside the block; they do not wrap.