Package Exports
- jss
- jss/lib
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (jss) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Dynamic stylesheets for web components.
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.
Built in preprocessors
Jss styles are just plain javascript objects. They map 1:1 to css rules, except of those modified by preprocessors.
Nested Rules
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;
}Extend keyword
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;
}API
Access the jss namespace
// Pure js
var jss = window.jss
// Commonjs
var jss = require('jss')Create stylesheet
jss.createStylesheet([rules], [named], [attributes])
rulesis an object, where keys are selectors ifnamedis not truenamedrule keys are not used as selectors, but as names, will cause auto generated class names and selectors. It will also make class names accessible viastylesheet.classes.attributesallows 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>Attach stylesheet
stylesheet.attach()
Insert stylesheet into render tree.
stylesheet.attach()Detach stylesheet
stylesheet.detach()
Remove stylesheet from render tree for performance optimization.
stylesheet.detach()Add a rule
stylesheet.addRule([selector], rule)
You might want to add rules dynamically.
var button = stylesheet.addRule('.my-button', {
padding: '20px',
background: 'blue'
})Generated namespace.
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>'Get a rule
stylesheet.getRule(selector)
// Using selector
var rule = stylesheet.getRule('.my-button')
// Using name, if named rule was added.
var rule = stylesheet.getRule('myButton')
Add a rules
stylesheet.addRules(rules)
Add a list of rules.
stylesheet.addRules({
'.my-button': {
float: 'left',
},
'.something': {
display: 'none'
}
})Create a rule without a stylesheet.
jss.createRule([selector], rule)
var rule = jss.createRule({
padding: '20px',
background: 'blue'
})
// Apply styles directly using jquery.
$('.container').css(rule.style)Install
npm i jssRun tests
npm i
open test/index.htmlLicense
MIT