An easy peasy UI binding library.

Overview

Peasy UI

This is the repository for Peasy UI, a small-ish and relatively easy to use UI binding library.

Introduction

Peasy UI provides uncomplicated UI bindings for HTML via string templating. It's intended to be used in vanilla JavaScript/Typescript projects where using createElement is too cumbersome and adding a complete SPA framework is overkill or simply not desired. Thanks to the small scope of the library, performance is decent.

First look

In Peasy UI, an HTML template is combined with a JavaScript/Typescript object, the model, into a UI View that's added to an element. By calling update() on the view, typically after updating the model or in a recurring (game) loop, the one-way, two-way and event bindings will sync state between the UI and the model.

const template = `
    Color: <input \${value <=> color}>
    <span style="background-color: \${color}">\${color}</span>
    <button \${click @=> clicked}>Gold</button>
    `;

const model = {
    color: 'red';
    clicked: () => model.color = 'gold';
};

const view = UI.create(document.body, template, model);

setInterval(() => UI.update(), 1000 / 30);

This example creates a two-way bound input field where whatever color is typed in is displayed in a span with that background color. When the button Gold is clicked, the click event binding will update the color property in the model which in turn will update all bindings in the view. The update in the setInterval at the end causes the bindings to be checked and updated 30 times per second.

Getting started

If you've got a build process and are using npm, install Peasy UI with

npm i peasy-ui

and import it into whichever files you want to use it

import { UI } from 'peasy-ui';

If you don't have a build process or don't want to install it, use a script tag

<script src="https://unpkg.com/peasy-ui">

to make UI available.

Features and syntax

Peasy UI uses the JavaScript/Typescript string interpolation syntax of ${ } in combination with different versions of the spaceship operator <=> to bind between an attribute on the element and a property on the model.

'Color: <input ${value <=> color}>' // Two-way binding between value attribute and color property

Available bindings

${attr <== prop}    Bindning from model property to element attribute
${attr ==> prop}    Bindning from element attribute to model property 
${attr <=> prop}    Two-way binding between element attribute and model property

${prop}             Bindning from model property to attribute or text

${event @=> method} Event bindning from element attribute to model method

${'value' ==> prop} Binding from element to model property, used to bind
                    values of radio buttons and select inputs to a model property

${ ==> prop}        One-time binding that stores the element in model property

${ === prop}        Binding that renders the element if model property is true
${ !== prop}        Binding that renders the element if model property is false

${alias <=* list}   Bindning from model list property to view template
                    alias for each item in the list

A combination of the string value binding and a binding for the change event can be used to capture and react to changes in radio buttons and selects.

const template = `
    <input type="radio" \${'red' ==> color} \${change @=> changedColor}> Red
    <input type="radio" \${'green' ==> color} \${change @=> changedColor}> Green
    `;
const model = {
    color: 'red';
    changedColor: (event, model) => alert(`Changed color to ${model.color}.`),
};
const template = `
    <select \${change @=> changedColor}>
        <option \${'red' ==> color}>Red</option>
        <option \${'green' ==> color}>Green</option>
    </select>
    `;
const model = {
    color: 'red';
    changedColor: (event, model) => alert(`Changed color to ${model.color}.`),
};
const template = `
    <div \${ === preferCats}>I prefer cats.</div>
    <div \${ !== preferDogs}>I DON'T prefer dogs.</div>
`;
const model = { preferCats: true, preferDogs: false };
const template = `<div \${item <=* list}>Item: \${item}</div>`;
const model = { list: ['one', 'two', 'three'] };
const template = `<div \${object <=* list}>Item: \${object.id}</div>`;
const model = { list: [{ id: 'one' }, { id: 'two' }, { id: 'three' }] };

Development and contributing

If you're interested in contributing, please see the development guidelines.

You might also like...

A robust form library for Lit that enriches input components with easy-to-use data validation features.

A robust form library for Lit that enriches input components with easy-to-use data validation features.

EliteForms A robust form library for Lit that enriches input components with easy-to-use data validation features. Installation npm install elite-form

Jun 28, 2022

Library to make your web page shareable fast and easy in all the majors social networks.

SocialShareJS This is a simple libray to make your web page shareable fast and easy. Its allow to include the social share link of the major social ne

Jul 24, 2022

Easy Pan and Zoom JS Library

EasyPZ Use this Javascript library to make your web visualization interactive via pan and zoom, for mobile and desktop! EasyPZ supports many interacti

Nov 21, 2022

Complete, flexible, extensible and easy to use page transition library for your static web.

Complete, flexible, extensible and easy to use page transition library for your static web.

We're looking for maintainers! Complete, flexible, extensible and easy to use page transition library for your static web. Here's what's new in v2. Ch

Jan 2, 2023

An easy-to-use JavaScript library aimed at making it easier to draw on SVG elements.

svg-pen-sketch An easy-to-use JavaScript library aimed at making it easier to draw on SVG elements when using a digital pen (such as the Surface Pen).

Jul 27, 2022

An easy-to-use library that provide acronymous sending on form changes

An easy-to-use library that provide acronymous sending on form changes

Form Async Form Async is an easy-to-use library that provide acronymous sending on form changes. It's a great solution to preventing data loss when fi

Jan 5, 2022

Lightweight and easy to use vanilla js library to add css animations to elements on scroll.

Scrollrisen Trigger css animations on scroll as elements enter the viewport. Zero dependencies, lightweight and easy to use and customize Installation

Oct 13, 2022

jquery-input-mask-phone-number.js - A simple, easy jquery format phone number mask library

jquery-input-mask-phone-number.js - A simple, easy jquery format phone number mask  library

jquery-input-mask-phone-number A jQuery Plugin to make masks on input field to US phone format. Quick start 1. Add latest jQuery and jquery-input-mask

Aug 25, 2022

A simple easy to use vanilla JavaScript library for creating input fields that accept multiple email addresses

MeiMei - Multiple Email Input MeiMei: A simple easy to use vanilla JavaScript library for creating input fields that accept multiple email addresses.

Apr 13, 2022
Owner
null
A data-binding function for the DOM.

Alert: this library is now deprecated. s2 is its successor. It implements what simulacra does in a better way (using Proxy), and more. Simulacra.js Si

郑达里 541 Nov 18, 2022
Necktie – a simple DOM binding tool

?? Necktie – a simple DOM binding tool Necktie is a library that binds your logic to the Document Object Model elements in an easy way. It has only ~3

Maciej Leśniewski 43 Oct 7, 2022
A Node.js binding to webview

webview-nodejs English | 中文(简体) A Node.js binding to webview, a tiny cross-platform webview library to build modern cross-platform desktop GUIs using

Winterreisender 12 Dec 13, 2022
Node.js implementation binding for the RWKV.cpp module

RWKV.cpp NodeJS bindings Arguably the easiest way to get RWKV.cpp running on node.js. # Install globally npm install -g rwkv-cpp-node # This will sta

RWKV 9 May 10, 2023
jQuery based scrolling Bar, for PC and Smartphones (touch events). It is modern slim, easy to integrate, easy to use. Tested on Firefox/Chrome/Maxthon/iPhone/Android. Very light <7ko min.js and <1Ko min.css.

Nice-Scrollbar Responsive jQuery based scrolling Bar, for PC and Smartphones (touch events). It is modern slim, easy to integrate, easy to use. Tested

Renan LAVAREC 2 Jan 18, 2022
An easy to implement marquee JQuery plugin with pause on hover support. I know its easy because even I can use it.

Simple-Marquee Copyright (C) 2016 Fabian Valle An easy to implement marquee plugin. I know its easy because even I can use it. Forked from: https://gi

null 16 Aug 29, 2022
Jaxit is an easy-to-use library that makes an interactive terminal for your programs.

Jaxit Jaxit is an easy-to-use library that makes an interactive terminal for your programs. Jaxit was made by Codeverse, so check on Codeverse's Profi

null 3 Dec 11, 2022
Library for Foundry VTT which provides easy access to dark comedy fantasy setting created by Richard Zadmar Woolcock

Library for Foundry VTT which provides easy access to dark comedy fantasy setting created by Richard Zadmar Woolcock. Current inclusions are a bestiary, edges, hindrances, and a journal with setting information, character creation, and more. The Savage Worlds Adventurers Edition Game System is the system this setting is designed for.

KevDog 2 Mar 18, 2022
An easy-to-use library to make your life easier when working with random numbers or random choices in javascript.

vrandom An easy-to-use library to make your life easier when working with random numbers or random choices in javascript. Table of contents Installati

Valerio Cipolla 1 Aug 16, 2022
A RESP 'Redis Serialization Protocol' library implementation to generate a server, uses a similar approach to express to define you serer, making it easy and fast.

RESPRESS A RESP 'Redis Serialization Protocol' library implementation to generate a server, uses a similar approach to express to define you serer, ma

Yousef Wadi 9 Aug 29, 2022