`deka-dom-el` — Events and Addons

Using not only events in UI declaratively.

Listenning to the native DOM events and other Addons

We quickly introduce helper to listening to the native DOM events. And library syntax/pattern so-called Addon to incorporate not only this in UI templates declaratively.

Events and listenners

In JavaScript you can listen to the native DOM events of the given element by using element.addEventListener(type, listener, options). The library provides an alternative (on) accepting the differen order of the arguments:

import { el, on } from "https://cdn.jsdelivr.net/gh/jaandrle/deka-dom-el/dist/esm-with-signals.js"; const log= mark=> console.log.bind(console, mark); const button= el("button", "Test click"); button.addEventListener("click", log("`addEventListener`"), { once: true }); on("click", log("`on`"), { once: true })(button); document.body.append( button );

…this is actually one of the two differences. The another one is thaton accepts only object as the options (but it is still optional).

The other difference is that there is no off function. You can remove listener declaratively using AbortSignal:

import { el, on } from "https://cdn.jsdelivr.net/gh/jaandrle/deka-dom-el/dist/esm-with-signals.js"; const log= mark=> console.log.bind(console, mark); const abort_controller= new AbortController(); const { signal }= abort_controller; const button= el("button", "Test click"); button.addEventListener("click", log("`addEventListener`"), { signal }); on("click", log("`on`"), { signal })(button); document.body.append( button, " ", el("button", { textContent: "Off", onclick: ()=> abort_controller.abort() }) );

Addons

From practical point of view, Addons are just functions that accept any html element as their first parameter. You can see that the on(…) fullfills this requirement.

You can use Addons as ≥3rd argument of el function. This way is possible to extends you templates by additional (3rd party) functionalities. But for now mainly, you can add events listeners:

import { el, on } from "https://cdn.jsdelivr.net/gh/jaandrle/deka-dom-el/dist/esm-with-signals.js"; const abort_controller= new AbortController(); const { signal }= abort_controller; /** @type {ddeElementAddon<HTMLButtonElement>} */ const onclickOff= on("click", ()=> abort_controller.abort(), { signal }); /** @type {(ref?: HTMLOutputElement)=> HTMLOutputElement | null} */ const ref= (store=> ref=> ref ? (store= ref) : store)(null); document.body.append( el("button", "Test `on`", on("click", console.log, { signal }), on("click", update, { signal }), on("mouseup", update, { signal })), " ", el("button", "Off", onclickOff), el("output", { style: { display: "block", whiteSpace: "pre" } }, ref) ); /** @param {MouseEvent} event @this {HTMLButtonElement} */ function update(event){ ref().append( event.type, "\n" ); }

As the example shows, you can also provide types in JSDoc+TypeScript by using global type ddeElementAddon. Also notice, you can use Addons to get element reference.

Life cycle events

Addons are called immediately when the element is created, event it is not connected to live DOM yet.