From 864cf22f5f2260abd902180a318d651772e2c611 Mon Sep 17 00:00:00 2001 From: DarkWiiPlayer Date: Mon, 16 Aug 2021 17:57:10 +0200 Subject: [PATCH] Add skooma style property for setting inline styles --- skooma.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/skooma.js b/skooma.js index d155b4c..6ca6fbf 100644 --- a/skooma.js +++ b/skooma.js @@ -10,6 +10,16 @@ or html.ul([1, 2, 3, 4, 5].map(x => html.li(x)), {class: "numbers"}) */ +const keyToPropName = key => key.replace(/^[A-Z]/, a => "-"+a).replace(/[A-Z]/g, a => '-'+a.toLowerCase()) + +const insertStyles = (rule, styles) => { + for (let [key, value] of Object.entries(styles)) + if (typeof value == "undefined") + rule.removeProperty(keyToPropName(key)) + else + rule.setProperty(keyToPropName(key), value.toString()) +} + const parseAttribute = (attribute) => { if (typeof attribute == "string" || typeof attribute == "number") return attribute @@ -30,7 +40,9 @@ const parseArgs = (element, ...args) => { parseArgs(element, ...arg) else for (let key in arg) - if (key == "shadowRoot") + if (key == "style" && typeof(arg[key]=="object")) + insertStyles(element.style, arg[key]) + else if (key == "shadowRoot") parseArgs((element.shadowRoot || element.attachShadow({mode: "open"})), arg[key]) else if (typeof arg[key] == "function") element.addEventListener(key.replace(/^on[A-Z]/, x => x.charAt(x.length-1).toLowerCase()), e => e.preventDefault() || arg[key](e))