-
-
Save jensgro/b53f3a36de16e0723fc89d4cce6b30b6 to your computer and use it in GitHub Desktop.
YouTube tutorial: https://youtu.be/vlmtVmWimD4
This file contains hidden or 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 lang="en"> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>Basic Setup</title> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <style type="text/css"> | |
| body { | |
| margin: 1em auto; | |
| max-width: 40em; | |
| width: 88%; | |
| } | |
| button { | |
| background-color: rebeccapurple; | |
| border: 0; | |
| border-radius: 0.25em; | |
| color: #fff; | |
| padding: 0.5em 1em; | |
| } | |
| [role="status"] { | |
| display: none; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <count-up></count-up> | |
| <script> | |
| customElements.define('count-up', class extends HTMLElement { | |
| /** | |
| * The class constructor object | |
| */ | |
| constructor () { | |
| // Always call super first in constructor | |
| super(); | |
| // Create a shadow root | |
| this.root = this.attachShadow({mode: 'closed'}); | |
| // Set attributes | |
| this.count = 0; | |
| // Create the button | |
| this.btn = document.createElement('button'); | |
| this.btn.textContent = 'Count up!'; | |
| // Create the announcement text | |
| this.announce = document.createElement('div'); | |
| this.announce.setAttribute('role', 'status'); | |
| this.announce.textContent = `Clicked ${this.count} times`; | |
| // Append HTML into the DOM | |
| this.append(this.btn); | |
| this.root.innerHTML = '<slot></slot>'; | |
| this.root.append(this.announce); | |
| // Event listener | |
| this.btn.addEventListener('click', this); | |
| } | |
| handleEvent () { | |
| this.count++; | |
| this.announce.textContent = `Clicked ${this.count} times`; | |
| } | |
| }); | |
| </script> | |
| </body> | |
| </html> |
This file contains hidden or 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 lang="en"> | |
| <head> | |
| <meta charset="utf-8"> | |
| <title>Basic Setup</title> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
| <style type="text/css"> | |
| body { | |
| margin: 1em auto; | |
| max-width: 40em; | |
| width: 88%; | |
| } | |
| button { | |
| background-color: rebeccapurple; | |
| border: 0; | |
| border-radius: 0.25em; | |
| color: #fff; | |
| padding: 0.5em 1em; | |
| } | |
| [role="status"] { | |
| display: none; | |
| } | |
| </style> | |
| </head> | |
| <body> | |
| <count-up></count-up> | |
| <script> | |
| customElements.define('count-up', class extends HTMLElement { | |
| /** | |
| * The class constructor object | |
| */ | |
| constructor () { | |
| // Always call super first in constructor | |
| super(); | |
| // Set attributes | |
| this.count = 0; | |
| // Create the button | |
| this.btn = document.createElement('button'); | |
| this.btn.textContent = 'Count up!'; | |
| // Create the announcement text | |
| this.announce = document.createElement('div'); | |
| this.announce.setAttribute('role', 'status'); | |
| this.announce.textContent = `Clicked ${this.count} times`; | |
| // Append HTML into the DOM | |
| this.append(this.btn); | |
| this.append(this.announce); | |
| // Event listener | |
| this.btn.addEventListener('click', this); | |
| } | |
| handleEvent () { | |
| this.count++; | |
| this.announce.textContent = `Clicked ${this.count} times`; | |
| } | |
| }); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment