Skip to content
grucloudPublic

About

Reactive library and components for building web user interface

Resources

Stars

142 stars

Watchers

5 watching

Forks

Repository files navigation

Bau.js

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.

Install

npm install @grucloud/bau

API

The 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.

Guide

Ecosystem

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 size

Bundle sizes compared with bundlephobia:

bundle size

Examples

The examples directory has about 50 apps, from a counter to complete ones:

Development

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            # both

To work on one example, run its own scripts: cd examples/feed-reader && npm run dev, or npm run test:e2e.

Releasing

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:minor

It 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.

Benchmark

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 lighthouse

History

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, bauUnmounted and bauChildMutated.

  • No global: each call of Bau() creates an instance, which can also be given a window.

    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 undefined or null, see issue 39.

Contributing

Please report bugs and suggestions on GitHub.

License

MIT

About

Reactive library and components for building web user interface

Resources

Stars

142 stars

Watchers

5 watching

Forks

Releases

Packages

Used by

Contributors

Languages