components now!

64

Upload: mikhail-davydov

Post on 10-May-2015

250 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Components now!
www.princexml.com
Prince - Non-commercial License
This document was created with Prince, a great way of getting web content onto paper.
Page 2: Components now!

About myself●Front-End Engineer at Yandex

●Developer of Yandex.Taxi and Yandex.Mobile

● JavaScript Teacher

●Blogger

02

Page 3: Components now!
Page 4: Components now!

ComponentsNow!

Mikhail Davydov

MetaRefresh, 15 Feb 2014

Page 5: Components now!

Code libraries

Page 6: Components now!

Code libraries● jQuery — fixes DOM

●Underscore — fills ECMAScript 3-5 gap

●Backbone — brings MV*

●They are created to fix Web

For some historical reasons many good tools are libraries.

The DOM is a Mess 2009

06

Page 7: Components now!

Libraries are everywhere

Page 8: Components now!

Libraries are big●Heavy

–jQuery 2.0.3 — 230K (unpacked)

●Hard to understand

–Cyclomatic complexity of $.ajax — 43

●Too powerful

–$.ajax, $.animate, _08

Page 9: Components now!

Libraries are tightly coupled●Hard to get a small part

–$.Deferred, $.ajax

–_.template

●Manual extraction

●Their modules don't help much

09

Page 10: Components now!

Dependencies management●Dependency on jQuery

●Libraries require libraries

–Backbone+jQuery+Underscore

●Manual dependencies management

–Bower and Npm are lifesavers

10

Page 11: Components now!

Web is fixed*

Page 13: Components now!

Critical DOM APIs●querySelector

●CSS3 Selectors*

●Web Storage (localStorage)

●Cross-Origin Resource Sharing*

* fully with polyfills

Can I Use

13

Page 15: Components now!

Polyfills can fix the rest●CSS3 Selectors (slow, not recommended)

●Cross-Origin Resource Sharing

●HTML5 Elements

●ECMAScript 5

HTML5 Please polyfills

15

Page 16: Components now!

Mobile

Page 17: Components now!

Mobile Growth

Page 18: Components now!

Why do we uselibraries?

Page 19: Components now!

Components

Page 20: Components now!

Simple

Page 21: Components now!

Components are simple●Lightweight

–dom — 28K, 3K self (unpacked)

●Single responsibility & KISS Principle

●Easy to understand

–Maintain

–Write tests21

Page 22: Components now!

Standalone

Page 23: Components now!

Components are standalone●Contain all dependencies

–Most of them are external

●Easy to reuse

– bower i name

– npm i name

● It is simple to use a part23

Page 24: Components now!

Isolated

Page 25: Components now!

Components are isolated●Do not interfere with others

–Scoped CSS

–Flexible layout

–No globals leak

●Have restricted access to others

– require()

25

Page 26: Components now!
Page 27: Components now!

WebComponents● Idea of Custom HTML Elements

●API/Framework

–Shadow DOM (Encapsulation)

–HTML Imports & Templates

–Template Binding

Web components and the future of web development from MR 201327

Page 28: Components now!

WebComponents in 2014HTML Templates

Shadow DOM

Custom Elements

HTML Imports

It is possible to polyfill others using Polymer.

28

Page 29: Components now!

Alternatives for these APIsWebComponents API Alternative

Custom Elements Component engines

Shadow DOM BEM Methodology

HTML Templates Template engines

HTML Imports Build tools

Scoped CSS BEM or OOCSS

Template Binding Data binding

29

Page 31: Components now!
Page 32: Components now!

BEM briefly●Avoid CSS cascade

●Block — Custom Element

●Element — Shadow DOM

●Modifier — Component State

Maintainable frontend development with BEM from MR 2013

32

Page 33: Components now!

.tab-panel — Block

.tab-panel__tab — Element

.tab-panel__tab_state_active

Page 34: Components now!

Let's build theComponent!

Page 35: Components now!
Page 36: Components now!

Keep in mind that●Component is Simple

–KISS

●Component is Standalone

–Dependencies

●Component is Isolated

–Layout, CSS, JS36

Page 37: Components now!

Package file

Page 38: Components now!

Package file is the contract// bower.json

{

"name""name": "my-share""my-share",

"version""version": "1.0.0""1.0.0",

"main""main": ["my-share.js""my-share.js"]

}

Bower and format of bower.json

01.

02.

03.

04.

05.

06.

38

Page 39: Components now!

HTML Layout

Page 40: Components now!

Private HTML Layout<!-- my-share.html -->

<aa hrefhref=""{{ href }}{{ href }}"" classclass="my-share""my-share">

<imgimg srcsrc=""{{ icon }}{{ icon }}"" classclass="my-share__icon""my-share__icon"/>

<spanspan classclass="my-share__label""my-share__label">{{ label }}</spanspan>

</aa>

This template is written using BEM Methodology.

01.

02.

03.

04.

05.

40

Page 41: Components now!

Interface

Page 42: Components now!

Interface<aa classclass="my-share""my-share"

data-href="{{ href }}"

data-icon="{{ icon }}"

>{{ label }}</aa>

This interface is similar to the WebComponents.

01.

02.

03.

04.

42

Page 43: Components now!

WebComponents for comparison<my-sharemy-share

href="{{ href }}"

icon="{{ icon }}"

>{{ label }}</my-sharemy-share>

01.

02.

03.

04.

43

Page 44: Components now!

CSS of Component..my-sharemy-share {}

.mymy-share__icon {

vertical-alignvertical-align: middle;middle;

heightheight: 16px;16px;

}

.mymy-share__label {

padding-leftpadding-left: 5px;5px;

}

01.

02.

03.

04.

05.

06.

07.

08.

44

Page 45: Components now!

CSS of WebComponentmy-share {}

.icon {

vertical-alignvertical-align: middle;middle;

heightheight: 16px;16px;

}

.label {

padding-leftpadding-left: 5px;5px;

}

01.

02.

03.

04.

05.

06.

07.

08.

45

Page 46: Components now!

CSS is isolated●WebComponent — DOM API

– <style scoped>

●BEM

–Avoid CSS cascade

–Naming conventions .block__element

46

Page 47: Components now!

Dependencies

Page 48: Components now!

External dependencies// bower.json

"dependencies""dependencies": {

"tpl""tpl": "azproduction/lodash-template""azproduction/lodash-template",

"domify""domify": "component/domify""component/domify",

"decl""decl": "azproduction/decl""azproduction/decl"

}

Declare all external dependencies.

01.

02.

03.

04.

05.

06.

48

Page 49: Components now!

Component relations// my-share.js

varvar tpl = require('tpl''tpl'),

decl = require('decl''decl'),

domify = require('domify''domify');

varvar html = require('templates/my-share''templates/my-share'),

template = tpl(html);

01.

02.

03.

04.

05.

06.

07.

49

Page 50: Components now!

Component logicfunctionfunction MyShare(el) {

thisthis.options = thisthis.collectOptions(el);

thisthis.el = thisthis.render();

thisthis.delegateEvents();

thisthis.replaceElement(el);

}

decl('.my-share''.my-share', MyShare);

module.exports = MyShare;

01.

02.

03.

04.

05.

06.

07.

08.

50

Page 51: Components now!

JavaScript is isolated●CommonJS/Modules

–No globals leak

–It asks for requirements

–It provides resources

●AMD can be used too

51

Page 52: Components now!

Assemble

Page 53: Components now!

Assemble component●Resolve all requirements

●Compile

–styles, scripts, templates

●Assemble scripts and templates

●Concatenate styles

53

Page 54: Components now!

Component build tools●bem-tools

●Component

●Browserify

●LMD

54

Page 55: Components now!

Using my-share Component<aa classclass="my-share""my-share" ......>Tweet</aa>

<linklink relrel="stylesheet""stylesheet" hrefhref=""my-share.cssmy-share.css""/>

<scriptscript srcsrc=""my-share.jsmy-share.js""></scriptscript>

Compare with WebComponents.

01.

02.

03.

55

Page 56: Components now!

Using my-share WebComponent<my-sharemy-share ......>Tweet</my-sharemy-share>

<linklink relrel="import""import" hrefhref=""my-share.htmlmy-share.html""/>

01.

02.

56

Page 57: Components now!

Tweet

Live example

57

Page 58: Components now!
Page 59: Components now!
Page 60: Components now!

Conclusion●Web is Fixed, Mobile is Growing

●Libraries are big and clumsy

●WebComponents == API

●Write and use Components

60

Page 62: Components now!

Useful resources●Code of my-share

●You might not need jQuery

●BEM Methodology

●Web Components

●Polymer Project

62

Page 63: Components now!

clck.ru/94Tqf

Page 64: Components now!

Sources of images● minecraft.gamepedia.com/Blocks

● fr.fotopedia.com/wiki/Bibliothèque#!/items/flickr-6899137527

● www.planetminecraft.com/project/minecraft-site/

● investments.academic.ru/1513/Форвардный_контракт

● frozen-lama.deviantart.com/art/Minecraft-Compound-Door-209872466

● www.postandcourier.com/article/20120620/pc1002/120629985

● isintu.com/features/responsive-design/

● nicolaijuhler.wordpress.com/

● r3c0nic.deviantart.com/art/minecraft-Blueprint-293455933

● ru-wallp.com/view/1542/

64