API Reference

Attributes, properties, methods, events, slots, and styling hooks for <code-playground>.

Attributes

AttributeTypeDefaultDescription
modestring"auto"Color mode for the shell. "auto" follows page signals, or set "light" / "dark".
run-modestring"auto""auto" for debounced live preview or "manual" for explicit runs.
debouncenumber300Auto-run delay in milliseconds.
preview-positionstring"right"Position of the preview pane: "right" or "bottom".
html-srcstringURL of an external HTML file to load into the HTML panel.
css-srcstringURL of an external CSS file to load into the CSS panel.
js-srcstringURL of an external JavaScript file to load into the JS panel.
head-srcstringURL of a preview-only head fragment (e.g. <link>, <style>, extra <meta> tags).
persiststringlocalStorage key used for saving source, active tab, and split position.
inherit-themebooleanfalseCopies page data-mode and data-theme to the preview document.
show-consolebooleantrueSet to "false" to hide the console panel.

Properties

PropertyTypeDescription
value{ html, css, js, head }Get or set the current source payload.
modestringGet or set the color mode.
runModestringGet or set the run mode ("auto" or "manual").
previewPositionstringGet or set the preview position ("right" or "bottom").
debouncenumberGet or set the debounce delay.
persistKeystringRead-only. The current localStorage key.
inheritThemebooleanRead-only. Whether theme inheritance is active.
showConsolebooleanRead-only. Whether the console panel is visible.
sourceUrlsobjectRead-only. The configured *-src URLs.

Methods

MethodReturnsDescription
getValue(){ html, css, js, head }Returns the current source payload.
setValue(value)voidUpdates one or more source panels programmatically.
run()voidRebuilds the preview iframe immediately.
softRun()voidRe-executes the JS panel inside the existing iframe without rebuilding the document.
reset()voidRestores the original authored source values.
reloadSources()Promise<void>Refetches any configured *-src files and replaces editor content.
copy(type?)Promise<void>Copies the requested source panel ("html", "css", or "js") to the clipboard. Defaults to the active tab.
copyConsole()Promise<void>Copies visible console rows, respecting the active filter, search, and repeat collapsing.
downloadHTML()voidDownloads a standalone HTML document assembled from the current state.
openInCodePen()voidOpens the current payload in a new CodePen pen.
clearConsole()voidClears captured console output.

Events

EventDetailDescription
code-playground:change{ type, value }Fires when an editor value changes.
code-playground:run{ html, css, js, head }Fires after the preview document is rebuilt.
code-playground:readyFires when the iframe bootstrap reports ready.
code-playground:console{ level, args }Fires for bridged console calls from the iframe.
code-playground:error{ level, args }Fires for bridged runtime errors and unhandled rejections.
code-playground:sources-loaded{ loaded }Fires after one or more external source files load successfully.
code-playground:source-error{ type, url, error }Fires when a configured external source file fails to load.

Slots

SlotExpected ElementDescription
html<textarea>Initial HTML source content.
css<textarea>Initial CSS source content.
js<textarea>Initial JavaScript source content.

CSS Parts

PartDescription
::part(toolbar)File tabs, status indicators, and primary action buttons.
::part(editor-panel)Code editor workspace containing the active tab's editor.
::part(preview-panel)Preview iframe panel.
::part(console-panel)Console output panel with filters and search.

CSS Custom Properties

Override these properties on the code-playground selector to customize the look. Each property falls back to a Vanilla Breeze design token when available.

PropertyDefault (light)Description
--cp-bg#ffffffShell background
--cp-panel-bg#f6f8fbPanel background
--cp-editor-bg#f8fafcEditor background
--cp-preview-bg#ffffffPreview iframe background
--cp-border#d0d7dePrimary border color
--cp-border-subtle#e5e7ebSubtle / inner border color
--cp-text#111827Primary text color
--cp-muted#6b7280Muted / secondary text color
--cp-accent#2563ebAccent / interactive color
--cp-radius16pxOuter border radius
--cp-radius-inner12pxInner panel border radius
--cp-font-sans"SF Pro Text", sans-serifUI font stack
--cp-font-mono"SFMono-Regular", monospaceCode font stack
--cp-shadow0 18px 40px ...Outer box shadow
--cp-split48%Editor/preview split position

Syntax Highlighting Tokens

PropertyLightDarkDescription
--cp-token-keyword#9a3412#ff7b72Keywords (const, if, etc.)
--cp-token-string#166534#7ee787String literals
--cp-token-number#1d4ed8#79c0ffNumeric literals
--cp-token-comment#64748b#8b949eComments
--cp-token-variable#0f172a#e6edf3Variable names
--cp-token-type#7c3aed#d2a8ffType names
--cp-token-tag#be123c#7ee787HTML tag names
--cp-token-attribute#0369a1#79c0ffHTML attributes
--cp-token-property#155e75#a5d6ffCSS properties
--cp-token-function#7c2d12#d2a8ffFunction names
--cp-token-meta#6d28d9#ffa657Meta / directives

Keyboard Shortcuts

KeyAction
Arrow Left / Arrow UpMove the editor/preview divider left/up (hold Shift for larger steps)
Arrow Right / Arrow DownMove the editor/preview divider right/down

Related Components