Skip to content

Commit 76f4b4d

Browse files
committed
Updated readme
1 parent 873dddb commit 76f4b4d

File tree

1 file changed

+99
-3
lines changed

1 file changed

+99
-3
lines changed

README.md

Lines changed: 99 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ Or use the CDN version:
2424
import toaster from "https://cdn.jsdelivr.net/npm/@codewithkyle/notifyjs@4/dist/toaster.js";
2525
import snackbar from "https://cdn.jsdelivr.net/npm/@codewithkyle/notifyjs@4/dist/snackbar.js";
2626
import notifications from "https://cdn.jsdelivr.net/npm/@codewithkyle/notifyjs@4/dist/notifications.js";
27+
import sonner from "https://cdn.jsdelivr.net/npm/@codewithkyle/notifyjs@5/dist/sonner.js";
2728
```
2829

2930
## Usage
@@ -37,6 +38,9 @@ import notifications from "https://cdn.jsdelivr.net/npm/@codewithkyle/notifyjs@4
3738
1. [Toast Notification](#toast)
3839
1. [Toast Interface](#toast-interface)
3940
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)
4044

4145
### Global Manager
4246

@@ -89,6 +93,58 @@ toaster.push({
8993
});
9094
```
9195

96+
```typescript
97+
import sonner from "@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 = new CustomEvent("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+
import sonner from "@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+
92148
---
93149

94150
## Snackbar Notification
@@ -98,16 +154,18 @@ Snackbar notifications are great for quick one-off notifications that require an
98154
### Snackbar Interface
99155

100156
```typescript
101-
interface SnackbarNotification {
157+
type SnackbarNotification = {
102158
message: string;
103159
duration?: number; // in seconds
104160
closeable?: boolean;
105161
buttons?: Array<{
106162
label: string;
107-
callback: Function;
163+
callback?: Function;
108164
ariaLabel?: string;
109165
classes?: Array<string> | string;
110166
autofocus?: boolean;
167+
event?: string;
168+
eventData?: any;
111169
}>;
112170
force?: boolean; // defaults to true
113171
classes?: Array<string> | string;
@@ -148,10 +206,12 @@ type Notification = {
148206
autofocus?: boolean; // defaults to true
149207
buttons?: Array<{
150208
label: string;
151-
callback: Function;
209+
callback?: Function;
152210
ariaLabel?: string;
153211
classes?: Array<string> | string;
154212
autofocus?: boolean;
213+
event?: string;
214+
eventData?: any;
155215
}>;
156216
timer?: "vertical" | "horizontal" | null; // defaults to null
157217
};
@@ -203,3 +263,39 @@ type ToastNotification = {
203263
<output role="status">Custom toast message.</output>
204264
</toaster-component>
205265
```
266+
267+
## Sonner
268+
269+
Sonner notifications are great for simple temporary alerts.
270+
271+
### Sonner Interface
272+
273+
```typescript
274+
type SonnerNotification = {
275+
heading?: string,
276+
message?: string,
277+
duration?: number,
278+
classes?: Array<string>|string,
279+
button?: {
280+
callback?: Function,
281+
label: string,
282+
classes?: Array<string>|string,
283+
event?: string;
284+
eventData?: any;
285+
}
286+
};
287+
```
288+
289+
### Sonner HTML Structure
290+
291+
```html
292+
<sonner-component>
293+
<sonner-toast-component>
294+
<copy-wrapper>
295+
<h3>Example heading</h3>
296+
<p>This is an example sonner message.</p>
297+
</copy-wrapper>
298+
<button>Click me</button>
299+
</sonner-toast-component>
300+
</sonner-component>
301+
```

0 commit comments

Comments
 (0)