Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Developer, improve your DOM structure with BEM

Developer, improve your DOM structure with BEM

Johnathan MEUNIER

May 23, 2018
Tweet

More Decks by Johnathan MEUNIER

Other Decks in Programming

Transcript

  1. Developper, improve your DOM structure with BEM Johnathan MEUNIER •

    @JohnathanSUP Samuel GOMEZ • samuelgomez.fr
  2. Johnathan MEUNIER • @JohnathanSUP Samuel GOMEZ • samuelgomez.fr -Ohans Emmanuel,

    CSS Naming Conventions that Will Save You Hours of Debugging « I have heard lots of developers say they hate CSS. In my experience, this comes as a result of not taking the time to learn CSS. »
  3. Johnathan MEUNIER • @JohnathanSUP Samuel GOMEZ • samuelgomez.fr CSS Examples

    .vb-homepage-item-medium .ctas .cta + .cta, .vb- homepage-item-large .ctas .cta + .cta {}
  4. Johnathan MEUNIER • @JohnathanSUP Samuel GOMEZ • samuelgomez.fr Problems •

    Unreadable selector • Complicated precedence • Non isolated style • DRY ??? What’s bad here ?
  5. Johnathan MEUNIER • @JohnathanSUP Samuel GOMEZ • samuelgomez.fr -Audrey Hacq

    « Le travail du designer d’interface est de définir la bonne typographie, les bonnes couleurs, les bons rapports de hiérarchies, les bons composants graphiques et de faire marcher ces éléments ensemble, de leur donner du sens. »
  6. Johnathan MEUNIER • @JohnathanSUP Samuel GOMEZ • samuelgomez.fr Block Element

    Modifier .org-block__element—modifier .af-header__logo—active Block is similar to a component. It’s a main part of the UI.
  7. Johnathan MEUNIER • @JohnathanSUP Samuel GOMEZ • samuelgomez.fr Block Element

    Modifier .org-block__element—modifier .af-header__logo—active Element is a part of a block. Its give the possibility to split a block in several parts
  8. Johnathan MEUNIER • @JohnathanSUP Samuel GOMEZ • samuelgomez.fr Block Element

    Modifier .org-block__element- -modifier .af-header__logo- -active A modifier allows to change the state of a tag
  9. Johnathan MEUNIER • @JohnathanSUP Samuel GOMEZ • samuelgomez.fr Block Element

    Modifier .org-block__element- -modifier .af-header__logo- -active The organization part avoids conflicts with other CSS Framework
  10. Johnathan MEUNIER • @JohnathanSUP Samuel GOMEZ • samuelgomez.fr Block Element

    Modifier .js-block__element- -modifier .js-header__logo- -active Use specific class to JS selector
  11. Johnathan MEUNIER • @JohnathanSUP Samuel GOMEZ • samuelgomez.fr English naming

    Promote English naming Bad ☹ Good .af-utilisateur .af-user .af-metier .af-business .af-sous-activite .af-subactivity
  12. Johnathan MEUNIER • @JohnathanSUP Samuel GOMEZ • samuelgomez.fr Bad ☹

    Good .af-headerTop .af-header-top .af-tmpMetier .af-tmp-metier .af-subActivity .af-sub-activity Kebab Case If the name of your component is too long
  13. Johnathan MEUNIER • @JohnathanSUP Samuel GOMEZ • samuelgomez.fr Advantages •

    Pre technical analysis, you will prepare a good structure and be able to discover potential problems of conception • Avoid complicated precedence • Isolated style • Simplify maintaining CSS • Strict format … Why using BEM ?
  14. Johnathan MEUNIER • @JohnathanSUP Samuel GOMEZ • samuelgomez.fr Cons •

    … Strict format • Difficulties to find a good name (specially for business terms) Be caution
  15. Johnathan MEUNIER • @JohnathanSUP Samuel GOMEZ • samuelgomez.fr -Phil Karlton

    « There are only two hard problems in Computer Science: cache invalidation and naming things »
  16. Johnathan MEUNIER • @JohnathanSUP Samuel GOMEZ • samuelgomez.fr Usage <Header>

    <Logo></Logo> <Nav></Nav> <User></User> </Header> Main
  17. Johnathan MEUNIER • @JohnathanSUP Samuel GOMEZ • samuelgomez.fr AngularJS Directive

    are visible in DOM. We have to think about this point when we split our components and we create classes. Classes will be written directly on the directive
  18. Johnathan MEUNIER • @JohnathanSUP Samuel GOMEZ • samuelgomez.fr AngularJS <Header

    class="af-header"> <Logo class="af-logo"></Logo> <Nav class="af-nav"></Nav> <User class="af-user"></User> </Header> Main
  19. Johnathan MEUNIER • @JohnathanSUP Samuel GOMEZ • samuelgomez.fr AngularJS <h4

    class="af-user__name">Joh</h4> <a href="/logout" class="af-user__logout"> Logout </a> User
  20. Johnathan MEUNIER • @JohnathanSUP Samuel GOMEZ • samuelgomez.fr AngularJS <h4

    class="af-user__name">Joh</h4> <a href="/logout" class="af-user__logout af-user__logout{{ disabled}}"> Logout </a> User
  21. Johnathan MEUNIER • @JohnathanSUP Samuel GOMEZ • samuelgomez.fr React Tags

    aren't directly visible in DOM. We will write our class on the first tag of each components.
  22. Johnathan MEUNIER • @JohnathanSUP Samuel GOMEZ • samuelgomez.fr React <Header>

    <Logo></Logo> <Nav></Nav> <User></User> </Header> Main
  23. Johnathan MEUNIER • @JohnathanSUP Samuel GOMEZ • samuelgomez.fr React <div

    className="af-logo"> <img className="af-logo__img"/> </div> Logo
  24. Johnathan MEUNIER • @JohnathanSUP Samuel GOMEZ • samuelgomez.fr React <div

    className="af-user"> <h4 className="af-user__name">Joh</h4> <a href="/logout" className="af-user__logout"> Logout </a> </div> User
  25. Johnathan MEUNIER • @JohnathanSUP Samuel GOMEZ • samuelgomez.fr React <div

    className="af-user"> <h4 className="af-user__name">Joh</h4> <a href="/logout" className="af-user__logout--disable"> Logout </a> </div> User
  26. Johnathan MEUNIER • @JohnathanSUP Samuel GOMEZ • samuelgomez.fr Useful and

    inspirational links L’atomic design, une méthode de co-création prometteuse Key concepts / Methodology / BEM Petite définition de BEM Fifty Shades of BEM - Ministry of Interface sturobson/BEM-resources: Just a repo full of BEM CSS Naming Conventions that Will Save You Hours
  27. Thank you ! Developper, improve your DOM structure with BEM

    Johnathan MEUNIER • @JohnathanSUP Samuel GOMEZ • samuelgomez.fr