Last active
July 12, 2019 11:52
-
-
Save FullZero5/35a3d2ca12a76604c0deb3e1bd7469d7 to your computer and use it in GitHub Desktop.
objtocss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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