@im4all/bitsy-js
v1.0.0
Published
Add interactivity to the HTML you render
Maintainers
Readme
A tiny, dependency-free, HTML-first view library. It adds data binding, signals, loops, conditions, fragments, events, remote data, and form validation to markup you already render. About 30.5 kB minified, 9.2 kB gzipped, no build step.
Docs, guides, and live examples: bitsyjs.habibhadi.com.
BitsyJS reads attributes and tags directly from the DOM and enhances the page after load. There is no virtual DOM, no component compiler, no runtime dependency, and no required build step.
Install
npm install @im4all/bitsy-jsimport { BitsyJS } from "@im4all/bitsy-js";Or load it straight from a CDN. No bundler needed:
<script type="module">
import { BitsyJS } from "https://unpkg.com/@im4all/bitsy-js/dist/bitsy.min.js";
const bitsy = new BitsyJS();
await bitsy.run();
</script>TypeScript declarations are included.
Why it is created
The world didn't need another JS library. I just wanted to include a damn HTML file.
Sometimes I want a quick HTML page: change a line, upload it over FTP, done. Yes, FTP. I know what year it is. If all I did was fix a typo, I don't want a laptop or a build server making an event out of it.
Plain HTML can't even include one file in another, so I wanted PHP's include. Then a variable in my markup, a loop over a list, a form check, an HTTP request. Small things, and I kept ending up with a full framework or three libraries in a trench coat. Build-free options existed, but some felt more complicated than the page I was making, and I usually needed another library anyway. I still have a soft spot for jQuery: drop in a script and get on with it.
I use modern frameworks when I'm building an app. For docs, a presentation, or a landing page, I wanted to start with HTML and keep it that way. So I built BitsyJS, by hand, even in the AI era, so I can come back in six months and still know how it works.
Quick start
<div b-data>
<h1>{title}</h1>
<p b-if="isLoggedIn">Welcome back, {user.name}</p>
<p b-if="!isLoggedIn">Please sign in.</p>
<ul>
<li b-for="player of players">{_index}: {player.name} - {player.score}</li>
</ul>
</div>import { BitsyJS } from "@im4all/bitsy-js";
const bitsy = new BitsyJS({
data: {
title: "Scoreboard",
isLoggedIn: true,
user: { name: "Ada" },
players: [
{ name: "Alpha", score: 10 },
{ name: "Beta", score: 7 },
],
},
});
bitsy.ready(() => console.log("rendered"));
await bitsy.run();Core directives
| Directive | Purpose |
| ----------------------------- | ------------------------------------------------------------------------------ |
| b-data="{ key: value }" | Local data scope. Placeholders use {path.to.value}. |
| b-for="item of items" | Repeats an element per item. Provides _index, _isFirst, _isLast. |
| b-if="expression" | Shows or hides an element with display: none. |
| b-view src="./partial.html" | Fetches and inlines an HTML fragment. |
| b-container | Groups markup and removes the wrapper after rendering. |
| b-js="expression" | Evaluates an expression and writes the result as HTML. |
Events use data-b-event="click" plus effect attributes such as -html, -toggle-class, -remote, and -indicator. Forms use data-b-validate-form and data-b-form-ajax. Full tables are in the API reference.
Signals
Use signals for state that changes after run():
import { BitsyJS, computed, signal } from "@im4all/bitsy-js";
const count = signal(0);
const doubled = computed(() => count() * 2);
await new BitsyJS({ data: { count, doubled } }).run();<div b-data>
<p>Count: {count()}. Doubled: {doubled()}.</p>
<button type="button" b-on:click="count.update(n => n + 1)">+1</button>
</div>Text and attribute bindings, b-if, and b-js update when the signals they read change. In handlers, $event is the DOM event and $el is the element that owns the handler. See the signals demo.
Pre-render
The package ships a bitsy-prerender CLI (Playwright based) for pages that need rendered HTML:
npm install -D playwright
npx playwright install chromium
npx bitsy-prerender --src=./site --dist=./dist --cleanCustom tags and security
Every directive name can be overridden through the tags option, and form classes and messages through validatorConfig.
Directive expressions run through new Function with access to browser globals, and HTML values are inserted as markup. Only use expressions, fragments, and remote endpoints you trust.
Development
npm install
npm run dev # local server
npm run build # dist/bitsy.min.js
npm test # Playwright + ChromiumRunnable demos live in demo/ and are linked from index.html. After changing the library, sync the docs bundle: cp dist/bitsy.min.js docs/assets/bitsy.min.js.
License
MIT. See LICENSE.
