Last active
August 20, 2018 06:59
-
-
Save lacolaco/be772ea24ff30f616560f8f1871810dd to your computer and use it in GitHub Desktop.
文化的な最低限度のWeb Component
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
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