Skip to content

Commit 2ba67e4

Browse files
author
Kyle Andrews
committed
Updates readme & inject newest slice of toast at the top
1 parent b2872d0 commit 2ba67e4

File tree

3 files changed

+40
-59
lines changed

3 files changed

+40
-59
lines changed

README.md

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Notify.js
22

3-
Notify.js is a utility library helping to manage simple [snackbar notifications](https://material.io/develop/web/components/snackbars/). Checkout the [live demo](https://components.codewithkyle.com/snackbars/dark-snackbar) to see the library in action.
3+
Notify.js is a hyper-lightweight utility library helping to manage simple [snackbar](https://material.io/develop/web/components/snackbars/) and [toaster](https://www.carbondesignsystem.com/components/notification/code/) notifications.
44

55
## Installation
66

@@ -14,41 +14,44 @@ npm i --save @codewithkyle/notifyjs
1414

1515
There are two ways to use this package. You can create a Notification Manager or use the global manager. Each manager has a queue and new notifications are placed in the queue in the order that they're requested. The queue can be skipped by settings the `force` value to true.
1616

17-
### Global Manager
17+
### Notification Manager
1818

19-
Import the global `notify()` function:
19+
Import the manager:
2020

2121
```typescript
22-
import { notify } from "@codewithkyle/notifyjs";
22+
import { Notifier } from "@codewithkyle/notifyjs";
23+
const notifier = new Notifier();
2324
```
2425

25-
Submit a notification to the global manager:
26+
Create a snackbar or toast notification:
2627

2728
```typescript
28-
notify({
29-
message: "All notifications require a message",
29+
notifier.snackbar({
30+
message: "All snackbar notifications require a message",
31+
});
32+
notifier.toast({
33+
title: "Toast notificaitons require a title",
34+
message: "And they require a message",
3035
});
3136
```
3237

33-
### Custom Manager
34-
35-
Import the `NotificationManager` class:
36-
37-
```typescript
38-
import { NotificationManager } from "@codewithkyle/notifyjs";
39-
```
38+
### Global Manager
4039

41-
Create a new instance of the class:
40+
Import the notification type:
4241

4342
```typescript
44-
const customManager = new NotificationManager();
43+
import { snackbar, toast } from "@codewithkyle/notifyjs";
4544
```
4645

47-
Call the `notify()` method:
46+
Create a notification:
4847

4948
```typescript
50-
customManager.notify({
51-
message: "All notifications require a message",
49+
notifier.snackbar({
50+
message: "All snackbar notifications require a message",
51+
});
52+
notifier.toast({
53+
title: "Toast notificaitons require a title",
54+
message: "And they require a message",
5255
});
5356
```
5457

@@ -57,7 +60,7 @@ customManager.notify({
5760
```typescript
5861
interface NotificationOptions {
5962
message: string;
60-
duration?: number;
63+
duration?: number; // in seconds
6164
closeable?: boolean;
6265
buttons?: Array<{
6366
label: string;
@@ -70,17 +73,6 @@ interface NotificationOptions {
7073
}
7174
```
7275

73-
### Duration
74-
75-
The duration value can be set to `Infinity` if a users interaction is required. Otherwise enter the number of seconds the notification should be displayed for.
76-
77-
```typescript
78-
notify({
79-
message: "This notification will last 3 seconds",
80-
duration: 3,
81-
});
82-
```
83-
8476
### User Interaction
8577

8678
Notify.js also allows for user interactions via a button element. The action requires a custom label for the button along with a callback function that will be called when the `click` event is fired on the button.
@@ -99,17 +91,6 @@ notify({
9991
});
10092
```
10193

102-
### Closeable
103-
104-
Notifications can be closeable by setting the `closeable` value to true.
105-
106-
```typescript
107-
notify({
108-
message: "The user will have to close this notification",
109-
closeable: true,
110-
});
111-
```
112-
11394
### HTML Structure
11495

11596
```html
@@ -131,7 +112,7 @@ type ToasterNotification = {
131112
title: string;
132113
message: string;
133114
closeable?: boolean;
134-
icon?: string; // svg
115+
icon?: string; // svg or img
135116
duration?: number; // in seconds
136117
classes?: string[];
137118
};
@@ -155,7 +136,3 @@ type ToasterNotification = {
155136
</toast-component>
156137
</toaster-component>
157138
```
158-
159-
## Stylesheets
160-
161-
This library doesn't provide/force any CSS, for a material design styled snackbar notification [click here](https://github.com/codewithkyle/notifyjs/blob/master/test/snackbar.css).

src/notifier.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,18 @@ export class Notifier {
155155

156156
toast.el = new ToastComponent(toast as ToasterNotification);
157157
this.toaster.push(toast as ToasterNotification);
158-
document.body.appendChild(toast.el);
158+
159+
let shell = document.body.querySelector("toaster-component") || null;
160+
if (!shell) {
161+
shell = document.createElement("toaster-component");
162+
document.body.appendChild(shell);
163+
}
164+
165+
const lastSlice = shell.querySelector("toast-component") || null;
166+
if (!lastSlice) {
167+
shell.insertBefore(toast.el, lastSlice);
168+
} else {
169+
shell.appendChild(toast.el);
170+
}
159171
}
160172
}

tsconfig.json

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,8 @@
1010
"module": "ESNext",
1111
"moduleResolution": "node",
1212
"declaration": true,
13-
"lib": [
14-
"DOM",
15-
"ES2019"
16-
]
13+
"lib": ["DOM", "ES2019"]
1714
},
18-
"exclude": [
19-
"./node_modules",
20-
"./docs"
21-
],
22-
"include": [
23-
"./src",
24-
]
15+
"exclude": ["./node_modules"],
16+
"include": ["./src"]
2517
}

0 commit comments

Comments
 (0)