Skip to content

Instantly share code, notes, and snippets.

@shaunwallace
Last active August 29, 2015 14:09
Show Gist options
  • Save shaunwallace/ac115fbbffeb5bb509b7 to your computer and use it in GitHub Desktop.
Save shaunwallace/ac115fbbffeb5bb509b7 to your computer and use it in GitHub Desktop.
Custom Elements - using templates & shadow DOM
<template id="x-template">
<style>
h1 { color: #bada55; }
</style>
<h1>What you are reading came from a template and is part of a custom elements shadow DOM</h1>
</template>
var proto = document.create(HTMLElement.prototype, {
proto.createdCallback : {
value : function() {
var template = document.querySelector('#x-template');
var clone = document.importNode(template.content, true);
this.createShadowRoot().appendChild(clone);
console.log('createdCallback fired');
}
}
});
document.registerElement('x-element', {prototype : proto});
document.body.appendChild(document.createElement('x-element'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment