Modern web applications need expressive language for styles description. This project takes a fresh look at the idea of writing stylesheets in javascript. It solves some major problems with css:
-
Cascading style sheets do not scale. There are some solutions like bem which solve this problem, however they introduce an overhead of writing long class names. Here is how true namespaces can look like in jss.
-
We often need to calculate layouts in javascript and we need to access properties defined in stylessheets. Jss allows us to do it easy and without DOM round trip by giving us a direct access to css values.
-
Its up to you whether to put some styles via style tag or to apply them directly to the element.
-
Optimize your app performance by detaching unused stylesheets from render tree.
-
Use full power of expressive full featured language. Any features you might know from stylus or sass and some more are already available.
-
Evil mixins are not a problem any more.
-
No need to precompile, but you can if you want to.
Take a look at examples directory.
Jss styles are just plain javascript objects. They map 1:1 to css rules, except of those modified by preprocessors.
Put an ampersand before a selector within a rule and it will be converted to a separate rule with a nested selector.
{
'.container': {
padding: '20px',
// Will result in .container.clear
'&.clear': {
clear: 'both'
},
// Will result in .container .button
'& .button': {
background: 'red'
}
}
}
.container {
padding: 20px;
}
.container.clear {
clear: both;
}
.container .button {
background: red;
}
Add extend
keyword to a rule and set any style or array of styles as value to extend some style definition with the current style object. See example.
var rules = {}
var button1 = {
padding: '20px',
background: 'blue'
}
rules['.button-1'] = button1
rules['.button-2'] = {
extend: button1, // can be an array of styles
padding: '30px'
}
.button-1 {
padding: 20px;
background: blue;
}
.button-2 {
padding: 30px;
background: blue;
}
// Pure js
var jss = window.jss
// Commonjs
var jss = require('jss')
jss.createStylesheet([rules], [generateClasses], [attributes])
rules
is an object, where keys are selectors ifgenerateClasses
is not truegenerateClasses
will cause auto generated class names for rules as selectors. It will also make class names accessible viastylesheet.classes
.attributes
allows to set any attributes on style element.
var stylesheet = jss.createStylesheet({
'.selector': {
width: '100px'
}
}, {media: 'print'}).attach()
<style type="text/css" media="print" title="Generated by jss.">
.selector {
width: 100px;
}
</style>
Create a stylesheet with namespaced rules.
var stylesheet = jss.createStylesheet({
myButton: {
width: '100px',
height: '100px'
}
}, true).attach()
console.log(stylesheet.classes.myButton) // .jss-0
<style type="text/css" media="screen" title="Generated by jss.">
.jss-0 {
width: 100px;
height: 100px;
}
</style>
stylesheet.attach()
Insert stylesheet into render tree.
stylesheet.attach()
stylesheet.detach()
Remove stylesheet from render tree for performance optimization.
stylesheet.detach()
stylesheet.addRule([selector], rule)
var button = stylesheet.addRule('.my-button', {
padding: '20px',
background: 'blue'
})
In case you have an element reference or you create elements in javascript you might want to write styles and attach them later to the element using a generated class name.
var button = stylesheet.addRule({
padding: '20px',
background: 'blue'
})
document.body.innerHTML = '<button class="' + button.className + '">Button</button>'
stylesheet.getRule(selector)
var rule = stylesheet.getRule('.my-button')
jss.createRule([selector], rule)
var rule = jss.createRule({
padding: '20px',
background: 'blue'
})
// Apply styles directly using jquery.
$('.container').css(rule.style)
npm i
open test/index.html
MIT