Vanilla Breeze v0.7.4

Vanilla Breeze v0.7.4 released

0.7.4 gives the Ambient pack weather. A page names one feed URL on its own site, and the pack sets data-weather from it, uses its sunrise and sunset for dawn and dusk, and never shows a stale observation. Helpers for producing the feed from Open-Meteo are included. Ambient p…

Read the release notes

code-block

A web component that shows source code with syntax highlighting, a copy button, optional line numbers and tabs for several files. The Vanilla Breeze stylesheet sets its colors, fonts and corner radius from the active theme, so it needs no extra styling on a VB page.

At a glance

Package@profpowell/code-block, version 2.10.0
Elements<code-block> and <code-block-group>
Renders inShadow DOM
Sourcegithub.com/ProfPowell/code-block
Documentationprofpowell.github.io/code-block
LicenceMIT
In Vanilla BreezeNot bundled. Vanilla Breeze lists it as a development dependency and imports it only in src/doc-extras.js, a bundle built for this docs site and left out of the npm package. You load the component yourself. The VB stylesheet does include the rules that theme it.

Install

Load the component from a CDN. The stylesheet is optional: it styles the element before the script has run, so the code does not show as a line of inline text and then jump.

Or install it from npm and import it in your own script.

Demo

Three blocks on a Vanilla Breeze page: highlighted lines with focus mode, a tabbed group of files, and a diff.

Usage

Put the code between the tags and name the language. The component reads the element's text, so write < and & in the code as &lt; and &amp;.

<code-block language="javascript" show-lines highlight-lines="2-3" focus-mode>function greet(name) {
  const message = `Hello, ${name}!`;
  console.log(message);
  return message;
}</code-block>

To avoid escaping markup by hand, put the code in a <textarea> child. The component takes the code from the textarea and removes it.

<code-block language="html" filename="index.html">
  <textarea><main>
  <h1>Trail report</h1>
</main></textarea>
</code-block>

Wrap several blocks in <code-block-group> to show them as tabs. Each tab is named from the block's filename, then its label, then its language.

<code-block-group show-download>
  <code-block language="html" filename="index.html">...</code-block>
  <code-block language="css" filename="styles.css">...</code-block>
</code-block-group>

Load the code from a file with src. The language and file name are taken from the URL unless you set them.

<code-block src="/examples/greet.js" show-lines></code-block>

Reference

Attributes

Attributes of <code-block>. A boolean attribute is on when present.

AttributeValuesDefaultPurpose
languagehtml, xml, xhtml, svg, markup, css, javascript, js, typescript, ts, json, yaml, yml, php, python, py, bash, shell, sh, zsh, markdown, http, diff, csv, plaintext, text, txtplaintextLanguage used for highlighting.
labelTextThe file name, else the language in capitalsLabel shown in the header.
filenameTextNoneFile name shown in the header and used as the tab name in a group.
themelight, darkFollows the page, else lightFixes the color scheme. Leave it off to follow the page.
show-linesBooleanOffShows line numbers.
start-lineNumber, 1 or more1Number given to the first line shown.
end-lineNumberNoneShows only the lines from start-line to this line of the source.
highlight-linesList such as 3,5-7NoneLines to highlight.
focus-modeBooleanOffDims and slightly blurs the lines that are not highlighted.
collapsedBooleanOffStarts collapsed, with a button to show the rest.
max-linesNumber10Lines visible while collapsed.
max-heightCSS length such as 300pxNoneLimits the height and scrolls the code inside.
wrapBooleanOffWraps long lines instead of scrolling sideways.
copy-textTextCopyText of the copy button.
copied-textTextCopied!Text shown after a successful copy.
no-copyBooleanOffHides the copy button and stops the code from being selected.
show-shareBooleanOffShows a share button with a menu that can open the code in CodePen.
show-downloadBooleanOffShows a button that downloads the code as a file.
lazyBooleanOffWaits to highlight until the block is near the visible part of the page.
srcURLNoneFetches the code from a file instead of using the element's content.

<code-block-group> accepts theme, show-share and show-download, and applies its theme to the blocks inside it.

Methods and properties

MemberOnPurpose
getCode()BlockReturns the current code as a string.
setCode(code)BlockReplaces the code and renders again.
copyCode()BlockCopies the code to the clipboard. Returns a promise.
downloadCode()Block, groupDownloads the code as a file. On a group, downloads the active block.
openInCodePen()Block, groupOpens the code in CodePen. A group sends the HTML, CSS and JavaScript of all its blocks.
toggleCollapsed()BlockCollapses or expands the block.
render()Block, groupRenders the component again.
CodeBlock.getSupportedLanguages()Class (static)Returns the list of language names.
activeIndexGroupIndex of the selected tab. Readable and writable.
selectTab(index)GroupSelects a tab.
getActiveBlock()GroupReturns the block shown in the selected tab.

Events

EventFired byDetail
code-loadedBlock, when a src file has loaded{ url, code }
code-load-errorBlock, when a src file fails to load{ url, error }
tab-changeGroup, when the selected tab changes{ index, block }

All three events bubble.

CSS custom properties

The component has more than 60 custom properties, all starting with --cb-. Set them on the element or on any ancestor.

GroupExamples
Container--cb-bg, --cb-code-bg, --cb-border-color, --cb-border-radius, --cb-margin, --cb-shadow
Type--cb-font-family, --cb-ui-font-family, --cb-font-size, --cb-line-height, --cb-text-color
Header--cb-header-bg, --cb-header-padding, --cb-label-color, --cb-filename-color
Buttons--cb-button-bg, --cb-button-color, --cb-button-border, --cb-button-radius, --cb-button-hover-bg, --cb-focus-color, --cb-success-color, --cb-error-color
Code area and line numbers--cb-code-padding, --cb-line-numbers-bg, --cb-line-numbers-color
Syntax colors--cb-keyword, --cb-string, --cb-comment, --cb-function, --cb-number, --cb-attribute, --cb-tag, --cb-meta, --cb-builtin
Highlight and focus mode--cb-highlight-bg, --cb-highlight-border, --cb-highlight-gutter, --cb-focus-dim-opacity, --cb-focus-blur
Diff--cb-diff-add-bg, --cb-diff-add-text, --cb-diff-remove-bg, --cb-diff-remove-text
Collapse and scrollbar--cb-expand-bg, --cb-expand-color, --cb-scrollbar-track, --cb-scrollbar-thumb

The package also exports @profpowell/code-block/ssr, a Node module that renders blocks to HTML at build time. See the code-block documentation for that and for the full property list.

With Vanilla Breeze

A token is a CSS custom property that holds one design value, such as --color-surface. Two things connect the component to Vanilla Breeze tokens.

First, the component itself reads five VB tokens as fallbacks, so it picks up a VB theme even without any VB rule aimed at it.

VB tokenDrives
--color-surfaceCode area background
--color-surface-raisedOuter background and header background
--color-textCode text and button text
--color-text-mutedComment color
--color-borderBorders

Second, the VB stylesheet contains a token bridge: a rule with the selector code-block that sets the component's --cb-* properties from VB tokens. It ships in vanilla-breeze.css and vanilla-breeze-core.css, so there is no extra file to load and no attribute to add. The bridge covers more than the five fallbacks.

VB tokenSet on
--color-surface, --color-surface-raisedBackgrounds of the block, header, line numbers, buttons and scrollbar track
--color-text, --color-text-mutedCode text, file name, label, comments and button text
--color-borderBorders and the scrollbar thumb
--color-interactiveButton hover, focus ring, expand link and the highlighted-line border
--color-primaryKeywords, functions, attributes and built-in names
--color-accentStrings, numbers, tags and meta tokens
--radius-m, --radius-sCorner radius of the block, and of buttons and menus
--font-mono, --font-sansCode font and interface font

For each value the order is: the --cb-* property if it is set, then the VB token, then the component's built-in color. With VB loaded, the bridge is what sets the --cb-* properties.

Light or dark is chosen separately. With no theme attribute, the component follows the nearest ancestor with data-mode, then data-theme, data-mode or a dark class on <html> or <body>, then the page's computed color-scheme. It updates when these change.

The bridge sits in a cascade layer, so any rule you write outside a layer wins. To change one block, set the property on it.

Accessibility

↑