Created
July 12, 2024 16:27
-
-
Save cferdinandi/fe2c4a8788e60a57227fcf44d7fcd3c8 to your computer and use it in GitHub Desktop.
Light + Shadow DOM together... https://gomakethings.com
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 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: 0 auto; | |
max-width: 40em; | |
width: 88%; | |
} | |
</style> | |
</head> | |
<body> | |
<count-up></count-up> | |
<script> | |
// JavaScript | |
customElements.define('count-up', class extends HTMLElement { | |
/** | |
* The class constructor object | |
*/ | |
constructor () { | |
// Always call super first in constructor | |
super(); | |
// Set attributes | |
this.count = 0; | |
// Creates a shadow root | |
this.root = this.attachShadow({mode: 'open'}); | |
// Inject some HTML | |
this.btn = document.createElement('button'); | |
this.btn.textContent = 'Count up!'; | |
this.announce = document.createElement('div'); | |
this.announce.setAttribute('role', 'status'); | |
// Generate HTML | |
this.root.innerHTML = `<slot></slot>`; | |
this.render(); | |
this.root.append(this.announce); | |
this.append(this.btn); | |
// Event listener | |
this.btn.addEventListener('click', () => { | |
this.count++; | |
this.render(); | |
}); | |
} | |
render () { | |
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