|
1 | 1 | # react-css-builder
|
2 |
| -CSS builder to create react component style objects |
| 2 | +CSS builder for creating inline react component style objects |
| 3 | + |
| 4 | +If you want React to do native well, you need to use inline styles. But, you can still get the DRY and maintainability benefits of an external stylesheet with react-css-builder. |
| 5 | + |
| 6 | +You can create a javascript stylesheet that is similary to what you would see with a CSS precompiler but you can actually use variablees calculated at runtime! |
| 7 | + |
| 8 | +A few examples |
| 9 | + |
| 10 | +Registering mixins and variables |
| 11 | +``` |
| 12 | + // the mixin can have any number of arguments provided when the mixin is referenced |
| 13 | + css.mixin('mixin-name', function(arg1, arg2, ...) { |
| 14 | + return { |
| 15 | + // any attributes will be included with the styleset results |
| 16 | + }; |
| 17 | + }); |
| 18 | +
|
| 19 | + // add variables that can be referenced in stylesets using this.get("varName"); |
| 20 | + css.vars({ |
| 21 | + foo: 'bar' |
| 22 | + }); |
| 23 | +``` |
| 24 | + |
| 25 | +Creating a stylesheet |
| 26 | +``` |
| 27 | +module.exports = require('react-css-builder').register('my-namespace', { |
| 28 | + // use javascript-style humpback CSS here |
| 29 | + myCssClass: { |
| 30 | + color: 'white' |
| 31 | + }, |
| 32 | +
|
| 33 | + fancierCssClass: function(css) { |
| 34 | + return css |
| 35 | + // include the attributes from the "myCssClass" (in this namespace / css file) |
| 36 | + .include('myCssClass') |
| 37 | +
|
| 38 | + // include the attributes from a class in another namespace (1st param to register method) |
| 39 | + .include('another-namespace.anotherCssClass') |
| 40 | +
|
| 41 | + // include attributes from a mixin (vendor-prefix example can be seen later) |
| 42 | + .mixin('vendor-prefix', 'border-radius', 3) |
| 43 | +
|
| 44 | + // add attitional styles |
| 45 | + .val({ |
| 46 | + // you can reference global or class reference variables |
| 47 | + background-color: this.get('backgroundColor'), |
| 48 | + // or just use simple attributes |
| 49 | + backgroundImage: ... |
| 50 | + }) |
| 51 | + }, |
| 52 | +
|
| 53 | + simpleReferenceTimeExecutionClass: function() { |
| 54 | + // if don't need any registered variables or mixin use but just want to evaluate |
| 55 | + // something when the styleset is referenced |
| 56 | + return { |
| 57 | + height: window.innerHeight + 'px' |
| 58 | + }; |
| 59 | + } |
| 60 | +}) |
| 61 | +``` |
| 62 | + |
| 63 | +To use the class references |
| 64 | +``` |
| 65 | + var css = require('path/to/my/css/file'); |
| 66 | + // eager fetch the styleset if possible |
| 67 | + var myStyleset = css.css('myCssClass'); |
| 68 | + // to provide variables and/or additional attributes (notice the method is "get" instead of "css") |
| 69 | + var myStylesetWithVarAndAdditionalAttributes = css.get('fancierCssClass') |
| 70 | + // add variables that can be referenced with styleset functions and/or mixins |
| 71 | + .vars({ |
| 72 | + border: 1 |
| 73 | + }) |
| 74 | + // add additional attributes defined here |
| 75 | + .attr({ |
| 76 | + fontFamily: 'arial' |
| 77 | + }) |
| 78 | + // get the styleset (now we use the "css" function) |
| 79 | + .css(); |
| 80 | +
|
| 81 | + ... |
| 82 | + render: function() { |
| 83 | + return <div style={myStylesetWithVarAndAdditionalAttributes}>Hello</div> |
| 84 | + } |
| 85 | + } |
| 86 | +``` |
| 87 | + |
| 88 | +more documentation to come |
0 commit comments