|
| 1 | +# JSCSS |
| 2 | + |
| 3 | +#### I recomend using SASS/SCSS for creating insteadof this library. But, if for some reason you wish to create CSS in JavaScript this library might help. |
| 4 | + |
| 5 | +### `class JSCSS` has 5 properties: |
| 6 | +- `style` : A `Proxy` for adding/removing styles in a block. |
| 7 | + |
| 8 | + Example |
| 9 | + ```javascript |
| 10 | + let mydiv = new JSCSS('#mydiv'); |
| 11 | + |
| 12 | + // Add styles |
| 13 | + mydiv.style.color = 'blue'; |
| 14 | + mydiv.style.padding = '0.8em 1.2em'; |
| 15 | + mydiv.style['box-shadow'] = '0 0 3px black'; |
| 16 | + |
| 17 | + // Multi-valued properties can be assigned using arrays |
| 18 | + mydiv.style.background = [ |
| 19 | + '#844af0', |
| 20 | + '-webkit-gradient(linear, left top, left bottom, from(#844af0),to(#6d2de3))', |
| 21 | + 'linear-gradient(to bottom, #844af0 0%,#6d2de3 100%)' |
| 22 | + ]; |
| 23 | + |
| 24 | + // Remove style |
| 25 | + delete mydiv.style.background; |
| 26 | + ``` |
| 27 | + |
| 28 | +- `selectors` : A string `Set` for adding and removing selectors in a block. See [Set MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) for more details on sets. |
| 29 | + |
| 30 | + Example |
| 31 | + ```javascript |
| 32 | + let highlight = new JSCSS; |
| 33 | + |
| 34 | + // Add selectors |
| 35 | + highlight.selectors.add('div.bold'); |
| 36 | + highlight.selectors.add('span.bold'); |
| 37 | + |
| 38 | + // Remove selector |
| 39 | + highlight.selectors.delete('div.bold'); |
| 40 | + |
| 41 | + // All the other Set functions are supported (like: has, entries etc.) |
| 42 | + ``` |
| 43 | + |
| 44 | +- `child` : A `Proxy` for adding/removing sub-blocks. Sub-blocks act in a manner similar to that in SCSS. |
| 45 | + ```scss |
| 46 | + /*SCSS*/ |
| 47 | + #mydiv { |
| 48 | + color: blue; |
| 49 | + button { |
| 50 | + color: black; |
| 51 | + } |
| 52 | + } |
| 53 | + ``` |
| 54 | + When compiled to CSS becomes |
| 55 | + ```css |
| 56 | + /*CSS*/ |
| 57 | + #mydiv { |
| 58 | + color: blue; |
| 59 | + } |
| 60 | + #mydiv button { |
| 61 | + color: black; |
| 62 | + } |
| 63 | + ``` |
| 64 | + `child` is used to achieve the same result in JSCSS. |
| 65 | + |
| 66 | + Example |
| 67 | + ```javascript |
| 68 | + let mydiv = new JSCSS('#mydiv'); |
| 69 | + mydiv.style.color = 'blue'; |
| 70 | + mydiv.child.btn = new JSCSS('button'); |
| 71 | + mydiv.child.btn.style.color = 'black'; |
| 72 | + |
| 73 | + //A more easily readable way to do this will be |
| 74 | + let mydiv = new JSCSS('#mydiv'); |
| 75 | + mydiv.style.color = 'blue'; |
| 76 | + mydiv.child.btn = new JSCSS('button').addStyles({ |
| 77 | + color: 'black' |
| 78 | + }); |
| 79 | + |
| 80 | + // Or |
| 81 | + let mydiv = new JSCSS('#mydiv'); |
| 82 | + mydiv.style.color = 'blue'; |
| 83 | + |
| 84 | + { |
| 85 | + let btn = new JSCSS('button'); |
| 86 | + btn.style.color = 'black'; |
| 87 | + |
| 88 | + mydiv.child.btn = btn; |
| 89 | + } |
| 90 | + |
| 91 | + /* |
| 92 | + When rendered this code will produce: |
| 93 | + #mydiv { |
| 94 | + color: blue; |
| 95 | + } |
| 96 | + #mydiv button { |
| 97 | + color: black; |
| 98 | + } |
| 99 | + */ |
| 100 | + ``` |
| 101 | + |
| 102 | +- `styleElement` : A read-only reference to a `<style>` element containg the rendered CSS. |
| 103 | + |
| 104 | + Example |
| 105 | + ```javascript |
| 106 | + let mydiv = new JSCSS('#mydiv').addStyles({ |
| 107 | + color: 'white', |
| 108 | + background: '#212121' |
| 109 | + }); |
| 110 | + |
| 111 | + document.head.appedChild(mydiv.styleElement); |
| 112 | + ``` |
| 113 | + |
| 114 | +- `spaceFromParentSelector` : A boolean value. Default value is `true`. This determines if a space will be added between this block's selector and its parent block's selectors. |
| 115 | + |
| 116 | + Example |
| 117 | + ```javascript |
| 118 | + let buttons = new JSCSS('button', 'input[type="button"]').addStyles({ |
| 119 | + color: 'white', |
| 120 | + background: '#212121' |
| 121 | + }); |
| 122 | + |
| 123 | + buttons.child.hover = new JSCSS(':hover').addStyles({ |
| 124 | + background: 'blue' |
| 125 | + }); |
| 126 | + |
| 127 | + buttons.child.active = new JSCSS(':active').addStyles({ |
| 128 | + background: 'darkblue' |
| 129 | + }); |
| 130 | + |
| 131 | + /* |
| 132 | + The above code will render: |
| 133 | + |
| 134 | + button, |
| 135 | + input[type="button"] { |
| 136 | + color: white; |
| 137 | + background: #212121; |
| 138 | + } |
| 139 | + button :hover, |
| 140 | + input[type="button"] :hover { |
| 141 | + color: blue; |
| 142 | + } |
| 143 | + button :active, |
| 144 | + input[type="button"] :active { |
| 145 | + color: darkblue; |
| 146 | + } |
| 147 | + |
| 148 | + Which is not what we intened. |
| 149 | + */ |
| 150 | + |
| 151 | + // So we must disable the space between the child block selector (:hover, :active) and the parent selector (button, input[type="button"]) |
| 152 | + |
| 153 | + buttons.child.hover.spaceFromParentSelector = false; |
| 154 | + buttons.child.active.spaceFromParentSelector = false; |
| 155 | + |
| 156 | + /* |
| 157 | + Now the rendered code will be what we intended: |
| 158 | + |
| 159 | + button, |
| 160 | + input[type="button"] { |
| 161 | + color: white; |
| 162 | + background: #212121; |
| 163 | + } |
| 164 | + button:hover, |
| 165 | + input[type="button"]:hover { |
| 166 | + color: blue; |
| 167 | + } |
| 168 | + button:active, |
| 169 | + input[type="button"]:active { |
| 170 | + color: darkblue; |
| 171 | + } |
| 172 | + */ |
| 173 | + ``` |
| 174 | + |
| 175 | +### `class JSCSS` has 7 functions: |
| 176 | +- `toString()` : Get the CSS string. |
| 177 | + |
| 178 | + Example |
| 179 | + ```javascript |
| 180 | + let mydiv = new JSCSS('#mydiv').addStyles({ |
| 181 | + color: 'blue' |
| 182 | + }); |
| 183 | + |
| 184 | + console.log(mydiv.toString()); |
| 185 | + /* Logs: |
| 186 | + #mydiv { |
| 187 | + color: blue; |
| 188 | + } |
| 189 | + */ |
| 190 | + ``` |
| 191 | + |
| 192 | +- `addStyles(object [, ... object])` : Add multiple styles. |
| 193 | + |
| 194 | + Example |
| 195 | + ```javascript |
| 196 | + let mydiv = new JSCSS('#mydiv').addStyles({ |
| 197 | + color: 'white', |
| 198 | + background: '#212121' |
| 199 | + }, { border: 'none' }, { 'font-size': '1.2em'}); |
| 200 | + ``` |
| 201 | + |
| 202 | +- `reRender()` : Update the CSS in the block's `<style>` element node. |
| 203 | + |
| 204 | +- `deleteStyles()` : Removes all the styles from block. |
| 205 | + |
| 206 | +- `copyStylesFrom(jscssObj)` : Remove all current block styles and then copy styles from the passed block. |
| 207 | + |
| 208 | +- `mergeStylesFrom(jscssObj)` : Merge styles from passed block into current block. |
| 209 | + |
| 210 | +- `unlinkFromDOM()` : If block's `<style>` element has been added in the document then it is removed from the document. |
0 commit comments