Deleight
This is now a group of 13 libraies that simplify web frontend development in vanilla HTML, CSS and JavasSript. Deleight aims to make frontend development more enjoyable for everyone.
Apart from this brief guide and the documentation, there are also some examples which can be used to understand how the parts fit together and to develop a feel for using deleight. To play with the exmples, you can run the included server with npm start
and visit http://localhost:8000/docs/examples/index.html. The demos are also hosted online here.
What follows is a brief description of the libraries and how to include them in your projects.
Class-Action
Class-action is a simple library for replacing functions and methods with more compasable objects. It enables a form of metaprogramming in JavaScript. Learn more about it here.
Action-object
Action-object is a simple library for making JavaScript objects reactive. This means we can set up actions to run when an object property is set or a a method is invoked. It demonstrates a powerful use-case for class-action. Learn more about it here.
Element-action
Element-action is the new reactivity library implemented with action-object and class-action. The library is mostly complete now (and stripped-down for simplicity). Learn more about it here.
Actribute
Actribute is a versatile library for associating element attributes with JavaScript code. In one instance it can be used as a more widely supported, flexible and powerful alternative to extending built-in HTML elements, exposing a similar API. In another, it can be used to establish conventions for manipulating the DOM. The library also has some notable enhancements including the ability to join or recurse components.
import { Actribute, props } from 'deleight/actribute';
const fallbackProps = {
prop1: 'Fallback', prop4: 'Last resort',
sig: '$2b$20$o7DWuroOjbA/4LDWIstjueW9Hi6unv4fI0xAit7UQfLw/PI8iPl1y'
};
const act = new Actribute();
const comp1 = (element, attr, ...context) => element.textContent = props(attr.value, context)[0]);
const comp2 = (element, attr) => element.style.left = attr.value;
act.register({ comp1, comp2 })
<section c-comp1="prop1" c-comp2="100px" >
First section
</section>
act.process({el: document.body, ctx: [{prop1: 2, prop3: 2}, fallbackProps]});
delete act.registry.comp2;
Appliance
Appliance provides another declarative API for manipulating the DOM and for structuring code in JavaScript. It can be used to attach behavior to HTML elements easily and efficiently. It is like custom elements without the DOM building aspects. Here the elements may already exist in the DOM. This can produce big gains in accessibility and flexibility as DOM can be built server-side or client-side using any technology of choice. This can also increase efficiency because all the elements can be set up in one call.
import { apply } from 'deleight/appliance'
import { mySophistry } from './my-style-manager.js'
import { myEutility } from './my-event-manager.js'
function(componentElement) {
apply({
div: (...divs) => mySophistry.styles.style(...divs) || myEutility.listener.listen('click', divs, myEutility.options)
}, componentElement);
}
Queryoperator
This provides a painless SQLesque API for manipulating the DOM. The library exports insert
, set
, update
and remove
functions for bulk manipulation of things on the DOM. It is an efficient, consistent and simple API to use. See the examples and the API docs.
import { set } from 'deleight/queryoperator'
import { range } from 'deleight/generational'
function(componentElement) {
const links = componentElement.querySelectorAll('a');
const indices = [...range(links.length)]
set(links, { textContent: indices , _href: indices.map(i => `./page/${i}.html`) });
}
Apriori
This is a fun library to use if you need to build DOM with JavaScript. It includes primitives for template creation, template rendering and document tree building. There are tools for building DOM from in-page resources or dynamically loaded ones. This gives us the flexibility to choose whatever works best for a project.
import { get, template } from "deleight/apriori";
const myTemplate = template(await get("markup.html"));
function(componentElement, ...args) {
componentElement.insertAdjacentHTML('beforeend', myTemplate(...args));
}
Sophistry
Styling is a crucial aspect of most pages on the web. We need to make the pages beautiful and improve the UX for visual users. CSS is easy to include globally in any page, but when localising styles with Shadow DOM, one currently has to decide between writing duplicitive declarative styles vs writing JavaScript boilerplate to manage styles efficiently.
Sophistry enables efficiently scoping of declaratively written styles to specific elements. The library provides an API which simplifies the code needed for such. It will internally create open shadow roots where necessary. it maintains a cache to avoid reloading or re-processing the same styles (unless we request). Sophistry can draw styles from anywhere.
import { Sophistry } from "deleight/sophistry";
export const mySophistry = new Sophistry();
mySophistry.import("pStyle.css");
Generational
Generational exports some useful generators to improve performance and reduce memory footprint. The range
and items
generators have been especially useful in the examples. They may not work in many places where an array is expected because we can only iterate them once. Thus they should be used with caution. When in doubt, use an array.
import { range, items } from "deleight/generational";
const everyTenthIn1000 = range(0, 1000, 10);
const everyHundredthIn1000 = items(everyTenthIn1000, range(0, 100, 10));
Withly
We are bringing with
back to JavaScript with the help of Proxies. This functionality was removed from the language
because of performance, code comprehension and readbility issues. Once we have an implementation without these limitations,
we can benefit from the improved concision and structure in our code.
import { With, SET } from "deleight/withly";
With(document.createElement('button'))[SET]({
textContent: 'Wow!', className: 'main'
})(btm => document.body.appendChild(btn));
OneToMany
OneToMany exports primitives to manipulate many objects simultaneously. There are methods for getting and setting properties and invoking object methods. It provides a potentially more extensible alternative to functions, although presently it is less performant and the usage pattern is not quite as natural.
import { One, wrap, args } from "deleight/onetomany";
const arr1 = [1, 2, 3, 4, 5];
const arr2 = [6, 7, 8, 9];
export const oneArray = new One({arr1, arr2});
oneArray.extend({arr3: [10, 11, 12]});
oneArray.call( { push: 50 })
oneArray.call( { push: { [args]: [51, 52, 53, 54] } } )
const wrappedOneArray = wrap(oneArray);
wrappedOneArray.push(99, 100, 101, 102, 103, 104);
Eutility
This library provides some useful primitives for simiplifying the code that will likely be included in many simple pages where JavaScript is used to support interactivity. Most JavaScript code can only run in response to an event. Eutility exports functions for:
- composing event handlers
- creating lazy handlers whose functionality can be injected later
- promoting handler reuse with different elements
- creating fewer event handlers by taking advantage of event bubbling
- disabling event firing until a running handler completes and all their promises get resolved.
- creating handlers for specific key events, like enter.
- creating reusable handler guards to stop event handling at any point.
- etc.
import { EventListener } from 'deleight/eutility'
export const myEutility = {
listener: new EventListener((e, runContext) => console.log({e.target, runContext}))
};
Reftype
Reftype is the 10th library which has now been abstracted into Class-action, Action-object and element-action. I think this library has been superceded by element-action and may be removed in the future. Prefer element-action which is based on more verssatile and composable parts.
Reftype aims to relieve the burden on the JavaScript programmer to know about the markup layout and structure in a large web application. Without Reftype, we manipulate all aspects of the DOM explicitly with Javascript. We may get some mileage from Actribute but the module is more abstract.
Reftype provides an alternative pattern quite similar to how Vue.JS and Angular operate. It lets you declaratively describe DOM operations using attribute directives. The major difference with Reftype is that it is more transparent, explicit and composable. It aligns with the policy of Deleight to use straight HTML, CSS and JavaScript. It is deliberately designed to be fast, lightweight and memory-efficient.
<main>
<p t> mercury +&+ venus </p>
<p t> mercury + or +venus</p>
<p t>mercury + before + venus</p>
<section t>earth</section>
<article t class-a="color1| |color2">mars</article>
</main>
import { RefType } from 'deleight/reftype'
const refs = {
mercury: 'Planet mercury',
venus: 'The second planet',
earth: 'Our planet!',
mars: 'Nearest planetary neighbor',
color1: 'red',
color2: 'green'
};
const reftype = new RefType(refs, { sep: { multivalue: '+' } });
reftype.add(document.querySelector('main'));
reftype.react();
reftype.set({ color1: 'blue' });
Installation
npm i deleight
Usage
import { Component } from "deleight/element-action";
import { Actribute } from "deleight/actribute";
Contributing
If you like this, I invite you to contribute. You can contribute in many areas. Sponsorship, issues, pull requests, benchmarks, testing, CI, examples; all are welcome. Please just maintain a positive disposition about this and about each-other.
Thank you for contributing.
Ongoing and Planned Work
- Make all the libraries stand on their own. Then we can include more useful libraries from other authors.
- Complete the site (deleightjs.com).
- Complete and add more examples.
- Improve the documentation.
- Fix the logo...
Ideas
- Progressive Web Apps.
- Efficient navigation library.