Skip to content
Norn
Menu

norn-code-block

Code sample tokenized at build time. Its script registers norn-code-block as a custom element that adds the highlight colors; without the script the markup is the whole sample. A compact norn-copy-button from @allons-y/webc-patterns sits over the top end corner of the code, as its icon until the pointer or focus is on it, and copies the code without any leading $ prompts. While the button shows, the code scrolls in an area that stops short of it, so no code runs beneath it. A caption, when there is one, sits above the code. The code reads left to right in any page direction. While the code scrolls sideways, the script makes it a tab stop, so a keyboard can scroll it. Shell samples (sh, bash, shell, shell-session) color the command, its subcommand, its flags, and its arguments apart. Needs tokenRanges registered as the nornTokenRanges JavaScript function, and the patterns package's components. The names and the copy button's texts are in the page, hidden: the script names the code while it scrolls, and shows the button. Each falls back to the site's componentStrings, under norn-code-block for label and copy-label and under norn-copy-button for the rest, then English.

<norn-code-block> a component, from @allons-y/webc-code-sample

Examples

Color scheme
Width

Open on its own

Source
<norn-code-block @language="js" @caption="example.js" :@code="'import { greet } from \'@example/core\';\n\nconsole.log(greet(\'world\')); // hello, world\n'"></norn-code-block>
Color scheme
Width

Open on its own

Source
<norn-code-block @language="sh" :@code="'$ npm install @example/core\n$ npx example --help\n'"></norn-code-block>
Color scheme
Width

Open on its own

Source
<norn-code-block @language="js" @caption="long.js" :@code="'const message = \'Welcome to the example project. Read the getting started guide first, then open the reference for every option and its default value.\';\n'"></norn-code-block>
Color scheme
Width

Open on its own

Source
<norn-code-block :@code="'Build finished in 1.2 seconds.\nWrote 12 files.\n'"></norn-code-block>
Color scheme
Width

Open on its own

Source
<norn-code-block @chrome="terminal" @language="sh" @caption="Terminal" :@code="'$ npm install @example/core\n'"></norn-code-block>
Color scheme
Width

Open on its own

Source
<div style="display: grid; gap: 1.5rem">
	<div style="display: grid; gap: 0.5rem">
		<p><code>chrome = terminal</code></p>
		<div>
			<norn-code-block @language="js" @caption="example.js" :@code="'import { greet } from \'@example/core\';\n\nconsole.log(greet(\'world\')); // hello, world\n'" @chrome="terminal"></norn-code-block>
		</div>
	</div>
</div>

Props

Write them with the @ prefix, and :@ for an expression.

NameTypeDescription
@code required string The code to show.
@language string A language Shiki has loaded: the defaults or one added with loadLanguages. Unknown or empty renders plain text.
@caption string Also names the code and its copy button.
@chrome string terminal frames the code with a header bar for the caption. Without it the code sits on its surface with the caption above.

Slots

NameDescription
label The code's name without a caption, as one element. Falls back to "{language} code", with the language filled in, or "code" with no language.
copy-label The copy button's name, with {name} for the code's name, as one element. Falls back to "Copy {name} to clipboard".
copy The copy button's text. Falls back to "Copy".
copied The copy button's text after a copy. Falls back to "Copied".
failed The copy button's text when the copy fails. Falls back to "Copy failed".

Tokens

The global tokens from tokens.json the component reads. Set one in a project's tokens.css to change it everywhere.

Type a value to try it in every example above. Reset puts the default back.

Token Type Default Override
--norn-ui-accentThe brand color, shared by every component. <color> #2346d4
--norn-ui-border-radiusCorner radius. <length> 6px
--norn-ui-icon-sIcon size beside text. It follows the font size. <length> 1em
--norn-surface-raisedRaised surface. <color> light-dark(hsl(40, 14%, 96%), #242932)
--norn-surface-codeCode surface. <color> light-dark(hsl(40, 14%, 94%), #2c313b)
--norn-text-mutedSecondary text color. <color> light-dark(#4b5160, #a4abba)
--norn-font-monoCode face. <font-family> ui-monospace, SFMono-Regular, Menlo, Consolas, monospace
--norn-font-size-sSmall text: captions, code, navigation, and notes. <length> 0.875rem
--norn-code-textPlain code text, on --norn-surface-code. Follows the text color unless a project sets it. <color> var(--norn-text-base, light-dark(#16181d, #eceef3))
--norn-code-punctuationCode punctuation, on --norn-surface-code. Follows the muted text color unless a project sets it. <color> var(--norn-text-muted, light-dark(#4b5160, #a4abba))
--norn-code-commentSyntax color, measured against --norn-surface-code. <color> light-dark(#566070, #9aa3b5)
--norn-code-stringSyntax color, measured against --norn-surface-code. <color> light-dark(#0b6b3a, #7ed6a1)
--norn-code-keywordSyntax color, measured against --norn-surface-code. <color> light-dark(#6b2fc2, #c4a3ff)
--norn-code-functionSyntax color, measured against --norn-surface-code. <color> light-dark(#1b4fc4, #8fb0ff)
--norn-code-numberSyntax color, measured against --norn-surface-code. <color> light-dark(#9a4a00, #ffb36b)
--norn-code-flagSyntax color for a command's flags, measured against --norn-surface-code. <color> light-dark(#00687a, #5ccfe0)
--norn-space-2xsSmallest spacing step. <length> 0.25rem
--norn-space-xsExtra small spacing step. <length> 0.5rem
--norn-space-sSmall spacing step. <length> 0.75rem
--norn-space-mMedium spacing step. <length> 1.25rem