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…
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.
Or install it from npm and import it in your own script.
npm
npm install @profpowell/code-playground
Import
import'@profpowell/code-playground';
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.
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.
auto runs the preview after each edit. manual waits for the Run button.
debounce
Milliseconds
300
Pause after typing before the preview runs in auto mode.
preview-position
right, bottom
right
Where the preview sits relative to the editor.
show-console
false to hide
Shown
Whether the console bar is available.
persist
A key of your choice
None
Saves 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-theme
Boolean
Off
Copies the page's data-theme and data-mode values onto the preview document's <html> element.
fit-preview
Boolean
Off
Sizes 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-src
URL
None
Files to load the starting code from.
title
Text
Code Playground Preview
Title of the preview document, and of the downloaded file and the CodePen export.
base-url
URL
The host page's URL
Base for relative URLs in the preview.
download-filename
File name
playground.html
Name of the file saved by the download action.
Methods and properties
Member
Purpose
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.
value
Property 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.
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.
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 token
Drives
--color-surface
Playground background
--color-surface-raised
Toolbar and panel backgrounds
--color-surface-sunken
Editor background
--color-background
Background behind the preview
--color-border, --color-border-subtle
Borders and dividers
--color-text, --color-text-muted
Text and secondary text
--color-interactive, --color-interactive-hover
Accent 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-primary
Console colors for log and info, warn, error, and debug entries
--radius-m, --radius-s
Outer and inner corner radius
--font-sans, --font-mono
Interface font and editor font
--shadow-m
Shadow 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-themeattribute 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.