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-playground

A web component with editors for HTML, CSS and JavaScript, a preview that runs the code in a sandboxed iframe, and a console that shows what the code logs. It reads Vanilla Breeze tokens directly, so its toolbar, panels and console match the active theme with no extra CSS.

At a glance

Package@profpowell/code-playground, version 0.3.1
Elements<code-playground>
Renders inShadow DOM. The starting code is read from <textarea> children in the light DOM. The preview runs in a sandboxed iframe.
Sourcegithub.com/ProfPowell/code-playground
Documentationprofpowell.github.io/code-playground
LicenceMIT
In Vanilla BreezeNot bundled. Load it yourself.

Install

Load the component from a CDN. The script fetches a second file from the same folder, editor-runtime-B2MdwU5J.js, which holds the CodeMirror editor.

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

Until the script has run, the source textareas are ordinary visible form fields. Add this rule to a stylesheet in the <head> to hide them and reserve the space.

Demo

A playground on a Vanilla Breeze page. Edit any tab and the preview runs again. Press the button in the preview, then open the console bar to see the log.

Usage

Give the starting code in textareas, one per slot: html, css, js, and optionally head for extra tags in the preview's <head>. With persist, a reader's edits are saved in their browser under that key.

Or load the starting code from files.

For a short example inside a tutorial, fit-preview makes the preview as tall as its content instead of filling a fixed pane.

By default the playground picks its own height. Set a height on the element and the layout fills it, with the editor scrolling inside.

Reference

Attributes

AttributeValuesDefaultPurpose
modeauto, light, darkautoColor scheme. auto follows the page.
run-modeauto, manualautoauto runs the preview after each edit. manual waits for the Run button.
debounceMilliseconds300Pause after typing before the preview runs in auto mode.
preview-positionright, bottomrightWhere the preview sits relative to the editor.
show-consolefalse to hideShownWhether the console bar is available.
persistA key of your choiceNoneSaves edits, the active tab and the split position in the reader's browser. An edit to a panel is dropped if you later change that panel's authored source.
inherit-themeBooleanOffCopies the page's data-theme and data-mode values onto the preview document's <html> element.
fit-previewBooleanOffSizes the preview to its content. The playground is as tall as the taller of the editor and the preview.
html-src, css-src, js-src, head-srcURLNoneFiles to load the starting code from.
titleTextCode Playground PreviewTitle of the preview document, and of the downloaded file and the CodePen export.
base-urlURLThe host page's URLBase for relative URLs in the preview.
download-filenameFile nameplayground.htmlName of the file saved by the download action.

Methods and properties

MemberPurpose
getValue()Returns { html, css, js, head } with the current code.
setValue(value)Replaces the code in one or more panels. Pass only the keys to change.
valueProperty form of getValue() and setValue().
run()Rebuilds the preview and runs the code.
softRun()Runs the JavaScript again without rebuilding the iframe, when that is possible.
reset()Returns every panel to its authored source and clears saved edits.
reloadSources(options)Fetches the *-src files again. Returns a promise with loaded and failed lists.
copy(type)Copies one panel to the clipboard: html, css or js. Defaults to the active tab.
copyConsole()Copies the visible console output.
clearConsole()Empties the console.
downloadHTML()Saves the playground as one HTML file.
openInCodePen()Sends the code to CodePen.
mode, runMode, previewPosition, debounce, persistKey, inheritTheme, showConsole, fitPreview, sourceUrlsProperties that mirror the attributes.

Events

All events bubble and cross the shadow boundary.

EventFired whenDetail
code-playground:changeThe reader edits a panel{ panel, value, values }
code-playground:runThe preview starts a run{ runId, values, executionMode }
code-playground:readyThe preview has finished loadingIncludes executionMode
code-playground:consoleThe preview code logs to the console{ origin, level, text, timestamp, executionMode }
code-playground:errorThe preview code throws an errorThe same shape as console
code-playground:sources-loadedFiles named by *-src have loaded{ loaded, values }
code-playground:source-errorA *-src file fails to load{ type, src, error }

CSS custom properties

All properties start with --cp-. The table lists the suffix. The element also exposes four CSS parts for ::part(): toolbar, editor-panel, preview-panel and console-panel.

GroupProperties
Surfacesbg, panel-bg, editor-bg, preview-bg, toolbar-bg, hover-bg
Text and borderstext, muted, border, border-subtle
Accent and focusaccent, on-accent, focus
Console levelslevel-log, level-info, level-warn, level-error, level-debug
Shape and fontsradius, radius-inner, font-sans, font-mono, shadow, menu-shadow
Editor syntax colorstoken-keyword, token-string, token-number, token-comment, token-tag, token-attribute, token-property, token-function

See the code-playground documentation for the full detail.

With Vanilla Breeze

A token is a CSS custom property that holds one design value, such as --color-surface. The component defines each of its own --cp-* properties from a VB token, with a built-in value for pages that do not use Vanilla Breeze.

VB tokenDrives
--color-surfacePlayground background
--color-surface-raisedToolbar and panel backgrounds
--color-surface-sunkenEditor background
--color-backgroundBackground behind the preview
--color-border, --color-border-subtleBorders and dividers
--color-text, --color-text-mutedText and secondary text
--color-interactive, --color-interactive-hoverAccent color, used for the Run button, active controls and the editor selection, and the Run button's hover color
--color-text-on-interactive-auto, then --color-text-on-primaryText on the Run button
--color-focus-ringFocus outline
--color-info-text, --color-warning-text, --color-error-text, --color-success-textConsole colors for log and info, warn, error, and debug entries
--radius-m, --radius-sOuter and inner corner radius
--font-sans, --font-monoInterface font and editor font
--shadow-mShadow of the actions menu

The order is: a --cp-* property you set on the element, then the VB token, then the built-in value. No token bridge is needed. A token bridge is a stylesheet rule that copies VB tokens onto a component's own properties, and this component already reads the tokens itself. The editor's syntax colors do not come from VB tokens. Set the --cp-token-* properties to change them.

Light or dark follows the page when mode is auto: the component watches data-mode, data-theme and the dark class on <html> and <body>, and the page's computed color-scheme.

The preview is a separate document in a sandboxed iframe. It does not load the Vanilla Breeze stylesheet, so VB tokens are not defined inside it. The inherit-theme attribute only copies the page's data-theme and data-mode values onto the preview's <html> element. To style the preview with Vanilla Breeze, add the VB stylesheet link through the head slot or head-src.

To change one playground, set the property on it.

↑