diff --git a/structure-and-scale/dice-component-interactivity/index.html b/structure-and-scale/dice-component-interactivity/index.html index 9804289..40206d2 100644 --- a/structure-and-scale/dice-component-interactivity/index.html +++ b/structure-and-scale/dice-component-interactivity/index.html @@ -4,13 +4,15 @@ +
- +
- + `; + + // Get result element + this.result = this.querySelector('[dice-result]') + + // LIsten for events + this.addEventListener('click', this); } - connectedCallback () { - // Attach a click event listener to the button - let btn = this.querySelector('button'); - if (!btn) return; + /** + * Show a status message in the form + * @param {String} msg The message to display + */ + showStatus (msg) { + // Create a notification + let notification = document.createElement('div'); + notification.setAttribute('role', 'status'); + + // Inject it into the DOM + this.result.append(notification); + + // Add text after it's in the UI + setTimeout(function () { + notification.textContent = msg; + }, 1); + + // Remove it after 4 seconds + setTimeout(function () { + notification.remove(); + }, NOTIFICATION_TIMEOUT); - btn.addEventListener('click', this); } - disconnectedCallback () { - - // Remove the click event listener from the button - let btn = this.querySelector('button'); - if (!btn) return; - - btn.removeEventListener('click', this); + /** + * Handle events + * @param {Event} event The event object + */ + handleEvent (event) { + this[`on${event.type}`](event); + } + /** + * Handle the click event + * @param {Event} event The event object + */ + onclick (event) { + this.showStatus(`You rolled a ${this.#dice.roll()}`); } - handleEvent (event) { - let messageEl = this.querySelector('div'); - messageEl.innerHTML = `You rolled a ${this.#dice.roll()}
`; - } - } diff --git a/structure-and-scale/dice-component-interactivity/style.css b/structure-and-scale/dice-component-interactivity/style.css new file mode 100644 index 0000000..8105279 --- /dev/null +++ b/structure-and-scale/dice-component-interactivity/style.css @@ -0,0 +1,7 @@ +dice-button [role="status"] { + background-color: #202b38; + border: 1px solid #e5e5e5; + border-radius: 0.25em; + padding: 0.6rem 1rem; + margin-top: 0.5rem; +}