Last active
November 16, 2018 17:27
-
-
Save asunar/27148e5200f09b60a8a4efa2a0bb3f53 to your computer and use it in GitHub Desktop.
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
<html> | |
<head> | |
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> | |
</head> | |
<body> | |
<p>Web Components Demo | |
<div> | |
<button is="hey-there-button" name="World">Howdy</button> | |
</div> | |
<script> | |
class GreetingButton extends HTMLButtonElement { | |
constructor() { | |
super(); | |
this.name = 'Stranger'; | |
} | |
connectedCallback() { | |
//called when an element is appended to a document | |
//may happen more than once ex. if elements is moved, removed and re-added | |
this.addEventListener('click', e=>alert(`Hello ${this.name}!`)); | |
} | |
attributeChangedCallback(attrName, oldValue, newValue) { | |
if (attrName === 'name') { | |
if (newValue) { | |
this.name = newValue; //too much recursion | |
} else { | |
this.name = 'Stranger'; | |
} | |
} | |
} | |
} | |
GreetingButton.observedAttributes = ['name'] | |
customElements.define('hey-there-button', GreetingButton, { | |
extends: 'button' | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment