code-playpad

Options

Everything you can set on <code-playpad>: attributes, themes, styling, the JavaScript API, events, page-wide settings and React props.

Jump to: Try them · Attributes · Themes · Styling · JavaScript · Events · Page-wide settings · React props

Try them

Change a setting, and the widget and its HTML below change with it.

import { shout } from "./util.js"; console.log(shout("hello")); export const shout = (text) => text.toUpperCase() + "!";

Attributes

AttributeWhat it does
languagepython (default), javascript, typescript, java, c, cpp, csharp (or cs), react. Which language the widget runs.
heightA number (pixels), any CSS length, or auto to fit the code.
min-heightThe smallest height with height="auto".
max-heightThe largest height with height="auto"; longer code scrolls inside it.
preview-heightreact only. The height of the preview.
tree-widthThe width of the file tree with file-view="tree": a percentage such as 35%, or a number of pixels. The default is 20% for react and 200 pixels for other languages.
file-viewHow several files are listed: tabs (default) or tree.
file-view-switchShows a toolbar button that switches between tabs and the tree. It only shows when the widget has a file list.
file-actionsLets the reader add, rename and remove files. readonly turns it off.
open-fileWhich file is on screen to begin with, if not the entry.
entryWhich file Run executes. A name that isn't one of the files is an error.
themeauto (default), light or dark. light and dark pin the widget and hide its theme controls.
theme-controlsoff hides the theme button and the editor theme list.
change-marksoff hides the marks that show code hasn't run yet: the dot on Run, ● and ○ on files, and the note over the output.
readonlyNo value. The editor can't be typed into, but Run still works.
autorunNo value. Runs once, on its own, when scrolled into view.
stdinNo value. Shows the standard-input box from the start, for lines the program reads as input. A <playpad-stdin> child prefills it, byte for byte.
persistA key to save the reader's edits under, in IndexedDB. They return on the next visit.
practiceJava only. Test cases for one method, as JSON.

Themes

Every widget has a moon or sun button for light and dark, and a palette for the editor theme. The reader's choice is saved in localStorage and every widget on the same site follows it, in other tabs and separate windows too. It changes only the widgets, never your page.

Which theme a widget uses, first match wins:

  1. Its theme="light" or theme="dark" attribute.
  2. The reader's saved choice.
  3. The page's color-scheme.
  4. The operating system's setting.

A pinned widget still uses the reader's editor theme for its mode. Editor themes also recolour the toolbar and output, but not a React project's own preview.

Editor themeIdMode
One Darkdefault-darkdark
Monokaimonokaidark
Draculadraculadark
Nordnorddark
Solarized Darksolarized-darkdark
Default Lightdefault-lightlight
GitHub Lightgithub-lightlight
Solarized Lightsolarized-lightlight

To connect your site's own switch:

import { setThemePreference } from 'code-playpad';

setThemePreference({ mode: 'dark', dark: 'monokai' });

Styling

The widget's styles are isolated from your page, so there are two supported ways to restyle it. These values beat an editor theme's colours.

CSS variables

code-playpad {
  --cp-accent: #b5179e;
  --cp-radius: 14px;
}
VariableWhat it sets
--cp-bgThe background of the widget and its buttons.
--cp-fgText.
--cp-mutedSecondary text, such as status messages.
--cp-borderBorders.
--cp-surfaceThe toolbar and tabs.
--cp-accentRun, the focus ring and the active tab.
--cp-accent-fgText on the accent colour.
--cp-dangerStop and errors.
--cp-okSuccess messages.
--cp-radiusCorner radius of the widget.
--cp-font-monoThe font for code and output.
--cp-font-uiThe font for buttons and labels.

Parts

code-playpad::part(run-button) { border-radius: 999px; }
PartWhat it is
run-buttonThe Run button.
stop-buttonThe Stop button.
file-view-buttonThe button that switches between tabs and the tree, with file-view-switch.
theme-buttonThe light and dark button.
editor-theme-pickerThe editor theme list.
window-buttonThe Open in window button, on React widgets.
tabsThe file tabs or tree.
outputThe output area.
stale-noteThe "Code changed since this run" note over the output.
noticeA line some runtimes show under the toolbar, such as a compiler's statement that it is not affiliated with the language's project. Hidden for the others.
previewThe React preview area.

JavaScript

On the element, such as document.querySelector('code-playpad').run().

NameKindWhat it does
codePropertyThe visible file's code. Setting it replaces that file.
filesPropertyEvery file, as { name: contents }. Setting it replaces the project.
entryPropertyWhich file Run executes.
stdinPropertyThe standard input box's text.
languagePropertyThe language.
practicePropertyJava practice test cases, or null.
run()MethodRuns the code, and resolves to the result.
stop()MethodStops this widget's run, queued or running.
reset()MethodGoes back to the code the page shipped.
prewarm()MethodStarts the runtime before the reader presses Run.
getCode()MethodReturns the visible file's code.
getFiles()MethodReturns every file.
openFile(name)MethodBrings a file on screen.
addFile(name, contents)MethodAdds a file and opens it. Throws if a rule refuses the name.
renameFile(from, to)MethodRenames a file. Throws if a rule refuses it.
removeFile(name)MethodRemoves a file. Throws if a rule refuses it.
openWindow()MethodMoves the project to a separate window. Call it from a click, because browsers block other popups. Returns false if blocked.
closeWindow()MethodCloses the window, and brings its edits back.

Events

Events bubble out of the widget, so you can listen on document. Read event.detail.

EventDetailWhen
playpad-ready{ language, label }The runtime has started.
playpad-run{ code, entry, files, language }A run started.
playpad-output{ stdout, stderr?, value?, table?, durationMs, failed, language }A run finished. For React, the entry has loaded.
playpad-error{ message, hint?, language }A run failed. React adds file, line and column when known.
playpad-console{ level, text, language }React only. One console line.
playpad-render{ rootText, language }React only. #root changed.
playpad-files-change{ action, name, previousName?, files }A file was added, renamed or removed, by the reader or a method. Assigning files doesn't fire it.

Page-wide settings

Imported from code-playpad. Call the configure* functions before the runtime starts.

NameWhat it does
configurePython({ indexURL })Loads Pyodide from your own host.
configureTypeScript({ sucraseURL })Loads the TypeScript transform from your own URL.
configureCpp({ indexURL, timeoutMs })Loads the C/C++ compiler from your own host, and sets the run time limit.
configureCSharp({ indexURL, timeoutMs })Loads the C# compiler from your own host, and sets the run time limit.
configureReact({ runtimeURL, nonce })Loads the React runtime from your own URL, and sets a CSP nonce.
setThemePreference(change)Changes the reader's theme choice for every widget on the site.
getThemePreference()Returns { mode, light, dark }.
onThemePreferenceChange(listener)Calls back on every change, and returns a function that stops it.
EDITOR_THEMESThe list of editor themes.

React props

For the wrapper in code-playpad/react, shown on the Wrapper page. Where a prop is an attribute or event, that's what it sets.

PropSets
codeThe code, for one file.
filesEvery file, as { name: contents }.
entryentry
openFileopen-file
stdinThe standard input box's text.
languagelanguage
heightheight
minHeightmin-height
maxHeightmax-height
previewHeightpreview-height
treeWidthtree-width
fileViewfile-view
fileViewSwitchfile-view-switch
fileActionsfile-actions
themetheme
themeControlsfalse is theme-controls="off".
changeMarksfalse is change-marks="off".
readOnlyreadonly
autoRunautorun
persistpersist
practiceThe Java practice test cases.
classNameThe element's class.
styleThe element's style.
onReadyplaypad-ready
onRunplaypad-run
onOutputplaypad-output
onErrorplaypad-error