← All Components
<mth-code-block>
Read-only syntax-highlighted code block using the CSS Custom Highlight API (CSS.highlights) with accessible token span fallback, line highlighting, and clipboard copy.
When NOT to use: Use a lightweight @methane-ui/css layout class instead of a custom element when static CSS suffices.
Live Interactive Preview
Ready · interact with <mth-code-block>, or toggle Theme, Direction (LTR/RTL), or props above.
Multi-Framework Playground & Snippets
<script type="module" src="@methane-ui/code/code-block"></script>
<mth-code-block language="text" show-copy>Example</mth-code-block>import '@methane-ui/code/code-block';
import '@methane-ui/react';
export function Example() {
return <mth-code-block language="text" show-copy>Example</mth-code-block>;
}<script setup lang="ts">
import '@methane-ui/code/code-block';
</script>
<template>
<mth-code-block language="text" show-copy>Example</mth-code-block>
</template>import { Component, CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import '@methane-ui/code/code-block';
import { MthCodeBlockDirective } from '@methane-ui/angular';
@Component({
standalone: true,
schemas: [CUSTOM_ELEMENTS_SCHEMA],
imports: [MthCodeBlockDirective],
template: `<mth-code-block language="text" show-copy>Example</mth-code-block>`,
})
export class ExampleComponent {}<script lang="ts">
import '@methane-ui/code/code-block';
import MthCodeBlock from '@methane-ui/svelte/MthCodeBlock.svelte';
</script>
<MthCodeBlock language="text" show-copy>Example</MthCodeBlock>Properties & Attributes
| Property | Attribute | Type | Default | Description |
|---|---|---|---|---|
code | code | string | "" | code property. |
language | language | string | "text" | language property. |
label | label | string | "" | label property. |
lineNumbers | line-numbers | boolean | false | lineNumbers property. |
highlightLines | highlight-lines | string | "" | highlightLines property. |
showCopy | show-copy | boolean | true | showCopy property. |
Events
| Event | Detail Type | Description |
|---|---|---|
mth-copy | { code: string } | Emitted event mth-copy. |
Accessibility Checklist
| Category | Requirement | Verified By |
|---|---|---|
| aria | All aria-* IDREFs resolve within the same DOM root (ADR-003). | scanSameRootAria |
| focus | delegatesFocus: true with zero host internals.role (ADR-014). | lint-plugin & spec-compiler |
| contrast | Renders legible borders, focus outlines, and selection indicators under forced-colors: active. | Playwright forced-colors matrix |
| states | Exposes ElementInternals CustomStateSet flags: :state(copied), :state(line-numbers), :state(custom-highlight). | Conformance suite |
| i18n | Routes 5 UI/ARIA strings through @methane-ui/core i18n catalog. | Locale context test |