24:19

Why Elements doesn't use React

9

Elements ships with its own reactive HTML language. It is plain HTML, with TypeScript expressions in braces and templates that take typed attributes the way a function takes parameters. Change the data and the page updates. There is no React, no virtual DOM and no state hooks. This post is the short tour. The video above goes deeper, with the React comparison and a look at how the runtime does it.

Here is a complete page.

app/pages/home/template.ehtml
<html (title: string, private count: number = 0)> <h1>{title}</h1> <button onclick={() => count++}>Clicked {count} times</button> </html>

The () after the tag name declares the constructor attributes. They are typed like function parameters. title is public, so the route or a parent template sets it. count is private, so the template owns it and callers cannot set it. Both are reactive automatically. The {count} in the button body updates when the click handler increments it, and nothing else on the page is touched. There is no useState, no setter, no effect and no dependency list to get wrong.

The route fills the public attributes, often from the database.

app/pages/home/index.ts
import { Request, Response } from "@elements/app"; import html from "./template"; export default function route(req: Request, res: Response) { return new html({ title: req.params.title ?? "Hello World!" }); }

The page renders on the server, so the first response is the finished HTML. The runtime then attaches to that HTML in the browser. It does not throw the page away and rebuild it. From there the page is reactive: handlers run in the browser, and changes patch the DOM in place.

Any top-level tag with a capitalized name is a template. Templates take constructor attributes too, and you call one like an HTML tag.

app/pages/home/template.ehtml
<html (title: string)> <Welcome greeting={title} /> </html> <Welcome (greeting: string)> <h1>{greeting}</h1> </Welcome>

Templates are TypeScript symbols. Pass an attribute the template does not declare, or the wrong type, or leave out a required one, and the build reports an error. Rename and find references work across templates in your editor the same way they work across functions.

Loops and conditions are attributes on the element itself, prefixed with e:.

app/pages/home/template.ehtml
<ul> <li e:for={user of users}>{user.name}</li> </ul> <p e:if={users.length === 0}>no users yet</p> <p e:else>{users.length} users</p>

e:for iterates any iterable and keys the rows by id, so when one row changes, one row is patched, and a row that moved is moved rather than re-rendered. e:if, e:elseif and e:else are siblings. There is an e:switch with e:case and e:default for the longer cases.

Form inputs are bound both ways. Type in the input and the variable changes. Change the variable and the input changes.

app/pages/home/template.ehtml
<Login (private form: LoginForm = { email: "", password: "" }, private error: string = "")> <form onsubmit={() => error = login(form)}> <input value={form.email}> <input type="password" value={form.password}> <p e:if={error} class="error">{error}</p> <button>Sign in</button> </form> </Login>

That is the login form, nothing left out. The same form in React needs a useState for each field, an onChange handler for each input and a setter call in each handler. Here the reactive parts are the expressions in braces, and the runtime does the rest.

A class attribute takes an array, and falsy entries are dropped, so a class comes and goes with a boolean.

app/pages/home/template.ehtml
<button class={[active && "is-primary"]} onclick={() => active = !active}>Toggle Me</button>

Under the hood there is no virtual DOM. The runtime knows which data each binding read, and when that data changes it patches that one text node, attribute or element. There is no component function to re-run, no tree to rebuild and no diff to compute. Work is proportional to the change, not to the size of the page.

The page's attributes are live in the browser console as $. Assign to one and the page updates through the same path the template uses, which is a quick way to see any state of the UI without clicking through it.

browser console
> $.title = "Boom!";

The language is built into the TypeScript toolchain inside the Elements project server, so a template is a TypeScript symbol. Go to definition, find references and rename work across templates in your editor the same way they work across functions, and every attribute is type checked.

Why not React? When this language started in 2012, React did not exist yet. The idea was that a user interface should reflect its data automatically, and that the reactivity belongs in the runtime, not in the code you write. React moved the other way. Getting started means learning a stack of abstractions, hooks, effects, dependency lists and re-render rules, and the APIs keep changing. Beginners get lost in it, and experienced developers still get it wrong in ways that show up as bugs and slow pages. I wanted our users to write HTML and have the runtime do the rest.

That decision turned out to matter for AI agents. An agent has seen far more HTML than JSX, and with lexically scoped constructor attributes and automatic reactivity it spends its effort on the presentation rather than on rendering mechanics. In practice that means fewer tokens and fewer bugs in the interface. Humans find the code easier to read too.

Every new app also installs Elements UI, a style system with about 80 templates for the parts every application needs: forms, overlays, data tables, charts, chat. The agent builds well with the raw language alone, and the templates mean it does not reinvent a date picker every time.

The manual has the full language at elements.dev/man/html (opens in a new tab), and the templates are at elements.dev/ui (opens in a new tab). Questions go in the comments.

5 comments ยท 4 replies

Good job boss. Read it twice. No mistakes detected. I for one, love coding in Elements HTML. ๐Ÿค–

If one person adds a row, how does everyone else's page update without a refresh?

Elements HTML handles the automatic UI updates when reactive data changes. The data can be updated over the wire, or locally like in the counter example. In this example of the "row" you might be thinking of a real time comment system, or todo list, or something like that. You can use the LiveTable class which is another part of the @elements/app framework, that enables publish subscribe. Each browser client connected to the live table will get data updates and the UI will update in response to that changing data.

How do I bind a checkbox, a select or radio buttons to a variable? The post only shows text inputs.

@chris editedAuthor

Checkboxes and radio buttons are interesting because typically they're used in groups. The checkbox can also be used by itself. Here's a couple of examples:

template.ehtml
<form> <!-- select: one value --> <select name="country" value={form.country}> <option value="us">United States</option> <option value="ca">Canada</option> </select> <!-- checkbox: one boolean --> <label><input type="checkbox" name="subscribed" checked={form.subscribed}> Subscribe</label> <!-- radio buttons: one value out of several --> <label><input type="radio" group={form.plan} value="free"> Free</label> <label><input type="radio" group={form.plan} value="pro"> Pro</label> <!-- checkbox group: a list of values --> <label><input type="checkbox" group={form.contacts} value="email"> Email</label> <label><input type="checkbox" group={form.contacts} value="phone"> Phone</label> </form>

How do I validate a form before it submits and show the error under the field it belongs to?

@chris editedAuthor

There's numerous ways you can accomplish this, since you primarily have to update reactive data and the UI will update automatically. Below is a simple example. You can use elements man recipes/form-validation for a more complex, full featured example.

template.ehtml
interface Form { email: string; } interface Errors { email?: string; } function validate(form: Form): Errors { if (!form.email.includes("@")) { return { email: "Enter a valid email" }; } return {}; } <html (private form: Form = { email: "" }, private errors: Errors = {})> <form onsubmit={() => errors = validate(form)}> <input name="email" value={form.email}> <p e:if={errors.email} class="error">{errors.email}</p> <button>Sign up</button> </form> </html>

If I push to an array or change something nested like user.address.city, does the page update or do I have to reassign the variable?

The UI updates at the lowest level of the data change. Only the minimum UI needed to reflect the changed data is updated.

template.ehtml
<div>{user.address.city}</div> <div>{user.address.state}</div>

In this sample code, if the city changes only the first expression block is updated. If state changes, only the second expression block is updated. If the address or user objects are reassigned, then both are updated.

Add a comment