Created
June 18, 2016 14:01
-
-
Save Domeee/8da6fbb450e6539347b47e07573132f3 to your computer and use it in GitHub Desktop.
HTML5 web components custom element
This file contains 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> | |
<style> | |
</style> | |
<div> | |
</div> | |
</template> | |
<script> | |
(function() { | |
// webcomponents.js Polyfill used | |
var template = document._currentScript.ownerDocument.querySelector('template'); | |
var proto = Object.create(HTMLElement.prototype); | |
proto.createdCallback = function() { | |
// Deep clone | |
var clone = document.importNode(template.content, true); | |
// Add the template contents to the shadow DOM | |
this.createShadowRoot().appendChild(clone); | |
// Or add the template contents to the DOM | |
this.appendChild(clone); | |
}; | |
proto.attachedCallback = function() { | |
console.log('element attached'); | |
}; | |
proto.detachedCallback = function() { | |
console.log('element detached'); | |
}; | |
proto.attributeChangedCallback = function(attrName, oldVal, newVal) { | |
console.log(`element attribute ${attrName} value changed from ${oldVal} to ${newVal}`); | |
}; | |
document.registerElement('hubi-widget', { | |
prototype: proto | |
}); | |
})(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment