Created
May 27, 2018 18:08
-
-
Save kyuwoo-choi/21b3cf680c67c3f15c3a20cee1a260db 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
class MyElement extends HTMLElement { | |
static get observedAttributes() {return ['lang']; } | |
constructor() { | |
super(); | |
// add shadow root in constructor | |
const shadowRoot = this.attachShadow({mode: 'open'}); | |
shadowRoot.innerHTML = ` | |
<style>div { background-color: #82b74b; }</style> | |
<div>yey</div> | |
`; | |
this._yey = shadowRoot.querySelector('div'); | |
} | |
attributeChangedCallback(attr, oldValue, newValue) { | |
if (attr == 'lang') { | |
let yey; | |
switch (newValue) { | |
case 'ko': | |
yey = '만세!'; | |
break; | |
case 'es': | |
yey = 'hooray!'; | |
break; | |
case 'jp'; | |
yey = '万歳!'; | |
break; | |
default: | |
yey = 'yey!'; | |
} | |
this._yey.innerText = yey; | |
} | |
} | |
yell() { | |
alert(this._yey.innerText); | |
} | |
} | |
window.customElements.define('my-element', MyElement); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment