Skip to content

Typed components and recursive children

Read as Markdown

Build a component with createElement, group its children in a Fragment, and render textual children.

Plain source. Run as a browser module after the document body exists. TSX uses jsx: "react-jsx" and jsxImportSource: "@pibbl/core".

/** @jsxImportSource @pibbl/core */
import {
pibbl,
createElement,
Fragment,
Text,
type PibblComponent,
type PibblElement,
type PibblElementType,
type PibblKey,
type PibblNode,
type PibblNodeInput,
type PibblContainerChild,
type PibblEmptyNode,
type PibblTextChild,
type PibblTextInput,
type PibblRenderResult,
} from "@pibbl/core";
const canvas = document.createElement("canvas");
canvas.width = 320;
canvas.height = 120;
document.body.append(canvas);
const Label: PibblComponent<{ value: string }> = ({ value }): PibblRenderResult => {
const words: PibblTextChild = ["Hello ", value];
const text: PibblTextInput = words;
return (
<Text
style={{ left: 20, top: 50, font: "24px sans-serif", fill: "#267d7c" }}
>
{text}
</Text>
);
};
const component: PibblElementType<{ value: string }> = Label;
const key: PibblKey = "greeting";
const element: PibblElement<{ value: string }> = createElement(component, {
value: "Pibbl",
key,
});
const empty: PibblEmptyNode = null;
const nodes: PibblNode = [element, empty];
const input: PibblNodeInput = nodes;
const children: PibblContainerChild = input;
const controller = pibbl(canvas, <Fragment>{children}</Fragment>);
window.addEventListener("pagehide", () => controller.dispose(), { once: true });
  • Textual values belong inside Text; arbitrary strings are not general scene nodes. Components are synchronous and createElement is equivalent to automatic JSX.

Documentation built with @pibbl/core 0.0.2, revision 380919b. ALPHA — NOT FOR PRODUCTION USE.