Skip to main content

Embedding a Code Playground

Learn how to embed a LiveCodes playground into any web page so your users can write, edit, and run code directly on your site.

Try the live demo below:

show code
import { createPlayground } from 'livecodes';

const options = {
"params": {
"html": "<div id=\"page\">\n <header>\n <h1>My Code Playground</h1>\n <p>Edit the code below and see the result instantly!</p>\n </header>\n <div id=\"playground\"></div>\n</div>\n",
"css": "body {\n font-family: system-ui, -apple-system, sans-serif;\n margin: 0;\n background: #f5f5f5;\n}\n#page {\n max-width: 900px;\n margin: 0 auto;\n padding: 20px;\n}\nheader {\n text-align: center;\n margin-bottom: 20px;\n}\nh1 {\n margin: 0 0 8px;\n color: #1a1a1a;\n}\np {\n margin: 0;\n color: #666;\n}\n#playground {\n border: 1px solid #ddd;\n border-radius: 8px;\n overflow: hidden;\n height: 500px;\n}\n",
"js": "import { createPlayground } from 'livecodes';\n\nconst config = {\n markup: {\n language: 'html',\n content: `<div class=\"card\">\n <h2>Hello LiveCodes!</h2>\n <p>Edit this code and see changes live.</p>\n <button id=\"actionBtn\">Click me</button>\n</div>`,\n },\n style: {\n language: 'css',\n content: `body {\n font-family: system-ui;\n display: flex;\n justify-content: center;\n align-items: center;\n min-height: 100vh;\n margin: 0;\n background: #f0f0f0;\n}\n.card {\n background: white;\n padding: 40px;\n border-radius: 12px;\n box-shadow: 0 4px 20px rgba(0,0,0,0.1);\n text-align: center;\n}\nh2 {\n margin: 0 0 12px;\n color: #333;\n}\nbutton {\n padding: 10px 24px;\n background: #007bff;\n color: white;\n border: none;\n border-radius: 6px;\n cursor: pointer;\n font-size: 16px;\n}\nbutton:hover {\n background: #0056b3;\n}`,\n },\n script: {\n language: 'javascript',\n content: `const btn = document.getElementById(\"actionBtn\");\nbtn.addEventListener(\"click\", () => {\n btn.textContent = \"Clicked!\";\n setTimeout(() => {\n btn.textContent = \"Click me\";\n }, 1000);\n});`,\n },\n};\n\nawait createPlayground('#playground', {\n config,\n loading: 'eager',\n});\n"
}
};
createPlayground('#container', options);

What We'll Build

A web page that:

  1. Embeds a LiveCodes playground in a styled container
  2. Pre-fills the playground with starter code
  3. Lets users edit HTML, CSS, and JavaScript directly
  4. Shows live results as they type

Basic Embedding

The simplest way to embed LiveCodes is with createPlayground:

import { createPlayground } from 'livecodes';

const config = {
markup: { language: 'html', content: '<h1>Hello!</h1>' },
style: { language: 'css', content: 'h1 { color: blue; }' },
script: { language: 'javascript', content: 'console.log("hi")' },
};

await createPlayground('#playground', { config });

This creates an interactive editor inside the #playground element.

Loading Modes

LiveCodes supports three loading modes:

Eager Loading (Default)

The playground loads immediately:

createPlayground('#playground', {
config,
loading: 'eager',
});

Lazy Loading

The playground only loads when it scrolls into view:

show code
import { createPlayground } from 'livecodes';

const options = {
"params": {
"html": "<div id=\"page\">\n <h1>Lazy Loaded Playground</h1>\n <p>Scroll down to see the playground load automatically.</p>\n <div class=\"spacer\"></div>\n <div id=\"playground\"></div>\n</div>\n",
"css": "body {\n font-family: system-ui, sans-serif;\n margin: 0;\n padding: 20px;\n}\n.spacer {\n height: 80vh;\n display: flex;\n align-items: center;\n justify-content: center;\n color: #999;\n}\n#playground {\n height: 400px;\n border: 1px solid #ddd;\n border-radius: 8px;\n}\n",
"js": "import { createPlayground } from 'livecodes';\n\nconst config = {\n markup: {\n language: 'html',\n content: '<h2>I loaded lazily!</h2><p>This playground only loads when it enters the viewport.</p>',\n },\n};\n\ncreatePlayground('#playground', {\n config,\n loading: 'lazy',\n});\n"
}
};
createPlayground('#container', options);

createPlayground('#playground', {
config,
loading: 'lazy',
});

Click-to-Load

Shows a "Click to load" screen until the user clicks:

show code
import { createPlayground } from 'livecodes';

const options = {
"params": {
"html": "<div id=\"page\">\n <h1>Click-to-Load Playground</h1>\n <p>The playground below won't load until you click it.</p>\n <div id=\"playground\"></div>\n</div>\n",
"css": "body {\n font-family: system-ui, sans-serif;\n margin: 0;\n padding: 20px;\n}\n#playground {\n height: 400px;\n border: 1px solid #ddd;\n border-radius: 8px;\n}\n",
"js": "import { createPlayground } from 'livecodes';\n\nconst config = {\n markup: {\n language: 'html',\n content: '<h2>You clicked to load me!</h2>',\n },\n};\n\ncreatePlayground('#playground', {\n config,\n loading: 'click',\n});\n"
}
};
createPlayground('#container', options);

createPlayground('#playground', {
config,
loading: 'click',
});

Full Example

Save this as index.html and open it in a browser:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Embedded LiveCodes Playground</title>
<style>
body {
font-family: system-ui, -apple-system, sans-serif;
margin: 0;
background: #f5f5f5;
}
#page {
max-width: 900px;
margin: 0 auto;
padding: 20px;
}
header {
text-align: center;
margin-bottom: 20px;
}
h1 {
margin: 0 0 8px;
color: #1a1a1a;
}
p {
margin: 0;
color: #666;
}
#playground {
border: 1px solid #ddd;
border-radius: 8px;
overflow: hidden;
height: 500px;
}
</style>
</head>
<body>
<div id="page">
<header>
<h1>My Code Playground</h1>
<p>Edit the code below and see the result instantly!</p>
</header>
<div id="playground"></div>
</div>

<script type="module">
import { createPlayground } from 'https://cdn.jsdelivr.net/npm/livecodes';

const config = {
markup: {
language: 'html',
content: '<div class="card">
<h2>Hello LiveCodes!</h2>
<p>Edit this code and see changes live.</p>
<button id="actionBtn">Click me</button>
</div>',
},
style: {
language: 'css',
content: 'body {
font-family: system-ui;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
background: #f0f0f0;
}
.card {
background: white;
padding: 40px;
border-radius: 12px;
box-shadow: 0 4px 20px rgba(0,0,0,0.1);
text-align: center;
}
h2 {
margin: 0 0 12px;
color: #333;
}
button {
padding: 10px 24px;
background: #007bff;
color: white;
border: none;
border-radius: 6px;
cursor: pointer;
font-size: 16px;
}
button:hover {
background: #0056b3;
}',
},
script: {
language: 'javascript',
content: 'const btn = document.getElementById("actionBtn");
btn.addEventListener("click", () => {
btn.textContent = "Clicked!";
setTimeout(() => {
btn.textContent = "Click me";
}, 1000);
});',
},
};

await createPlayground('#playground', {
config,
loading: 'eager',
});
</script>
</body>
</html>

How It Works

Container Setup

LiveCodes needs a container element. You can use any div:

<div id="playground"></div>

By default, the playground has a height of 300px. You can override this with CSS:

#playground {
height: 500px;
}

Or use the data-height attribute:

<div id="playground" data-height="500"></div>

Configuration

Pass a config object to pre-fill the playground:

const config = {
markup: {
language: 'html',
content: '<h1>Hello World</h1>',
},
style: {
language: 'css',
content: 'h1 { color: royalblue; }',
},
script: {
language: 'javascript',
content: 'console.log("Hello!");',
},
};

CDN Import

For quick testing without a build step, import from jsDelivr:

<script type="module">
import { createPlayground } from 'https://cdn.jsdelivr.net/npm/livecodes';
// ...
</script>

Testing Your App

  1. Save the HTML example to a file
  2. Open it in your browser
  3. Edit the code in the embedded playground
  4. Watch the result update live

Challenge: Enhance Your Playground

Try adding these features:

  • Configure the playground with a custom theme
  • Add a "Reset" button to restore the original config
  • Add a language selector to switch between different starter templates
  • Style the playground container to match your site's design

Next Steps

Complete Code Summary

Concepts Covered: Embedding, container setup, loading modes, configuration, CDN import

Key SDK Functions: createPlayground