1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2025-07-01 12:22:15 +02:00

🧱 move components, exports, and index files to examples directory

This commit is contained in:
2023-09-26 09:40:00 +02:00
parent 3823abc2d7
commit 7417565fa5
7 changed files with 7 additions and 1 deletions

View File

@ -0,0 +1,34 @@
import { style, el, on, S, namespace } from '../exports.js';
const className= style.host(fullNameComponent).css`
:host form{
display: flex;
flex-flow: column nowrap;
}
`;
export function fullNameComponent(){
const labels= [ "Name", "Surname" ];
const name= labels.map(_=> S(""));
const full_name= S(()=>
name.map(l=> l()).filter(Boolean).join(" ") || "-");
return el("div", { className }).append(
el("h2", "Simple form:"),
el("form", { onsubmit: ev=> ev.preventDefault() }).append(
...name.map((v, i)=>
el("label", labels[i]).append(
el("input", { type: "text", name: labels[i], value: v() }, on("change", ev=> v(ev.target.value)))
))
),
el("p").append(
el("strong", "Full name"),
": ",
el("#text", full_name)
),
namespace("svg").append(
el("svg", { viewBox: "0 0 240 80", style: { height: "80px", display: "block" } }).append(
//el("style", { })
el("text", { x: 20, y: 35, textContent: "Text" })
)
)
);
}

View File

@ -0,0 +1,87 @@
import { style, el, dispatchEvent, on, S } from '../exports.js';
const className= style.host(todosComponent).css`
:host{
display: flex;
flex-flow: column nowrap;
}
:host input{
margin-inline-start: .5em;
}
:host button{
margin-inline-start: 1em;
}
:host output{
white-space: pre;
}
`;
/** @param {{ todos: string[] }} */
export function todosComponent({ todos= [ "Task A" ] }= {}){
const todosS= S(todos.map(t=> S(t)), {
add(v){ this.value.push(S(v)); },
remove(i){ this.value.splice(i, 1); },
[S.symbols.onclear](){ S.clear(...this.value); },
});
const name= "todoName";
const onsubmitAdd= on("submit", event=> {
const el= event.target.elements[name];
event.preventDefault();
S.action(todosS, "add", el.value);
el.value= "";
});
const onremove= on("remove", event=>
S.action(todosS, "remove", event.detail));
const ul_todos= el("ul").append(
S.el(todosS, ts=> ts
.map((textContent, value)=>
el(todoComponent, { textContent, value, className }, onremove))
));
return el("div", { className }).append(
el("div").append(
el("h2", "Todos:"),
el("h3", "List of todos:"),
S.el(todosS, ts=> !ts.length
? el("p", "No todos yet")
: ul_todos),
el("p", "Click to the text to edit it.")
),
el("form", null, onsubmitAdd).append(
el("h3", "Add a todo:"),
el("label", "New todo: ").append(
el("input", { name, type: "text", required: true }),
),
el("button", "+")
),
el("div").append(
el("h3", "Output (JSON):"),
el("output", S(()=> JSON.stringify(todosS, null, "\t")))
)
)
}
/**
* @type {ddeComponent<
* { textContent: ddeSignal<string, any>, value: number },
* HTMLLIElement,
* [ "click" ]
* >}
* */
function todoComponent({ textContent, value }, host){
const onclick= on("click", event=> {
const value= Number(event.target.value);
event.preventDefault();
event.stopPropagation();
dispatchEvent(host(), "remove", value);
});
const is_editable= S(false);
const onedited= on("change", ev=> {
textContent(ev.target.value);
is_editable(false);
});
return el("li").append(
S.el(is_editable, is=> is
? el("input", { value: textContent(), type: "text" }, onedited)
: el("span", { textContent, onclick: is_editable.bind(null, true) }),
),
el("button", { type: "button", value, textContent: "-" }, onclick)
);
}

View File

@ -0,0 +1,122 @@
import { el } from "../../index.js";
import { S } from "../../signals.js";
const { hasOwnProperty }= Object.prototype;
const store= attrsPropsToSignals([ "test" ]);
/**
* Compatible with `npx-wca test/components/webComponent.js`
* */
export class CustomHTMLTestElement extends HTMLElement{
static get observedAttributes(){
return [ "name", "pre-name" ];
}
connectedCallback(){
this.attachShadow({ mode: "open" }).append(
customElementRender(this, store.toRender(this), this.render)
);
}
render({ name, test, preName }, host){
host(on.connected(console.log));
return el("p").append(
el("#text", { textContent: name }),
el("#text", { textContent: test }),
el("#text", { textContent: preName }),
);
}
}
// https://gist.github.com/WebReflection/ec9f6687842aa385477c4afca625bbf4
customElementsAssign(
CustomHTMLTestElement,
reflectObservedAttributes,
lifecycleToEvents(false),
store.connect
);
customElements.define("custom-test", CustomHTMLTestElement);
function customElementRender(_this, attrs, render){
const host= (...a)=> a.length ? a[0](_this) : _this;
return render(attrs, host);
}
/** @returns {HTMLElement} */
function customElementsAssign(class_base, ...automatize){
automatize.forEach(a=> a(class_base));
}
function reflectObservedAttributes(c){
for(const name of c.observedAttributes){
const name_camel= name.replace(/([a-z])-([a-z])/g, (_, l, r)=> l+r.toUpperCase());
if(hasOwnProperty.call(c.prototype, name_camel))
continue;
Reflect.defineProperty(c.prototype, name_camel, {
get(){ return this.getAttribute(name); },
set(value){ this.setAttribute(name, value); }
});
}
}
function lifecycleToEvents(is_attrs){
return function(c){
wrapMethod(c.prototype, "connectedCallback", function(target, thisArg, detail){
target.apply(thisArg, detail);
thisArg.dispatchEvent(new Event("dde:connected"));
});
wrapMethod(c.prototype, "disconnectedCallback", function(target, thisArg, detail){
target.apply(thisArg, detail);
thisArg.dispatchEvent(new Event("dde:disconnected"));
});
if(is_attrs)
wrapMethod(c.prototype, "attributeChangedCallback", function(target, thisArg, detail){
thisArg.dispatchEvent(new CustomEvent("dde:attribute", { detail }));
target.apply(thisArg, detail);
});
};
}
function attrsPropsToSignals(props= []){
const store_attrs= new WeakMap();
const store_props= new WeakMap();
return {
toRender(target){
const out= {};
const sattrs= get(store_attrs, target);
target.constructor.observedAttributes.forEach(function(name){
const name_camel= name.replace(/([a-z])-([a-z])/g, (_, l, r)=> l+r.toUpperCase());
if(!hasOwnProperty.call(sattrs, name)) sattrs[name]= S(undefined);
out[name_camel]= sattrs[name];
});
const sprops= get(store_props, target);
props.forEach(p=> !hasOwnProperty.call(sprops, p) && (sprops[p]= S(undefined)));
return Object.assign(out, sprops);
},
connect(c){
wrapMethod(c.prototype, "attributeChangedCallback", function(target, thisArg, detail){
const [ name, _, value ]= detail;
const s= get(store_attrs, thisArg);
if(s[name]) s[name](value);
else s[name]= S(value);
target.apply(thisArg, detail);
});
for(const name of props){
Reflect.defineProperty(c.prototype, name, {
get(){
const s= get(store_props, this);
if(s[name]) return s[name]();
},
set(value){
const s= get(store_props, this);
if(s[name]) s[name](value);
else s[name]= S(value);
}
});
}
}
};
function get(store, t){
if(store.has(t)) return store.get(t);
const s= {};
store.set(t, s);
return s;
}
}
function wrapMethod(obj, method, apply){
obj[method]= new Proxy(obj[method] || (()=> {}), { apply });
}

31
examples/exports.js Normal file
View File

@ -0,0 +1,31 @@
import * as dde_dom from "../index.js";
export * from "../index.js";
import * as dde_s from "../signals.js";
export * from "../signals.js";
// import { empty, namespace, on, dispatch } from "../index.js";
// import "../dist/dde-with-signals.js";
// Object.assign(globalThis, dde);
// import { el, on, off, S } from "../dist/esm-with-signals.js";
export const style= createStyle();
Object.assign(globalThis, dde_dom, dde_s);
function createStyle(){
const element= dde_dom.el("style");
const store= new WeakSet();
let host;
return {
element,
host(k, h= k.name){
if(store.has(k)) return { css: ()=> {} };
store.add(k);
host= h;
return this;
},
css(...args){
const textContent= String.raw(...args).replaceAll(":host", "."+host);
const className= host;
element.appendChild(el("#text", { textContent }));
return className;
}
};
}

View File

@ -0,0 +1,26 @@
<!DOCTYPE html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- DEL CSP https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP | https://github.com/Prinzhorn/minimal-csp -->
<!-- DEL https://github.com/jensimmons/cssremedy -->
<!-- <link rel="stylesheet" href="https://github.com/jensimmons/cssremedy/raw/master/css/remedy.css"> -->
<title>Small DOM element creation enhancements</title>
<meta name="description" content="Making creatig elements easier">
<script type="module">
import { el } from "../index.js";
document.body.append(
el("input", { type: "checkbox", name: "name", checked: true, id: undefined }),
el("p", { textContent: "Ahoj", style: { color: "green" } }),
el("select", { value: "a" }).append(
el("option", { value: "a", textContent: "A" }),
el("option", { value: "b", textContent: "B" })
)
);
</script>
</head>
<body>
</body>
</html>

16
examples/index.html Normal file
View File

@ -0,0 +1,16 @@
<!DOCTYPE html>
<html class="no-js" lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<!-- DEL CSP https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP | https://github.com/Prinzhorn/minimal-csp -->
<!-- DEL https://github.com/jensimmons/cssremedy -->
<!-- <link rel="stylesheet" href="https://github.com/jensimmons/cssremedy/raw/master/css/remedy.css"> -->
<title>Small DOM element creation enhancements</title>
<meta name="description" content="Making creatig elements easier">
<script src="index.js" type="module"></script>
</head>
<body>
</body>
</html>

12
examples/index.js Normal file
View File

@ -0,0 +1,12 @@
import { style, el } from './exports.js';
document.head.append(style.element);
import { fullNameComponent } from './components/fullNameComponent.js';
import { todosComponent } from './components/todosComponent.js';
import { CustomHTMLTestElement } from "./components/webComponent.js";
document.body.append(
el("h1", "Experiments:"),
el(fullNameComponent),
el(todosComponent),
el(customElements.getName(CustomHTMLTestElement), { name: "attr" })
);