`deka-dom-el` — Elements

Basic concepts of elements modifications and creations.

Native JavaScript DOM elements creations

Let’s go through all patterns we would like to use and what needs to be improved for better experience.

// use NPM or for example https://cdn.jsdelivr.net/gh/jaandrle/deka-dom-el/dist/esm.js import { assign, el, createElement, elNS, createElementNS } from "deka-dom-el"; el===createElement elNS===createElementNS // “internal” utils import { assignAttribute, classListDeclarative, chainableAppend } from "deka-dom-el";

# Creating element(s) (with custom attributes)

You can create a native DOM element by using the document.createElement(). It is also possible to provide a some attribute(s) declaratively using Object.assign(). More precisely, this way you can sets some IDL also known as a JavaScript property.

Console01
Hide console
Emty div is generated inside <body>:true

To make this easier, you can use the el function. Internally in basic examples, it is wrapper around assign(document.createElement(…), { … }).

Console00
Hide console

The assign function provides improved behaviour of Object.assign(). You can declaratively sets any IDL and attribute of the given element. Function prefers IDL and fallback to the element.setAttribute if there is no writable property in the element prototype.

You can study all JavaScript elements interfaces to the corresponding HTML elements. All HTML elements inherits from HTMLElement. To see all available IDLs for example for paragraphs, see HTMLParagraphElement. Moreover, the assign provides a way to sets declaratively some convenient properties:

For processing, the assign internally uses assignAttribute and classListDeclarative.

Console02
Hide console
<p data-test1="v1" data-test2="v2" aria-label="v1" aria-role="none" onclick="console.log(event)" class="classAdd classAdd1 classToggle" style="color: navy; font-weight: bold;">Hello, world!</p>
paragraph.something=something

# Native JavaScript templating

By default, the native JS has no good way to define HTML template using DOM API:

Console02
Hide console
true
true

This library therefore overwrites the append method of created elements to always return parent element.

Console00
Hide console

# Basic (state-less) components

You can use functions for encapsulation (repeating) logic. The el accepts function as first argument. In that case, the function should return dom elements and the second argument for el is argument for given element.

Console00
Hide console

As you can see, in case of state-less/basic components there is no difference between calling component function directly or using el function.

It is nice to use similar naming convention as native DOM API. This allows us to use the destructuring assignment syntax and keep track of the native API (things are best remembered through regular use).

# Creating non-HTML elements

Similarly to the native DOM API (document.createElementNS) for non-HTML elements we need to tell JavaScript which kind of the element to create. We can use the elNS function:

Console01
Hide console
true

# Mnemonic

  • assign(<element>, ...<objects>): <element> — assign properties (prefered, or attributes) to the element
  • el(<tag-name>, <primitive>)[.append(...)]: <element-from-tag-name> — simple element containing only text
  • el(<tag-name>, <object>)[.append(...)]: <element-from-tag-name> — element with more properties (prefered, or attributes)
  • el(<function>, <function-argument(s)>)[.append(...)]: <element-returned-by-function> — using component represented by function (must accept object like for <tag-name>)
  • el(<...>, <...>, ...<addons>) — see following section of documentation
  • elNS(<namespace>)(<as-el-see-above>)[.append(...)]: <element-based-on-arguments> — typically SVG elements