What's new in the Web

Tomek Wytrębowicz

@tomalecpl

tomalec

Starcounter Tech Day 2016, 14 December, Stockholm, Sweden

Web Components

  • <template>
  • Shadow DOM
  • Custom Elements
  • HTML Imports
@tomalecpl

<template>

  • W3C Recommendation
  • Part of HTML 5.1
  • Browser support:

@tomalecpl

Shadow DOM

  • All browser vendors agreed
  • .. on V1
@tomalecpl

Shadow DOM V1

What's New in Shadow DOM v1 (by examples) hayato.io/2016/shadowdomv1/ by Hayato Ito - spec editor, Chromium dev

V0V1
<content select=".css[selector]">
<slot name="my-name">
+
<div slot="my-name">
element.createShadowRoot();
element.attachShadow({ mode: 'open' });
let olderShadowRoot = e.createShadowRoot();
let youngerShadowRoot = e.createShadowRoot(); 
Multiple shadow roots per element - not supported
Fallback content - not supported
<slot name='slot2'>
  <div id='fallback2'></div>
</slot>
@tomalecpl

Shadow DOM V1

What's New in Shadow DOM v1 (by examples) hayato.io/2016/shadowdomv1/ by Hayato Ito - spec editor

V0V1
#element::content .selector
#element::slotted(.selector)
#element /deep/ .selector
Deprecated
<slot>
becomes a scope of sequential focus navigation.
new methods and utilities: slot.assignedNodes(), Element.assignedSlot, Node.isConnected, Node.getRootNode(options)
@tomalecpl

Custom Elements

  • All browser vendors agreed
  • .. on V1 - again
@tomalecpl

Custom Elements v1

Reintroducing Custom Elements V1 webreflection.co.uk/blog/2016/08/21/custom-elements-v1 by Andrea Giammarchi - author of CEv1 polyfill

V0V1
document.registerElement('my-element', prototype);
customElements.define('my-element', Class);
document.registerElement('my-element', {
	/*...*/,
	extends: 'button'
});
customElements.define('my-element',
	class extends HTMLButtonElement {
		/*...*/
	},
	{extends: 'button'}
);
@tomalecpl

Custom Elements V1

V0V1
createdCallback = function(){/*...*/}
constructor(){ super(); /*...*/}

Doing any DOM related changes in constructor are strongly discouraged!
attachedCallback

detachedCallback
connectedCallback

disconnectedCallback
attributeChangedCallback
observed every attribute
observes only attributes declated in
static get observedAttributes() {
    return ['attrName'];
}
@tomalecpl

Custom Elements
is="v1"

  • Safari will not ship
    is
    in V1
  • Other vendors will
  • It's still in spec
  • It's still in polyfill
@tomalecpl

HTML Imports

  • Spec is on hold
  • Polyfills are still supported
  • Easy to polyfill
  • W3C wants to provide such feature
  • ... in cleaner form
  • ... co-op with ES6 modules
@tomalecpl

Polymer 2.0

  • Preview available
  • Build on top of WebComponents v1
  • Hybrid mode
  • 1.7 is forward compat. w/ V1
  • Data-binding is claimed to be fixed
  • Does not use
    is
    (
    <dom-bind>
    )
@tomalecpl

Migrations

  • Polymer elements via:
    1.7->Hybrid->2.0

    Hybrid should work with 1.7, 2.0, v0 and v1

  • Vanilla elements
    • Chrome runs v0 & v1
    • Polyfilled CE - Andrea's
      document-register-element
      should make v0 & v1 run side by side
    • Polyfilled SD - ?
  • SC will update WC.js@v1 & Polymer@2.0 only after all our elements will be ready
  • ... it will be available at branch/flag/upgrade appl
@tomalecpl

Custom (CSS) Properites

Editor’s Draft, 13 December 2016

  • CSS variables
  • Styling Custom Elements
# my-element's Shadow DOM
<style>
:host{
  --my-element-title-color: red;
}
.title {
  color: var(--my-element-title-color);
}
</style>
<h3 class="title">I'm a .title from Shadow DOM</h3>
<style>
  .some-class{
    --my-element-title-color: blue;
  }
</style>
<my-element class="some-class"></my-element>
@tomalecpl

CSS @apply

A Collection of Interesting Ideas, 18 September 2016

Polymer claims WD

tabatkins.github.io/specs/css-apply-rule/

  • CSS mixins
  • Styling Custom Elements ♥

:root,
.scope,
my-element{
  --my-button: {
	  min-width: 5.14em;
	  margin: 0 0.29em;
	  /* any rules */
	  padding: 0.7em 0.57em;
	  @apply --shadow-elevation-2dp;
  };
}

/* my-element Shadow DOM*/
.button {
  @apply --my-button;
}
@tomalecpl

::part()

idea in W3C WebPlatform WG

github.com/w3c/webcomponents/issues/300

  • Custom pseudo-element selector
  • SD (CE) exposing API for styling it's shadow children
<date-range-selector>
  <!-- #shadow-root -->
    <div id="container">
      <input part="start-date" id="start-date" type="date">
    </div>
  <!-- /shadow-root -->
</date-range-selector>
date-range-selector::part(start-date){
  /* normal styles */
}
@tomalecpl

Custom (HTML) Attributes

idea at W3C WebPlatform WG issues

  • Addresses is issue
  • Mixins for HTML Elements
class FooBarAttr {
  attachedCallback() {
    console.log('was created for first time');
  }

  changedCallback(newValue, oldValue) {
    console.log('it changed');
  }
}
customAttributes.define('foo-bar', FooBarAttr);
@tomalecpl

CSS Grid Layout

THE Way to create layouts

See my presenation from Meet.js Summit 2016

tomalec.github.io/talks youtu.be/8xQWMf0NH_s

Page layout

.fb-layout {
  display: grid;
  grid: "head head"   3em
        "nav  subnav" auto
        "nav  main"   1fr
        "foot foot"
       / 5em 1fr;
}

CSS Grid Framework, srsly?

.grid {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
}
@tomalecpl

CSS Grid Layout

W3C-logoW3C
ChromeChrome
OperaOpera
FirefoxFirefox
WebkitSafari TP ⚑ behind the flag
IE/EdgeIE/Edge Old spec supported for years.
"High priority" for upgrade
@tomalecpl

Thanks!

Tomek Wytrębowicz

tomalec

@tomalecpl

Specs:

Other:

This & other talks: