···11---
22-title: Composable HTML
22+title: Imaginary HTML
33date: '2025-05-01'
44spoiler: Tags on both sides.
55---
···16161717Suppose it was the only piece of HTML you've ever seen in your life. If you had complete freedom, which features would you add to HTML, and in what order?
18181919-How would you evolve the *HTML itself?*
1919+How would you reimagine the *HTML itself?*
20202121---
22222323### Components
24242525-Personally, I'd like to start by adding a way to define our own custom HTML tags.
2525+Personally, I'd like to start by adding a way to define my own custom HTML tags.
26262727It doesn't need to be complicated. We can just use JavaScript functions:
2828···111111</html>
112112```
113113114114-But what should we do with the `style={{ color: '...' }}` objects? If we wanted to translate our "imaginary" HTML into "real" HTML, we'd stringify them:
114114+Still, we haven't gotten rid of all objects.
115115+116116+What should we do with those `{ color: '...' }` objects?
117117+118118+---
119119+120120+### Objects
121121+122122+The "real" HTML we know and love has no first-class notion of objects. If we wanted to output some "real" HTML, we'd have to turn them into `style` strings:
115123116124```js {3,4}
117125<html>
···124132125133But we don't *have to* turn our "imaginary" HTML into "real" HTML right away. We can stay in the imaginary land for a bit longer by turning *the entire thing* into JSON.
126134127127-Note how in this case, `style` can remain completely intact as an object within:
135135+Note how, in this format, `style` can remain completely intact as an object within:
128136129137```js {6,10}
130138["html", {
···145153146154We can *then* easily turn this JSON into the "real" HTML if we want. But JSON is a *richer* representation because it preserves objects in a way that is easy to parse.
147155148148-This isn't particularly interesting or useful yet.
156156+This strange JSON representation isn't particularly interesting or useful yet. But going forward, we'll co-evolve these two representations. In fact, we'll consider the JSON representation to be the primary one because it preserves the most original information--notably, *it preserves objects.* We'll think of the "real" HTML representation as secondary because it can always be computed from that JSON.
157157+158158+In other words, we have a little pipeline:
159159+160160+1. *Imaginary HTML:* The language we are inventing.
161161+1. *JSON:* The primary output format of our language.
162162+1. *Real HTML:* What browsers can handle directly.
149163150150-But going forward, we'll co-evolve these two representations.
164164+Let's see how each piece evolves with new requirements.
151165152166---
153167···227241}]
228242```
229243230230-Or, in the "real" HTML form:
244244+(which, as you might recall, can always be converted to the "real" HTML:)
231245232246```js
233247<html>