Skip to content

Instantly share code, notes, and snippets.

@FullZero5
Last active July 12, 2019 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FullZero5/35a3d2ca12a76604c0deb3e1bd7469d7 to your computer and use it in GitHub Desktop.
Save FullZero5/35a3d2ca12a76604c0deb3e1bd7469d7 to your computer and use it in GitHub Desktop.
objtocss
const o2s = (o, className) => {
let elm = new Option();
Object.keys(o).forEach(a => (elm.style)[a] = o[a]);
return `.${className}{\n${elm.getAttribute("style")}\n}`;
}
const obj = {
visibility: "visible",
position: "fixed",
background: "transparent",
border: "0",
textAlign: "center"
};
console.log(o2s(obj, 'menu'));
const o2s = (o, className) => {
let elm = new Option();
Object.keys(o).forEach(a => (elm.style)[a] = o[a]);
return `.${className}{\n${elm.getAttribute("style")}\n}`;
}
class CSSMiniEngine {
_classes = [];
addClass = (o, className) => {
this._classes.push(o2s(o, className));
};
compile = () => {
const head = document.head || document.getElementsByTagName("head")[0];
const style = document.createElement("style");
head.appendChild(style);
style.type = "text/css";
style.appendChild(document.createTextNode(this._classes.join("\n")));
}
}
const cssEngine = new CSSMiniEngine();
const className = "MyClass";
cssEngine.addClass(
{
visibility: "visible",
position: "fixed",
color: "red",
background: "transparent",
border: "1",
textAlign: "center",
},
className
);
cssEngine.compile()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment