Skip to content

Instantly share code, notes, and snippets.

@Domeee
Created June 18, 2016 14:01
Show Gist options
  • Save Domeee/8da6fbb450e6539347b47e07573132f3 to your computer and use it in GitHub Desktop.
Save Domeee/8da6fbb450e6539347b47e07573132f3 to your computer and use it in GitHub Desktop.
HTML5 web components custom element
<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