styled = (styles, tokens = {}) => {
const res = {};
for (let k in styles) {
const [styledName, tagName] = k.split(":");
const normalizedStyle = Object.entries(styles[k]).reduce(
(r, [k, v]) => {
if (k.startsWith("@") || k.startsWith("&")) {
r[k] = v;
} else {
r["&"][k] = v;
}
return r;
},
{ "&": {} }
);
const s = registerStyle(
style(normalizedStyle, tokens, undefined, parseToken)
);
const n = className(s);
res[styledName] = Object.assign(
(
(styleName, tagName = "div") =>
(...value) => {
const node = H[tagName || "div"](...value);
const klass = node.attributes["class"];
node.attributes["class"] = klass
? `${styleName} ${klass}`
: styleName;
return node;
}
)(n, tagName),
{ className: n }
);
}
return res;
}