Code block
A code block shows source code for reading, with syntax colors, a copy button, and optional line numbers.
interface User {
id: number;
name: string;
}
<tk-code-block language="ts"><pre>
interface User {
id: number;
name: string;
}
</pre></tk-code-block>Anatomy
| Part | What it is |
|---|---|
tk-code-block |
the element |
| default slot | the code, ideally inside a <pre> |
block part |
the bordered panel |
header part |
the row with the language name and the copy button |
scroll part |
the area that scrolls sideways |
Put the code inside a <pre>. Until the script loads, the browser shows that <pre> as
is, so the code stays readable. The component removes the shared indentation, so you can
indent the code with your HTML.
Languages
npm install thykes
{ "theme": "dark", "primary": "blue" }<tk-code-block language="bash"><pre>npm install thykes</pre></tk-code-block>
<tk-code-block language="json"><pre>{ "theme": "dark", "primary": "blue" }</pre></tk-code-block>Set language to get colors and the language name in the header. Without it, or with an
unknown value, the code shows as plain monospaced text. The language is never guessed.
language |
Also accepts |
|---|---|
javascript |
js |
typescript |
ts |
jsx, tsx |
|
html |
markup, xml, svg |
css |
|
json |
|
bash |
shell, sh |
python |
py |
yaml |
yml |
Line numbers
const total = items.reduce((sum, item) => sum + item.price, 0); console.log(total);
<tk-code-block language="js" line-numbers><pre> const total = items.reduce((sum, item) => sum + item.price, 0); console.log(total); </pre></tk-code-block>
line-numbers adds a column of numbers on the left. It stays in place when the code
scrolls sideways. Screen readers skip it, and it's never selected or copied.
Copy and scrolling
- The copy button copies the code without line numbers. The icon turns into a check, and screen readers hear "Copied to clipboard" (or "Copy failed"). Both reset after about 2 seconds.
- Long lines don't wrap; the code scrolls sideways. When it overflows, the scroll area becomes a named region you can reach with Tab and scroll with the arrow keys. When the code fits, there's no extra tab stop.
When to use
- Use a code block for a snippet the reader reads or copies: an example, a config, a command.
- Use inline code inside a sentence:
<code>inProse , orText with acode-*variant. - It's for reading, not editing. It isn't an editor, a console, or a diff viewer.
Do / Don't
| ✅ Do | ❌ Don't |
|---|---|
Set language |
Expect the language to be detected |
Put the code in a <pre> |
Rely on the script for the code to be readable |
| Use it to show code | Use it as an editor or a diff viewer |
| Use inline code for a word in a sentence | Put one word in a code block |
Related
Prose : a plain<pre>with minimal styling for markdown.Text :code-*variants for inline code.Kbd : keyboard keys and shortcuts.Icon button : the copy button.
API
Attributes
| Attribute | Type | Default | What it does |
|---|---|---|---|
language |
string | none | the language for colors and the header name; see |
line-numbers |
boolean | false |
shows line numbers |
label |
string | none | the name of the scroll region; defaults to the language, such as "TypeScript code", or "Code" |
Slots
| Slot | Content |
|---|---|
| default | the code as text, ideally inside a <pre> |
Events
No custom events.
CSS
::part(block): the panel. If you change its corner radius, the scroll area's corners won't follow.::part(header): the header row.::part(scroll): the scrolling area. Its focus ring is drawn by a separate layer, so restyling this part doesn't move the ring.
Tokens
| Element | Token |
|---|---|
| panel background | syntax/surface |
| panel and gutter border | border/subtle; corner radius-12 |
| plain code | text/default |
| language name | text/secondary |
| keywords | syntax/keyword |
| strings | syntax/string |
| comments (italic) and line numbers | syntax/comment |
| numbers | syntax/number |
| functions | syntax/function |
| classes | syntax/class |
| variables | syntax/variable |
| punctuation | syntax/punctuation |
The code uses Code Small (14/20, mono). The syntax/* colors are their own palette, not
the status colors. Values per theme:
Accessibility
Built in:
- The code is real, selectable text; colors don't change the characters.
- Every syntax color, comments included, is at least 4.5:1 on the panel in every theme.
- Line numbers are hidden from screen readers and never copied.
- The copy button is a named
Icon button , and its result is announced. - The scroll area is reachable by keyboard and named when it overflows.
Your part:
- Set
language; there's no auto-detection. - Put the code inside a
<pre>so it reads well before the script loads. - Set
labelwhen a page has several blocks in the same language.
| Key | Action |
|---|---|
Tab |
moves to the copy button, then to the scroll area if it overflows |
Space / Enter |
copies the code |
← / → |
scroll the code sideways when the scroll area has focus |