On Github pndewit / sgti-tm-webcomponents
<template>... does not have to be recreated each time ... Unused at load, instantiated later on at runtime
<template id="mytemplate"> <img src="" alt="great image"> <div class="comment"></div> </template>
Inside a table:
<table> <tr> <template id="cells-to-repeat"> <td>some content</td> </template> </tr> </table>
<template id="mytemplate"> <img src="" alt="great image"> <div class="comment"></div> </template>
var t = document.querySelector('#mytemplate'); t.content.querySelector('img').src = 'logo.png'; var clone = document.importNode(t.content, true); document.body.appendChild(clone);
var MyElement = document.registerElement('my-element');
is equivalent to...
var MyElement = document.registerElement('my-element', { prototype: Object.create(HTMLElement.prototype) });
document.createElement('myelement').__proto__ === HTMLUnknownElement.prototype document.createElement('my-element').__proto__ === HTMLElement.prototype
from HTML...
<my-element></my-element>
from JS...
document.body.appendChild(new MyElement());
var MyElement = document.registerElement('my-element', { prototype: Object.create(HTMLButtonElement.prototype), extends: 'button' });
from HTML...
<button is="my-element">
from JS...
var myElement = document.createElement('button', 'my-element');
or...
var myElement = new MyElement();
var FunctionalElement = document.registerElement('functional-element', { prototype: Object.create(HTMLElement.prototype, { five: { get: function() { return 5; } }, notify: { value: function() { alert('notify() called'); } } }) });var functionalElement = new FunctionalElement() functionalElement.notify() FunctionalElement.prototype.notify()
var MyElementPrototype = Object.create(HTMLButtonElement.prototype); MyElementPrototype.createdCallback = function () { this.textContent = "I'm an my-element button!"; }; var MyElement = document.registerElement('my-element', { prototype: MyElementPrototype, extends: 'button' }); var testdiv = document.querySelector('#CustomElementExample #testdiv'); testdiv.appendChild(new MyElement()); testdiv.appendChild(document.createElement('button', 'my-element'));
<div id="testdiv"><button is="my-element"></button></div>
<input id="slider-example" type="range">
shows...
document.getElementById("slider-example").firstChild;<button>Hello, world!</button>
var host = document.querySelector('button'); var root = host.createShadowRoot(); root.textContent = 'Bye world!';
<p>I am on the outside</p> <div> <style> p {color: red;} </style> <p>My name is:</p> <p>Patrick</p> </div>
I am on the outside
My name is:
Patrick
<p>I am on the outside</p> <p id="name-tag">Patrick</p> <template id="my-template"> <style> p {color: red;} </style> <p>My name is:</p> <p><content></content></p> </template>
var shadow = document.querySelector('#name-tag').createShadowRoot(); var template = document.querySelector('#my-template'); var clone = document.importNode(template.content, true); shadow.appendChild(clone);
I am on the outside
Patrick
My name is:
<div id="name-tag"> <span class="first">Bob</span> <span>B. Love</span> </div> <template id="my-template"> <p>First name: <content select=".first"></content></p> <p>Last name: <content select="span"></content></p> </template>
First name:
Last name:
<script src>
<link rel="stylesheet">
<img>
...
<script type="text/html">
<link rel="import" href="/path/to/imports.html">
<link rel="import" href="bootstrap.html">
bootstrap.html could then be something like:
<link rel="stylesheet" href="bootstrap.css"> <link rel="stylesheet" href="fonts.css"> <script src="jquery.js"></script> <script src="bootstrap.js"></script> <script src="bootstrap-tooltip.js"></script> <script src="bootstrap-dropdown.js"></script> ...