Created
January 16, 2020 15:36
-
-
Save leMaur/f94c6deaa80bde3d5f73e6669d0f361b to your computer and use it in GitHub Desktop.
Notification component
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> | |
<body> | |
<notification-p></notification-p> | |
<h1>Notification component</h1> | |
<p>You can also notify a message via javascript</p> | |
<pre>$ window.notify('Your message')</pre> | |
</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
export default class Notification extends HTMLElement { | |
constructor() { | |
super(); | |
this.render = () => { | |
this.setAttribute('role', 'status'); | |
this.setAttribute('aria-live', 'polite'); | |
} | |
} | |
connectedCallback() { | |
this.render(); | |
window.notify = (message) => { | |
let note = document.createElement('p'); | |
note.innerHTML = message; | |
this.appendChild(note); | |
window.setTimeout(() => { | |
this.removeChild(note); | |
}, 5000); | |
}; | |
} | |
attributeChangedCallback() { | |
this.render(); | |
} | |
} | |
if ('customElements' in window) { | |
customElements.define('notification-p', Notification) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment