Skip to content

Instantly share code, notes, and snippets.

@leMaur
Created January 16, 2020 15:36
Show Gist options
  • Save leMaur/f94c6deaa80bde3d5f73e6669d0f361b to your computer and use it in GitHub Desktop.
Save leMaur/f94c6deaa80bde3d5f73e6669d0f361b to your computer and use it in GitHub Desktop.
Notification component
<!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>
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