A small JavaScript library to build reactive user interfaces for the web: about 3 kB minified and gzipped.
No JSX, no templates, no virtual DOM, no compiler. A component is a plain function that returns a real DOM element.
Bau is reactive: the views derive from the state. A state is a JavaScript proxy; when its value changes, the parts of the DOM that read it are updated, in the next animation frame.
import Bau from "@grucloud/bau";
const bau = Bau();
const { button, span } = bau.tags;
const counter = bau.state(0);
const Counter = () =>
span(
"❤️ ",
counter,
" ",
button({ onclick: () => ++counter.val }, "👍"),
button({ onclick: () => --counter.val }, "👎")
);
// The html page has an element with the id "app"
document.getElementById("app").replaceChildren(Counter());Try the hello world example on CodeSandbox.
npm install @grucloud/bauThe API is a handful of functions on the object returned by Bau():
| Function | Description |
|---|---|
bau.tags |
The HTML tags as functions: const { div, p } = bau.tags, then div({ class: "card" }, p("Hello")) |
bau.tagsNS(namespace) |
The tags of another namespace, such as SVG or MathML |
bau.state(value) |
A reactive state, read and written with .val. Holds a primitive, an object or an array |
bau.derive(() => value) |
A state computed from other states, also used for side effects |
bau.loop(arrayState, container, renderItem) |
Renders an array state into a container, applying push, splice... to the DOM without redrawing the whole list |
bau.bind({ deps, render }) |
Renders an element from explicit state dependencies |
A function given as a child or as a property is reactive: () => (show.val ? "ON" : "OFF"). Bau tracks the states it reads and runs it again when one of them changes.
The properties bauCreated, bauMounted, bauUnmounted and bauChildMutated are lifecycle methods.
TypeScript definitions are included.
- Component pattern
- Reactivity with bau.state
- Event handling
- Conditional rendering
- Arrays: bau.loop
- State derivation and side effects: bau.derive
- Explicit dependencies: bau.bind
- Lifecycle methods
- A hash based router
| Package | Description |
|---|---|
| bau | The core library |
| bau-css | CSS in JS in about 35 lines of code: css, keyframes and createGlobalStyles |
| bau-ui | 50+ themable components: Button, Input, Tabs, Tree View, Modal, Lazy... |
| bau-router | A router: nested routes as regular expressions, link interception, under 1 kB minified and gzipped |
| bausaurus | A static site generator for documentation written in Markdown |
| bau-astro | Bau in Astro, for server side rendering |
| bau-kit | A starter kit for a multi page app |
Bundle sizes compared with bundlephobia:
The examples directory has about 50 apps, from a counter to complete ones:
- Frontend Mentor challenges, each with a live demo.
- feed-reader: an RSS reader with bau-router, lazy loaded pages and a feed proxy.
- gccd: a cloud management console, with a mock of its API to run it without a backend.
- todoapp, todo-minimal, lifecycle, bau-d3, mathml...
The repository is a pnpm workspace:
pnpm install
pnpm test # the unit tests of the packages
pnpm test:e2e:install # once: the Chromium of Playwright
pnpm test:e2e # the end to end tests of the examples
pnpm test:all # bothTo work on one example, run its own scripts: cd examples/feed-reader && npm run dev, or npm run test:e2e.
From an up to date main, bump the minor version of all the packages, for instance from 0.202.0 to 0.203.0:
pnpm release:minorIt commits the new version, tags it, and pushes the commit and the tag.
The Publish workflow then builds, tests and publishes to npm the packages whose version is not on npm yet, with npm trusted publishing: no npm token or one-time password.
Bau has been measured with js-framework-benchmark, see the preliminary results.
Chrome Lighthouse gives a performance score of 100 to an app built with all the Bau UI components.
Bau is inspired by VanJS. The differences:
-
A Bau state holds primitives, objects and arrays. Mutating an array, like
todos.val.push(todo), updates the DOM without rendering the whole list again. -
Lifecycle methods:
bauCreated,bauMounted,bauUnmountedandbauChildMutated. -
No global: each call of
Bau()creates an instance, which can also be given awindow.import Bau from "@grucloud/bau"; const bau = Bau(); const { div } = bau.tags;
-
A reactive function can return an array of elements, like a React Fragment.
-
One paradigm: the views derive from the state. The DOM is not changed by hand, which makes Bau hard to misuse.
-
An attribute can be
undefinedornull, see issue 39.
Please report bugs and suggestions on GitHub.
