Skip to content

Instantly share code, notes, and snippets.

@kyuwoo-choi
Created May 27, 2018 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyuwoo-choi/21b3cf680c67c3f15c3a20cee1a260db to your computer and use it in GitHub Desktop.
Save kyuwoo-choi/21b3cf680c67c3f15c3a20cee1a260db to your computer and use it in GitHub Desktop.
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