Skip to content

Commit 34888b6

Browse files
committed
Added custom event support
1 parent 67a2cda commit 34888b6

File tree

4 files changed

+81
-7
lines changed

4 files changed

+81
-7
lines changed

src/notifications.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ class Notifications {
88
this.notifications = [];
99
this.time = performance.now();
1010
this.loop();
11+
window.addEventListener("notify:alert", (e:CustomEvent) => {
12+
if (e?.detail) this.push(e.detail);
13+
});
1114
}
1215

1316
private loop() {

src/snackbar.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ class Snackbar {
88
this.snackbarQueue = [];
99
this.time = performance.now();
1010
this.loop();
11+
window.addEventListener("notify:snackbar", (e:CustomEvent) => {
12+
if (e?.detail) this.snackbar(e.detail);
13+
});
1114
}
1215

1316
private loop() {

src/toaster.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ class Toaster {
88
this.toastQueue = [];
99
this.time = performance.now();
1010
this.loop();
11+
window.addEventListener("notify:toast", (e:CustomEvent) => {
12+
if (e?.detail) this.push(e.detail);
13+
});
1114
}
1215

1316
private loop() {

test/index.html

Lines changed: 72 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,20 @@
2020
<link href="./sonner.css" rel="stylesheet" />
2121
</head>
2222
<body>
23-
<div class="w-screen h-screen" flex="justify-center items-center">
24-
<button style="padding: 0 1rem;margin-right: 1rem;background-color: var(--blue-700);color: #fff;height: 36px;" class="js-snackbar">Test Snackbar</button>
25-
<button style="padding: 0 1rem;margin-right: 1rem;background-color: var(--blue-700);color: #fff;height: 36px;" class="js-notif">Test Notification</button>
26-
<button style="padding: 0 1rem;margin-right: 1rem;background-color: var(--blue-700);color: #fff;height: 36px;" class="js-custom-notif">Test Custom Notification</button>
27-
<button style="padding: 0 1rem;margin-right: 1rem;background-color: var(--blue-700);color: #fff;height: 36px;" class="js-toast">Test Toast</button>
28-
<button style="padding: 0 1rem;margin-right: 1rem;background-color: var(--blue-700);color: #fff;height: 36px;" class="js-sonner">Test Sonner</button>
23+
<div class="w-screen h-screen" flex="column wrap justify-center items-center">
24+
<div class="w-full mb-2" flex="justify-center items-center">
25+
<button style="padding: 0 1rem;margin-right: 1rem;background-color: var(--blue-700);color: #fff;height: 36px;" class="js-snackbar">Test Snackbar</button>
26+
<button style="padding: 0 1rem;margin-right: 1rem;background-color: var(--blue-700);color: #fff;height: 36px;" class="js-notif">Test Notification</button>
27+
<button style="padding: 0 1rem;margin-right: 1rem;background-color: var(--blue-700);color: #fff;height: 36px;" class="js-custom-notif">Test Custom Notification</button>
28+
<button style="padding: 0 1rem;margin-right: 1rem;background-color: var(--blue-700);color: #fff;height: 36px;" class="js-toast">Test Toast</button>
29+
<button style="padding: 0 1rem;margin-right: 1rem;background-color: var(--blue-700);color: #fff;height: 36px;" class="js-sonner">Test Sonner</button>
30+
</div>
31+
<div class="w-full" flex="justify-center items-center">
32+
<button style="padding: 0 1rem;margin-right: 1rem;background-color: var(--amber-700);color: #fff;height: 36px;" class="js-snackbar2">Test Snackbar</button>
33+
<button style="padding: 0 1rem;margin-right: 1rem;background-color: var(--amber-700);color: #fff;height: 36px;" class="js-notif2">Test Notification</button>
34+
<button style="padding: 0 1rem;margin-right: 1rem;background-color: var(--amber-700);color: #fff;height: 36px;" class="js-toast2">Test Toast</button>
35+
<button style="padding: 0 1rem;margin-right: 1rem;background-color: var(--amber-700);color: #fff;height: 36px;" class="js-sonner2">Test Sonner</button>
36+
</div>
2937
</div>
3038

3139
<script type="module">
@@ -34,6 +42,64 @@
3442
import notifications from "./dist/notifications.js";
3543
import sonner from "./dist/sonner.js";
3644

45+
let count = 0;
46+
47+
const sonner2 = document.body.querySelector(".js-sonner2");
48+
sonner2.addEventListener("click", ()=>{
49+
const event = new CustomEvent("notify:sonner", {
50+
detail: {
51+
message: `Example sonner toast message.`,
52+
}
53+
});
54+
window.dispatchEvent(event);
55+
});
56+
57+
const toast2 = document.body.querySelector(".js-toast2");
58+
toast2.addEventListener("click", ()=>{
59+
const event = new CustomEvent("notify:toast", {
60+
detail: {
61+
message: `Example toast message ${count++}.`,
62+
}
63+
});
64+
window.dispatchEvent(event);
65+
});
66+
67+
const alert2 = document.body.querySelector(".js-notif2");
68+
alert2.addEventListener("click", ()=>{
69+
const event = new CustomEvent("notify:alert", {
70+
detail: {
71+
title: "Example Toast Message",
72+
message: "This is what a toaster message might look like. Click the button to stack notifications.",
73+
buttons: [
74+
{
75+
label: "Reload",
76+
callback: ()=>{ location.reload(); },
77+
},
78+
],
79+
duration: 10,
80+
}
81+
});
82+
window.dispatchEvent(event);
83+
});
84+
85+
const snackbarButton2 = document.body.querySelector(".js-snackbar2");
86+
snackbarButton2.addEventListener("click", ()=>{
87+
const event = new CustomEvent("notify:snackbar", {
88+
detail: {
89+
message: "Example snackbar message.",
90+
autofocus: true,
91+
duration: Infinity,
92+
buttons: [
93+
{
94+
label: "Reload",
95+
callback: ()=>{ location.reload(); },
96+
},
97+
],
98+
}
99+
});
100+
window.dispatchEvent(event);
101+
});
102+
37103
const snackbarButton = document.body.querySelector(".js-snackbar");
38104
snackbarButton.addEventListener("click", () =>{
39105
snackbar({
@@ -88,7 +154,6 @@
88154
notifications.append(new CustomNotificationElement("Custom toaster notification example."));
89155
});
90156

91-
let count = 0;
92157
const toastButton = document.body.querySelector(".js-toast");
93158
toastButton.addEventListener("click", () => {
94159
toaster.push({

0 commit comments

Comments
 (0)