· 2 min read
A frame for code blocks, and a wider column
Widened the reading composition from 60ch to --reading-width: 80ch (tokens.css), for text and code alike. I picked one wider column over a breakout layout for…
What we did
- Widened the reading composition from 60ch to
--reading-width: 80ch(tokens.css), for text and code alike. I picked one wider column over a breakout layout for code or the full 70rem. - Added
CodeBlock.svelte.highlight()wraps every Shiki<pre>in it, asComponents.CodeBlock(exported fromMarkdownLayout). It adds:- a bar with the language;
- a copy button with
role="status"feedback; - wrapped lines;
- line numbers.
- Removed Shiki's
tabindex="0"with a transformer: nothing scrolls now, so there's nothing to focus. The old.prose prerule is gone.
What I learned
- Lines wrap with
white-space: pre-wrapplusoverflow-wrap: anywhere, which also breaks long words and URLs. - Line numbers are a CSS counter in
::before. Generated content isn't in the DOM text, so copying (by hand or with the button) never includes them. - Each
.lineis a full-widthinline-blockwithpadding-inline-start: 4ch, and the number is pulled into that room with a negative margin. A wrapped line continues under its own text, not under the number. :only-childonly counts elements, not the\ntext nodes between lines, so a one-line block gets no number.- The button only renders after an
$effectruns and only ifnavigator.clipboardexists. Without JS there's no button that does nothing. - Shiki's HTML comes in through
{@html}, so the component's styles reach it with:global(), still scoped to the component'sfigure.
Problems & fixes
display: blocklines added an empty line after each one: Shiki puts a\nbetween lines, and a<pre>shows it.inline-blockwithinline-size: 100%fixed it.- With
position: absolute, an empty line lost its height and its number overlapped the next one. An in-flow::beforewith a negative margin fixed it.
Links
- MDN: Using CSS counters
- MDN:
overflow-wrap - MDN:
Clipboard.writeText() - MDN: ARIA
statusrole - Shiki: transformers
Next
- Tag pages,
/blog/tags/[tag].