Last active
April 13, 2017 21:22
-
-
Save aleroddepaz/b149a90b1389b59bfb5e to your computer and use it in GitHub Desktop.
ES6 class for a counter widget
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Hello, ES6</title> | |
<script src="counter.js"></script> | |
</head> | |
<body> | |
<my-counter></my-counter> | |
</body> | |
</html> |
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
'use strict'; | |
class Counter extends HTMLElement { | |
createdCallback() { | |
this.innerHTML = `<div> | |
<button class="increase">+</button> | |
<button class="decrease">-</button> | |
<span>Counter: </span><span class="count"/> | |
</div>`; | |
this.count = 0; | |
this.querySelector('.increase').addEventListener('click', () => this.updateCount(1)); | |
this.querySelector('.decrease').addEventListener('click', () => this.updateCount(-1)); | |
} | |
attachedCallback() { | |
this.querySelector('.count').innerText = this.count; | |
} | |
updateCount(offset) { | |
this.count += offset; | |
this.querySelector('.count').innerText = this.count; | |
} | |
} | |
document.registerElement('my-counter', Counter); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment