+ All Categories
Home > Documents > BEM. What you can borrow from Yandex frontend dev

BEM. What you can borrow from Yandex frontend dev

Date post: 18-Dec-2014
Category:
Upload: varvara-stepanova
View: 2,200 times
Download: 3 times
Share this document with a friend
Description:
Varvara Stepanova presents BEM methodology and tools. BEM stans for "Block Element Modifier". First, it is a methodology, a way of thinking when developing web interface applicable for any technology. Besides, BEM is a range of tools to automate developer's work and to optimase code for production. Finnally, BEM is a bunch of interface libraries helping developers to make their work faster and better. Informal video: https://vimeo.com/53219242
133
Transcript
Page 1: BEM. What you can borrow from Yandex frontend dev
Page 2: BEM. What you can borrow from Yandex frontend dev

Frontend developer

BEMWhat you can borrow from Yandex frontend dev

Varvara Stepanova

Page 3: BEM. What you can borrow from Yandex frontend dev
Page 4: BEM. What you can borrow from Yandex frontend dev
Page 5: BEM. What you can borrow from Yandex frontend dev

Over 80 services

• search– web pages– images, video– goods, news, blog posts, people

• web mail• web maps

– traffic jams, wiki maps, taxi• hosting and sharing

– files, photos, video

5

Page 6: BEM. What you can borrow from Yandex frontend dev

Inside Yandex

• 2500 developers• 150+ frontend developers

6

Page 7: BEM. What you can borrow from Yandex frontend dev

Dark side of the Force

Page 8: BEM. What you can borrow from Yandex frontend dev

Programming is a team work

Page 9: BEM. What you can borrow from Yandex frontend dev

Developers community

Page 10: BEM. What you can borrow from Yandex frontend dev

We do enjoy sharing

Page 11: BEM. What you can borrow from Yandex frontend dev

Web Interface

Page 12: BEM. What you can borrow from Yandex frontend dev
Page 13: BEM. What you can borrow from Yandex frontend dev

Behind the page

13

CSS

body { font: 0.8em Arial, sans-serif; margin: 0; color: black;}.sidebar ul li {

HTML

<!DOCTYPE html><html> <head>...</head> <body> ... </body>

Page 14: BEM. What you can borrow from Yandex frontend dev
Page 15: BEM. What you can borrow from Yandex frontend dev
Page 16: BEM. What you can borrow from Yandex frontend dev
Page 17: BEM. What you can borrow from Yandex frontend dev

Block

Page 18: BEM. What you can borrow from Yandex frontend dev
Page 19: BEM. What you can borrow from Yandex frontend dev
Page 20: BEM. What you can borrow from Yandex frontend dev

Menu block

20

HTML

<ul class="menu"> <li><a href="/new">New titiles</a></li> <li><a href="/soon">Coming soon</a></li> <li><a href="/best">Bestsellers</a></li> ...

CSS

.menu { list-style: none;

Page 21: BEM. What you can borrow from Yandex frontend dev

Search block

2120

HTML

<div class="search"> <input type="text" name="search" value="..."/> <input type="button" name="sbmt" value="Search"/></div>

CSS

.search { padding: 2em 0;

Page 22: BEM. What you can borrow from Yandex frontend dev

Tabbed pane block

22

HTML

<div class="tabbed-pane"> <ul> <li>Bestsellers</li><li>...</li> </ul> <div> The Casual Vacancy, J.K. Rowling </div></div>

CSS

.tabbed-pane { margin: 0.5em; ...}

Page 23: BEM. What you can borrow from Yandex frontend dev

INDEPENDENT

Page 24: BEM. What you can borrow from Yandex frontend dev

index.html

<!DOCTYPE html><html> <head>..</head> <body>

<div class="head"> <div class="logo">...</div> <ul class="menu">...</ul> <div class="search">...</div> </div>

<div class="layout">...</div>

<div class="foot">...</div>

</body></html>

The page of blocks

24

Page 25: BEM. What you can borrow from Yandex frontend dev

Repeating

Page 26: BEM. What you can borrow from Yandex frontend dev
Page 27: BEM. What you can borrow from Yandex frontend dev

Menu block

27

HTML<ul id="menu"> ...

HTML<ul class="menu"> ...

CSS#menu { ...

CSS.menu { ...

Page 28: BEM. What you can borrow from Yandex frontend dev

Block is independent, if

• a block has its "name"–no "id" but "classname" selectors

• avoids cascade• no "tag" selectors

28

Page 29: BEM. What you can borrow from Yandex frontend dev

Movement within a Page

Page 30: BEM. What you can borrow from Yandex frontend dev
Page 31: BEM. What you can borrow from Yandex frontend dev

Block is independent, if

• a block has its "name"–no "id" but "classname" selectors

• avoid cascade• no "tag" selectors

31

Page 32: BEM. What you can borrow from Yandex frontend dev

almost NO CASCADEwhen using Cascading Style Sheets

32

Page 33: BEM. What you can borrow from Yandex frontend dev
Page 34: BEM. What you can borrow from Yandex frontend dev
Page 35: BEM. What you can borrow from Yandex frontend dev
Page 36: BEM. What you can borrow from Yandex frontend dev

Moving within a Site

Page 37: BEM. What you can borrow from Yandex frontend dev
Page 38: BEM. What you can borrow from Yandex frontend dev
Page 39: BEM. What you can borrow from Yandex frontend dev
Page 40: BEM. What you can borrow from Yandex frontend dev

index page

Pages are sets of blocks

40

search page contacts page

Page 41: BEM. What you can borrow from Yandex frontend dev

CSS for different pages

41

common.css

body { ... }

.head { ... }

.head ul li { ...}

a:link, ... {}

index.css

.advert { ... }

.layout .books { ...}

contacts.css

body .phone { ...}

.phone a { ...}

#map {}

style.css

body { ... }.head { .. }.head ul li { ... }a:link, ... { ... }.advert { ... }.layout .books { ... }body .phone { ... }.phone a { ... }#map { ... }

Page 42: BEM. What you can borrow from Yandex frontend dev
Page 43: BEM. What you can borrow from Yandex frontend dev
Page 44: BEM. What you can borrow from Yandex frontend dev

index page

Pages are sets of blocks

44

search page contacts page

Page 45: BEM. What you can borrow from Yandex frontend dev

blocks/ header.css menu.css button.css tabbed-pane.css logo.css footer.css

CSS for every block

45

Page 46: BEM. What you can borrow from Yandex frontend dev

index.css

@import url(blocks/header.css);@import url(blocks/menu.css);@import url(blocks/tabbed-pane.css);@import url(blocks/text.css);@import url(blocks/logo.css);@import url(blocks/footer.css);...

CSS for a page

46

Page 47: BEM. What you can borrow from Yandex frontend dev

blocks/

advert.css books-list.css head.css foot.css logo.css menu.css search.css tabbed-pane.css

pages/

index.html index.css

contacts.html contacts.css

book.html book.css

Project file system

47

Page 48: BEM. What you can borrow from Yandex frontend dev

WE NEED TO GO

DEEPER

Page 49: BEM. What you can borrow from Yandex frontend dev

Inside a Block

Page 50: BEM. What you can borrow from Yandex frontend dev

Menu block

50

HTML

<ul class="menu"> <li><a href="/new">New titles</a></li> <li><a href="/soon">Coming soon</a></li> <li><a href="/best">Bestsellers</a></li> ...</ul>

Page 51: BEM. What you can borrow from Yandex frontend dev

Menu block

51

blocks/menu.css

.menu li{ list-style: none; display: inline-block; padding: 0 0.5em;}

Page 52: BEM. What you can borrow from Yandex frontend dev

Menu and Dropdown blocks

52

blocks/menu.css.menu li{ list-style: none; display: inline-block; padding: 0 0.5em;}

pages/index.html<li> <a href="#">Bestsellers</a> <div class="dropdown"> <ul> <li>Item One</li> <li>Item Two</li> <li>The best item</li> <li>Item Three</li>

Page 53: BEM. What you can borrow from Yandex frontend dev

Block is independent, if

• a block has its "name"–no "id" but "classname" selectors

• avoid cascade• no "tag" selectors

53

Page 54: BEM. What you can borrow from Yandex frontend dev

The style system matches rules by starting with the key selector, then moving to the left (looking for any ancestors in the rule’s selector).

David Hyatt, MDN

54

mzl.la/UuwZql

Page 55: BEM. What you can borrow from Yandex frontend dev

.menu li {

55

Page 56: BEM. What you can borrow from Yandex frontend dev

.menu li {

56

Page 57: BEM. What you can borrow from Yandex frontend dev

Menu block

57

Page 58: BEM. What you can borrow from Yandex frontend dev

Element

Page 59: BEM. What you can borrow from Yandex frontend dev

Elements

59

Page 60: BEM. What you can borrow from Yandex frontend dev

Search block

60

Page 61: BEM. What you can borrow from Yandex frontend dev

Tabbed-pane block

61

Page 62: BEM. What you can borrow from Yandex frontend dev

Menu

62

page.html

<ul class="menu"> <li class="menu__item"><a href="/">Index</a></li> <li class="menu__item"><a href="/new">New</a></li> <li class="menu__item"><a href="/offer">Special offer</a></li> <li class="menu__item"><a href="/shipping">Shipping</a></li></ul>

Page 63: BEM. What you can borrow from Yandex frontend dev

Menu

63

.tabbed-pane__tab

.block-element

.block--element

.block___element

blocks/menu.css

.menu li{ list-style: none; display: inline-block; padding: 0 0.5em;}

Page 64: BEM. What you can borrow from Yandex frontend dev

Optional elements

64

Page 65: BEM. What you can borrow from Yandex frontend dev

blocks/

search.css search__checkbox.css search__autocomlete.css

tabbed-pane.css tabbed-pane__tab.css tabbed-pane__pane.css

menu.css menu__item.css

book.css book__title.css book__image.css

Files for elements

65

Page 66: BEM. What you can borrow from Yandex frontend dev

Modifier

Page 67: BEM. What you can borrow from Yandex frontend dev

Similar but different

67

Page 68: BEM. What you can borrow from Yandex frontend dev

HTML

<div class="tabbed-pane tabbed-pane_theme_blue"> <ul> <li class="tabbed-pane__tab">Tab 1</li> <li class="tabbed-pane__tab">Tab 2</li> </ul> <div class="tabbed-pane__pane"> ... </div></div>

Modifier

68

Page 69: BEM. What you can borrow from Yandex frontend dev

HTML

<div class="tabbed-pane tabbed-pane_theme_blue tabbed-pane_direction_bottom"> ...</div>

<input class="button button_theme_black button_size_l" ... />

Modifier

69

Page 70: BEM. What you can borrow from Yandex frontend dev

tabbed-pane_theme_bluetabbed-pane_theme_white

tabbed-pane_size_stabbed-pane_size_l

button_size_sbutton_size_l

block-name_mod-name_mod-val

70

Page 71: BEM. What you can borrow from Yandex frontend dev

blocks/

tabbed-pane.css tabbed-pane__tab.css tabbed-pane__pane.css tabbed-pane_theme_blue.css tabbed-pane_theme_black.css tabbed-pane_direction_bottom.css

Block files structure

71

Page 72: BEM. What you can borrow from Yandex frontend dev

CSS

@import url(blocks/header.css);@import url(blocks/search.css);@import url(blocks/tabbed-pane.css);@import url(blocks/tabbed-pane__tab.css);@import url(blocks/tabbed-pane__pane.css);@import url(blocks/tabbed-pane_theme_blue.css);@import url(blocks/button.css);@import url(blocks/logo.css);@import url(blocks/footer.css);

Taken when necessary

72

Page 73: BEM. What you can borrow from Yandex frontend dev

Modifiers for elements

73

Page 74: BEM. What you can borrow from Yandex frontend dev

CSS

<div class="tabbed-pane"> <span class=" tabbed-pane__tab tabbed-pane__tab_state_current">...</span></div>

Taken when necessary

74

Page 75: BEM. What you can borrow from Yandex frontend dev

Block modifier DO affects elements

75

Page 76: BEM. What you can borrow from Yandex frontend dev

CSS

.tabbed-pane_theme_blue

.tabbed-pane__tab{ background-color: #9CF;}

Cascade is possible

76

Page 77: BEM. What you can borrow from Yandex frontend dev

Block File Structure

Page 78: BEM. What you can borrow from Yandex frontend dev

blocks/

tabbed-pane.css tabbed-pane__tab.css tabbed-pane__pane.css tabbed-pane_theme_blue.css tabbed-pane_theme_black.css tabbed-pane_size_l.css tabbed-pane_size_s.css

menu.css menu__item.css menu_size_l.css menu_size_s.css

logo.css search.css search__checkbox.css search__autocomplete.css

File structure. The flat case

78

Page 79: BEM. What you can borrow from Yandex frontend dev

blocks/

tabbed-pane/ tabbed-pane.css tabbed-pane__tab.css tabbed-pane__pane.css tabbed-pane_theme_blue.css tabbed-pane_theme_black.css tabbed-pane_size_l.css tabbed-pane_size_s.css

logo/ logo.css

menu/ menu.css menu__item.css menu_size_l.css menu_size_s.css

search/ search.css search__checkbox.css search__autocomplete.css

File structure. Block folder

79

Page 80: BEM. What you can borrow from Yandex frontend dev

blocks/

tabbed-pane/ __tab/ _state/ tabbed-pane__tab_state_current.css tabbed-pane__tab.css __pane/ tabbed-pane__pane.css _theme/ tabbed-pane_theme_blue.css tabbed-pane_theme_black.css tabbed-pane.css

File structure. Yandex way

80

Page 81: BEM. What you can borrow from Yandex frontend dev

BEM entities

• Block– combined in different ways– included one into another– independent

• Element• Modifier

81

Page 82: BEM. What you can borrow from Yandex frontend dev
Page 83: BEM. What you can borrow from Yandex frontend dev

BEM is

• Methodologyis a way of thinking when developing

• Toolsdo monkey job for you

• Block librariesmake you develop faster and better

83

Page 84: BEM. What you can borrow from Yandex frontend dev

IE

Page 85: BEM. What you can borrow from Yandex frontend dev

HTML

<html> <head> <!--[if gt IE 7]><!--> <link rel="stylesheet" href="index.css"> <!--<![endif]--> <!--[if lt IE 8]> <link rel=stylesheet href="index.ie.css"> <![endif]--> </head> ...

Render to IE the things that are IE's

85

Page 86: BEM. What you can borrow from Yandex frontend dev

HTML

<html> <head> <!--[if gt IE 7]><!--> <link rel="stylesheet" href="index.css"> <!--<![endif]--> <!--[if lt IE 8]> <link rel=stylesheet href="index.ie.css"> <![endif]--> </head> ...

Render to IE the things that are IE's

86

Page 87: BEM. What you can borrow from Yandex frontend dev

CSS

@import url(index.css);@import url(blocks/menu/menu.ie.css);@import url(blocks/button/button.ie.css);@import url(blocks/footer/footer.ie.css);

CSS file for IE

87

Page 88: BEM. What you can borrow from Yandex frontend dev

CSS

@import url(index.css);@import url(blocks/menu/menu.ie.css);@import url(blocks/button/button.ie.css);@import url(blocks/footer/footer.ie.css);

CSS file for IE

88

Page 89: BEM. What you can borrow from Yandex frontend dev

blocks/

tabbed-pane/ tabbed-pane.css tabbed-pane.ie.css ...

menu/ menu.css menu.ie.css

Block files for IE

89

Page 90: BEM. What you can borrow from Yandex frontend dev

blocks/

tabbed-pane/ tabbed-pane.css tabbed-pane.ie.css tabbed-pane__item.css tabbed-pane__item.ie.css tabbed-pane_theme_blue.css tabbed-pane_theme_blue.ie.css

Element files for IE

90

Page 91: BEM. What you can borrow from Yandex frontend dev

Block encapsulates all

91

blocks/ menu/ menu__item.css menu__item.ie.css menu.css menu.ie.css

@import...@import...@import...@import...@import...@import...

index.css

@import...@import...@import...@import...@import...@import...

index.ie.css

Page 92: BEM. What you can borrow from Yandex frontend dev

JavaScript

Page 93: BEM. What you can borrow from Yandex frontend dev
Page 94: BEM. What you can borrow from Yandex frontend dev

Tabbed-pane block

94

Page 95: BEM. What you can borrow from Yandex frontend dev

Dropdown block

95

Page 96: BEM. What you can borrow from Yandex frontend dev

Dropdown block

96

Page 97: BEM. What you can borrow from Yandex frontend dev

index.html

<!DOCTYPE html><html> <head> <link rel=stylesheet href="index.css"> <script type="text/javascript" src="all.js"/> </head> ...

Page with JavaScript

97

Page 98: BEM. What you can borrow from Yandex frontend dev

index.html

<!DOCTYPE html><html> <head> <link rel=stylesheet href="index.css"> <script type="text/javascript" src="index.js"/> </head> ...

Page with JavaScript

98

Page 99: BEM. What you can borrow from Yandex frontend dev

index page

Pages are sets of blocks

99

search page contacts page

Page 100: BEM. What you can borrow from Yandex frontend dev

blocks/ menu/ menu.css menu.js dropdown/ dropdown.css dropdown.js tabbed-pane/ tabbed-pane.css tabbed-pane.js

JavaScript for blocks

100

Page 101: BEM. What you can borrow from Yandex frontend dev

index.js

borschik:include:blocks/menu/menu.jsborschik:include:blocks/tabbed-pane/tabbed-pane.js...

Page JavaScript of blocks

101

index.js

/* Menu block begins */(function($){ $('.menu__item').on('click', function(e) { $(this).toggleClass('menu__item_state_current'); });})(jQuery)

Page 102: BEM. What you can borrow from Yandex frontend dev

git.io/borschik

Borschik — CSS&JS flattening

102

Page 103: BEM. What you can borrow from Yandex frontend dev

index.css

@import url(blocks/header.css);@import url(blocks/menu.css);...

CSS flatenning

103

_index.css

.header { ...}.menu { ...}

Page 104: BEM. What you can borrow from Yandex frontend dev

Relative paths in CSS

104

pages/_index.css

.menu {

background: url(../blocks/menu/menu__bg.png);

}

blocks/menu/menu.css.menu {

background: url(menu__bg.png);

}

pages/index.css@import url(blocks/menu/menu.css);

Page 105: BEM. What you can borrow from Yandex frontend dev

105

HTML<!DOCTYPE html><html> <head> <title>Awesome online bookshop</title> <link rel="stylesheet" href="style.css"/> <script type="text/javascript" src="magic.js"></script> </head> <body> <div class="head"> <ul class="menu"> <li class="menu__item"><a href="/">Home</a></li> <li class="menu__item"><a href="/news">News</a></li> ... </ul> <div class="phone">+3 71 1234567</div> </div> <div class="layout"> ... </div> </body></html>

general CSS@import url(blocks/head/head.css);@import url(blocks/search/search.css);@import url(blocks/layout/layout.css);@import url(blocks/menu/menu.css);@import url(blocks/text/text.css);@import url(blocks/foot/foot.css);

CSS for IE@import url(index.css);@import url(blocks/menu/menu.ie.css);@import url(blocks/text/text.ie.css);@import url(blocks/foot/foot.ie.css);

JavaScript(function($){

$('.menu__item').on('click', function() {

$(this).toggleClass('menu__item_state_current');

});})(jQuery)

Page 106: BEM. What you can borrow from Yandex frontend dev

Building Page Files

Page 107: BEM. What you can borrow from Yandex frontend dev
Page 108: BEM. What you can borrow from Yandex frontend dev

index.xml

<b:page> <b:head> <b:logo/> <b:search> <e:input/> <e:button/> </b-search> <b:menu> <e:item>Home</e:item> <e:item>Contacts</e:item> ...

Page declaration

108

Page 109: BEM. What you can borrow from Yandex frontend dev

index.bemjson

{ block: 'page', content: [ { block: head, content: [ { block: 'logo' }, { block: 'search', ....

Page declaration. BEM tree

109

Page 110: BEM. What you can borrow from Yandex frontend dev

Change BEM tree if you need changes

110

page.htmlpage.csspage.js}

Page 111: BEM. What you can borrow from Yandex frontend dev

git.io/bem-tools

BEM tools build pages with blocks

111

Page 112: BEM. What you can borrow from Yandex frontend dev

blocks/

menu/ menu.sass menu.less menu.coffee

tabbed-pane/ tabbed-pane.sass tabbed-pane.coffee

pages/

index.htmlindex.sass -> index.cssindex.coffee -> index.js

BEM tools support many technologies

112

Page 113: BEM. What you can borrow from Yandex frontend dev

blocks/

menu.css menu_theme_black.css menu_theme_blue.css menu_size_l.css menu_size_s.css menu-item.css menu-item_state_current.css

tabbed-pane.css tabbed-pane_theme_black.css tabbed-pane_theme_blue.css tabbedpane-item.css tabbedpane-pane.css

logo.css

BEM tools support any file structure

113

blocks/

menu/ __item/ menu__item.css _theme/ menu_theme_black.css menu_theme_blue.css _size/ menu_size_l.css menu_size_s.css menu.css

tabbed-pane/

logo/

Page 114: BEM. What you can borrow from Yandex frontend dev

Multilingual

Page 115: BEM. What you can borrow from Yandex frontend dev

blocks/

menu/ tabbed-pane.css tabbed-pane.js tabbed-pane.md

BEM tools support custom technologies

115

Page 116: BEM. What you can borrow from Yandex frontend dev

blocks/

menu/ tabbed-pane.css tabbed-pane.js tabbed-pane.md tabbed-pane.xsl

BEM tools support custom technologies

116

Page 117: BEM. What you can borrow from Yandex frontend dev

Gruelling race... every day

Page 118: BEM. What you can borrow from Yandex frontend dev

blocks/

menu/ tabbed-pane.css tabbed-pane.js tabbed-pane.md tabbed-pane.bemhtml

BEMHTML template engine

118

Page 119: BEM. What you can borrow from Yandex frontend dev

bit.ly/bemhtml-doc-raw

BEMHTML reference [RU]

119

Page 120: BEM. What you can borrow from Yandex frontend dev

Libraries

Page 121: BEM. What you can borrow from Yandex frontend dev

Block libraries

121

Page 122: BEM. What you can borrow from Yandex frontend dev

bit.ly/bem-bl-site

BEM block library

122

Page 123: BEM. What you can borrow from Yandex frontend dev

Block page and block examples

123

Page 124: BEM. What you can borrow from Yandex frontend dev

Block code on GitHub

124

Page 126: BEM. What you can borrow from Yandex frontend dev

Wanna try?

Page 127: BEM. What you can borrow from Yandex frontend dev

git.io/bem-project-stub

BEM-based project

127

Page 128: BEM. What you can borrow from Yandex frontend dev

Dev Tools

Page 129: BEM. What you can borrow from Yandex frontend dev

git.io/borschik

Borschik — CSS & JS flattening

129

Page 130: BEM. What you can borrow from Yandex frontend dev

bem.info/tools/csso

CSS Optimizer

130

BEFORE.test1 { border: none; background-color: red;}

.test2 { border: none}

.test3 { background-color: #FF000;}

AFTER.test1, .test2 { border: none}

.test1, .test3 { background-color: #F00;}

Page 131: BEM. What you can borrow from Yandex frontend dev

git.io/svgo

SVG Optimizer

131

Page 133: BEM. What you can borrow from Yandex frontend dev

Varvara StepanovaVladimir VarankinFrontend developers

[email protected]@toivonens@tvii

@bem_tw

Thank you

[email protected]


Recommended