The library tries to provide pure JavaScript tool(s) to create reactive interfaces. The main goals are:
It is, in fact, an reimplementation of jaandrle/dollar_dom_component.
import { el, S } from "https://cdn.jsdelivr.net/gh/jaandrle/deka-dom-el/dist/esm-with-signals.js";
+`deka-dom-el` — Introduction/Guide `deka-dom-el` — Introduction/Guide
The library tries to provide pure JavaScript tool(s) to create reactive interfaces. The main goals are:
- provide a small wrappers/utilities around the native JavaScript DOM
- keep library size around 10kB at maximum (if possible)
- zero dependencies (if possible)
- prefer a declarative/functional approach
- unopinionated (allow alternative methods)
It is, in fact, an reimplementation of jaandrle/dollar_dom_component.
import { el, S } from "https://cdn.jsdelivr.net/gh/jaandrle/deka-dom-el/dist/esm-with-signals.js";
const clicks= S(0);
document.body.append(
- el("<>").append(
+ el().append(
el("p", S(()=>
"Hello World "+"🎉".repeat(clicks())
)),
@@ -12,7 +12,7 @@ document.body.append(
})
)
);
-
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.
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.
document.body.append(
+
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.
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.
document.body.append(
document.createElement("div")
);
console.log(
@@ -26,7 +26,7 @@ document.body.append(
{ textContent: "Element’s text content.", style: "color: coral;" }
)
);
-
To make this easier, you can use the el
function. Internally in basic examples, it is wrapper around assign(document.createElement(…), { … })
.
import { el, assign } from "https://cdn.jsdelivr.net/gh/jaandrle/deka-dom-el/dist/esm-with-signals.js";
+
To make this easier, you can use the el
function. Internally in basic examples, it is wrapper around assign(document.createElement(…), { … })
.
import { el, assign } from "https://cdn.jsdelivr.net/gh/jaandrle/deka-dom-el/dist/esm-with-signals.js";
const color= "lightcoral";
document.body.append(
el("p", { textContent: "Hello (first time)", style: { color } })
@@ -37,7 +37,7 @@ document.body.append(
{ textContent: "Hello (second time)", style: { color } }
)
);
-
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:
- It is possible to sets
data-*
/aria-*
attributes using object notation. - In opposite, it is also possible to sets
data-*
/aria-*
attribute using camelCase notation. - You can use string or object as a value for
style
property. className
(IDL – preffered)/class
are ways to add CSS class to the element. You can use string (similarly to class="…"
syntax in HTML) or array of strings. This is handy to concat conditional classes.- Use
classList
to toggle specific classes. This will be handy later when the reactivity via signals is beeing introduced. - The
assign
also accepts the undefined
as a value for any property to remove it from the element declaratively. Also for some IDL the corresponding attribute is removed as it can be confusing. For example, natievly the element’s id
is removed by setting the IDL to an empty string.
For processing, the assign
internally uses assignAttribute
and classListDeclarative
.
import { assignAttribute, classListDeclarative } from "https://cdn.jsdelivr.net/gh/jaandrle/deka-dom-el/dist/esm-with-signals.js";
+
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:
- It is possible to sets
data-*
/aria-*
attributes using object notation. - In opposite, it is also possible to sets
data-*
/aria-*
attribute using camelCase notation. - You can use string or object as a value for
style
property. className
(IDL – preffered)/class
are ways to add CSS class to the element. You can use string (similarly to class="…"
syntax in HTML) or array of strings. This is handy to concat conditional classes.- Use
classList
to toggle specific classes. This will be handy later when the reactivity via signals is beeing introduced. - The
assign
also accepts the undefined
as a value for any property to remove it from the element declaratively. Also for some IDL the corresponding attribute is removed as it can be confusing. For example, natievly the element’s id
is removed by setting the IDL to an empty string.
For processing, the assign
internally uses assignAttribute
and classListDeclarative
.
import { assignAttribute, classListDeclarative } from "https://cdn.jsdelivr.net/gh/jaandrle/deka-dom-el/dist/esm-with-signals.js";
const paragraph= document.createElement("p");
assignAttribute(paragraph, "textContent", "Hello, world!");
@@ -62,7 +62,7 @@ console.log(paragraph.outerHTML);
document.body.append(
paragraph
);
-
Native JavaScript templating
By default, the native JS has no good way to define HTML template using DOM API:
document.body.append(
+
Native JavaScript templating
By default, the native JS has no good way to define HTML template using DOM API:
document.body.append(
document.createElement("div"),
document.createElement("span"),
document.createElement("main")
@@ -73,7 +73,7 @@ const template= document.createElement("main").append(
document.createElement("span"),
);
console.log(typeof template==="undefined");
-
This library therefore ooverwrites the append
method to always return parent element.
import { el } from "https://cdn.jsdelivr.net/gh/jaandrle/deka-dom-el/dist/esm-with-signals.js";
+
This library therefore ooverwrites the append
method to always return parent element.
import { el } from "https://cdn.jsdelivr.net/gh/jaandrle/deka-dom-el/dist/esm-with-signals.js";
document.head.append(
el("style").append(
"tr, td{ border: 1px solid red; padding: 1em; }",
@@ -97,7 +97,7 @@ document.body.append(
)
)
);
-
Creating advanced (reactive) templates and components
Basic 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.
import { el } from "https://cdn.jsdelivr.net/gh/jaandrle/deka-dom-el/dist/esm-with-signals.js";
+
Creating advanced (reactive) templates and components
Basic 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.
import { el } from "https://cdn.jsdelivr.net/gh/jaandrle/deka-dom-el/dist/esm-with-signals.js";
document.head.append(
el("style").append(
".class1{ font-weight: bold; }",
@@ -114,4 +114,4 @@ function component({ className, textContent }){
el("p", textContent)
);
}
-
It is nice to use similar naming convention as native DOM API.
\ No newline at end of file
+
It is nice to use similar naming convention as native DOM API.
\ No newline at end of file diff --git a/docs_src/components/example.html.js b/docs_src/components/example.html.js index e066e17..88a2f8e 100644 --- a/docs_src/components/example.html.js +++ b/docs_src/components/example.html.js @@ -15,7 +15,7 @@ export function example({ src, language= "javascript" }){ .toString() .replaceAll(' from "../../../index-with-signals.js";', ' from "https://cdn.jsdelivr.net/gh/jaandrle/deka-dom-el/dist/esm-with-signals.js";'); const id= "code-"+Math.random().toString(36).slice(2, 7); - return el("<>").append( + return el().append( el("div", { id, className: example.name }).append( el("pre").append( el("code", { className: "language-"+language, textContent: code }) diff --git a/docs_src/components/examples/helloWorld.js b/docs_src/components/examples/helloWorld.js index 7f05790..c20c0db 100644 --- a/docs_src/components/examples/helloWorld.js +++ b/docs_src/components/examples/helloWorld.js @@ -1,7 +1,7 @@ import { el, S } from "../../../index-with-signals.js"; const clicks= S(0); document.body.append( - el("<>").append( + el().append( el("p", S(()=> "Hello World "+"🎉".repeat(clicks()) )), diff --git a/docs_src/index.html.js b/docs_src/index.html.js index 35cd8cf..a6a199a 100644 --- a/docs_src/index.html.js +++ b/docs_src/index.html.js @@ -21,7 +21,7 @@ export const css= styles() ` .include(example_css); export function body(pkg){ - return el("<>").append( + return el().append( el("h1", pageName(pkg)), el("p").append( "The library tries to provide pure JavaScript tool(s) to create reactive interfaces. ", diff --git a/docs_src/layout/head.html.js b/docs_src/layout/head.html.js index 99ffb50..0fa6aa3 100644 --- a/docs_src/layout/head.html.js +++ b/docs_src/layout/head.html.js @@ -12,7 +12,7 @@ import { el } from "deka-dom-el"; * @param {object} def * */ export function head({ id, title, description, pkg, path_target }){ - return el("<>").append( + return el().append( el("meta", { name: "viewport", content: "width=device-width, initial-scale=1" }), el("link", { rel: "stylesheet", href: stylesheetHref(path_target, id) }), @@ -23,7 +23,7 @@ export function head({ id, title, description, pkg, path_target }){ ); } function metaTwitter({ name, description, homepage }){ - return el("<>").append( + return el().append( el("meta", { name: "twitter:card", content: "summary_large_image" }), //el("meta", { name: "twitter:domain", content: "" }), el("meta", { name: "twitter:url", content: homepage }), @@ -34,7 +34,7 @@ function metaTwitter({ name, description, homepage }){ ); } function metaFacebook({ name, description, homepage }){ - return el("<>").append( + return el().append( el("meta", { name: "og:url", content: homepage }), el("meta", { name: "og:title", content: name }), el("meta", { name: "og:description", content: description }), diff --git a/index.d.ts b/index.d.ts index 95c38cf..ccffb6c 100644 --- a/index.d.ts +++ b/index.d.ts @@ -38,14 +38,13 @@ type ElementAttributes