1
0
mirror of https://github.com/jaandrle/deka-dom-el synced 2025-07-04 05:32:13 +02:00

Add signals functionality and reactive attributes in assign

This commit is contained in:
2023-08-24 14:15:55 +02:00
parent 7a2c3e6a4b
commit 404971f484
6 changed files with 88 additions and 36 deletions

View File

@ -20,14 +20,22 @@ export function createElementNS(tag, attributes, attributes_todo){
}
export { createElementNS as elNS };
import { watch } from './signals.js';
function isReactive(key, attr){
if(typeof attr !== "function") return false;
if(key.startsWith("on")) return false;
return true;
}
export function assign(element, ...attributes){
if(!attributes.length) return element;
const is_svg= element instanceof SVGElement;
const setRemoveAttr= (is_svg ? setRemoveNS : setRemove).bind(null, element, "Attribute");
Object.entries(Object.assign({}, ...attributes)).forEach(function([ key, attr ]){
Object.entries(Object.assign({}, ...attributes)).forEach(function assignNth([ key, attr ]){
if(key[0]==="=") return setRemoveAttr(key.slice(1), attr);
if(key[0]===".") return setDelete(element, key.slice(1), attr);
if(isReactive(key, attr))
return watch(()=> assignNth([ key, attr() ]));
if(typeof attr === "object"){
switch(key){
case "style": return forEachEntries(attr, setRemove.bind(null, element.style, "Property"))