From 5512c955e52fc8069864e106855aa95e7ea5fc3f Mon Sep 17 00:00:00 2001 From: DarkWiiPlayer Date: Tue, 8 Feb 2022 13:14:20 +0100 Subject: [PATCH] Refactor CSS and add "style" function --- css.js | 16 +++++++++++----- skooma.js | 1 - 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/css.js b/css.js index 5023f00..46fe307 100644 --- a/css.js +++ b/css.js @@ -10,6 +10,13 @@ const diversify = (prefix, now, ...rest) => const keyToPropName = key => key.replace(/^[A-Z]/, a => "-"+a).replace(/[A-Z]/g, a => '-'+a.toLowerCase()) +const buildValue = value => + Array.isArray(value) + ? value.map(e => Array.isArray(e) ? e.map(e => e.toString()).join(' ') : e.toString()).join(", ") + : value.toString() + +const buildRule = (prop, value) => `${keyToPropName(prop)}: ${buildValue(value)}` + const walkStyles = (styles, trail=[], buffer=[]) => { let inner const position = buffer.push(undefined)-1 @@ -20,17 +27,16 @@ const walkStyles = (styles, trail=[], buffer=[]) => { trail.pop() } else { inner ||= [] - const rules = Array.isArray(children) - ? children.map(e => Array.isArray(e) ? e.map(e => e.toString()).join(' ') : e.toString()).join(", ") - : children.toString() - inner.push(`${keyToPropName(name)}: ${rules}`) + inner.push(buildRule(name, children)) } }) if (inner) buffer[position] = (`${diversify("", ...trail)} {${inner.join("; ")}}`) return buffer } -export const css = (styles) => walkStyles(styles).filter(e=>e).join("\n") +export const style = rules => Object.entries(rules).map(buildRule) + +export const css = styles => walkStyles(styles).filter(e=>e).join("\n") const mkVar = name => { const v = (def) => `var(--${name}, ${def})` diff --git a/skooma.js b/skooma.js index bb093b2..a542ddf 100644 --- a/skooma.js +++ b/skooma.js @@ -89,7 +89,6 @@ export const bind = transform => { const update = (...data) => { const next = transform(...data) if (next) { - console.log(element) if (element) element.replaceWith(next) element = inject(next) return element