Skip to content

talves-forked/jss

 
 

Repository files navigation

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:

  1. 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.

  2. 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.

  3. Its up to you whether to put some styles via style tag or to apply them directly to the element.

  4. Optimize your app performance by detaching unused stylesheets from render tree.

  5. Use full power of expressive full featured language. Any features you might know from stylus or sass and some more are already available.

  6. Evil mixins are not a problem any more.

  7. 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], [generateClasses], [attributes])

  • rules is an object, where keys are selectors if generateClasses is not true
  • generateClasses will cause auto generated class names for rules as selectors. It will also make class names accessible via stylesheet.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>

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)

var rule = stylesheet.getRule('.my-button')

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)

Run tests

npm i
open test/index.html

License

MIT

Releases

No releases published

Packages

No packages published