H3 Microframework Developer Guide
• Overview
• I'm sold! Where can I get it?
• Hello, World?
• Something more complex?
• No, I meant a real web application...
• Can I use it then, no strings attached?
• What if something is broken?
• Can I download a copy of all the documentation as a standalone HTML file?
• Quick Start
• Create a basic HTML file
• Import h3 and h from h3.js
• Create your SPA
• Key Concepts
• HyperScript
• Component
• Router
• Screen
• Store
• Module
• How everything works...
• Tutorial
• Create a simple HTML file
• Create a single-page application
• Initialization
• Next steps
• API
• h(selector: string, attributes: object, children: array)
• Create an element, with an ID, classes, attributes and children
• Create an empty element
• Create an element with a textual child node
• Create an element with child nodes
• Render a component
• Render child components
• Special attributes
• h3.screen({setup, display, teardown})
• h3.dispatch(event: string, data: any)
• h3.init(config: object)
• h3.navigateTo(path: string, params: object)
• h3.on(event: string, handler: function)
• h3.redraw()
• h3.route
• h3.state
• About
• Why the weird release labels?
• A brief history of H3
• Credits
• Special Thanks
Overview
H3 is a microframework to build client-side single-page applications (SPAs) in modern JavaScript.
H3 is also:
• tiny, less than 4KB minified and gzipped.
• modern, in the sense that it runs only in modern browsers (latest versions of Chrome, Firefox, Edge & similar).
• easy to learn, its API is comprised of only seven methods and two properties.
I’m sold! Where can I get it?
Here, look, it’s just one file:
Download v600-0 (Lucky Lurian)
Hello, World?
Here’s an example of an extremely minimal SPA created with H3:
import { h3, h } from "./h3.js";
h3.init(() => h("h1", "Hello, World!"));
This will render a h1 tag within the document body, containing the text "Hello, World!".
Something more complex?
Have a look at the code of a simple todo list (demo) with several components, a store and some routing.
No, I meant a real web application…
OK, have a look at litepad.h3rald.com — it’s a powerful notepad application that demonstrates how to create custom controls, route components, forms, and integrate third-party tools. The code is of course on GitHub.
Can I use it then, no strings attached?
Yes. It’s MIT-licensed.
What if something is broken?
Go fix it! Or at least open an issue on the Github repo, pleasy.
Can I download a copy of all the documentation as a standalone HTML file?
What a weird thing to ask… sure you can: here!
Quick Start
Getting up and running with H3 is simple enough, and you don’t even need any special tool to build or transpile your application (unless you really, really require IE11 support).
Create a basic HTML file
Start with a minimal HTML file like this one, and include an app.js script. That will be the entry point of your application:
My H3-powered App
Note that the script must be marked as an ES6 module (type="module"), otherwise your imports won’t work.
Import h3 and h from h3.js
Then, inside your app.js file, import h and h3 from h3.js, which should be accessible somewhere in your app:
import { h3, h } from "./h3.js";
This will work in every modern browser except Internet Explorer. You don’t need a transpiler, you don’t need something to convert your beautiful ES6 code back to clunky ES5.
Unless your company tells you to, do yourself a favor and don’t support IE. It’s 2020, even Microsoft moved on, and now ES6 modules work in all major browsers.
Create your SPA
After importing the h3 object and the h function, you can start developing your SPA. A bare minimum SPA is comprised by a single component passed to the h3.init() method:
// A simple component printing the current date and time
// Pressig the Refresh button causes the application to redraw
// And updates the displayed date/dime.
const Page = () => {
return h("main", [
h("h1", "Welcome!"),
h("p", `The current date and time is ${new Date()}`),
h("button", {
onclick: () => h3.redraw()
}, "Refresh")
]);
}
// Initialize your SPA
h3.init(Page);
Key Concepts
There are just a few things you should know about if you want to use H3.
Oh… and a solid understanding of HTML and JavaScript wouldn’t hurt either ;)
HyperScript
H3 uses a HyperScript-like syntax to create HTML elements in pure JavaScript. No, you are actually creating Virtual DOM nodes with it but it can be easier to think about them as HTML elements, or better, something that eventually will be rendered as an HTML element.
How, you ask? Like this:
h("div.test", [
h("ul", [
h("li", "This is..."),
h("li", "...a simple..."),
h("li", "unordered list.")
])
]);
…which will output:
Render a component
const TestComponent = () => {
return h(
"button.test",
{
onclick: () => alert("Hello!"),
},
"Show Alert"
);
};
h(TestComponent);
↓
Note: The event listener will not be added to the markup.
Render child components
const TestLi = (text) => h("li.test", text);
h("ul", ["A", "B", "C"].map(TestLi));
↓
A
B
C
Special attributes
• Any attribute starting with on (e.g. onclick, onkeydown, etc.) will be treated as an event listener.
• The classList attribute can be set to a list of classes to apply to the element (as an alternative to using the element selector shorthand).
• The data attribute can be set to a simple object containing data attributes.
• The special $html attribute can be used to set the innerHTML property of the resulting HTML element. Use only if you know what you are doing!
• The special $onrender attribute can be set to a function that will executed every time the VNode is rendered and added to the DOM.
The $html and the $onrender special attributes should be used sparingly, and typically only when interfacing with third-party libraries that need access to the real DOM.
For example, consider the following code snippet that can be used to initialize the InscrybMDE Markdown editor on a textarea element:
h("textarea", {
$onrender: (element) => {
const editor = new window.InscrybMDE({
element,
});
},
});
h3.screen({setup, display, teardown})
Creates a Screen by providing a (mandatory) display function used to render content, an an optional setup and teardown functions, executed before and after the display function respectively.
Each of these functions takes a single screen parameter, which is initialized as an empty object before the setup, and is (optionally) returned by the teardown function should state be preserved across different screens.
Consider the following example:
h3.screen({
setup: await (state) => {
state.data = state.data || {};
state.id = h3.route.parts.id || 1;
const url = `http://dummy.restapiexample.com/api/v1/employee/${id}`;
state.data[id] = state.data[id] || await (await fetch(url)).json();
},
display(state) => {
const employee = state.data[state.id];
if (!employee) {
return h("div.error", "Invalid Employee ID.");
}
return h("div.employee",
h("h2", "Employee Profile"),
h("dl", [
h("dt", "Name:"),
h("dd", data.employee_name),
h("dt", "Salary:"),
h("dd", `${data.employee_salary} €`),
h("dt", "Age:"),
h("dd", data.employee_age),
])
)
},
teardown: (state) => ({ data: state.data })
})
This example shows how to implement a simple component that renders an employee profile in the display function, fetches data (if necessary) in the setup function, and preserves data in the teardown function.
Tip To interrupt navigation or perform redirects, return false in the setup method.
h3.dispatch(event: string, data: any)
Dispatches a event and optionally some data. Messages are typically handled centrally by modules.
h3.dispatch("settings/set", { logging: true });
A event name can be any string, but keep in mind that the following names (and typically any name starting with $) are reserved for framework use:
• $init — Dispatched when the application is initialized. Useful to initialize application state.
• $redraw — Dispatched after an application redraw is triggered.
• $navigation — Dispatched after a navigation occurs.
• $log — Dispatched after any event (except $log iself) is dispatched. Very useful for debugging.
h3.init(config: object)
The initialization method of every H3 application. You must call this method once to initialize your application by providing a component to render or configuration object with the following properties:
• element (Element) — The DOM Element to which the Application will be attached (default: document.body).
• modules (Array) — An array of functions used to handle the application state that will be executed once before starting the application.
• routes (Object) — An object containing routing definitions, using paths as keys and components as values. Routing paths can contain named parts like :name or :id which will populate the parts property of the current route (h3.route).
• preStart (Function) — An optional function to be executed before the application is first rendered.
• postStart (Function) — An optional function to be executed after the application is first rendered.
This is an example of a simple routing configuration:
const routes = {
"/posts/:id": Post,
"/pages/:id": Page,
"/": HomePage,
};
For more a complete example of initialization, see this.
h3.navigateTo(path: string, params: object)
Navigates to the specified path. Optionally, it is possibile to specify query string parameters as an object.
The following call causes the application to switch to the following URL: #/posts/?orderBy=date&direction=desc.
h3.navigateTo("/posts/", { orderBy: "date", direction: "desc" });
h3.on(event: string, handler: function)
Subscribes to the specified event and executes the specified handler function whenever the event is dispatched. Returns a function that can be used to delete the subscription.
Subscriptions should be typically managed in modules rather than in components: a component gets rendered several times and subscriptions must be properly cleaned up to avoid memory leaks.
Example:
const pages = (store) => {
store.on("$init", () => ({ pagesize: 10, page: 1 }));
store.on("pages/set", (state, page) => ({ page }));
};
h3.redraw()
Triggers an application redraw. Unlike most frameworks, in H3 redraws must be triggered explicitly. Just call this method whenever you want something to change and components to re-render.
h3.route
An read-only property containing current route (Route object). A Route object has the following properties:
• path — The current path (fragment without #) without query string parameters, e.g. /posts/134
• def — The matching route definition, e.g. /posts/:id
• query — The query string, if present, e.g. ?comments=yes
• part — An object containing the values of the parts defined in the route, e.g. {id: "134"}
• params — An object containing the query string parameters, e.g. {comments: "yet"}
h3.state
A read-only property containing the current application state. The state is a plain object, but its properties should only be modified using event subscription handlers.
About
Or: everything you wanted to know about H3, but you were afraid to ask.
Why the weird release labels?
Ubuntu started naming their releases after animals in alphabetical order… In a similar way, H3 releases are named after Star Trek species.
A brief history of H3
A while ago, I was interviewing with several companies trying to find a new job in the JavaScript ecosystem. One of these companies asked me, as a part of their interview process, to create a simple Todo List app in JavaScript without using any libraries.
I spent some time thinking about it, started cobbling together a few lines of code doing the usual DOM manipulation stuff (how hard can it be, right? It’s a Todo List!) and then stopped.
No way! — I thought.
There has to be a better way. If only I could use something small like Mithril, it would take me no time! But sadly I couldn’t. Unless…
Unless I coded the whole framework myself of course. And so I did, and that’s more or less how H3 was born. You can see a slightly-modified version of the resultig Todo List app here (with all the bonus points implemented, like localStorage support, pagination, filtering, etc.).
The original version only had an even smaller (and even buggier) Virtual DOM and hyperscript implementation, no routing and no store, but it did the job. After a few additional interviews I was actually offered the job, however I didn’t take it, but that’s another story ;)
A few months after that interview, I decided to take a look at that code, tidy it up, add a few bits and bobs, package it up and release it as a proper microframwork. Well, kind of.
Credits
The H3 web site is built with H3 itself, plus the following third-party libraries:
• marked.js
• Prism.js
• mini.css
Special Thanks
Special thanks to the following individuals, that made H3 possible:
• Leo Horie, author of the awesome Mithril framework that inspired me to write the H3 microframework in a moment of need.
• Andrey Sitnik, author of the beatiful Storeon state management library, that is used (with minor modifications) as the H3 store.
Fabio Cevasco – June 17, 2026
Powered by