Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Web Components: Getting Started

Web Components: Getting Started

An introduction to Web Components, starting with the Shadow DOM. Presented at State of the Browser 3, 20 April 2013. #sotb3

Peter Gasston

April 20, 2013
Tweet

More Decks by Peter Gasston

Other Decks in Technology

Transcript

  1. Shadow root var el = document.getElementById('foo'), newRoot = el.createShadowRoot(), newEl

    = document.createElement('h2); newEl.textContent = 'Shadow world!'; newRoot.appendChild(newEl);
  2. .getDistributedNodes() var newRoot = foo.createShadowRoot(), newEls = '<h1>OK</h1><content />', rootNodes

    = newRoot.querySelector('content') .getDistributedNodes(); console.log(rootNodes[1].textContent); <div id="foo"> <p>GO</p> </div> > GO
  3. The problem <div class="foo" /> .foo { color: #f00; }

    <div class="foo" /> .foo { color: #00f; } (^0_0^) (^_^;)
  4. Encapsulation 1. Everything the thing needs is contained within itself.

    2. The thing doesn't interfere with things outside itself, and vice versa.
  5. Scoped styles <div> <style scoped> h1 { color: #f00; }

    </style> <h1>I am red</h1> </div> <h1>I am not</h1>
  6. <template> <div> <style scoped> h1 { color: #f00; } </style>

    <content></content> <h2>Hello world!</h2> </div> </template> Scoped styles
  7. The pseudo attribute var newRoot = el.createShadowRoot(), newEl = document.createElement('h2');

    newEl.pseudo = 'x-foo'; newEl.textContent = 'Shadow world!'; newRoot.appendChild(newEl);
  8. Decorators <h1>Front End United</h1> h1 { decorator: url(#foo); } <div>

    <h1>Front End United</h1> <h2>Hello world!</h2> </div>