Last active
August 29, 2015 14:09
-
-
Save shaunwallace/ac115fbbffeb5bb509b7 to your computer and use it in GitHub Desktop.
Custom Elements - using templates & shadow DOM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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