Created
February 28, 2023 22:04
-
-
Save angusgrant/a8958f40576be45914c63d0ab0df5255 to your computer and use it in GitHub Desktop.
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> | |
<head> | |
<meta charset="utf-8"> | |
<title>Dice - Web Component</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> | |
<h1>Dice - Web Component</h1> | |
<roll-dice></roll-dice> | |
<roll-dice>Roll a D6</roll-dice> | |
<script> | |
//Extend the HTMLElement to create the web component | |
class RollDice extends HTMLElement { | |
/** | |
* The class constructor object | |
*/ | |
constructor () { | |
//Always call super first in constructor | |
super(); | |
const btnText = this.innerHTML.trim(); | |
this.innerHTML = `<p> | |
<button>${btnText ? btnText : 'Roll Dice'}</button> | |
</p> | |
<div aria-live="polite"></div> | |
` | |
} | |
} | |
if ('customElements' in window) { | |
customElements.define('roll-dice', RollDice); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment