README.md

@burokku/react #

A React renderer for Burokku's UI tree. Host components are Burokku's elements (div, flex, grid, text, and window), and React commits changes to them directly through the app global.

import { useState } from "react";
import { createRoot } from "@burokku/react";

function Counter() {
  const [count, setCount] = useState(0);
  return (
    <window title={`Clicked ${count}`}>
      <flex style={{ padding: "16px", gap: "8px" }} onClick={() => setCount(count + 1)}>
        <text style={{ fontSize: "16px", color: "#f6f6f7" }}>Clicked {count} times</text>
      </flex>
    </window>
  );
}

createRoot().render(<Counter />);

example/react is a complete application, with the Rust binary that runs it.

Setup #

Point TypeScript's JSX at this package, so tags and their props are Burokku's:

{
  "compilerOptions": {
    "jsx": "react-jsx",
    "jsxImportSource": "@burokku/react"
  }
}

Bundle the application into one script for BurokkuBuilder::script, with process.env.NODE_ENV defined, since QuickJS has no process. Vite's library mode does this, and compiles JSX as tsconfig.json says:

import { defineConfig } from "vite";

export default defineConfig({
  define: { "process.env.NODE_ENV": JSON.stringify("production") },
  build: {
    lib: {
      entry: "src/main.tsx",
      formats: ["iife"],
      name: "App",
      fileName: () => "app.js",
    },
  },
});

Rendering #

createRoot(container?, options?) renders into the app by default; the app holds only window elements. It can also render into an element already in the tree. As with React DOM, render commits on React's schedule, the first render replaces whatever the container held, and unmount removes everything synchronously. options takes React DOM's onUncaughtError, onCaughtError, and onRecoverableError.

flushSync(callback) commits the updates callback schedules before returning.

Props #

  • style takes the camel-case keys setStyles does, typed per tag. A key that becomes undefined or null is removed from the element.
  • A prop named on and a capital letter handles the event named by the rest in lower case: onPointerDown handles pointerdown. So onFocus handles the non-bubbling focus, and onFocusIn the bubbling focusin. Updates a handler makes take the priority React DOM gives that event, so a click's update commits before its task ends.
  • ref receives the element.
  • Every other prop sets the attribute of its name, and undefined or null removes it. Burokku reads tabindex on any element, and title, width, and height on windows.

Strings render as text nodes, which only a text or an input element may hold; elsewhere, rendering one throws. An input's text is its value.

Not supported #

  • Hiding elements: the UI tree has no display: none, so the elements inside <Activity mode="hidden">, and inside a Suspense boundary that suspends again, stay visible. Only the text in them hides, including text that changes or goes in while they are hidden, and it shows again with them.
  • <ViewTransition>, refs on fragments, portals, and hydration.