Introducing Blokd and Blokd UI
What if a component could remember an edit the way JavaScript already remembers a local variable? A small stargazing bulletin shows how Blokd generators and browser templates work together.
When I started Blokd, I wanted to answer two questions. Where does a component keep a value between clicks? And when that value changes, how does the browser know what to update?
Take a text field for a meeting place. In React, I’d use useState to keep the address when the component function runs again. In Solid, I’d use a signal so the part of the view that reads the address can subscribe to it. What interested me was a third possibility. JavaScript already has a function that can pause with its local variables intact. The browser already has a way to clone a piece of HTML. Could those two things carry most of the work?
That was the first design decision for Blokd. A JavaScript generator remembers where an interaction is. A compiler turns its JSX into a template the browser can clone and points Blokd to the places that need updating. Here’s what that feels like in a small piece of software you can actually use.
Try the bulletin
Saturday’s stargazing bulletin says to meet on the north lawn, but the telescope is already on the west terrace. Type West terrace and choose Send for review. Read the invitation, request edits, and change the address once more. Send it back and choose Approve & preview. Which address made it into the reader’s edition?
What the component remembers
Start at SkyNotes. Read down from the input to yield* review(revision, invitation). That’s the point where the reviewer takes over.
import type { View } from '@fierst/blokd';
export default function* SkyNotes() {
let meetingPlace = 'North lawn';
for (let revision = 1; ; revision++) {
let submitted = '';
while (!submitted) {
yield bulletin('Note / Draft', revision,
<label>Where should we meet?
<input bind:value={meetingPlace} maxLength={60} />
</label>,
<button disabled={!meetingPlace.trim()}
onClick={() => (submitted = meetingPlace.trim())}>Send for review</button>
);
}
const invitation = <p class="dispatch">
Meet at <strong>{submitted}</strong> at dusk. Bring binoculars.
</p>;
const approved = yield* review(revision, invitation);
if (!approved) continue;
let nextEdition = false;
while (!nextEdition) {
yield bulletin('Note / Reader preview', revision, invitation,
<button onClick={() => (nextEdition = true)}>Draft next edition</button>
);
}
}
}
function* review(revision: number, invitation: View) {
let approved: boolean | undefined;
while (approved === undefined) {
yield bulletin('Flow / Review', revision, invitation, <>
<button onClick={() => (approved = false)}>Request edits</button>
<button onClick={() => (approved = true)}>Approve & preview</button>
</>);
}
return approved;
}
function bulletin(edition: string, revision: number, body: View, actions: View) {
return <section class="sky-note">
<p class="edition-line" role="status">{edition} · Revision {revision}</p>
<h2>Saturday stargazing</h2>
{body}
<div class="dispatch-actions">{actions}</div>
</section>;
}
The star in function* SkyNotes() lets JavaScript pause at yield with the local variables still in scope. The first time through, you see the draft. As you type, bind:value updates meetingPlace and Blokd schedules the generator’s next turn. The loop keeps showing the input while submitted is empty. On each turn, the button checks !meetingPlace.trim(), so Send for review stays disabled if the field is empty or contains only spaces.
Choose Send for review and the handler copies the trimmed address into submitted. On the next turn the draft loop ends. The generator builds invitation from that copy and hands it to review. Request edits sets approved to false. When review returns that decision, continue takes the outer loop to its next revision with your address still in the field. Approve & preview sets approved to true, so the reader sees the same invitation the reviewer saw. It stays on screen until Draft next edition sets nextEdition to true.
Where you declare a variable tells you how long it lives. meetingPlace sits above the revision loop and survives every trip around it. Each revision starts with an empty submitted. That’s why the next draft remembers the address and still waits for you to send it again.
What the browser updates
Look at “Saturday stargazing.” The plain bulletin function near the bottom of the listing supplies that heading on every screen. Its arguments supply the parts that change, including the edition label, the invitation, and the buttons.
At build time, Blokd turns the static markup into an HTML <template> and records paths to the dynamic parts. On mount, the browser clones the template into real DOM nodes. On subsequent turns, Blokd compares the values in those slots and patches the ones that changed. The heading stays in place. There’s no virtual tree to rebuild and reconcile to find it.
This is the division of work I wanted. I can read the TypeScript to see how long a value lives, and inspect the browser’s DOM to see which element is on screen. The compiler does the bookkeeping between them.
When an interaction grows
At the end of review, the generator returns approved. That value goes straight back to the line that called yield* review(…). While the reviewer is deciding, JavaScript keeps the caller’s place and Blokd displays the view from the delegated generator. Each open bulletin has its own meeting place and decision. For a value that several components need to observe, Blokd also offers a shared signal.
A component also needs a way to finish. On unmount, Blokd closes the generator, which lets its author release resources in a finally block. For an async function*, Blokd also supplies ctx.signal, a browser AbortSignal. Pass it to fetch and an outstanding request can be canceled when the component goes away.
There’s a learning curve here. If you’ve rarely used generators, it takes a moment to see that yield pauses the function and that the next update continues from that point. Once that clicks, the loop, the local variables, and the cleanup all use JavaScript rules you can take to another project. Read ADR 0001, the original design decision
Where Blokd UI fits
The bulletin uses ordinary HTML inputs and buttons. On the Fierst homepage, a Blokd UI example viewer wraps that same component. Open its source, change at dusk to at midnight, and run the edit. The invitation changes because you edited the line that renders it. The copy, edit, and run controls belong to the viewer around it. Try the homepage viewer
Blokd UI is a registry of components you can install individually. Run npx blokd-ui add button inside a Blokd project and you get the button’s source in your project. You can trace its behavior, change its markup, or give it your site’s colors through the shared theme. Hue picks the color family. Chroma controls how vivid that color feels. That lets a button and a viewer look like they belong to the same site even after you’ve customized how each one works.
To try both tools from scratch, start with npx blokd night-sky --install, then cd night-sky and npm run dev. Get started with Blokd · Browse Blokd UI
From the bulletin to this note
You could close the bulletin’s tab and lose the address. That’s fine for a demonstration. An engineering note needs a draft I can return to tomorrow, and a reviewer needs to know exactly which revision they approved.
At Fierst I write that draft in Note. When I send it for review, Flow binds the decision to the submitted revision. Think back to the moment the bulletin copied meetingPlace into submitted. If I move the telescope again after you’ve read the invitation, your approval belongs to the address you saw. The demo lets you feel that distinction in a few clicks. Note and Flow carry it through to the article you’re reading. Explore Note · Explore Flow