├── LICENSE ├── README.md ├── js-in-css.js ├── mixin.js └── tests ├── container-queries.html ├── ie6-jsincss.html ├── ie7-jsincss.html └── ie8-jsincss.html /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Tommy Hodgins 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # JS-in-CSS Patterns 2 | 3 | Patterns for building simple Event-Driven Virtual Stylesheets. 100% CSS, plus the expressive power of JavaScript! 4 | 5 | 6 | ## In this repository 7 | 8 | There is a simple JS-in-CSS pattern that can be used for writing individual event-driven virtual stylesheets. You can write CSS code, as well as include JS template string interpolation to compose stylesheets, and trigger recalculations of those stylesheets by listening to JavaScript events. 9 | 10 | - [Simple JS-in-CSS Template](js-in-css.js) 11 | 12 | In addition to writing stylesheets, since you can interpolate JavaScript at any point inside the virtual stylesheet you are able to easily extend your expressive ability by writing JavaScript functions that return CSS code as strings (sometimes in addition to doing other things). I call this writing 'mixins', and there is a very basic mixin pattern included here as well that forms the base of many useful mixins listed below. 13 | 14 | - [Simple Mixin Template](mixin.js) 15 | 16 | Last, to demonstrate some ways in which the JS-in-CSS patterns for writing event-driven virtual stylesheets can be used together with mixins to extend CSS's styling ability I've included a modern container queries demo page that shows off a wide variety of tests that are useful for styling: 17 | 18 | - [Container Queries Demo using JS-in-CSS](tests/container-queries.html) 19 | 20 | And to prove how simple and flexible these patterns are, I've included examples of JS-in-CSS patterns written for legacy versions of IE, demonstrating that this way of building sites has been possible for a long time already and doesn't rely on new features such as [Resize Observer](https://wicg.github.io/ResizeObserver) or [Houdini](http://ishoudinireadyyet.com) to achieve (though they will offer some amazing improvements some day!) 21 | 22 | - [Container Queries with JS-in-CSS in IE8](tests/ie8-jsincss.html) 23 | - [Container Queries with JS-in-CSS in IE7](tests/ie7-jsincss.html) 24 | - [Container Queries with JS-in-CSS in IE6](tests/ie6-jsincss.html) 25 | 26 | 27 | ## Compatible Mixins 28 | 29 | - [Cascading JS Variables](https://github.com/tomhodgins/cascading-js-variables) 30 | - [Container Queries Mixin](https://gist.github.com/tomhodgins/fc42b334beaafc75a271b1ef7c8e33ee) (stylesheet version with `$this`, functions as tests) 31 | - [reproCSS/ancestor-selector](https://github.com/tomhodgins/reprocss#ancestor-selector-mixin) 32 | - [reproCSS/aspect-ratio](https://github.com/tomhodgins/reprocss#aspect-ratio-mixin) 33 | - [reproCSS/auto-expand](https://github.com/tomhodgins/reprocss#auto-expand-mixin) 34 | - [reproCSS/closest-selector](https://github.com/tomhodgins/reprocss#closest-selector-mixin) 35 | - [reproCSS/container-queries](https://github.com/tomhodgins/reprocss#container-queries-mixin) (rule version, strings as tests) 36 | - [reproCSS/elder-selector](https://github.com/tomhodgins/reprocss#elder-selector-mixin) 37 | - [reproCSS/parent-selector](https://github.com/tomhodgins/reprocss#parent-selector-mixin) 38 | - [reproCSS/prev-selector](https://github.com/tomhodgins/reprocss#prev-selector-mixin) 39 | - [reproCSS/scoped-eval](https://github.com/tomhodgins/reprocss#scoped-eval-mixin) 40 | - [reproCSS/xpath-selector](https://github.com/tomhodgins/reprocss#xpath-selector-mixin) 41 | - [respec](https://github.com/tomhodgins/respec) 42 | 43 | 44 | ## Similar JS-in-CSS Patterns 45 | 46 | ### [PrinCSS](https://gist.github.com/tomhodgins/1b7ac98825ed31afaae972a1b24bc03b) 47 | 48 | Simple JSinCSS pattern with add, remove, and debounce functions 49 | 50 | ### [reproCSS](https://github.com/tomhodgins/reprocss) 51 | 52 | A JavaScript plugin that adds a `process=""` attribute to ` 128 | 129 |

Container Queries Demo

130 | 131 |

Width Queries

132 | 133 |

min-width

134 |
class="minwidth"
135 | 136 |

max-width

137 |
class="maxwidth"
138 | 139 |

Height Queries

140 | 141 |

min-height

142 |
class="minheight"
143 | 144 |

max-height

145 |
class="maxheight"
146 | 147 |

Quantity Queries

148 | 149 |

min-characters on block elements

150 |

(Use keyboard)

151 |
class="mincharacters"
152 | 153 |

min-characters on form inputs

154 |

(Use keyboard)

155 | 156 | 157 | 158 |

max-characters on block elements

159 |

(Use keyboard)

160 |
class="maxcharacters"
161 | 162 |

max-characters on form inputs

163 |

(Use keyboard)

164 | 165 | 166 | 167 | 168 |

min-children

169 | 170 |
171 | 172 |
173 | 174 |

max-children

175 | 176 |
177 | 178 |
179 | 180 |

Scroll queries

181 | 182 |

min-scroll-y

183 |
184 |

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

185 |

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

186 |
187 | 188 |

max-scroll-y

189 |
190 |

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

191 |

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

192 |
193 | 194 |

min-scroll-x

195 |
196 |

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

197 |
198 | 199 |

max-scroll-x

200 |
201 |

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

202 |
203 | 204 |

Aspect queries

205 | 206 |

orientation

207 |
100 × 100
208 |
100 × 200
209 |
200 × 100
210 | 211 |

min-aspect-ratio

212 |
class="minaspectratio"
213 | 214 |

max-aspect-ratio

215 |
class="maxaspectratio"
216 | 217 | 440 | 441 | 524 | -------------------------------------------------------------------------------- /tests/ie6-jsincss.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | JSinCSS in IE6 7 | 8 | 9 | 74 | 75 | 76 | 163 | 164 | 165 | 166 |

Type more than 5 characters into the INPUT below:

167 | 168 | 169 | 170 |

DIVs below blue if wider than 500px

171 |
100% wide
172 |
66.6% wide
173 |
33.33% wide
174 | 175 | 176 | -------------------------------------------------------------------------------- /tests/ie7-jsincss.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | JSinCSS in IE7 7 | 8 | 9 | 74 | 75 | 76 | 163 | 164 | 165 | 166 |

Type more than 5 characters into the INPUT below:

167 | 168 | 169 | 170 |

DIVs below blue if wider than 500px

171 |
100% wide
172 |
66.6% wide
173 |
33.33% wide
174 | 175 | 176 | -------------------------------------------------------------------------------- /tests/ie8-jsincss.html: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | JSinCSS in IE8 7 | 8 | 9 | 74 | 75 | 76 | 151 | 152 | 153 | 154 |

Type more than 5 characters into the INPUT below:

155 | 156 | 157 | 158 |

DIVs below blue if wider than 500px

159 |
100% wide
160 |
66.6% wide
161 |
33.33% wide
162 | 163 | 164 | --------------------------------------------------------------------------------