This is a demonstration of how SassyJSON can be useful for Responsive Design.
Sometimes you need to trigger some JS actions, like initializing a lib, changing some plugin options, but only when the layout is in a specific configuration. For instance, changing the zoom of a map depending on how many columns it fills, or switching from a simple lightweight list of images to a full featured gallery or just loading content ...
There is some micro-libraries for this kind of thing, or you can do it yourself. One solution is to hard-code in your JS the same media-queries you used in your CSS. Another solution is to check if an element is visible or not, and bind this visibility with the same media-queries that drives the layout. The same hack can be implemented in many ways, with the
content
property for instance but none of those is clean :
That being said, this demo is neither efficient nor clean. It is just a proof-of-concept about how we can solve this kind of problematics with SassyJSON.
The idea here is to use Sass maps (the variable, not the geographical) for defining grids. Those maps can then be used by Javascript through SassyJSON. In this demo, the foo cell's content is changing when it enters or leaves the 6 columns configuration (resize your window).
The JS code is still relying on the size, but not the width in pixels. Even if columns is a better parameter than width, it still comes from the CSS. I think we should not choose between having the directives parameters on one side or another, whether it's CSS or JS. Another way could be defining the "responsive logic" in a dedicated JSON file, both Sass & JS
@media
and
matchMedia()
. This kind of separation of concerns could have been done before through dirty hacks (like parsing Sass variables file with JS), it's much cleaner and learnable to use JSON for this.
Unfortunately, and as far as I know, there's no way right now for SassyJSON to use an external JSON file, only decoding from a string is possible. A workaround could be to use grunt-contrib-concat to generate a Sass file from the JSON, and watch it...