290 likes | 440 Views
Forward Thinking. Web Pre-Processors. The Future of CSS and JavaScript Today Daniel Laughland. . scss. .sass. .min.css. .less. .min.js. . ts. .coffee. Why Use Pre-Processors?. Repetition Utility Classes Browser Prefixes Monolithic Files f unction s as Classes var self = this;
E N D
Forward Thinking Web Pre-Processors The Future of CSS and JavaScript Today Daniel Laughland
.scss .sass .min.css .less .min.js .ts .coffee fast. forward. thinking.
Why Use Pre-Processors? • Repetition • Utility Classes • Browser Prefixes • Monolithic Files • functions as Classes • var self = this; • "0" == true • Awkward Namespacing fast. forward. thinking.
Concise, Consistent, and Reusable CSS Pre-Processors fast. forward. thinking.
Nested Rules • #header h1 {font-size:200%;} • #header a {text-decoration:none;} • #headernav{float:right;} • @media screen and (max-width:480px) { • #headernav{float:none;} • } fast. forward. thinking.
Nested Rules • #header{ • h1 {font-size:200%;} • a {text-decoration:none;} • nav{float:right;} • @media screen and (max-width:480px) { • nav{float:none;} • } • } fast. forward. thinking.
Variables • h1, h2 {color:#D9A164;} • .callout{ • background-color:#D9A164; • } • li:before{ • content:"• "; • color:#D9A164; • } fast. forward. thinking.
Variables • $orange-peanut:#D9A164; • h1, h2 {color:$orange-peanut; } • .callout{ • background-color:$orange-peanut; • } • li:before{ • content:"• "; • color:$orange-peanut; • } SASS: $variable LESS: @variable fast. forward. thinking.
Mixins • <div id="related-links" class="box right">…</div> • .right{ • float:right; • margin:1em; • } • .box{ • border:1pxsolidblack; • padding:1em; • } fast. forward. thinking.
Mixins • <div id="related-links">…</div> • .right{ • float:right; • margin:1em; • } • .box{ • border:1pxsolidblack; • padding:1em; • } #related-links { .right; .box; } [LESS] fast. forward. thinking.
Mixins • <div id="related-links">…</div> • @mixin right{ • float:right; • margin:1em; • } • @mixinbox{ • border:1pxsolidblack; • padding:1em; • } #related-links { @include right; @include box; } [SASS] fast. forward. thinking.
Functions • @mixinboxify($box-color,$width) { • width:$width – 2px; • border:2px solid $box-color; • background-color:lighten($box-color,50%); • margin:$width / 15; • } • #callout {@includeboxify(#B1946C,150px); } fast. forward. thinking.
Functions • Color Manipulation • saturate() green() fade-out() hsl() complement() • Math and Units • round() max() (1in + 12px) fast. forward. thinking.
Ported to many platforms • Simple setup & integration • Limited capability • Bootstrap • Kendo UI • Built on Ruby • Many scripting options • Powerful add-on libraries • ZURB Foundation • Sencha fast. forward. thinking.
Unified Syntax for Prefixed Features@include background(linear-gradient(top left,#333, #030)); • Automatic Image Sprites@import "my-icons/*.png";@include all-my-icons-sprites; • Detect Image Height and Width width:image-width('banner.jpg'); • In-Line Base64 Encodingbackground:inline-image('pattern.png'); • Built-In CSS Reset@include "compass/reset"; fast. forward. thinking.
SASS+Compass Demo fast. forward. thinking.
Tools for LESS/SASS • Visual Studio 2012 Web Tools Update 2 • Web Essentials 2012 • Mindscape Web Workbench fast. forward. thinking.
Easier Structure and Code Safety JavaScript Pre-Processors fast. forward. thinking.
window.myApp.OrangePeanut=(function(ko){ • functionOrangePeanut(){ • this.forMe=ko.observable(true); • } • OrangePeanut.prototype.givePeanut=function(){ • if(this.forMe() === true){ • console.log("I accept you!"); • } • } • returnOrangePeanut; • })(ko); • ko.applyBindings(window.myApp.OrangePeanut);
window.myApp.OrangePeanut=function(){ • varthisPeanut=this; • this.forMe=ko.observable(true); • this.givePeanut=function(){ • if(thisPeanut.forMe() === true){ • console.log("I accept you!"); • } • }; • }; • ko.applyBindings(newOrangePeanut()); fast. forward. thinking.
classOrangePeanut { • public Observable<bool>forMe; • publicvoidgivePeanut() { • if (forMe) console.log ("I accept you!"); • } • } • ko.applyBindings(newOrangePeanut()); fast. forward. thinking.
TypeScript • Microsoft Open-Source • Superset of ECMAScript 5 • Well-Integrated with VS • “It’s just JavaScript” • Inspired by Ruby syntax • Best with Node.JS fast. forward. thinking.
classOrangePeanut • constructor: (@forMe)-> • givePeanut: -> • console.log"I accept you!"if@forMe • myPeanut = new OrangePeanut true fast. forward. thinking.
classOrangePeanut • constructor: (data)-> • @forMe = ko.observabledata.forMe • givePeanut: -> • console.log"I accept you!"if@forMe() • myPeanut = new OrangePeanut true fast. forward. thinking.
classOrangePeanut{ • forMe:KnockoutObservableBool; • constructor(mine:boolean){ • this.forMe=ko.observable(mine); • } • givePeanut(){ • if(this.forMe())console.log("I accept you!"); • } • } • varmyPeanut=newMyApp.OrangePeanut(true); fast. forward. thinking.
TypeScript Demo fast. forward. thinking.
Thinking Forward • Integration with Bundler/WebGrease • Real Source Map Support in SASS • New Language Features (like Generics) fast. forward. thinking.
Daniel Laughland daniel.laughland@centare.com Twitter @jabberwik Thanks! fast. forward. thinking.