Skip to content

Instantly share code, notes, and snippets.

@lacolaco
Last active August 20, 2018 06:59
Show Gist options
  • Save lacolaco/be772ea24ff30f616560f8f1871810dd to your computer and use it in GitHub Desktop.
Save lacolaco/be772ea24ff30f616560f8f1871810dd to your computer and use it in GitHub Desktop.
文化的な最低限度のWeb Component
export class AwesomeCounter extends HTMLElement {
constructor() {
super();
this.attachShadow({mode: 'open'});
this.state = {};
}
connectedCallback() {
this.setState({count: 0});
}
countUp() {
this.setState({ count: this.state.count + 1 });
}
setState(newState) {
this.state = Object.assign({}, this.state, newState);
this.render();
}
render() {
this.shadowRoot.innerHTML = `
<style>
.content { color: blue; }
</style>
<div>
<span>Count: ${this.state.count}</span>
<button id="countUpBtn">+1</button>
<div class="content"><slot></slot></div>
</div>
`;
this.shadowRoot.getElementById('countUpBtn').addEventListener('click', this.countUp.bind(this));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment