introduction to web components

36
Introduction to Web Components HTML Templates, Custom Elements & Shadow DOM Fu Cheng @alexcheng1982

Upload: fu-cheng

Post on 10-May-2015

3.032 views

Category:

Technology


1 download

DESCRIPTION

Introduction to Web Components. Examples can be found at https://github.com/alexcheng1982/learn-web-components.

TRANSCRIPT

Page 1: Introduction to Web Components

Introduction to Web ComponentsHTML Templates, Custom Elements & Shadow DOM

Fu Cheng

@alexcheng1982

Page 2: Introduction to Web Components

2

Agenda

Software Components

HTML Templates

Custom Elements

Shadow DOM

HTML Imports

Polymer Project

Page 3: Introduction to Web Components

3

Web Components is

Component Model of the Web

Page 4: Introduction to Web Components

4

Components

http://www.flickr.com/photos/bdesham/2432400623

Page 5: Introduction to Web Components

5

Software Component

An individual software component is a software package, a web service, a web resource, or a module that encapsulates a set of related functions (or data).

Components are modular and cohesive.Components communicate with each other via interfaces.A component model is a definition of standards for

component implementation, documentation and deployment.– EJB, COM, COBRA

http://en.wikipedia.org/wiki/Software_component

Page 6: Introduction to Web Components

6

Software Component is for

Reuse

Sharing

http://www.flickr.com/photos/wheatfields/118700012/

Don't Reinvent the Wheels

Page 7: Introduction to Web Components

7

Components for the Web

We now have different components for the web– Dojo widgets

– jQuery plugins

– YUI widget

– Sencha components

Different frameworks have their own solutions

No common standards

Page 8: Introduction to Web Components

8

What's the Problem?

Bad encapsulation

http://www.flickr.com/photos/27898848@N06/3174611184

Page 9: Introduction to Web Components

9

In the Web We Have

HTML CSS JavaScript

StructureContent Presentation Behaviour

Page 10: Introduction to Web Components

10

After You Add a Component

You may introduce– Additional DOM nodes

• Statically inlined

• Dynamically created using JavaScript

– CSS rules

– JavaScript objects

These may conflict with your own application– Duplicated DOM element IDs

– Component's styles mingles with existing styles

– Conflicted global JavaScript object names

Page 11: Introduction to Web Components

11

Add a Twitter Badge to Your Page (1)

Go to https://twitter.com/about/resources/buttons

Select type

You get code like this

Page 12: Introduction to Web Components

12

Add a Twitter Badge to Your Page (2)

You get DOM like this

iframe is the primary tool we have for encapsulation

Page 13: Introduction to Web Components

13

Components Contribute to Current Page

HTML CSS JS

HTML CSS JS HTML CSS JS HTML CSS JS

Widget 1 Widget 2 Widget 3

Page 14: Introduction to Web Components

14

Web Components

Component model standard for the Web

W3C working draft (http://www.w3.org/standards/techs/components)

A very new standard

Page 15: Introduction to Web Components

15

Web Components

Web Components standards consist of– HTML Templates (http://www.w3.org/TR/html-templates/)

– Decorators

– Custom Elements (http://www.w3.org/TR/custom-elements/)

– Shadow DOM (http://www.w3.org/TR/shadow-dom/)

– HTML Imports (http://www.w3.org/TR/html-imports/)

Page 16: Introduction to Web Components

16

HTML Templates

Templates = basic structure + place holders

Dynamic web applications use templates to generate HTML fragments

Current template techniques– Inline invisible markup

• Set CSS style 'display:none'

– Embed HTML markup in string

• In JavaScript files

Page 17: Introduction to Web Components

17

HTML Templates

Client-side templates are widely used today– Server-side exposes REST API over JSON– Combine data with template strings– Use innerHTML to render

Problems– Invisible markup

• Resources are still loaded– Template strings

• XSS attacks• Not intuitive

Page 18: Introduction to Web Components

18

HTML Templates

<template> element

Declarative way to create HTML fragments

Use content attribute to access the fragment

Content of templates are not part of the document and are inactive– Not display

– Cannot query

– Resources not load

Move or clone template to current DOM tree to render

Page 19: Introduction to Web Components

19

Custom Elements

Create your own HTML elementsUse <element> to create

– <element name="x-my-element"><p>My Element</p></element>

Extend existing element– <element name="x-my-button" extends="button"></element>

How to use in the page– New element

• <x-my-element></x-my-element>– Extended element

• <button is="x-my-button>Push</button>

Page 20: Introduction to Web Components

20

Custom Elements

Support lifecyle callbacks– ready

– inserted

– removed

Use HTML Imports to import custom elements– <link rel="import" href="x-nice-image.html">

Page 21: Introduction to Web Components

21

Shadow DOM

Shadow DOM is the key of encapsulation within the DOM tree

Establish functional boundaries in a document tree

Page 22: Introduction to Web Components

22

Typical Web Application

Page 23: Introduction to Web Components

23

Shadow Tree

Shadow DOM allows elements in the document tree to host other DOM trees

A set of rules establish encapsulation boundaries between document tree and shadow trees

Page 24: Introduction to Web Components

24

Document Tree & Shadow Tree

Shadow root is the root of shadow tree

Page 25: Introduction to Web Components

25

Shadow Tree Rendered

When rendered, the shadow tree takes place of the shadow host's content

Page 26: Introduction to Web Components

26

Insertion Point

Insertion point is where shadow host's children are transposed when rendering

Page 27: Introduction to Web Components

27

Insertion Point Rendered

When rendered, shadow host's children are distributed to different insertion points

Page 28: Introduction to Web Components

28

Encapsulation

All nodes in the shadow tree are NOT accessible in shadow host's document

Node ids are NOT addressable in shadow host's document

Style sheets are NOT accessible using shadow host's document's CSSOM

Page 29: Introduction to Web Components

29

Encapsulation

Styles– CSS rules declared in an enclosing tree not apply in a shadow tree,

except when the apply-author-styles flag is set for this tree

– CSS selectors cannot cross the shadow boundary

– Use reset-style-inheritance flag to set inheritable CSS properties to the initial value

Use @host to add styles to shadow host

Page 30: Introduction to Web Components

30

Encapsulation

Events dispatched in the shadow tree may– Cross the shadow boundary or

– Be terminated at the shadow boundary

Events crossing the shadow boundary are retargetted– Event's information is adjusted to hide shadow tree

Page 31: Introduction to Web Components

31

Simple Sample

Page 32: Introduction to Web Components

32

Simple Sample

Use JavaScript to create a shadow host

Use <content> to specify insertion point

Shadow host's chidren and shadow tree are composited together when rendered

CSS selectors only match wholly inside or outside of the shadow tree

Style 'color: #f00' not applied to shadow tree

Page 33: Introduction to Web Components

33

HTML Imports

HTML Imports are a way to include and reuse HTML documents in other HTML documents.

<link rel="import" href="x-person-badge.html">

Page 34: Introduction to Web Components

34

Polymer Project

Polymer is a new type of library for the web, built on top of Web Components, and designed to leverage the evolving web platform on modern browsers.– Created by Google

– http://www.polymer-project.org/

– Announced in Google I/O 2013

Embrace latest standards

Provide polyfills for old browsers

Going to replace Web UI in Dart

Page 35: Introduction to Web Components

35

Polymer Architecture

Page 36: Introduction to Web Components

36

Examples

Web Components examples in my GitHub repository: https://github.com/alexcheng1982/learn-web-components