Skip to content

Components and children

A component is a normal function with typed props. Call it inside the factory passed to mount, a branch, or a list row. The function creates its nodes and bindings. It runs again when a new instance mounts.

import { mount, signal } from "min-reactive-framework";
import type { ReadonlySignal } from "min-reactive-framework";
import { p, section } from "min-reactive-framework/html";
function Profile({ name }: { name: ReadonlySignal<string> }) {
return section(p("Name ", name));
}
const name = signal("Ada");
const stop = mount(document.querySelector("#app")!, () => Profile({ name }));
name.value = "Mina";

Pass a plain prop for a setup-time value and a signal for a value the child will observe. You can pass an ordinary callback for an action. An event handler can read the current .value at the time of the event.

HTML and SVG builders take children directly, as in section(p("Hello"), p("World")). A user-defined container can take children?: Children. The type is a factory () => Child, so the container decides when to create its content. Use the same type for named slots such as header and footer.

A lazily mounted slot

Loading the example…

View source
children.ts
import { mount, signal, when } from "min-reactive-framework";
import type { Children, Dispose, ReadonlySignal } from "min-reactive-framework";
import { button, div, p, section, strong } from "min-reactive-framework/html";
import type { DemoLanguage } from "../example-language.js";
interface DetailsProps {
title: string;
open: ReadonlySignal<boolean>;
children?: Children;
}
function Details({ title, open, children }: DetailsProps) {
return section(
{ class: "mr-demo-panel" },
p(strong(title)),
when(open, () => children?.()),
);
}
export function run(target: HTMLElement, lang: DemoLanguage): Dispose {
const ru = lang === "ru";
const open = signal(false);
return mount(target, () =>
div(
{ class: "mr-demo-stack" },
button(
{
type: "button",
"aria-expanded": open,
on: {
click: () => {
open.value = !open.value;
},
},
},
ru ? "Переключить содержимое" : "Toggle content",
),
Details({
title: ru ? "Компонент Details" : "Details component",
open,
children: () =>
p(ru ? "Содержимое создано при открытии." : "This content was created on open."),
}),
),
);
}

A Child can be an element, text node, comment, document fragment, scalar text binding, or nested array of children. null, undefined, and booleans produce empty content. 0 renders as text. A function passed directly as a child computes a scalar. Use when for a function that creates a subtree.

mount(target, App) adds its range to an Element or ShadowRoot. Use onMount to read the DOM, set focus, or start a resource after insertion. Register cleanup with onCleanup in the owner that created the resource. Call the disposer returned by mount to remove that range and release its bindings. Multiple calls are safe.

See rendering and lifetime for the order of updates and the scope of list rows and branches.