Components Data display
Code block
Display escaped source with its language or filename and a copy action.
package main
import "fmt"
func main() {
fmt.Println("Hello, Mono.")
}
Usage
Pass source as plain text in Code. Go escapes the content in the document. The copy control appears when its module mounts and offers manual selection when clipboard access is unavailable.
Register code-block with Head and pass its properties to Render. Go setup guide
Go usage
err := mono.UI.Render(w, "code-block", mono.Props{
"Code": "package main\n\nimport \"fmt\"\n\nfunc main() {\n fmt.Println(\"Hello, Mono.\")\n}",
"Filename": "main.go",
})Rendered HTML
<figure data-mono="code-block" id="demo-code-block">
<figcaption><span>main.go</span><button class="mu-btn mu-small" type="button" data-copy hidden>Copy code</button></figcaption>
<pre tabindex="0"><code>package main
import "fmt"
func main() {
fmt.Println("Hello, Mono.")
}</code></pre>
<span class="mu-sr-only" role="status" data-status></span>
</figure>Go template
<figure data-mono="code-block" id="{{.ID}}">
<figcaption><span>{{.Filename}}</span><button class="mu-btn mu-small" type="button" data-copy hidden>Copy code</button></figcaption>
<pre tabindex="0"><code>{{.Code}}</code></pre>
<span class="mu-sr-only" role="status" data-status></span>
</figure>Component CSS
[data-mono="code-block"] { margin:0; overflow:hidden; border:1px solid #323232; border-radius:.5rem; background:#191919; color:#fafafa; }
[data-mono="code-block"] figcaption { display:flex; align-items:center; justify-content:space-between; gap:.7rem; padding:.5rem .8rem; background:#262626; font:.77rem ui-monospace,monospace; }
[data-mono="code-block"] pre { overflow:auto; margin:0; padding:.9rem 1rem; line-height:1.6; }
[data-mono="code-block"] .mu-btn { padding:.25rem .55rem; min-height:auto; }JavaScript module
export function mount(root) {
const button = root.querySelector('[data-copy]');
const code = root.querySelector('code');
const status = root.querySelector('[data-status]');
button.hidden = false;
button.addEventListener('click', async () => {
try {
await navigator.clipboard.writeText(code.textContent);
button.textContent = 'Copied';
status.textContent = 'Code copied to the clipboard.';
} catch {
const range = document.createRange();
range.selectNodeContents(code);
const selection = window.getSelection();
if (selection) {
selection.removeAllRanges();
selection.addRange(range);
}
button.textContent = 'Copy selection';
status.textContent = 'Code selected. Use your device’s copy command.';
}
});
}Use the HTML in a static site
Copy the rendered HTML above. Download these assets into the same folder as your page, then add the tags below to its head.
Shared CSS Component CSS Ready-to-use module
<link rel="stylesheet" href="./base.css">
<link rel="stylesheet" href="./code-block.css">
<script type="module" src="./code-block.js"></script>Copy into your project
Run this command from the Mono checkout. Set -out to the component directory in your application.
go run ./cmd/mono-ui add -out ./components code-blockExample properties (JSON)
{
"Code": "package main\n\nimport \"fmt\"\n\nfunc main() {\n fmt.Println(\"Hello, Mono.\")\n}",
"Filename": "main.go"
}