Skip to content

Commit 9d52dfb

Browse files
author
Lewis Youl
committed
feat: add new toast component with icons
1 parent 8ca4aa6 commit 9d52dfb

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

app/javascript/controllers/toast_controller.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import { Controller } from 'stimulus';
22
import axios from 'axios';
33

44
export default class extends Controller {
5-
static targets = ["toast", "message"];
6-
static classes = ["notice", "alert"];
5+
static targets = ["toast", "message", "noticeIcon", "alertIcon"];
76

87
connect() {
98
this.element[this.identifier] = this
@@ -34,15 +33,35 @@ export default class extends Controller {
3433

3534
display(message, type = 'notice') {
3635
this.messageTarget.innerHTML = message
37-
this.element.classList.remove(this.noticeClass, this.alertClass)
38-
this.element.classList.add(this[`${type}Class`])
36+
this.displayIconForToastType(type)
37+
this.showToast()
38+
}
39+
40+
showToast() {
3941
this.toastTarget.classList.remove('hidden')
40-
setTimeout(() => this.hideToast(), 3000)
42+
43+
this.setToastTimeout()
4144
}
4245

4346
hideToast() {
4447
this.toastTarget.classList.add('hidden')
4548
}
49+
50+
displayIconForToastType(type) {
51+
this.iconTargets.forEach(this.hideIcon)
52+
53+
this[`${type}IconTarget`].classList.remove('hidden')
54+
}
55+
56+
hideIcon(iconTarget) {
57+
iconTarget.classList.add('hidden')
58+
}
59+
60+
setToastTimeout() {
61+
const toastTimeoutInMilliseconds = 10000
62+
63+
setTimeout(() => this.hideToast(), toastTimeoutInMilliseconds)
64+
}
4665

4766
get notice() {
4867
return this.data.get('notice')
@@ -51,4 +70,8 @@ export default class extends Controller {
5170
get alert() {
5271
return this.data.get('alert')
5372
}
73+
74+
get iconTargets() {
75+
return [this.noticeIconTarget, this.alertIconTarget]
76+
}
5477
}

app/views/shared/_toast.html.erb

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
<div
44
class="z-20 fixed bottom-0 left-0 right-0 mx-auto mb-4 max-w-sm w-full bg-white shadow-lg rounded-lg pointer-events-auto ring-1 ring-black ring-opacity-5 overflow-hidden hidden"
55
id="toast"
6-
data-toast-notice-class="''"
7-
data-toast-alert-class="''"
86
data-controller="toast"
97
data-toast-target="toast"
108
data-toast-notice="<%= notice %>"
@@ -13,20 +11,21 @@
1311
<div class="p-4">
1412
<div class="flex items-start">
1513
<div class="flex-shrink-0">
16-
<!-- Heroicon name: outline/check-circle -->
17-
<svg class="h-6 w-6 text-green-400" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
14+
<svg data-toast-target="noticeIcon" class="hidden h-6 w-6 text-green-400" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
1815
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z" />
1916
</svg>
17+
<svg data-toast-target="alertIcon" class="hidden w-6 h-6 text-red-400" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
18+
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z"></path>
19+
</svg>
2020
</div>
2121
<div class="ml-3 w-0 flex-1 pt-0.5">
22-
<p class="text-sm font-medium text-gray-900" data-toast-target="message">
23-
Successfully saved!
24-
</p>
22+
<p class="text-sm font-medium text-gray-900" data-toast-target="message"></p>
2523
</div>
2624
<div class="ml-4 flex-shrink-0 flex">
27-
<button class="bg-white rounded-md inline-flex text-gray-400 hover:text-gray-500 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500">
28-
<span class="sr-only">Close</span>
29-
<!-- Heroicon name: solid/x -->
25+
<button
26+
class="bg-white rounded-md inline-flex text-gray-400 hover:text-gray-500 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
27+
data-action="click->toast#hideToast"
28+
>
3029
<svg class="h-5 w-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
3130
<path fill-rule="evenodd" d="M4.293 4.293a1 1 0 011.414 0L10 8.586l4.293-4.293a1 1 0 111.414 1.414L11.414 10l4.293 4.293a1 1 0 01-1.414 1.414L10 11.414l-4.293 4.293a1 1 0 01-1.414-1.414L8.586 10 4.293 5.707a1 1 0 010-1.414z" clip-rule="evenodd" />
3231
</svg>

0 commit comments

Comments
 (0)