brownbag on basics of web components
TRANSCRIPT
CUSTOM ELEMENTREGISTERING NEW ELEMENTS
var XFoo = document.registerElement('x-foo');
document.body.appendChild(new XFoo());
CUSTOM ELEMENTEXTENDING ELEMENTS
var XFooProto = Object.create(HTMLElement.prototype);
var XFooExtended = document.registerElement('x-foo-extended', {
prototype: XFooProto,
extends: 'x-foo'
});
CUSTOM ELEMENTINSTANTIATE ELEMENTS
Declare them:
<x-foo></x-foo>
Create DOM in JS:
var xFoo = document.createElement(‘x-foo’); (= var xFoo = new XFoo())
xFoo.addEventListener('click', function(e) {
alert('Thanks!');
});
document.body.appendChild(xFoo);
TEMPLATES BEFORE HTML5#1: Offscreen DOM
✓ Using DOM
✓ Nothing gets rendered
‣ Network request is still made for the image
‣ Difficult to scope CSS rules for the template
TEMPLATES BEFORE HTML5#2: Overloading script
✓ Nothing is rendered
✓ Browser doesn’t parse the content as it doesn’t recognise the mime type
‣ Security issues. Runtime string parsing of user supplied data can easily lead to XSS vulnerabilities.
TEMPLATES
✓ Content is effectively inert until activated
✓ Throw in any content (audio, script, style, etc) inside template. There won’t be a side effect until the template is used
✓ Content is considered not to be in the document
HTML IMPORT
• Distribute HTML/CSS/JS in a single bundle
• Manage dependencies - resources are automatically de-duped by leveraging browser caching
• Choose what you need on a page.
PUBLISH WEB COMPONENTS
• Web components are not website specific.
• HTML, CSS and script are all packaged in a single file - loaded via HTML import
• http://customelements.io
WEB
• Only on evergreen browsers.
• Most of them are experimental.
• http://builtwithpolymer.org/
MOBILE/DESKTOP
• Crosswalk project - run HTML5 apps on iOS & Android
• node-webkit - app runtime based on Chronium & node.js. Distribute to Mac, Linux and Windows.