/grainjs

Javascript library from Grist Labs

Primary LanguageTypeScriptApache License 2.0Apache-2.0

GrainJS

npm version Build status codecov

GrainJS is a Javascript (and TypeScript) library for building highly performant dynamic applications.

GrainJS provides convenient pure-JS interfaces for building DOM. It has observables inspired by Knockout to create declarative data models and tie them to UI. It includes light-weight event dispatching, DOM event subscriptions, disposable components, and in-code CSS styling.

GrainJS is in part inspired by React, but based on observables instead of virtual dom, and with a convenient way to build DOM without JSX. It is lighter weight, and has less magic happening under the covers.

The focus is on performance and conciseness. The library has no dependencies and is only 31K. minified.

Installation

npm install --save grainjs

Basic Example

const name = observable("");

dom.update(document.body,
  dom('input', {type: 'text', placeholder: 'Enter your name'},
    dom.on('input', (ev, elem) => name.set(elem.value)),
  ),
  dom('div', 'Hello, ',
    dom.text((use) => use(name).toUpperCase() || 'Stranger'),
    '!',
  ),
);

Documentation

At a basic level, GrainJS allows you to describe DOM structure in one place, using Javascript (or TypeScript), and to keep the dynamic aspects of it separated into variables called "observables". These observables serve as the model of the UI; other code can update them to cause UI to update, without knowing the details of the DOM construction.

In addition, the library provides approaches to create and dispose resources (important for long-lived single-page applications), and an assortment of other related tools.