69
by Max Shirshin Frontend Team Lead, Deltamethod Consultant, Yandex BEM it! BEM Methodology for small companies with high expectations

BEM it!

Embed Size (px)

DESCRIPTION

BEM Methodology for small companies with high expectations

Citation preview

Page 1: BEM it!

by Max ShirshinFrontend Team Lead, Deltamethod

Consultant, Yandex

BEM it!BEM Methodology for small companies

with high expectations

Page 2: BEM it!

Why bother?

Page 3: BEM it!

Web development needs:

● Methodologies, not frameworks● Same entities across different

technologies● Scalability

Page 4: BEM it!

BEM to the rescue

Page 5: BEM it!

What is BEM?

“BEM claims that three basic entities (Blocks, Elements, and Modifiers)are enough to define the way you author HTML / CSS / JavaScript, structure codeand components, set up interactionand scale your project to buildan industry-leading service.”

Page 6: BEM it!

What is BEM?● A methodology

“Theoretical underpinning” for methods and best practices

Page 7: BEM it!

What is BEM?● A methodology

“Theoretical underpinning” for methods and best practices

● Originally introduced by Yandex— www.yandex.ru (English: www.yandex.com)— 200+ Yandex services using full BEM stack (methodology + tools)— 19 million users daily audience

Page 8: BEM it!

What is BEM?● A methodology

“Theoretical underpinning” for methods and best practices

● Originally introduced by Yandex— www.yandex.ru (English: www.yandex.com)— 200+ Yandex services using full BEM stack (methodology + tools)— 19 million users daily audience

● Used by external projects— other services— tools / libraries

Page 9: BEM it!

Some theory :-)

Page 10: BEM it!

What is BEM?

BLOCK– Standalone part of an interface:

● button● text field● flyout● heading● menu

Page 11: BEM it!

What is BEM?

BLOCK– Standalone part of an interface:

● button● text field● flyout● heading● menu

– Re-usable in different contexts– Self-sufficient

Page 12: BEM it!

What is BEM?

ELEMENT– An integral part of a block:

● button● text field● flyout● heading● menu

Page 13: BEM it!

What is BEM?

ELEMENT– An integral part of a block:

● button icon● text field label● flyout title● heading logo● menu item

Page 14: BEM it!

What is BEM?

ELEMENT– An integral part of a block:

● button icon● text field label● flyout title● heading logo● menu item

– No standalone meaning outside of a block– Some blocks have no elements

Page 15: BEM it!

What is BEM?

MODIFIER– Defines property or state on a block or element:

● button● text field● flyout● heading● menu item

Page 16: BEM it!

What is BEM?

MODIFIER– Defines property or state on a block or element:

● button color● text field disabled state● flyout alignment● heading level● menu item bullet type

Page 17: BEM it!

What is BEM?

MODIFIER– Defines property or state on a block or element:

● button color● text field disabled state● flyout alignment● heading level● menu item bullet type

– Multiple modifiers may co-existon a single block/element

Page 18: BEM it!

BEM + DOM● Blocks are mapped to DOM

Page 19: BEM it!

BEM + DOM● Blocks are mapped to DOM● Blocks/elems/mods are denoted

with CSS classes

Page 20: BEM it!

BEM + DOM● Blocks are mapped to DOM● Blocks/elems/mods are denoted

with CSS classes● DOM nodes can be “shared”: you can mix

(block1 + block2) or (element1 + block2)on a same node

Page 21: BEM it!

BEM markup forms a semantic overlay over the existing DOM structure.

This overlay is called a BEM tree.

Page 22: BEM it!

BEM HOWTOfor your beloved projectwith benefits explained

Page 23: BEM it!

HOWTO: HTML / CSS

Page 24: BEM it!

CSS naming conventions

“BEM uses CSS class names to denote blocks, elements and modifiers.”

Page 25: BEM it!

CSS naming conventions

BLOCK

.b-button

.b-text-field

.b-flyout

.b-heading

.b-menu

Page 26: BEM it!

CSS naming conventions

<ul class=”b-menu”>

<li> <a href=”/more”>Read More</a> </li>

<li> <a href=”/buy”>Buy Online</a> </li>

<li> <a href=”/buy”>Contact</a> </li>

</ul>

Page 27: BEM it!

CSS naming conventions

ELEMENT

.b-button__icon

.b-text-field__label

.b-flyout__title

.b-heading__logo

.b-menu__item

Page 28: BEM it!

CSS naming conventions

<ul class=”b-menu”>

<li class=”b-menu__item”> <a href=”/more”>Read More</a> </li>

<li class=”b-menu__item”> <a href=”/buy”>Buy Online</a> </li>

<li class=”b-menu__item”> <a href=”/buy”>Contact</a> </li>

</ul>

Page 29: BEM it!

CSS naming conventions

MODIFIER

.b-button_color_blue

.b-text-field_disabled

.b-flyout_align_top

.b-heading_level_alpha

.b-menu__item_pos_first

Page 30: BEM it!

CSS naming conventions

<ul class=”b-menu b-menu_horizontal”>

<li class=”b-menu__item”> <a href=”/more”>Read More</a> </li>

<li class=”b-menu__item”> <a href=”/buy”>Buy Online</a> </li>

<li class=”b-menu__item”> <a href=”/buy”>Contact</a> </li>

</ul>

Page 31: BEM it!

CSS naming conventions

<ul class=”b-menu b-menu_horizontal”>

<li class=”b-menu__item b-menu__item_pos_first”> <a href=”/more”>Read More</a> </li>

<li class=”b-menu__item”> <a href=”/buy”>Buy Online</a> </li>

<li class=”b-menu__item”> <a href=”/buy”>Contact</a> </li>

</ul>

Page 32: BEM it!

Additional notes on CSS

● Page (Document) is also a block

Page 33: BEM it!

Additional notes on CSS

● Page (Document) is also a block● Layouts are blocks (same rules apply)

Page 34: BEM it!

Additional notes on CSS

● Page (Document) is also a block● Layouts are blocks (same rules apply)● No CSS outside of blocks

Page 35: BEM it!

Additional notes on CSS

● Page (Document) is also a block● Layouts are blocks (same rules apply)● No CSS outside of blocks● No common CSS resets (block independency!)

Page 36: BEM it!

Additional notes on CSS

● Page (Document) is also a block● Layouts are blocks (same rules apply)● No CSS outside of blocks● No common CSS resets (block independency!)● No “shared” styles (no need)

Page 37: BEM it!

Additional notes on CSS

● Page (Document) is also a block● Layouts are blocks (same rules apply)● No CSS outside of blocks● No common CSS resets (block independency!)● No “shared” styles (no need)● DOM tree → BEM tree

Page 38: BEM it!

Benefits!

Drop tag names and IDs

Page 39: BEM it!

Benefits!

Drop tag names and IDs● Faster selectors● Re-use same semantic block on:

— <DIV class=”b-block”>

— <SPAN class=”b-block”>

— <TABLE class=”b-block”>

Page 40: BEM it!

Benefits!

CSS specificity magic solved

Page 41: BEM it!

Benefits!

CSS specificity magic solvedPriority of CSS rules:by specificity first, then by rule order

Page 42: BEM it!

Benefits!

CSS specificity magic solvedPriority of CSS rules:by specificity first, then by rule ordertd.odd { background-color: gray }

td.even { background-color: white }

.highlighted { background-color: yellow }

<TD class="odd highlighted">

<!-- Still gray, baby :-( -->

</TD>

Page 43: BEM it!

Benefits!

CSS specificity magic solvedPriority of CSS rules:by specificity first, then by rule ordertd.odd { background-color: gray }

td.even { background-color: white }

td.highlighted { background-color: yellow }

<TD class="odd highlighted">

<!-- This works, I'm yellow now -->

</TD>

Page 44: BEM it!

Benefits!

Bye-bye CSS cascade?!

Page 45: BEM it!

Benefits!

Bye-bye CSS cascade?!

Only one CSS class needed to:● style a block container● style any element within a block● add extras/overrides with a modifier

Doesn't it cover 90% of your styling needs?

Page 46: BEM it!

Benefits!

Bye-bye CSS cascade?!...well, not exactly.

Example of an element affected by a block modifier:

/* hide labels for disabled text fields */.b-text-input_disabled .b-text-input__label{ display: none;}

Page 47: BEM it!

HOWTO: JavaScript

Page 48: BEM it!

JavaScript

Components → BlocksWork with BEM tree, not DOM tree

Page 49: BEM it!

JavaScript

jQuery BEM plugin

http://xslc.org/jquery-bem/● Extends jQuery Sizzle with selectors for BEM

entities (mix them with “normal” selectors)● Add callbacks on modifiers set/change● Supports methods tied to blocks/elements

Page 50: BEM it!

JavaScript

i-bem.js framework by Yandex + tutorial

https://github.com/toivonen/bem-js-tutorial

● First English draft docs (expect more!)● 100% BEM-based declarative API● Part of a larger bem-core library

Page 51: BEM it!

JavaScriptTwitter Flight (used by Deltamethod)http://flightjs.github.io

● Self-contained components, 100% event-driven● Based on jQuery; AMD-friendly; BEM-friendly● Built-in syntax sugar for predefined selectors

(good for BEM tree)● [Shameless self-promo] Try Flight with Reggirt:

http://github.com/ingdir/reggirtjQuery plugin that emulates event capturing(opposite of event bubbling)

Page 52: BEM it!

JavaScriptTwitter Flight (used by Deltamethod)http://flightjs.github.io

● Self-contained components, 100% event-driven● Based on jQuery; AMD-friendly; BEM-friendly● Built-in syntax sugar for predefined selectors

(good for BEM tree)● [Shameless self-promo] Try Flight with Reggirt:

http://github.com/ingdir/reggirtjQuery plugin that emulates event capturing(opposite of event bubbling)

Page 53: BEM it!

Benefits!

HTML is no longer semantic.

JavaScript is.

Page 54: BEM it!

HOWTO: Design / UX

Page 55: BEM it!

BEM is the universal languagefor developers and designers,the bridge across technology gaps.

Page 56: BEM it!

UX + Frontend

● Design a style guide

Page 57: BEM it!

UX + Frontend

● Design a style guide● Keep it up-to-date

Page 58: BEM it!

UX + Frontend

● Design a style guide● Keep it up-to-date, always!

Page 59: BEM it!

UX + Frontend

● Design a style guide● Keep it up-to-date, always!● Build new screens quickly

Page 60: BEM it!

UX + Frontend

Build your block library.

The code itself is the styleguide.

Page 61: BEM it!

Benefits!

● Prototyping mapped to code from day 1

Page 62: BEM it!

Benefits!

● Prototyping mapped to code from day 1● Code mapped to prototypes from day 1

Page 63: BEM it!

HOWTO: File structure

Page 64: BEM it!

File and folder structure

Flat block structure with a folder for each block.

Simple structure for BEM beginners:

/b-block block.css block.js block.tpl ...whatever you need

Page 65: BEM it!

File and folder structure

Advanced structure to expose semantics

/block /css block.css block__elem1.css block__elem2.css block_mod.css /js block.js /template block.tpl block__elem1.tpl block__elem2.tpl

Page 66: BEM it!

File and folder structure

Advanced structure to expose semantics

/block /css block.css block__elem1.css block__elem2.css block_mod.css /js block.js /template block.tpl block__elem1.tpl block__elem2.tpl

Page 67: BEM it!

Benefits!● Unified structure for automation● Redefinition levels: different libraries, same

structure

/common /b-block /css block.css /template block.tpl

/app1 /b-block /css block__elem1.css /js block.js /template block__elem1.tpl

+

Page 68: BEM it!

Build process and deployment

Use a build tool!

Borschik:an open-source build tool by Yandex

Code:https://github.com/bem/borschik

English docs:http://bem.info/articles/borschik

Page 69: BEM it!

[email protected] http://gplus.to/ingdir

@ingdir maxshirshin

Thank you!