SlideShare a Scribd company logo
1 of 32
Web Components 101
Polymer & Brick
전용우
Web Components?
• 컴포넌트를 만드는 기술.
• 기존의 자바스크립트 컴포넌트와 비슷함.
설정하기.
• Chrome Canary (31+)
• about:flags
– Enable HTML Imports
• HTML 가져오기 사용

– Enable Experimental JavaScript
• 실험용 자바스크립트 사용

– Enable experimental Web Platform features
• 실험용 웹 플랫폼 기능 사용

• Devtool
– General - Show Shadow DOM
구성
•
•
•
•

Shadow DOM
Template
Custom Element
HTML Import
Shadow DOM
• 숨어있는 DOM subtree.
• 캡슐화할 수 있음.
• 외부와 context가 다르기 때문에 접근할 수
없음.
Shadow DOM 만들기
<div id="temp"></div>
<script>
var host = document.querySelector('#temp');
var root = host.createShadowRoot();
root.innerHTML = ‘<b>안녕. Shadow DOM!</b>';
</script>
#temp – shadow host

shadow root

shadow
boundary

<b>안녕. Shadow DOM</b>
Content Element
• root에 있는 엘리먼트의 위치를 이동시키는
엘리먼트.
<div id="temp">
<div class="pre">앞으로</div>
<div class="next">뒤로</div>
</div>
<script>
var host = document.querySelector('#temp');
var root = host.webkitCreateShadowRoot();
root.innerHTML = '<content select=".next"></content>'+
'<b part="hi">안녕. Shadow DOM!</b>'+
'<content select=".pre"></content>';
</script>
Content Element
#temp – shadow host
div.pre

div.next
shadow root

b
select=“.next”

select=“.pre”
Styling #1
• applyAuthorStyles, resetStyleInheritance
– 외부에서 정의한 스타일이 적용 유무(false)
– 상속되는 스타일의 적용 유무(true)

• Part Pseudo Selector
– 외부에서 Shadow DOM의 스타일을 변경할 수
있게 해주는 가상 선택자.
(구. pseudo)
Styling #2
• CSS Variable
– 변수를 이용하여 Shadow DOM을 스타일링 가
능. (위젯 같은 데서 사용하기 좋음)
Template
•
•
•
•
•
•

일반적인 자바스크립트 템플릿과 유사
HTML, CSS, JS 묶음.
사용하기 전에는 아무런 영향이 없음.
내부 리소스 등을 내려 받지 않음.(image)
파싱만 함.
스크립트 실행 안됨.
Template 만들기
<div id="temp">
<div class="pre">이건 앞이지</div>
<div class="next">이건 뒤지</div>
</div>
<template id="test">
<style type="text/css">
b{ color : var(preColor); }
</style>
<content select=".next"></content>
<b part="hi">안녕. Shadow DOM!</b>
<content select=".pre"></content>
</template>
<script>
var host = document.querySelector('#temp');
var root = host.webkitCreateShadowRoot();
var temp = document.getElementById("test").content.cloneNode(true);
root.appendChild(temp);
</script>
Custom Element
• 개발자가 새로운 엘리먼트를 만들 수 있음.
• Native 엘리먼트를 확장하여 만들 수 있음.
• 이벤트나 속성들을 만들 수 있음.
등록하기

var XButton = document.register('x-button');
var xButton = new XButton();
//var xButton = document.createElement("x-button");
document.body.appendChild(xButton);

<x-button></x-button>

※ element 태그를 이용하여 선언하는(declarative)방법으로 등록했지만 현재 spec에서 삭제.
속성/메서드 추가하기
var xButtonProto = Object.create(HTMLButtonElement.prototype,{
length : {
get : function(){//length readonly 속성 지정
return this.textContent.length;
}
}
});
xButtonProto.some = function(){ //method 추가
alert("some");
}
var XButton = document.register('x-button',{ prototype: xButtonProto });
var xButton = new XButton();
xButton.textContent="테스트버튼입니다.";
document.body.appendChild(xButton);
xButton.some();//alert("some");
alert(xButton.length);// 9
lifecycle 콜백
이벤트명

설명

createdCallback

생성될 때

enteredViewCallback

document에 붙일 때

leftViewCallback

document에서 때어 졌을 때

attributeChangedCallback
(attributeName, oldVal, newVal)

속성이 변경될 때
(속성 명, 예전 값, 변경 값)

xButtonProto.createdCallback = function(){
alert("생성");
}
xButtonProto.enteredViewCallback = function(){
alert("삽입");
}
xButtonProto.leftViewCallback = function(){
alert("삭제");
}
xButtonProto.attributeChangedCallback = function(attr, oldAttr, newAttr){
alert(attr+"가 "+oldAttr+"에서 "+newAttr+"으로 변경");
}
가상 이벤트 생성
var xButtonProto = Object.create(HTMLButtonElement.prototype,{
createdCallback : {
value : function(){
this.style.cssText="border:1px solid red; padding:10px;";
var key;
this.addEventListener("mousedown",function(){
key = setTimeout(function(){
var event = new CustomEvent("longtap",
{"detail": {"time":new Date()}});
this.dispatchEvent(event);
key = null;
}.bind(this),1000);
});
this.addEventListener("mouseup",function(){
if(key){ clearTimeout(key); }
});
}
}
});
var XButton = document.register('x-button',{ prototype: xButtonProto });
var xButton = new XButton();
xButton.textContent="버튼입니다.";
xButton.addEventListener("longtap",function(e){
alert("longtap");
console.log(e);
});
HTML Import
• Web Components을 적용하기 위한 방법
– import된 html은 표현되지 않음
– Cross-Origin Resource Sharing
– 같은 document가 아님.
<link rel="import" href="/path/to/some/import.html">
var link = document.querySelector('link[rel=import]');
var import = link.import;
var templateButton= import.querySelector('template.button');
간단한 버튼 만들어 보기
지원 현황
Chrome(m)
Shadow DOM
Template
Custom Element
HTML Import

Chrome

Firefox

IE10+

Safari 6+

Safari(m)
Web Components 라이브러리
• Polymer
– 구글에서 개발

• Brick(with xTag)
– 모질라에서 개발
Polymer
platform.js
•
•
•
•

Web Components polyfills 라이브러리
Template의 기능이 추가.(bind, if, repeat…)
html import 가능(불안함.)
기타 기능 제공
– Pointer Event
– Web Animation
– Node.bind
polymer.js
• web components기능을 확장
– 선언하여 등록
– Data-binding
– 선언하여 함수 할당 …..
<polymer-element name="g-cool">
<template>
<button on-click="{{buttonClick}}">{{name}}</button>
</template>
<script>
Polymer('g-cool', {
name : "mixed",
buttonClick: function(event, detail, sender) { ... }
});
</script>
</polymer-element>
기타
Polymer로 만든 엘리먼트(컴포넌트)
• Element
– UI가 없고 기능만 있는 엘리먼트
– Ajax, localStorage….

• UI Element
– UI가 있는 엘리먼트
– Accordion, Slider….
지원 현황
Chrome(m)
Shadow DOM
Template
Custom Element
HTML Import

Chrome

Firefox

IE10+

Safari 6+

Safari(m)
Brick
• xTag을 기반으로 만든 라이브러리
xTag
• polyfill
– document.register 사용가능
– shadow dom은 지원 안 함.
– html import가능

• xTag
– polyfill에서 유틸 기능들이 추가.
Brick
• xTag을 이용하여 만든 컴포넌트
– Flipbox, tooltip….
지원 현황
Chrome(m)
Shadow DOM
Template
Custom Element
HTML Import

Chrome

Firefox

IE9+

Safari 6+

Safari(m)
Why Web Components?
• 컴포넌트가 동작하는 영역이 분리되어
성능이 좋음.
• 모듈화 되어 있어 사용하기 좋음.
• 높은 접근성.
• 보다 시멘틱해짐.
• 향후 컴포넌트 표준이 될 가능성이 높음.
(AngularJS, EmberJS은 Web Components로 변경할 계획. Dart에서도 사용)

• 웹 생태계에 많은 도움을 줄 것 같음.
감사합니다.

More Related Content

What's hot

Web Components & Polymer
Web Components & PolymerWeb Components & Polymer
Web Components & PolymerJae Sung Park
 
처음배우는 자바스크립트, 제이쿼리 #1
처음배우는 자바스크립트, 제이쿼리 #1처음배우는 자바스크립트, 제이쿼리 #1
처음배우는 자바스크립트, 제이쿼리 #1성일 한
 
[D2 오픈세미나]5.robolectric 안드로이드 테스팅
[D2 오픈세미나]5.robolectric 안드로이드 테스팅[D2 오픈세미나]5.robolectric 안드로이드 테스팅
[D2 오픈세미나]5.robolectric 안드로이드 테스팅NAVER D2
 
안드로이드 개발자를 위한 스위프트
안드로이드 개발자를 위한 스위프트안드로이드 개발자를 위한 스위프트
안드로이드 개발자를 위한 스위프트병한 유
 
5-5. html5 connectivity
5-5. html5 connectivity5-5. html5 connectivity
5-5. html5 connectivityJinKyoungHeo
 
Clean Front-End Development
Clean Front-End DevelopmentClean Front-End Development
Clean Front-End Development지수 윤
 
5-4. html5 offline and storage
5-4. html5 offline and storage5-4. html5 offline and storage
5-4. html5 offline and storageJinKyoungHeo
 
Html5 web workers
Html5 web workersHtml5 web workers
Html5 web workersWoo Jin Kim
 
ReactJS | 서버와 클라이어트에서 동시에 사용하는
ReactJS | 서버와 클라이어트에서 동시에 사용하는ReactJS | 서버와 클라이어트에서 동시에 사용하는
ReactJS | 서버와 클라이어트에서 동시에 사용하는Taegon Kim
 
처음배우는 자바스크립트, 제이쿼리 #4
처음배우는 자바스크립트, 제이쿼리 #4처음배우는 자바스크립트, 제이쿼리 #4
처음배우는 자바스크립트, 제이쿼리 #4성일 한
 
AngularJS의 개발방식에 대하여
AngularJS의 개발방식에 대하여AngularJS의 개발방식에 대하여
AngularJS의 개발방식에 대하여Jin wook
 
자바스크립트 프레임워크 살펴보기
자바스크립트 프레임워크 살펴보기자바스크립트 프레임워크 살펴보기
자바스크립트 프레임워크 살펴보기Jeado Ko
 
Polymer따라잡기
Polymer따라잡기Polymer따라잡기
Polymer따라잡기Han Jung Hyun
 
회사에서 써보는 SQLAlchemy
회사에서 써보는 SQLAlchemy회사에서 써보는 SQLAlchemy
회사에서 써보는 SQLAlchemyJc Kim
 
다시보는 Angular js
다시보는 Angular js다시보는 Angular js
다시보는 Angular jsJeado Ko
 

What's hot (20)

Web Components & Polymer
Web Components & PolymerWeb Components & Polymer
Web Components & Polymer
 
4-3. jquery
4-3. jquery4-3. jquery
4-3. jquery
 
처음배우는 자바스크립트, 제이쿼리 #1
처음배우는 자바스크립트, 제이쿼리 #1처음배우는 자바스크립트, 제이쿼리 #1
처음배우는 자바스크립트, 제이쿼리 #1
 
[D2 오픈세미나]5.robolectric 안드로이드 테스팅
[D2 오픈세미나]5.robolectric 안드로이드 테스팅[D2 오픈세미나]5.robolectric 안드로이드 테스팅
[D2 오픈세미나]5.robolectric 안드로이드 테스팅
 
안드로이드 개발자를 위한 스위프트
안드로이드 개발자를 위한 스위프트안드로이드 개발자를 위한 스위프트
안드로이드 개발자를 위한 스위프트
 
5-5. html5 connectivity
5-5. html5 connectivity5-5. html5 connectivity
5-5. html5 connectivity
 
Clean Front-End Development
Clean Front-End DevelopmentClean Front-End Development
Clean Front-End Development
 
5-4. html5 offline and storage
5-4. html5 offline and storage5-4. html5 offline and storage
5-4. html5 offline and storage
 
Html5 web workers
Html5 web workersHtml5 web workers
Html5 web workers
 
ReactJS | 서버와 클라이어트에서 동시에 사용하는
ReactJS | 서버와 클라이어트에서 동시에 사용하는ReactJS | 서버와 클라이어트에서 동시에 사용하는
ReactJS | 서버와 클라이어트에서 동시에 사용하는
 
처음배우는 자바스크립트, 제이쿼리 #4
처음배우는 자바스크립트, 제이쿼리 #4처음배우는 자바스크립트, 제이쿼리 #4
처음배우는 자바스크립트, 제이쿼리 #4
 
Express 프레임워크
Express 프레임워크Express 프레임워크
Express 프레임워크
 
AngularJS의 개발방식에 대하여
AngularJS의 개발방식에 대하여AngularJS의 개발방식에 대하여
AngularJS의 개발방식에 대하여
 
Handlebars
HandlebarsHandlebars
Handlebars
 
자바스크립트 프레임워크 살펴보기
자바스크립트 프레임워크 살펴보기자바스크립트 프레임워크 살펴보기
자바스크립트 프레임워크 살펴보기
 
Polymer따라잡기
Polymer따라잡기Polymer따라잡기
Polymer따라잡기
 
회사에서 써보는 SQLAlchemy
회사에서 써보는 SQLAlchemy회사에서 써보는 SQLAlchemy
회사에서 써보는 SQLAlchemy
 
4-2. ajax
4-2. ajax4-2. ajax
4-2. ajax
 
다시보는 Angular js
다시보는 Angular js다시보는 Angular js
다시보는 Angular js
 
4-1. javascript
4-1. javascript4-1. javascript
4-1. javascript
 

Viewers also liked

혁신적인 웹컴포넌트 라이브러리 - Polymer
혁신적인 웹컴포넌트 라이브러리 - Polymer혁신적인 웹컴포넌트 라이브러리 - Polymer
혁신적인 웹컴포넌트 라이브러리 - PolymerJae Sung Park
 
Writing Reusable Web Components with jQuery and jQuery UI
Writing Reusable Web Components with jQuery and jQuery UIWriting Reusable Web Components with jQuery and jQuery UI
Writing Reusable Web Components with jQuery and jQuery UIYnon Perek
 
모바일 디버깅
모바일 디버깅모바일 디버깅
모바일 디버깅yongwoo Jeon
 
2016 W3C Conference #4 : ANGULAR + ES6
2016 W3C Conference #4 : ANGULAR + ES62016 W3C Conference #4 : ANGULAR + ES6
2016 W3C Conference #4 : ANGULAR + ES6양재동 코드랩
 
안정적인 서비스 운영 2013.08
안정적인 서비스 운영   2013.08안정적인 서비스 운영   2013.08
안정적인 서비스 운영 2013.08Changyol BAEK
 
2016 W3C Conference #1 : 웹 개발의 현재와 미래
2016 W3C Conference #1 : 웹 개발의 현재와 미래2016 W3C Conference #1 : 웹 개발의 현재와 미래
2016 W3C Conference #1 : 웹 개발의 현재와 미래양재동 코드랩
 
2016 W3C Conference #8 : IONIC으로 하이브리드 앱 개발하기, 사례와 시사점
2016 W3C Conference #8 : IONIC으로 하이브리드 앱 개발하기, 사례와 시사점2016 W3C Conference #8 : IONIC으로 하이브리드 앱 개발하기, 사례와 시사점
2016 W3C Conference #8 : IONIC으로 하이브리드 앱 개발하기, 사례와 시사점양재동 코드랩
 
알아봅시다, Polymer: Web Components & Web Animations
알아봅시다, Polymer: Web Components & Web Animations알아봅시다, Polymer: Web Components & Web Animations
알아봅시다, Polymer: Web Components & Web AnimationsChang W. Doh
 
메모리 할당에 관한 기초
메모리 할당에 관한 기초메모리 할당에 관한 기초
메모리 할당에 관한 기초Changyol BAEK
 
Polymer, lego같이 만드는 웹어플리케이션
Polymer, lego같이 만드는 웹어플리케이션Polymer, lego같이 만드는 웹어플리케이션
Polymer, lego같이 만드는 웹어플리케이션Jeado Ko
 

Viewers also liked (14)

혁신적인 웹컴포넌트 라이브러리 - Polymer
혁신적인 웹컴포넌트 라이브러리 - Polymer혁신적인 웹컴포넌트 라이브러리 - Polymer
혁신적인 웹컴포넌트 라이브러리 - Polymer
 
Writing Reusable Web Components with jQuery and jQuery UI
Writing Reusable Web Components with jQuery and jQuery UIWriting Reusable Web Components with jQuery and jQuery UI
Writing Reusable Web Components with jQuery and jQuery UI
 
Web component
Web componentWeb component
Web component
 
Devfest
DevfestDevfest
Devfest
 
모바일 디버깅
모바일 디버깅모바일 디버깅
모바일 디버깅
 
2016 W3C Conference #4 : ANGULAR + ES6
2016 W3C Conference #4 : ANGULAR + ES62016 W3C Conference #4 : ANGULAR + ES6
2016 W3C Conference #4 : ANGULAR + ES6
 
안정적인 서비스 운영 2013.08
안정적인 서비스 운영   2013.08안정적인 서비스 운영   2013.08
안정적인 서비스 운영 2013.08
 
2016 W3C Conference #1 : 웹 개발의 현재와 미래
2016 W3C Conference #1 : 웹 개발의 현재와 미래2016 W3C Conference #1 : 웹 개발의 현재와 미래
2016 W3C Conference #1 : 웹 개발의 현재와 미래
 
deview2014
deview2014deview2014
deview2014
 
2016 W3C Conference #8 : IONIC으로 하이브리드 앱 개발하기, 사례와 시사점
2016 W3C Conference #8 : IONIC으로 하이브리드 앱 개발하기, 사례와 시사점2016 W3C Conference #8 : IONIC으로 하이브리드 앱 개발하기, 사례와 시사점
2016 W3C Conference #8 : IONIC으로 하이브리드 앱 개발하기, 사례와 시사점
 
알아봅시다, Polymer: Web Components & Web Animations
알아봅시다, Polymer: Web Components & Web Animations알아봅시다, Polymer: Web Components & Web Animations
알아봅시다, Polymer: Web Components & Web Animations
 
메모리 할당에 관한 기초
메모리 할당에 관한 기초메모리 할당에 관한 기초
메모리 할당에 관한 기초
 
D3.js workshop
D3.js workshopD3.js workshop
D3.js workshop
 
Polymer, lego같이 만드는 웹어플리케이션
Polymer, lego같이 만드는 웹어플리케이션Polymer, lego같이 만드는 웹어플리케이션
Polymer, lego같이 만드는 웹어플리케이션
 

Similar to Web Components 101 polymer & brick

Web Components - Part.I, @KIG 30th
Web Components - Part.I, @KIG 30thWeb Components - Part.I, @KIG 30th
Web Components - Part.I, @KIG 30thChang W. Doh
 
Hacosa js study 4주차
Hacosa js study 4주차Hacosa js study 4주차
Hacosa js study 4주차Seong Bong Ji
 
Software Architect day - 웹 프레임워크 종결 - metaworks3
Software Architect day - 웹 프레임워크 종결 -  metaworks3Software Architect day - 웹 프레임워크 종결 -  metaworks3
Software Architect day - 웹 프레임워크 종결 - metaworks3uEngine Solutions
 
Meteor React Tutorial 따라하기
Meteor React Tutorial 따라하기Meteor React Tutorial 따라하기
Meteor React Tutorial 따라하기Jiam Seo
 
20131217 html5
20131217 html520131217 html5
20131217 html5DK Lee
 
Reflow and repaint 성능 비용
Reflow and repaint 성능 비용Reflow and repaint 성능 비용
Reflow and repaint 성능 비용Doo Sung Eom
 
[KGIT_EWD]class03 0322
[KGIT_EWD]class03 0322[KGIT_EWD]class03 0322
[KGIT_EWD]class03 0322jylee6977
 
Introduction To Xaml
Introduction To XamlIntroduction To Xaml
Introduction To Xamlphoooo
 
Java script 강의자료_ed13
Java script 강의자료_ed13Java script 강의자료_ed13
Java script 강의자료_ed13hungrok
 
20150912 windows 10 앱 tips tricks
20150912 windows 10 앱 tips  tricks20150912 windows 10 앱 tips  tricks
20150912 windows 10 앱 tips tricks영욱 김
 
First Step In Ajax Korean
First Step In Ajax KoreanFirst Step In Ajax Korean
First Step In Ajax KoreanTerry Cho
 
웹개발자가 알아야할 기술
웹개발자가 알아야할 기술웹개발자가 알아야할 기술
웹개발자가 알아야할 기술jaypi Ko
 
I phone 2 release
I phone 2 releaseI phone 2 release
I phone 2 releaseJaehyeuk Oh
 
[DevOn 2013] Backbone.js로 능동적 M-V 디자인 구현하기
[DevOn 2013] Backbone.js로 능동적  M-V 디자인 구현하기[DevOn 2013] Backbone.js로 능동적  M-V 디자인 구현하기
[DevOn 2013] Backbone.js로 능동적 M-V 디자인 구현하기Gyutae Jo
 

Similar to Web Components 101 polymer & brick (20)

Web Components - Part.I, @KIG 30th
Web Components - Part.I, @KIG 30thWeb Components - Part.I, @KIG 30th
Web Components - Part.I, @KIG 30th
 
Hacosa jquery 1th
Hacosa jquery 1thHacosa jquery 1th
Hacosa jquery 1th
 
3-2. selector api
3-2. selector api3-2. selector api
3-2. selector api
 
Hacosa js study 4주차
Hacosa js study 4주차Hacosa js study 4주차
Hacosa js study 4주차
 
Software Architect day - 웹 프레임워크 종결 - metaworks3
Software Architect day - 웹 프레임워크 종결 -  metaworks3Software Architect day - 웹 프레임워크 종결 -  metaworks3
Software Architect day - 웹 프레임워크 종결 - metaworks3
 
[Codelab 2017] ReactJS 기초
[Codelab 2017] ReactJS 기초[Codelab 2017] ReactJS 기초
[Codelab 2017] ReactJS 기초
 
Swt J Face 2/3
Swt J Face 2/3Swt J Face 2/3
Swt J Face 2/3
 
Meteor React Tutorial 따라하기
Meteor React Tutorial 따라하기Meteor React Tutorial 따라하기
Meteor React Tutorial 따라하기
 
20131217 html5
20131217 html520131217 html5
20131217 html5
 
Reflow and repaint 성능 비용
Reflow and repaint 성능 비용Reflow and repaint 성능 비용
Reflow and repaint 성능 비용
 
(C#, WPF강좌)WPF, XAML 데이터바인딩이란? Data Binding 개요 및 예제_WPF학원/WPF교육
(C#, WPF강좌)WPF, XAML 데이터바인딩이란? Data Binding 개요 및 예제_WPF학원/WPF교육(C#, WPF강좌)WPF, XAML 데이터바인딩이란? Data Binding 개요 및 예제_WPF학원/WPF교육
(C#, WPF강좌)WPF, XAML 데이터바인딩이란? Data Binding 개요 및 예제_WPF학원/WPF교육
 
[KGIT_EWD]class03 0322
[KGIT_EWD]class03 0322[KGIT_EWD]class03 0322
[KGIT_EWD]class03 0322
 
Nexacro
NexacroNexacro
Nexacro
 
Introduction To Xaml
Introduction To XamlIntroduction To Xaml
Introduction To Xaml
 
Java script 강의자료_ed13
Java script 강의자료_ed13Java script 강의자료_ed13
Java script 강의자료_ed13
 
20150912 windows 10 앱 tips tricks
20150912 windows 10 앱 tips  tricks20150912 windows 10 앱 tips  tricks
20150912 windows 10 앱 tips tricks
 
First Step In Ajax Korean
First Step In Ajax KoreanFirst Step In Ajax Korean
First Step In Ajax Korean
 
웹개발자가 알아야할 기술
웹개발자가 알아야할 기술웹개발자가 알아야할 기술
웹개발자가 알아야할 기술
 
I phone 2 release
I phone 2 releaseI phone 2 release
I phone 2 release
 
[DevOn 2013] Backbone.js로 능동적 M-V 디자인 구현하기
[DevOn 2013] Backbone.js로 능동적  M-V 디자인 구현하기[DevOn 2013] Backbone.js로 능동적  M-V 디자인 구현하기
[DevOn 2013] Backbone.js로 능동적 M-V 디자인 구현하기
 

More from yongwoo Jeon

Html5 시대의 웹앱 프로그래밍 방식의 변화
Html5 시대의 웹앱 프로그래밍 방식의 변화Html5 시대의 웹앱 프로그래밍 방식의 변화
Html5 시대의 웹앱 프로그래밍 방식의 변화yongwoo Jeon
 
Html5 앱과 웹사이트를 보다 빠르게 하는 50가지
Html5 앱과 웹사이트를 보다 빠르게 하는 50가지Html5 앱과 웹사이트를 보다 빠르게 하는 50가지
Html5 앱과 웹사이트를 보다 빠르게 하는 50가지yongwoo Jeon
 
Client side storage in html5
Client side storage in html5Client side storage in html5
Client side storage in html5yongwoo Jeon
 

More from yongwoo Jeon (10)

Web notification
Web notificationWeb notification
Web notification
 
Html5 시대의 웹앱 프로그래밍 방식의 변화
Html5 시대의 웹앱 프로그래밍 방식의 변화Html5 시대의 웹앱 프로그래밍 방식의 변화
Html5 시대의 웹앱 프로그래밍 방식의 변화
 
asm.js
asm.jsasm.js
asm.js
 
Html5 앱과 웹사이트를 보다 빠르게 하는 50가지
Html5 앱과 웹사이트를 보다 빠르게 하는 50가지Html5 앱과 웹사이트를 보다 빠르게 하는 50가지
Html5 앱과 웹사이트를 보다 빠르게 하는 50가지
 
CoffeeScript
CoffeeScriptCoffeeScript
CoffeeScript
 
Html5 use cases
Html5 use casesHtml5 use cases
Html5 use cases
 
Html5 performance
Html5 performanceHtml5 performance
Html5 performance
 
Client side storage in html5
Client side storage in html5Client side storage in html5
Client side storage in html5
 
Scriptable cache
Scriptable cacheScriptable cache
Scriptable cache
 
Whats jindo
Whats jindoWhats jindo
Whats jindo
 

Web Components 101 polymer & brick

  • 1. Web Components 101 Polymer & Brick 전용우
  • 2. Web Components? • 컴포넌트를 만드는 기술. • 기존의 자바스크립트 컴포넌트와 비슷함.
  • 3. 설정하기. • Chrome Canary (31+) • about:flags – Enable HTML Imports • HTML 가져오기 사용 – Enable Experimental JavaScript • 실험용 자바스크립트 사용 – Enable experimental Web Platform features • 실험용 웹 플랫폼 기능 사용 • Devtool – General - Show Shadow DOM
  • 5. Shadow DOM • 숨어있는 DOM subtree. • 캡슐화할 수 있음. • 외부와 context가 다르기 때문에 접근할 수 없음.
  • 6. Shadow DOM 만들기 <div id="temp"></div> <script> var host = document.querySelector('#temp'); var root = host.createShadowRoot(); root.innerHTML = ‘<b>안녕. Shadow DOM!</b>'; </script> #temp – shadow host shadow root shadow boundary <b>안녕. Shadow DOM</b>
  • 7. Content Element • root에 있는 엘리먼트의 위치를 이동시키는 엘리먼트. <div id="temp"> <div class="pre">앞으로</div> <div class="next">뒤로</div> </div> <script> var host = document.querySelector('#temp'); var root = host.webkitCreateShadowRoot(); root.innerHTML = '<content select=".next"></content>'+ '<b part="hi">안녕. Shadow DOM!</b>'+ '<content select=".pre"></content>'; </script>
  • 8. Content Element #temp – shadow host div.pre div.next shadow root b select=“.next” select=“.pre”
  • 9. Styling #1 • applyAuthorStyles, resetStyleInheritance – 외부에서 정의한 스타일이 적용 유무(false) – 상속되는 스타일의 적용 유무(true) • Part Pseudo Selector – 외부에서 Shadow DOM의 스타일을 변경할 수 있게 해주는 가상 선택자. (구. pseudo)
  • 10. Styling #2 • CSS Variable – 변수를 이용하여 Shadow DOM을 스타일링 가 능. (위젯 같은 데서 사용하기 좋음)
  • 11. Template • • • • • • 일반적인 자바스크립트 템플릿과 유사 HTML, CSS, JS 묶음. 사용하기 전에는 아무런 영향이 없음. 내부 리소스 등을 내려 받지 않음.(image) 파싱만 함. 스크립트 실행 안됨.
  • 12. Template 만들기 <div id="temp"> <div class="pre">이건 앞이지</div> <div class="next">이건 뒤지</div> </div> <template id="test"> <style type="text/css"> b{ color : var(preColor); } </style> <content select=".next"></content> <b part="hi">안녕. Shadow DOM!</b> <content select=".pre"></content> </template> <script> var host = document.querySelector('#temp'); var root = host.webkitCreateShadowRoot(); var temp = document.getElementById("test").content.cloneNode(true); root.appendChild(temp); </script>
  • 13. Custom Element • 개발자가 새로운 엘리먼트를 만들 수 있음. • Native 엘리먼트를 확장하여 만들 수 있음. • 이벤트나 속성들을 만들 수 있음.
  • 14. 등록하기 var XButton = document.register('x-button'); var xButton = new XButton(); //var xButton = document.createElement("x-button"); document.body.appendChild(xButton); <x-button></x-button> ※ element 태그를 이용하여 선언하는(declarative)방법으로 등록했지만 현재 spec에서 삭제.
  • 15. 속성/메서드 추가하기 var xButtonProto = Object.create(HTMLButtonElement.prototype,{ length : { get : function(){//length readonly 속성 지정 return this.textContent.length; } } }); xButtonProto.some = function(){ //method 추가 alert("some"); } var XButton = document.register('x-button',{ prototype: xButtonProto }); var xButton = new XButton(); xButton.textContent="테스트버튼입니다."; document.body.appendChild(xButton); xButton.some();//alert("some"); alert(xButton.length);// 9
  • 16. lifecycle 콜백 이벤트명 설명 createdCallback 생성될 때 enteredViewCallback document에 붙일 때 leftViewCallback document에서 때어 졌을 때 attributeChangedCallback (attributeName, oldVal, newVal) 속성이 변경될 때 (속성 명, 예전 값, 변경 값) xButtonProto.createdCallback = function(){ alert("생성"); } xButtonProto.enteredViewCallback = function(){ alert("삽입"); } xButtonProto.leftViewCallback = function(){ alert("삭제"); } xButtonProto.attributeChangedCallback = function(attr, oldAttr, newAttr){ alert(attr+"가 "+oldAttr+"에서 "+newAttr+"으로 변경"); }
  • 17. 가상 이벤트 생성 var xButtonProto = Object.create(HTMLButtonElement.prototype,{ createdCallback : { value : function(){ this.style.cssText="border:1px solid red; padding:10px;"; var key; this.addEventListener("mousedown",function(){ key = setTimeout(function(){ var event = new CustomEvent("longtap", {"detail": {"time":new Date()}}); this.dispatchEvent(event); key = null; }.bind(this),1000); }); this.addEventListener("mouseup",function(){ if(key){ clearTimeout(key); } }); } } }); var XButton = document.register('x-button',{ prototype: xButtonProto }); var xButton = new XButton(); xButton.textContent="버튼입니다."; xButton.addEventListener("longtap",function(e){ alert("longtap"); console.log(e); });
  • 18. HTML Import • Web Components을 적용하기 위한 방법 – import된 html은 표현되지 않음 – Cross-Origin Resource Sharing – 같은 document가 아님. <link rel="import" href="/path/to/some/import.html"> var link = document.querySelector('link[rel=import]'); var import = link.import; var templateButton= import.querySelector('template.button');
  • 20. 지원 현황 Chrome(m) Shadow DOM Template Custom Element HTML Import Chrome Firefox IE10+ Safari 6+ Safari(m)
  • 21. Web Components 라이브러리 • Polymer – 구글에서 개발 • Brick(with xTag) – 모질라에서 개발
  • 23. platform.js • • • • Web Components polyfills 라이브러리 Template의 기능이 추가.(bind, if, repeat…) html import 가능(불안함.) 기타 기능 제공 – Pointer Event – Web Animation – Node.bind
  • 24. polymer.js • web components기능을 확장 – 선언하여 등록 – Data-binding – 선언하여 함수 할당 ….. <polymer-element name="g-cool"> <template> <button on-click="{{buttonClick}}">{{name}}</button> </template> <script> Polymer('g-cool', { name : "mixed", buttonClick: function(event, detail, sender) { ... } }); </script> </polymer-element>
  • 25. 기타 Polymer로 만든 엘리먼트(컴포넌트) • Element – UI가 없고 기능만 있는 엘리먼트 – Ajax, localStorage…. • UI Element – UI가 있는 엘리먼트 – Accordion, Slider….
  • 26. 지원 현황 Chrome(m) Shadow DOM Template Custom Element HTML Import Chrome Firefox IE10+ Safari 6+ Safari(m)
  • 27. Brick • xTag을 기반으로 만든 라이브러리
  • 28. xTag • polyfill – document.register 사용가능 – shadow dom은 지원 안 함. – html import가능 • xTag – polyfill에서 유틸 기능들이 추가.
  • 29. Brick • xTag을 이용하여 만든 컴포넌트 – Flipbox, tooltip….
  • 30. 지원 현황 Chrome(m) Shadow DOM Template Custom Element HTML Import Chrome Firefox IE9+ Safari 6+ Safari(m)
  • 31. Why Web Components? • 컴포넌트가 동작하는 영역이 분리되어 성능이 좋음. • 모듈화 되어 있어 사용하기 좋음. • 높은 접근성. • 보다 시멘틱해짐. • 향후 컴포넌트 표준이 될 가능성이 높음. (AngularJS, EmberJS은 Web Components로 변경할 계획. Dart에서도 사용) • 웹 생태계에 많은 도움을 줄 것 같음.