nanotags


A thin Web Components wrapper powered by Nano Stores reactivity. It leans on the platform—Custom Elements, standard DOM, regular CSS—instead of reinventing them. The result is a typed, reactive component model with automatic cleanup in under 2.5 KB.
- No Shadow DOM: markup stays in the regular DOM, styled with normal CSS
- Reactive props via Nano Stores atoms: subscribe when you need updates,
.get() when you don't
- Typed fluent builder: props, refs, and contexts are fully inferred through the chain
- Automatic cleanup: event listeners, store subscriptions, and bindings are removed on disconnect
- Tree-shakeable:
nanotags/render and nanotags/context are separate entry points
- Standard Schema: built-in validators plus any Standard Schema‑compatible library (Valibot, Zod, ArkType)
- Hydration-first: built for statically rendered markup. Pair with Astro, server-rendered HTML, or any static-first setup to hydrate lightweight interactive islands
<x-hello></x-hello>
import { define } from "nanotags";
define("x-hello", () => alert("Hello, world!"));
For components with props and refs, use the fluent builder:
<x-counter count="0">
<span data-ref="display">0</span>
<button data-ref="increment">+1</button>
</x-counter>
import { define } from "nanotags";
const Counter = define("x-counter")
.withProps((p) => ({
count: p.number(),
}))
.withRefs((r) => ({
increment: r.one("button"),
display: r.one("span"),
}))
.setup((ctx) => {
ctx.on(ctx.refs.increment, "click", () => {
ctx.props.$count.set(ctx.props.$count.get() + 1);
});
ctx.effect(ctx.props.$count, (value) => {
ctx.refs.display.textContent = String(value);
});
});
Made at Evil Martians, product consulting for developer tools.
Table of Contents
Installation
npm install nanotags nanostores
nanostores is a peer dependency.
Component Builder
define(name) returns a ComponentBuilder with an immutable fluent chain. Each step returns a new builder, accumulating type information:
define("x-my-comp") // ComponentBuilder (entry point)
.withProps((p) => ({ ... })) // + prop types
.withRefs((r) => ({ ... })) // + ref types
.setup((ctx) => { ... }); // terminates chain, registers element
withProps, withRefs, and withContexts are optional and can appear in any order. setup ends the chain: it calls customElements.define under the hood and returns a typed constructor.
For simple components, pass setup directly as the second argument:
const Logger = define("x-logger", (ctx) => {
console.log("connected:", ctx.host.tagName);
});
Props
Declare reactive attributes via withProps. Each prop becomes:
- An observed HTML attribute (auto-synced via
attributeChangedCallback)
- A Nano Stores
WritableAtom at ctx.props.$propName
- A typed getter/setter on the element instance
Four built-in validators coerce raw attribute strings to typed values: