1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2024-11-22 15:59:37 +01:00
deka-dom-el/test/index.js

64 lines
1.6 KiB
JavaScript
Raw Normal View History

import { el, elNS, assign, listen, dispatch } from "../index.js";
Object.assign(globalThis, { el, elNS, assign, listen, dispatch });
2023-08-22 16:30:03 +02:00
const { style, css }= createStyle();
globalThis.test= console.log;
const app= el(component, null, listen("change", globalThis.test));
dispatch("change", "Peter")(app);
console.log(app, app instanceof HTMLDivElement);
2023-08-22 16:30:03 +02:00
document.head.append(style);
document.body.append(app);
function component({ value= "World" }= {}){
const name= "naiveForm";
css`
.${name}{
display: flex;
flex-flow: column nowrap;
}
.${name} input{
margin-inline-start: .5em;
}
`;
2023-08-23 20:15:30 +02:00
const output= eventsSink(store=> ({
onchange: listen("change", function(event){
assign(store.element, { textContent: event.target.value });
})
}));
const input= eventsSink(store=> ({
onchange: listen("change", function(event){
assign(store.element, { value: event.detail });
dispatch("change")(input.element);
})
}));
return el("div", { className: name }, input.onchange).append(
el("p").append(
el("#text", { textContent: "Hello " }),
2023-08-23 20:15:30 +02:00
el("strong", { textContent: value }, output.target),
),
el("label").append(
el("#text", { textContent: "Set name:" }),
2023-08-23 20:15:30 +02:00
el("input", { type: "text", value }, output.onchange, input.target)
)
2023-08-22 16:30:03 +02:00
)
}
2023-08-23 20:15:30 +02:00
function eventsSink(fn){
const store= {
element: null,
target: function(element){ store.element= element; },
};
Object.assign(store, fn(store));
return store;
}
function createStyle(){
const style= el("style");
return {
style,
css(...args){
style.appendChild(el("#text", { textContent: String.raw(...args) }));
}
};
}