JSPM

  • Created
  • Published
  • Downloads 2225609
  • Score
    100M100P100Q187251F

Dynamic stylesheets for web components.

Package Exports

  • jss
  • jss/lib
  • jss/lib/StyleSheet

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 style sheets for web components.

![Gitter](https://badges.gitter.im/Join Chat.svg)

JSS is a very thin layer which compiles JSON structures to CSS.

Why do we need transpilers like sass or stylus when we can use javascript to do the same and much more?

By leveraging namespaces we can solve the cascading problem better than bem and make our components truly reusable and composable.

Access css declarations and values from js without DOM round trip.

Smaller footprint because of code reuse and no vendor specific declarations

Take a look at examples directory.

Syntactic differences compared to CSS

Jss styles are just plain javascript objects. They map 1:1 to css rules, except of those modified by plugins.

// Some random jss code example
{
  '.carousel-caption': {
    'position': 'absolute',
    'z-index': '10',
  },
  'hr': {
    'border': '0',
    'border-top': '1px solid #eee'
  },
  '@media (min-width: 768px)': {
    '.modal-dialog': {
      'width': '600px',
      'margin': '30px auto'
    },
    '.modal-content': {
      'box-shadow': '0 5px 15px rgba(0, 0, 0, .5)'
    },
    '.modal-sm': {
      'width': '300px'
    }
  }
}

Multiple declarations with identical property names

I recommend to not to use this if you use jss on the client. Instead you should write a function, which makes a test for this feature support and generates just one final declaration.

In case you are using jss as a server side precompiler, you might want to have more than one property with identical name. This is not possible in js, but you can use an array.

{
    '.container': {
        background: [
            'red',
            '-moz-linear-gradient(left, red 0%, green 100%)',
            '-webkit-linear-gradient(left, red 0%, green 100%)',
            '-o-linear-gradient(left, red 0%, green 100%)',
            '-ms-linear-gradient(left, red 0%, green 100%)',
            'linear-gradient(to right, red 0%, green 100%)'
        ]
    }
}
.container {
    background: red;
    background: -moz-linear-gradient(left, red 0%, green 100%);
    background: -webkit-linear-gradient(left, red 0%, green 100%);
    background: -o-linear-gradient(left, red 0%, green 100%);
    background: -ms-linear-gradient(left, red 0%, green 100%);
    background: linear-gradient(to right, red 0%, green 100%);
}

API

Access the jss namespace

// Pure js
var jss = window.jss

// Commonjs
var jss = require('jss')

Create style sheet

jss.createStyleSheet([rules], [named], [attributes])

  • rules is an object, where keys are selectors if named is not true
  • named rules keys are not used as selectors, but as names, will cause auto generated class names and 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 media="print">
    .selector {
        width: 100px;
    }
</style>

Create namespaced style sheet.

Create a style sheet with namespaced rules. For this set second parameter to true.

var styleSheet = jss.createStyleSheet({
    myButton: {
        width: '100px',
        height: '100px'
    }
}, true).attach()

console.log(styleSheet.classes.myButton) // .jss-0
<style>
    .jss-0 {
        width: 100px;
        height: 100px;
    }
</style>

Attach style sheet

styleSheet.attach()

Insert style sheet into render tree.

styleSheet.attach()

Detach style sheet

styleSheet.detach()

Remove style sheet from render tree to increase runtime performance.

styleSheet.detach()

Add a rule

styleSheet.addRule([selector], rule)

Returns an array of rules, because you might have a nested rule in your style.

You might want to add rules dynamically.

var rules = styleSheet.addRule('.my-button', {
    padding: '20px',
    background: 'blue'
})

Add rule with generated class name.

var rules = styleSheet.addRule({
    padding: '20px',
    background: 'blue'
})
document.body.innerHTML = '<button class="' + rules[0].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 style sheet.

jss.createRule([selector], rule)

var rule = jss.createRule({
    padding: '20px',
    background: 'blue'
})

// Apply styles directly using jquery.
$('.container').css(rule.style)

Apply a rule to an element inline.

rule.applyTo(element)

jss.createRule({
    background: 'blue'
}).applyTo(document.body)

Register plugin.

jss.use(fn)

Passed function will be invoked with Rule instance. Take a look at plugins like extend, nested or vendorPrefixer.

jss.use(function(rule) {
    // Your modifier.
})

Convert to CSS

styleSheet.toString()

If you want to get a pure CSS string from jss e.g. for preprocessing jss on the server.

var jss = require('jss')

var styleSheet = jss.createStyleSheet({
    '.my-button': {
        float: 'left',
    }
})

console.log(styleSheet.toString())
.my-button {
  float: left;
}

Plugins

Things you know from stylus like @extend, nested selectors, vendor prefixer are separate plugins.

Full list of available plugins

Install

npm install jss
#or
bower install jsstyles

Convert CSS to JSS

# print help
jss
# convert css
jss source.css -p > source.jss

Benchmarks

  1. How fast would bootstrap css lib render? I have converted bootstraps css to jss. In bench/bootstrap folder you will find jss and css files. You need to try more than once to have some average value.

    On my machine overhead is about 10-15ms.

  2. Rendering jss vs. css (same styles) jsperf bench.

Run tests

Locally

npm i
open test/local.html

From github

Tests

License

MIT