Monday, May 09, 2016

CSSx: CSS by JavaScript (like JSx = HTML by JavaScript)

Finally, CSS In JavaScript! Meet CSSX – Smashing Magazine

krasimir/cssx: CSS in JavaScript @ GitHub
Generate and/or apply CSS with JavaScript.

"CSSX is not only about writing vanilla CSS in JavaScript. Even though you get this the main idea here is to have a good API for managing styles. CSSX doesn't inline styles so you keep your markup clean. It works directly with injected stylesheets. Here is a short example:"
function setStyles (fontSize, margin) {
  return <style>
    body {
      font-size: {{ fontSize }}px;
      line-height: {{ fontSize * 1.2 }}px;
      margin: {{ margin }}px;
    }
  </style>
}

var sheet = cssx();
sheet.add(setStyles(20, 6));
sheet.add(<style>
  p > a {
    text-decoration: none;
    color: #F00;
  }
</style>);
And it results in the following CSS:
body {
  margin: 6px;
  line-height: 24px;
  font-size: 20px;
}
p > a {
  color: #F00;
  text-decoration: none;
}
var myDivElement = <div className="foo" />; ReactDOM.render(myDivElement, document.getElementById('example'));

No comments: