.title { font-size: 24px; } </style> <!-- Some behavior --> <script type="text/javascript"> </script> <!-- Some structure and content --> <div class="frame"> <h3 class="title">I am a title</h3> <p class="body">I am the body</p> </div> </template>
the template var tmpl = document.querySelector('#something'); // Add the cloned document fragment to the DOM document.body.appendChild( // Clone the template's document fragment document.importNode(tmpl.content, true) );
var elem = document.querySelector("#good-host"); // Create the shadow root var root = elem.createShadowRoot(); // Create a new element var h1 = document.createElement("h1"); h1.innerHTML = "Hello World"; // Append to the shadow root root.appendChild(h1);
<template id="something"> <h1>Hello, world!</h1> <p>This is part of my component</p> </template> <div id="good-host"></div> // Grab an element var elem = document.querySelector("#good-host"); // Create the shadow root var root = elem.createShadowRoot(); // Grab the template var tmpl = document.querySelector('#something'); // Add the cloned document fragment to the DOM root.appendChild( // Clone the template's document fragment document.importNode(tmpl.content, true) );
content • <content select=“”> to project selected content <template id="something"> <h1><content select=".h1"></content></h1> <p>This is part of my component</p> <content></content> </template> Examples: http://codepen.io/TrueNorth/pen/emPZMv
MegaButtonProto = Object.create(HTMLButtonElement.prototype); // Register your new element type var MegaButton = document.registerElement("mega-button", { prototype: MegaButtonProto, extends: "button" });
hooks • Can include CSS, JS and HTML • Content can be accessed from the outside • HTML Imported JS can reference either the main DOM or imported document fragment <link rel="import" href="myfile.html" /> // Get document fragment of an import var content = document .querySelector('link[rel="import"]') .import; // From a <script> included, // with the import access, // imported DOM document .currentScript .ownerDocument .querySelector('.abc');