Created
June 14, 2018 19:54
-
-
Save dlockhart/2486a83fe626dbdb8ad1c21ee8c7b59f to your computer and use it in GitHub Desktop.
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
(function() { | |
var proto = Object.create(HTMLElement.prototype); | |
proto.createdCallback = function() { | |
var tmpl = document.createElement('template'); | |
tmpl.innerHTML = '<style>p {color:red; display: block;}</style><p><b>Foo</b> <content></content></p>'; | |
this.createShadowRoot() | |
.appendChild(document.importNode(tmpl.content, true)); | |
}; | |
proto.attachedCallback = function() { | |
return 'a'; | |
}; | |
proto.detachedCallback = function() { | |
console.log('d'); | |
}; | |
proto.getFoo = function() { | |
return 'f'; | |
}; | |
proto.setFoo = function(val) { | |
if (val) { | |
this.setAttribute('foo', ''); | |
} else { | |
this.removeAttribute('foo'); | |
} | |
}; | |
document.registerElement('test-element-1', { | |
prototype: proto | |
}); | |
})(); |
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
customElements.define('test-element-1', class extends HTMLElement { | |
constructor() { | |
super(); | |
let tmpl = document.createElement('template'); | |
tmpl.innerHTML = '<style>p {color:red; display: block;}</style><p><b>Foo</b> <slot></slot></p>'; | |
let shadowRoot = this.attachShadow({mode: 'open'}); | |
shadowRoot.appendChild(tmpl.content.cloneNode(true)); | |
} | |
connectedCallback() { | |
console.log('connected'); | |
} | |
disconnectedCallback() { | |
console.log('disconnected'); | |
} | |
static get observedAttributes() { | |
return ['foo']; | |
} | |
get foo() { | |
return this.hasAttribute('foo'); | |
} | |
set foo(val) { | |
if (val) { | |
this.setAttribute('foo', ''); | |
} else { | |
this.removeAttribute('foo'); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment