Svelte
The JS/TS SDK can be used directly in Svelte components without the need for any wrappers.
Installation
Please refer to the SDK installation section.
Usage
This is an example of using the LiveCodes JS SDK in a Svelte component:
Component.svelte
<script>
import { onMount } from 'svelte';
import { createPlayground } from 'livecodes';
// Embed Options
const options = {
params: {
html: '<h1>Hello World!</h1>',
css: 'h1 {color: blue;}',
js: 'console.log("Hello, Svelte!")',
console: 'open',
},
};
let container;
let playground = $state(null);
onMount(() => {
createPlayground(container, options).then((p) => {
playground = p; // now the SDK is available
});
// cleanup when the component is destroyed
return () => playground?.destroy();
});
</script>
<div bind:this="{container}"></div>
Embed options, SDK methods and TypeScript types are available as described in the JS/TS SDK documentations.
Alternatively, the SDK function createPlayground
can be used as an action.
Example:
Component.svelte
<script>
import { createPlayground } from 'livecodes';
let options = {
// embed options
};
</script>
<div use:createPlayground="{options}"></div>
However, it is recommended to cleanup when the node is unmounted, like that:
Component.svelte
<script>
import { createPlayground } from 'livecodes';
let options = {
// embed options
};
const livecodes = (node, opts) => {
let playground = $state(null);
const ready = new Promise(async (res) => {
playground = await createPlayground(node, opts);
res();
});
return { destroy: () => ready.then(() => playground?.destroy()) };
};
</script>
<div use:livecodes="{options}"></div>