WEB
COMP
ONENTS
September 23rd, 2014
Mark Santiago
MWDT
Meetup #4
Housekeeping
-
Still looking for Speakers
- Have a new tool that's improve your workflow?
- Recently used a hot new framework or library on a project?
- Share your experience and help fellow devs!
-
Recently presented with an offer to merge with WWT.
- willing to hear opinions on merging
- pros, cons.
Web Components
ARE More than just one thing:

ARE terribly supported:

ARE PRETTY WONDERFUL
COMPLICATED

We're Gonna KEEP IT SIMPLE...

THIS WILL JUST BE A SIMPLIFIED PICTURE
OF SOMETHING POWERFUL AND COMPLEX

DOG
Let's Start !
So, what are Web Components?
- A new collection of Standards that are still moving W3C but already have some support in MODERN browsers.
- Give super bundling powers for markup.
- Markup and Style in CUSTOM(!) elements.
- What you created will render as you intended
- Safely tucked away from external JS and Global stylesheets.
HTML
CSS
Javascript
DOM
Shadow
KEEPS OUR COMPONENT COMPLETELY ENCAPSULATED
insertion points for content injection
Like I-FRAMES (But better).
Hidden SubTree
It's already in use...

<<< This is a custom Element hiding a shadow DOM
Crack it open in CHROME:


Shadow Host
Shadow Root
Child
Child
Child
Child
Shadow Boundary
<TEMPLATE>
Store Markup on the page to use later and clone for easy re-use
Completely Innert until activated
Place anywhere and grab later
Parsed but not rendered

Nothing would load:
No HTTP Request for that GIF!
Custom Element
YOU CAN DEFINE NEW ELEMENTS
-ABSTRACTS AWAY COMPLEXITY FROM END USER
-HAS UNIQUE SET OF LIFE CYCLE EVENTS
CAN EXTEND EXISTING ELEMENTS
<cats-rule>
<h1>Oh, YEAH!</h1>
</cats-rule>
<button>
Submit
</button>
<evil-button>
Resistance is FUTILE
</evil-button>
-LOGICALLY BUNDLE TOGETHER CUSTOM FUNCTIONALITY INTO A SINGLE TAG
Super Button
User Avatar
STAT-HOLDER
SINGLE-BILL
Ticket-DEET
// Create a prototype for a new element that extends HTMLElement
var ImgSliderProto = Object.create(HTMLElement.prototype);
// Setup Shadow DOM and clone template
ImgSliderProto.createdCallback = function() {
var root = this.createShadowRoot();
root.appendChild(document.importNode(tmpl.content, true));
};
// Register new element
var ImgSlider = document.registerElement('img-slider', {
prototype: ImgSliderProto
});
Using Custom Element with Web Components
HTML IMPORTs
<link rel="import" href="nameof-element.html">
That's about it...
Using it:
-Easy to bring a component to use on page
-A component can import more components
-Easy to distribute through bower or npm.
Using only one URL, you can package together a single relocatable bundle of web goodness for others to consume.
-html5Rocks
<link rel="stylesheet" href="custelem.css">
<link rel="stylesheet" href="fonts.css">
<script src="jquery.js"></script>
...
<!-- scaffolding markup -->
<template>
...
</template>
The file it's importing:
Putting it Together...

With an example from Rob Dobson
<div id="myCarousel" class="carousel slide">
<ol class="carousel-indicators">
<li data-target="#myCarousel" data-slide-to="0" class=""></li>
<li data-target="#myCarousel" data-slide-to="1" class=""></li>
<li data-target="#myCarousel" data-slide-to="2" class=""></li>
</ol>
<div class="carousel-inner">
<div class="item">
<img src="assets/img/bootstrap-mdo-sfmoma-01.jpg" alt="">
<div class="carousel-caption">
<h4>First Thumbnail label</h4>
<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
</div>
</div>
<div class="item active left">
<img src="assets/img/bootstrap-mdo-sfmoma-02.jpg" alt="">
<div class="carousel-caption">
<h4>Second Thumbnail label</h4>
<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
</div>
</div>
<div class="item next left">
<img src="assets/img/bootstrap-mdo-sfmoma-03.jpg" alt="">
<div class="carousel-caption">
<h4>Third Thumbnail label</h4>
<p>Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus. Nullam id dolor id nibh ultricies vehicula ut id elit.</p>
</div>
</div>
</div>
<a class="left carousel-control" href="#myCarousel" data-slide="prev">‹</a>
<a class="right carousel-control" href="#myCarousel" data-slide="next">›</a>
</div>
</div>
A Bootstrap Carousel widget
HOW WOULD IT LOOK AS A WEB ELEMENT?
<img-slider>
<img src="rock.jpg">
<img src="grooves.jpg">
<img src="arch.jpg">
<img src="sunset.jpg">
</img-slider>
(To the developer using it)
The Element file being imported would look like this:
<template>
<style>
//Our Styling
</style>
// what gets inserted into the SHADOW DOM
<div id="slider">
<input checked="" type="radio" name="slider" id="slide1" selected="false">
<input type="radio" name="slider" id="slide2" selected="false">
<input type="radio" name="slider" id="slide3" selected="false">
<input type="radio" name="slider" id="slide4" selected="false">
<div id="slides">
<div id="overflow">
<div class="inner">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/5689/rock.jpg">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/5689/grooves.jpg">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/5689/arch.jpg">
<img src="https://s3-us-west-2.amazonaws.com/s.cdpn.io/5689/sunset.jpg">
</div> <!-- .inner -->
</div> <!-- #overflow -->
</div>
<label for="slide1"></label>
<label for="slide2"></label>
<label for="slide3"></label>
<label for="slide4"></label>
</div>
</template>
And this:
// Polyfill support
HTMLElement.prototype.createShadowRoot =
HTMLElement.prototype.createShadowRoot ||
HTMLElement.prototype.webkitCreateShadowRoot ||
function() {};
// Add the template to the Shadow DOM
var tmpl = document.querySelector('template');
var host = document.querySelector('.img-slider');
var root = host.createShadowRoot();
root.appendChild(document.importNode(tmpl.content, true));
Example from CSS TRICKS ARTICLE BY ROB DOBSON
But is it practical?


Relax! There's always a way!
POLYMER

BRICK

By Google
By Mozilla
Both
- Allow you to build today
- Are striving for interoperability
a ton of polyfills
Top layer keeps you from having to use JS to declare custom element
Offers Data Binding
POLYMER IS:
AND MUCH MORE....
Has dependency management and concatination functionality
Aggressively tackling accessibility issues inherent with components.
Places to find more information:
THANKS!
WEB COMPONENTS
By Mark Santiago
WEB COMPONENTS
Web components are the future. I learned about them a few months ago and decided to share what i've learned so far with my fellow dev members of WMDT.
- 1,272