Rook
РусскийRU

One state. Two updates.

Change a value and follow the pulse through its dependency graph. Only the DOM nodes that read it update; the independent label stays still.

Ready · count = 0

Rook source

comp Counter() {
    state count = 0
    return <main>
        <button onClick={count++}>{count}</button>
        <p>{count * 2}</p>
        <small>{"always here"}</small>
    </main>
}
document.body.append(<Counter />)

Dependency graph

statecount = 0
button text0
paragraph0

Live DOM preview

updated nodes flash

0

always here

Components

A component is declared with comp and used as a tag with a capital letter. Its parameters are its properties: they are checked like the arguments of a function, they can have default values, and children Content gets the markup between the tags. An object with the properties can be passed whole: <Product {...product} />.

Components do not exist in JS: every use is replaced by the component's code. The JavaScript tab shows only the elements being created. When the copies of a component would add more than 100 lines to a function and its calls, the component becomes a function instead. Elements are created by $$element of the runtime, which takes their attributes.

type Priority "low" | "normal" | "high"

interface Task {
    id number
    title string
    done bool
    priority Priority
}

comp TaskRow(task Task) {
    return <li>
        <label><input type="checkbox" checked={task.done} />{task.title}</label>
        <small>{task.priority}</small>
    </li>
}

const tasks Task[] = [
    { id: 1, title: "Write the task model", done: true, priority: "normal" },
    { id: 2, title: "Build the task board", done: false, priority: "high" },
]

document.body.append(
    <ul>{for task in tasks { <TaskRow task={task} /> }}</ul>,
)

On the task board, a TaskRow receives one task and renders its title and controls. The parent keeps the collection; each row owns only the presentation of one item.

The last property ...attrs takes the attributes of a use that are not properties, and {...attrs} puts them on one element of the component. They are checked as attributes of that element, so a button keeps all of its own: disabled, type, aria-*, handlers.

// ...attrs takes the attributes that are not properties, and {...attrs} puts them on the button:
// type, disabled and onClick are checked as attributes of <button>
comp Button(kind "primary" | "ghost" = "primary", children Content, ...attrs) {
    return <button class={kind} type="button" {...attrs}>{children}</button>
}

comp Form() {
    state saved = 0
    return <p>
        <Button onClick={saved++} title="Save the draft">Save</Button>
        <Button kind="ghost" disabled={saved == 0} onClick={saved = 0}>Reset</Button>
        Saved {saved} times
    </p>
}

document.body.append(<Form />)

Properties are read-only, except bound ones. A component that changes a value of the parent, a panel that closes or tabs that switch, declares the property with bind, comp Panel(bind open bool), and changes it as its state. The use writes the changes into its variable with bind:open={menu}, as bind:value of an input does, so it needs no pair of open and onClose. Without bind: the property is the component's own state. The item of a for can be bound too, bind:todo={todo}: what the component changes in it updates the list.

// A bound property is one the component changes itself: bind:open
// writes its changes into the parent's variable, as bind:value does
comp Panel(bind open bool = false, title string, children Content) {
    return <section class="card" hidden={!open}>
        <h4>{title} <button onClick={open = false}>×</button></h4>
        {children}
    </section>
}

interface Todo {
    title string
    done bool
}

// The item of a for is bound too: what the component changes updates the list
comp Check(bind todo Todo) {
    return <label><input type="checkbox" bind:checked={todo.done} /> {todo.title}</label>
}

comp Page() {
    state menu = false
    state todos Todo[] = [{ title: "Buy guavas", done: false }, { title: "Wash limes", done: true }]
    return <div>
        <button onClick={menu = !menu}>{menu ? "Hide" : "Show"} the menu</button>
        <Panel bind:open={menu} title="Menu">Close it with ×: the button above knows.</Panel>
        {/* Without bind: the property is the panel's own state */}
        <Panel open={true} title="Note">This one keeps its state to itself.</Panel>
        {for todo in todos { <Check bind:todo={todo} /> }}
        <p>{todos.filter(t => t.done).length} done</p>
    </div>
}

document.body.append(<Page />)

A component can use itself, for a tree or nested comments. Such a component cannot be inlined, so it becomes a function, and every call of it has its own state. The recursion must end: the compiler accepts a use inside {if ...} or {for ...} or after an early return, but not an unconditional one. An early return picks the markup once, when the component is created.

interface Item {
    name string
    children Item[]
}

// A recursive component becomes a function: every node has its own open state
comp Tree(item Item, depth number = 0) {
    state open = depth == 0
    return <li>
        <button onClick={open = !open}>{item.children.length == 0 ? "·" : open ? "−" : "+"}</button>
        {item.name}
        {if open && item.children.length > 0 {
            <ul>{for child in item.children { <Tree item={child} depth={depth + 1} /> }}</ul>
        }}
    </li>
}

const project Item = {
    name: "Rook",
    children: [
        {
            name: "src",
            children: [
                { name: "lexer", children: [{ name: "lexer.ts", children: [] }] },
                { name: "parser", children: [{ name: "parser.ts", children: [] }] },
                { name: "index.ts", children: [] },
            ],
        },
        { name: "README.md", children: [] },
    ],
}

document.body.append(<ul class="tree"><Tree item={project} /></ul>)

A component exported with export comp is imported by its name, as in import { Card } from "./card.rk", and used as a tag. Its module compiles it to a function, which other modules call, or inline it when it is small, by the same 100 lines.

A component is also a value: it can go into an array, a property or state. Its type lists the properties with their names, comp(name string), and a tag takes the component from a value: a tag with a dot, <tab.content />, or a variable with a capital letter, <Current />. The tag passes the properties in the order of the type, so a component fits it with the same names in the same order. Such a component becomes a function; when the value changes, it is created again.

interface Tab {
    title string
    content comp(name string)
}

comp Greeting(name string) {
    return <p>Hello, {name}!</p>
}

comp Farewell(name string) {
    mount {
        console.log("Farewell is shown")
        defer console.log("Farewell is removed")
    }
    return <p>Goodbye, {name}.</p>
}

// Components in data: a tag takes one from a value
const TABS Tab[] = [
    { title: "Hello", content: Greeting },
    { title: "Goodbye", content: Farewell },
]

comp Tabs() {
    state name = "Ann"
    state Current comp(name string) = Greeting
    return <div>
        <input bind:value={name} />
        {for tab in TABS {
            <button onClick={Current = tab.content}>{tab.title}</button>
        }}
        <Current name={name} />
    </div>
}

document.body.append(<Tabs />)