Created
July 17, 2020 18:32
-
-
Save rjcorwin/779d94dfc886723c2967a6f5f56beecd to your computer and use it in GitHub Desktop.
Basic web component example
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 MyCounter extends HTMLElement { | |
constructor() { | |
super(); | |
this.count = 0; | |
} | |
render() { | |
this.innerHTML = ` | |
<style> | |
my-counter { | |
font-size: 200%; | |
} | |
my-counter span { | |
width: 4rem; | |
display: inline-block; | |
text-align: center; | |
} | |
my-counter button { | |
width: 64px; | |
height: 64px; | |
border: none; | |
border-radius: 10px; | |
background-color: seagreen; | |
color: white; | |
} | |
</style> | |
<button id="dec">-</button> | |
<span>${this.count}</span> | |
<button id="inc">+</button> | |
`; | |
this.querySelector('#inc').addEventListener('click', () => this.inc()); | |
this.querySelector('#dec').addEventListener('click', () => this.dec()); | |
} | |
inc() { | |
this.count++; | |
this.render(); | |
} | |
dec() { | |
this.count--; | |
this.render(); | |
} | |
connectedCallback() { | |
this.render() | |
} | |
} | |
customElements.define('my-counter', MyCounter); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment