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.
Attributes
| Attribute | What it does |
|---|---|
language | python (default), javascript, typescript, java, c, cpp, csharp (or cs), react. Which language the widget runs. |
height | A number (pixels), any CSS length, or auto to fit the code. |
min-height | The smallest height with height="auto". |
max-height | The largest height with height="auto"; longer code scrolls inside it. |
preview-height | react only. The height of the preview. |
tree-width | The 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-view | How several files are listed: tabs (default) or tree. |
file-view-switch | Shows a toolbar button that switches between tabs and the tree. It only shows when the widget has a file list. |
file-actions | Lets the reader add, rename and remove files. readonly turns it off. |
open-file | Which file is on screen to begin with, if not the entry. |
entry | Which file Run executes. A name that isn't one of the files is an error. |
theme | auto (default), light or dark. light and dark pin the widget and hide its theme controls. |
theme-controls | off hides the theme button and the editor theme list. |
change-marks | off hides the marks that show code hasn't run yet: the dot on Run, ● and ○ on files, and the note over the output. |
readonly | No value. The editor can't be typed into, but Run still works. |
autorun | No value. Runs once, on its own, when scrolled into view. |
stdin | No 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. |
persist | A key to save the reader's edits under, in IndexedDB. They return on the next visit. |
practice | Java 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:
- Its
theme="light"ortheme="dark"attribute. - The reader's saved choice.
- The page's
color-scheme. - 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 theme | Id | Mode |
|---|---|---|
| One Dark | default-dark | dark |
| Monokai | monokai | dark |
| Dracula | dracula | dark |
| Nord | nord | dark |
| Solarized Dark | solarized-dark | dark |
| Default Light | default-light | light |
| GitHub Light | github-light | light |
| Solarized Light | solarized-light | light |
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;
}
| Variable | What it sets |
|---|---|
--cp-bg | The background of the widget and its buttons. |
--cp-fg | Text. |
--cp-muted | Secondary text, such as status messages. |
--cp-border | Borders. |
--cp-surface | The toolbar and tabs. |
--cp-accent | Run, the focus ring and the active tab. |
--cp-accent-fg | Text on the accent colour. |
--cp-danger | Stop and errors. |
--cp-ok | Success messages. |
--cp-radius | Corner radius of the widget. |
--cp-font-mono | The font for code and output. |
--cp-font-ui | The font for buttons and labels. |
Parts
code-playpad::part(run-button) { border-radius: 999px; }
| Part | What it is |
|---|---|
run-button | The Run button. |
stop-button | The Stop button. |
file-view-button | The button that switches between tabs and the tree, with file-view-switch. |
theme-button | The light and dark button. |
editor-theme-picker | The editor theme list. |
window-button | The Open in window button, on React widgets. |
tabs | The file tabs or tree. |
output | The output area. |
stale-note | The "Code changed since this run" note over the output. |
notice | A 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. |
preview | The React preview area. |
JavaScript
On the element, such as document.querySelector('code-playpad').run().
| Name | Kind | What it does |
|---|---|---|
code | Property | The visible file's code. Setting it replaces that file. |
files | Property | Every file, as { name: contents }. Setting it replaces the project. |
entry | Property | Which file Run executes. |
stdin | Property | The standard input box's text. |
language | Property | The language. |
practice | Property | Java practice test cases, or null. |
run() | Method | Runs the code, and resolves to the result. |
stop() | Method | Stops this widget's run, queued or running. |
reset() | Method | Goes back to the code the page shipped. |
prewarm() | Method | Starts the runtime before the reader presses Run. |
getCode() | Method | Returns the visible file's code. |
getFiles() | Method | Returns every file. |
openFile(name) | Method | Brings a file on screen. |
addFile(name, contents) | Method | Adds a file and opens it. Throws if a rule refuses the name. |
renameFile(from, to) | Method | Renames a file. Throws if a rule refuses it. |
removeFile(name) | Method | Removes a file. Throws if a rule refuses it. |
openWindow() | Method | Moves the project to a separate window. Call it from a click, because browsers block other popups. Returns false if blocked. |
closeWindow() | Method | Closes the window, and brings its edits back. |
Events
Events bubble out of the widget, so you can listen on document. Read event.detail.
| Event | Detail | When |
|---|---|---|
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.
| Name | What 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_THEMES | The 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.
| Prop | Sets |
|---|---|
code | The code, for one file. |
files | Every file, as { name: contents }. |
entry | entry |
openFile | open-file |
stdin | The standard input box's text. |
language | language |
height | height |
minHeight | min-height |
maxHeight | max-height |
previewHeight | preview-height |
treeWidth | tree-width |
fileView | file-view |
fileViewSwitch | file-view-switch |
fileActions | file-actions |
theme | theme |
themeControls | false is theme-controls="off". |
changeMarks | false is change-marks="off". |
readOnly | readonly |
autoRun | autorun |
persist | persist |
practice | The Java practice test cases. |
className | The element's class. |
style | The element's style. |
onReady | playpad-ready |
onRun | playpad-run |
onOutput | playpad-output |
onError | playpad-error |