15
DECOUPLING Tomislav Mesić @tomislavmesic FIVE HTML-CSS-JS

Decoupling Your html, css, and java script

Embed Size (px)

DESCRIPTION

Any non-trivial site or application on the Web today will contain a large amount of HTML, CSS, and JavaScript. As the use of the Internet evolves and our dependence on it increases, having a plan for organizing and maintaining your front-end code is an absolute must.

Citation preview

Page 1: Decoupling Your html, css, and java script

DECOUPLING Tomislav Mesić@tomislavmesicFIVEHTML-CSS-JS

Page 2: Decoupling Your html, css, and java script

// HTML, CSS, JavaScript coupling$("#sidebar a").on("click", function(){ $(this) .parent("li") .addClass("menu-item-highlight"); });

Page 3: Decoupling Your html, css, and java script

DECOUPLING CSS Zen Garden The Beauty of CSS Design

the garden

Page 4: Decoupling Your html, css, and java script

/* CSS extremely coupled to the HTML */#sidebar section:first-child h3 + p { }

Page 5: Decoupling Your html, css, and java script

/* CSS knows too much about your HTML */#sidebar ul > li > ul { }#/* Good */.submenu { }

Page 6: Decoupling Your html, css, and java script

DECOUPLINGOOCSS#SMACSSScalable and Modular Architecture for CSSBase, Layout, Module, State, Theme#Preprocessors

HTML-CSS-JS

Page 7: Decoupling Your html, css, and java script

<!-- OOCSS madness --><article class="article block block-md

blue centered text-center text-white">…</article>

Page 8: Decoupling Your html, css, and java script

<!-- SMACSS principles --><div class="pop-up is-visible">…</div>#/* state .is-* classes */.pop-up.is-visible {…}

Page 9: Decoupling Your html, css, and java script

<button id="add-to-cart" class="add-item"> Add to cart </button>#// JavaScript That Knows Too Much About Structure$("#add-to-cart").addToCart({…});

Page 10: Decoupling Your html, css, and java script

<button id="add-to-cart" class="add-item"> Add to cart </button>#// Classes With More Than One Responsibility$(".add-item").addToCart({…});

Page 11: Decoupling Your html, css, and java script

<button class="js-add-to-cart add-item"> Add to cart </button>#// Behaviour classes js-*$(".js-add-to-cart").addToCart({…});

Page 12: Decoupling Your html, css, and java script

<button class="add-item" data-action="add-to-cart"> Add to cart </button>#// It’s better without ghost classes$("[data-action]").action({…});#$.fn.action = function() { switch($(this).data().action) { case "add-to-cart": … } }

Page 13: Decoupling Your html, css, and java script

DECOUPLING

Favor explicit component over complex CSS selectors in CSS#

Style components based on what they are, not where they are#

Favor data attributes or behavior classes in Javascript#

Don’t use the same class for both style and behavior#

Differentiate state styles from default styles#

Chain state class to the component class

RECAP

Page 14: Decoupling Your html, css, and java script

INSPIRED BYPhilip Walton @philwalton http://bit.ly/1pHHvty#

Jonathan Snook@snookca http://smacss.comhttp://bit.ly/1oCYqOz

RESOURCES

Page 15: Decoupling Your html, css, and java script

PARTEEY