Svelte#
OneJS renders React by default, and Svelte 5 is the other choice. A Svelte app runs on the same runtime: the same UI Toolkit elements, styles, events, Tailwind and hot reload, with no React installed or bundled. Install onejs-svelte and write .svelte components.
Setup#
Make the app as usual (add a JSRunner, then Initialize Project). In its ~ folder, run:
npx onejs-svelte initinit swaps the React packages for Svelte's (and TypeScript 7 for 6, the newest svelte-check runs on), writes index.ts, App.svelte and svelte.config.js, adds the Svelte plugin to esbuild.config.mjs, points tsconfig.json at the Svelte types, then installs and builds. It replaces a file only if it still holds what OneJS wrote. A file you edited stays as it is, and init prints the change to make by hand.
If the editor was open, enter and leave Play mode once so its watcher picks up the new config.
By hand#
The setup is a plugin in the build config and a three-line entry file:
// esbuild.config.mjs
import { oneJSConfig } from "onejs-unity/esbuild"
import { sveltePlugin } from "onejs-svelte/esbuild"
export default oneJSConfig({
entry: "index.ts",
tailwind: { content: ["./**/*.{svelte,ts,js}"] },
plugins: [sveltePlugin()],
})// index.ts
import { render } from "onejs-svelte"
import App from "./App.svelte"
render(App, __root)Install with npm install onejs-svelte svelte@5.59.2. The plugin compiles .svelte files against the OneJS renderer and turns each component's <style> into USS. The editor gets Svelte's development build, with its warnings. A player build gets the production build.
Components#
Tags are UI Toolkit elements, named like React's components but in lowercase: view, text, label, button, textfield, toggle, slider, scrollview, image. They take the same props as the components (text, label, value, lowValue, ...), with class in place of className.
A value in quotes is a string and a bare attribute is the empty string, as in HTML, so numbers and booleans go in braces: maxLength={5}, multiline={true}.
<script lang="ts">
let count = $state(0)
let name = $state("")
</script>
<view class="card">
<label text={`Hello, ${name || "stranger"}`} />
<textfield label="Name" value={name} onchange={(e) => name = e.value} />
<button text={`Clicked ${count} times`} onclick={() => count++} />
</view>
<style>
.card {
padding: 16px;
background-color: rgb(40, 40, 48);
}
</style>Runes, components, {#if}, keyed {#each}, {#await}, {#key}, snippets, class:, style:, stores and TypeScript all work as in Svelte on the web. A <style> block stays scoped to its component. For a list, use a keyed {#each} inside a <scrollview>: ListView and TreeView render their rows through React.
Events#
An event attribute is the event name with on in front: onclick, onpointerdown, onkeydown, onchange. Its argument carries the same event data a React handler gets. A field's onchange carries the new value as e.value.
Two-way binding#
Svelte does not allow bind: on an element under a custom renderer, so write the two halves yourself:
<textfield value={text} onchange={(e) => text = e.value} />bind: on a component's prop works.
Actions#
A use: action or an {@attach} gets a node whose element is the VisualElement:
<script>
const focus = (node) => { node.element.Focus() }
</script>
<textfield use:focus />What Svelte refuses#
Svelte rejects these with a compile error under any custom renderer, OneJS included:
bind:value,bind:thisand every otherbind:on an elementtransition:,in:,out:andanimate:. Use USS transitions instead.on:clickand the otheron:directives. Useonclick.{@html}<svelte:window>,<svelte:head>,<svelte:body>and<svelte:document>
Types#
init adds two settings to tsconfig.json, which tell svelte-check and the Svelte editor extension which tags and props exist:
{
"compilerOptions": { "types": ["svelte", "onejs-svelte/elements"] },
"svelteOptions": { "namespace": "svelteOneJS" }
}With them, text={count} with a number is an error, and so is an HTML tag such as <div>. An action's node is typed as the renderer's, with its element. npm run typecheck runs svelte-check.
svelte.config.js hides Svelte's HTML warnings (accessibility, self-closing tags), which do not apply to UI Toolkit elements.
Tailwind#
As in a React app, import "onejs:tailwind" once in index.ts, then use classes in your components. The tailwind.content line above makes the build scan .svelte files for them.
onejs-unity#
These parts of onejs-unity work in a Svelte app: assets, audio, interop, sl and physics2d. The root module, input, gpu, fx and proc are React hooks, so they need React.
Svelte versions#
Svelte's custom renderer API is new (Svelte 5.59) and still experimental, so onejs-svelte requires the exact Svelte version it was tested with. Every night it is tested against the latest Svelte. When a new Svelte release passes, a new onejs-svelte release moves to it.