Skip to content

A library expanding the capabilities of the native DOM API with the aim of offering the possibility of writing reactive UI templates/components declaratively directly in JavaScript.

License

Notifications You must be signed in to change notification settings

jaandrle/deka-dom-el

Repository files navigation

WIP (the experimentation phase) | source code on GitHub | mirrored on Gitea

Vanilla for flavouring — a full-fledged feast for large projects

…use simple DOM API by default and library tools and logic when you need them

document.body.append(
	el(HelloWorldComponent, { initial: "🚀" })
);
/** @typedef {"🎉" | "🚀"} Emoji */
/** @param {{ initial: Emoji }} attrs */
function HelloWorldComponent({ initial }){
	const clicks= S(0);
	const emoji= S(initial);
	/** @param {HTMLOptionElement} el */
	const isSelected= el=> (el.selected= el.value===initial);
	// @ts-expect-error 2339: The <select> has only two options with {@link Emoji}
	const onChange= on("change", event=> emoji(event.target.value));

	return el().append(
		el("p", {
			textContent: S(() => `Hello World ${emoji().repeat(clicks())}`),
			className: "example",
			ariaLive: "polite", //OR ariaset: { live: "polite" },
			dataset: { example: "Example" }, //OR dataExample: "Example",
		}),
		el("button",
			{ textContent: "Fire", type: "button" },
			on("click", ()=> clicks(clicks() + 1)),
			on("keyup", ()=> clicks(clicks() - 2)),
		),
		el("select", null, onChange).append(
			el(OptionComponent, "🎉", isSelected),//OR { textContent: "🎉" }
			el(OptionComponent, "🚀", isSelected),//OR { textContent: "🚀" }
		)
	);
}
function OptionComponent({ textContent }){
	return el("option", { value: textContent, textContent })
}

Deka DOM Elements

Creating reactive elements, components and Web components using [IDL](https://developer.mozilla.org/en-US/docs/ Glossary/IDL)/JavaScript DOM API and signals/observables.

Inspiration and suggested alternatives

Why an another one?

This library falls somewhere between van/hyperscript and solid-js in terms of size, complexity, and usability.

Another goal is to proceed in the best spirit of functional programming. This involves starting with pure JavaScript (DOM API) and gradually adding auxiliary functions, ranging from “minor” improvements to the capability of writing complete declarative reactive UI templates.

As a result, any “internal” function (assign, classListDeclarative, on, dispatchEvent, …, S, …) can be used independently, although they are primarily designed for use in combination. This can also, hopefully, help in integrating the library into existing projects.

To balance these requirements, numerous compromises have been made. To summarize:

  • Library size: 10–15kB minified (the original goal was a maximum of 10kB)
  • Optional use of signals with the ability to register your own signals/observables implementation
  • No build step required
  • Preference for a declarative/functional approach
  • Focus on zero/minimal dependencies
  • Support for/enhancement of custom elements (web components)
  • Support for SSR (jsdom)
  • WIP providing types

First steps

  • Guide
  • Documentation
  • Installation
    • npm
    • dist/ (https://cdn.jsdelivr.net/gh/jaandrle/deka-dom-el/dist/…)

Signals