Files
basecoat/docs/src/_includes/macros/code_block.njk
T

28 lines
938 B
Plaintext
Raw Normal View History

2025-05-01 12:50:40 +08:00
{% macro code_block(code, language="html", class="my-4") %}
2025-04-25 16:42:10 +08:00
<div
2025-05-01 12:50:40 +08:00
class="relative {{ class }}"
2025-04-25 16:42:10 +08:00
x-data="{
copied: false,
copy() {
const codeElement = $refs.code;
if (!codeElement) return;
navigator.clipboard.writeText(codeElement.textContent || '').then(() => {
this.copied = true;
setTimeout(() => { this.copied = false }, 2000);
}).catch(err => {
console.error('Failed to copy text: ', err);
});
}
}"
>
2025-06-14 16:48:01 +08:00
<pre x-ref="code" class="grid text-sm max-h-[650px] overflow-y-auto rounded-xl scrollbar"><code class="language-{{ language }} !bg-muted !p-3.5">{{- code | escape -}}</code></pre>
2025-04-25 16:42:10 +08:00
<button
@click="copy()"
2025-06-13 14:14:38 +08:00
class="btn-icon-ghost size-8 absolute right-2.5 top-2 text-muted-foreground hover:text-foreground"
2025-04-25 16:42:10 +08:00
>
2025-06-13 14:14:38 +08:00
{% lucide "clipboard", {"x-show": "!copied" } %}
{% lucide "check", {"x-show": "copied" } %}
2025-04-25 16:42:10 +08:00
</button>
</div>
2025-05-01 12:50:40 +08:00
{% endmacro %}