You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -37,6 +38,9 @@ import notifications from "https://cdn.jsdelivr.net/npm/@codewithkyle/notifyjs@4
37
38
1.[Toast Notification](#toast)
38
39
1.[Toast Interface](#toast-interface)
39
40
1.[Toast HTML Structure](#toast-html-structure)
41
+
1.[Sonner Notification](#sonner)
42
+
1.[Sonner Interface](#sonner-interface)
43
+
1.[Sonner HTML Structure](#sonner-html-structure)
40
44
41
45
### Global Manager
42
46
@@ -89,6 +93,58 @@ toaster.push({
89
93
});
90
94
```
91
95
96
+
```typescript
97
+
importsonnerfrom"@codewithkyle/notifyjs/sonner";
98
+
sonner.push({
99
+
heading: "Sonner toast example",
100
+
message: "Heading and message are optional."
101
+
});
102
+
```
103
+
104
+
### Custom Events
105
+
106
+
If you are working with a server rendered web application using tools like [HTMX](https://htmx.org/) you can trigger events using [custom events](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent) dispatched on the `window`
107
+
108
+
```typescript
109
+
const event =newCustomEvent("notify:sonner", {
110
+
detail: {
111
+
heading: "Sonner Example",
112
+
message: `Example sonner toast message.`,
113
+
}
114
+
});
115
+
window.dispatchEvent(event);
116
+
```
117
+
118
+
The follow events are supported:
119
+
120
+
-`notify:sonner`
121
+
-`notify:alert`
122
+
-`notify:toast`
123
+
-`notify:snackbar`
124
+
125
+
The `detail` object accepts the same interfaces as the function versions (see below).
126
+
127
+
### Custom Event Callbacks
128
+
129
+
As of version 5 all callback interfaces support `event` and `eventData` properties. When the user interacts with but button a custom event will be fired on the `window`.
130
+
131
+
```typescript
132
+
importsonnerfrom"@codewithkyle/notifyjs/sonner";
133
+
sonner.push({
134
+
message: "Heading and message are optional.",
135
+
button: {
136
+
label: "Test",
137
+
event: "test-event",
138
+
eventData: "Hi mom!",
139
+
}
140
+
});
141
+
142
+
// received when the user clicks the button within the sonner notification
143
+
window.addEventListener("test-event", (e)=>{
144
+
alert(e.detail);
145
+
});
146
+
```
147
+
92
148
---
93
149
94
150
## Snackbar Notification
@@ -98,16 +154,18 @@ Snackbar notifications are great for quick one-off notifications that require an
98
154
### Snackbar Interface
99
155
100
156
```typescript
101
-
interfaceSnackbarNotification {
157
+
typeSnackbarNotification= {
102
158
message:string;
103
159
duration?:number; // in seconds
104
160
closeable?:boolean;
105
161
buttons?:Array<{
106
162
label:string;
107
-
callback:Function;
163
+
callback?:Function;
108
164
ariaLabel?:string;
109
165
classes?:Array<string> |string;
110
166
autofocus?:boolean;
167
+
event?:string;
168
+
eventData?:any;
111
169
}>;
112
170
force?:boolean; // defaults to true
113
171
classes?:Array<string> |string;
@@ -148,10 +206,12 @@ type Notification = {
148
206
autofocus?:boolean; // defaults to true
149
207
buttons?:Array<{
150
208
label:string;
151
-
callback:Function;
209
+
callback?:Function;
152
210
ariaLabel?:string;
153
211
classes?:Array<string> |string;
154
212
autofocus?:boolean;
213
+
event?:string;
214
+
eventData?:any;
155
215
}>;
156
216
timer?:"vertical"|"horizontal"|null; // defaults to null
0 commit comments