Created
August 21, 2017 09:06
-
-
Save kenchris/0188cac7467df2b9c03e777948d08fa2 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
<html> | |
<script type="module"> | |
import { html, render } from '/lit-html.js'; | |
class MyGreater extends HTMLElement { | |
static get observedAttributes() { return ['data-name']; } | |
constructor() { | |
super(); | |
this.attachShadow({mode: 'open'}); | |
this._name = null; | |
} | |
attributeChangedCallback(attr, oldValue, newValue) { | |
console.log(attr); | |
if (attr == 'data-name') { | |
this._name = newValue; | |
this.invalidate(); | |
} | |
} | |
render() { | |
return html` | |
Hi <b>${this._name ? this._name : "some body"}</b> | |
`; | |
} | |
connectedCallback() { | |
this.invalidate(); | |
} | |
invalidate() { | |
if (!this.needsRender) { | |
this.needsRender = true; | |
Promise.resolve().then(() => { | |
this.needsRender = false; | |
render(this.render(), this.shadowRoot); | |
}); | |
} | |
} | |
} | |
class FancyGreater extends HTMLElement { | |
constructor() { | |
super(); | |
this.attachShadow({mode: 'open'}); | |
} | |
render() { | |
return html` | |
<style> | |
div { | |
background-color: green; | |
} | |
</style> | |
<div> | |
<my-greeter data-name="fancy-pancy"></my-greeter> | |
</div> | |
`; | |
} | |
connectedCallback() { | |
this.invalidate(); | |
} | |
invalidate() { | |
if (!this.needsRender) { | |
this.needsRender = true; | |
Promise.resolve().then(() => { | |
this.needsRender = false; | |
render(this.render(), this.shadowRoot); | |
}); | |
} | |
} | |
} | |
customElements.define('my-greeter', MyGreater); | |
customElements.define('fancy-greeter', FancyGreater); | |
</script> | |
<body> | |
<my-greeter data-name="Kenneth">Hi someone</my-greeter> | |
<br> | |
<fancy-greeter></fancy-greeter> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Seems like
MyGreater
should beMyGreeter
. Same withFancyGreater
. Looks like cool stuff!