Skip to content

Commit ba979b4

Browse files
committed
Closes #21 - opened toaster to custom notifications
1 parent c61817e commit ba979b4

File tree

12 files changed

+112
-39
lines changed

12 files changed

+112
-39
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,9 @@ toast-component.js
1313
toast-component.js.map
1414
notify.min.mjs
1515
notify.mjs
16+
types.d.ts
17+
notifier.d.ts
18+
notify.d.ts
19+
snackbar-component.d.ts
20+
toast-component.d.ts
21+
notify.min.js

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- type declaration files
13+
- new `append()` function
14+
- alternate CDN version
15+
1016
### Fixed
1117

1218
- autofocus bug ([#19](https://github.com/codewithkyle/notifyjs/issues/19))

README.md

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

3-
Notify.js is a lightweight (2.5kb) utility library for managing simple [snackbar](https://material.io/develop/web/components/snackbars/) and [toaster](https://www.carbondesignsystem.com/components/notification/code/) notifications.
3+
Notify.js is a lightweight (2.4kb) utility library for managing simple [snackbar](https://material.io/develop/web/components/snackbars/) and [toaster](https://www.carbondesignsystem.com/components/notification/code/) notifications.
44

55
## Installation
66

@@ -13,7 +13,15 @@ npm i --save @codewithkyle/notifyjs
1313
Or use the CDN version:
1414

1515
```javascript
16-
import { Notifier, snackbar, toast } from "https://cdn.jsdelivr.net/npm/@codewithkyle/notifyjs@2.1.1/notify.min.mjs";
16+
import { Notifier, snackbar, toast, append } from "https://cdn.jsdelivr.net/npm/@codewithkyle/notifyjs@3.0.0/notify.min.mjs";
17+
```
18+
19+
```html
20+
<script src="https://cdn.jsdelivr.net/npm/@codewithkyle/notifyjs@3.0.0/notify.min.js"></script>
21+
<script>
22+
Notifier.snackbar({ message: "This is a snackbar notification." });
23+
Notifier.toast({ title: "CDN Example", message: "This is a toast notification." });
24+
</script>
1725
```
1826

1927
## Usage
@@ -83,6 +91,24 @@ snackbar({
8391
});
8492
```
8593

94+
Append custom toast notifications:
95+
96+
```typescript
97+
import { append } from "@codewithkyle/notifyjs";
98+
99+
class CustomToasterElement extends HTMLElement {
100+
constructor(message){
101+
super();
102+
this.innerText = message;
103+
setTimeout(() => {
104+
this.remove();
105+
}, 5000);
106+
}
107+
}
108+
109+
append(new CustomToasterElement());
110+
```
111+
86112
---
87113

88114
## Snackbar Notification
@@ -103,9 +129,9 @@ interface SnackbarNotification {
103129
classes?: Array<string> | string;
104130
autofocus?: boolean;
105131
}>;
106-
force?: boolean;
132+
force?: boolean; // defaults to true
107133
classes?: Array<string> | string;
108-
autofocus?: boolean;
134+
autofocus?: boolean; // defaults to true
109135
}
110136
```
111137

@@ -139,15 +165,15 @@ type ToasterNotification = {
139165
icon?: string; // svg or img
140166
duration?: number; // in seconds
141167
classes?: string[];
142-
autofocus?: boolean;
168+
autofocus?: boolean; // defaults to true
143169
buttons?: Array<{
144170
label: string;
145171
callback: Function;
146172
ariaLabel?: string;
147173
classes?: Array<string> | string;
148174
autofocus?: boolean;
149175
}>;
150-
timer?: "vertical" | "horizontal";
176+
timer?: "vertical" | "horizontal" | null; // defaults to null
151177
};
152178
```
153179

build/cleanup.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ const files = [
1212
"snackbar-component.js.map",
1313
"toast-component.js",
1414
"toast-component.js.map",
15+
"notifier.d.ts",
16+
"notify.d.ts",
17+
"snackbar-component.d.ts",
18+
"toast-component.d.ts",
19+
"types.d.ts",
20+
"notify.min.js",
1521
];
1622

1723
function normalizePath(file){

package-lock.json

Lines changed: 7 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,20 @@
1212
"snackbar-component.js.map",
1313
"toast-component.js",
1414
"toast-component.js.map",
15-
"notify.min.mjs"
15+
"notify.min.mjs",
16+
"notifier.d.ts",
17+
"notify.d.ts",
18+
"snackbar-component.d.ts",
19+
"toast-component.d.ts",
20+
"types.d.ts",
21+
"notify.min.js"
1622
],
1723
"scripts": {
1824
"cleanup": "node ./build/cleanup.js",
1925
"compile": "tsc",
2026
"test": "npm run compile && npm run bundle && cp ./notify.min.mjs ./test/notify.min.mjs && serve ./test",
21-
"bundle": "rollup notify.js --file notify.mjs --format es --compact && terser notify.mjs -o notify.min.mjs -c && rm notify.mjs",
22-
"prerelease": "npm run cleanup && npm run compile && npm run bundle"
27+
"bundle": "esbuild ./notify.js --outfile=notify.min.mjs --format=esm --minify --bundle && esbuild ./notify.js --outfile=notify.min.js --format=iife --minify-whitespace --global-name=Notifier --bundle",
28+
"prerelease": "npm run cleanup && npm run compile && npm run bundle && cp ./src/types.d.ts ./types.d.ts"
2329
},
2430
"keywords": [
2531
"snackbar",
@@ -31,7 +37,7 @@
3137
"author": "Kyle Andrews",
3238
"license": "MIT",
3339
"devDependencies": {
34-
"rollup": "^2.37.1",
40+
"esbuild": "^0.10.0",
3541
"serve": "^11.3.2",
3642
"terser": "^5.5.1",
3743
"typescript": "^4.1.3"

src/notifier.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@ export class Notifier {
99
private shell: HTMLElement;
1010

1111
constructor() {
12-
this.shell = document.createElement("toaster-component");
13-
document.body.appendChild(this.shell);
12+
this.shell = document.body.querySelector("toaster-component");
13+
if (this.shell === null){
14+
this.shell = document.createElement("toaster-component");
15+
document.body.appendChild(this.shell);
16+
}
1417
this.snackbarQueue = [];
1518
this.toaster = [];
1619
this.time = performance.now();
@@ -130,4 +133,8 @@ export class Notifier {
130133
this.toaster.push(toast);
131134
this.shell.appendChild(toast.el);
132135
}
136+
137+
public append(el:HTMLElement){
138+
this.shell.appendChild(el);
139+
}
133140
}

src/notify.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { Notifier } from "./notifier";
22
import { SnackbarComponent } from "./snackbar-component";
33
import { ToastComponent } from "./toast-component";
4+
import { SnackbarNotification, ToasterNotification } from "./types";
45

56
const globalNotifier = new Notifier();
6-
const snackbar = globalNotifier.snackbar.bind(globalNotifier);
7-
const toast = globalNotifier.toast.bind(globalNotifier);
7+
const snackbar:(settings:Partial<SnackbarNotification>)=>void = globalNotifier.snackbar.bind(globalNotifier);
8+
const toast:(settings:Partial<ToasterNotification>)=>void = globalNotifier.toast.bind(globalNotifier);
9+
const append:(el:HTMLElement)=>void = globalNotifier.append.bind(globalNotifier);
810

911
// @ts-ignore
1012
customElements.define("snackbar-component", SnackbarComponent);
1113
// @ts-ignore
1214
customElements.define("toast-component", ToastComponent);
1315

14-
export { Notifier, snackbar, toast };
16+
export { Notifier, snackbar, toast, append };

src/snackbar-component.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export class SnackbarComponent extends HTMLElement {
44
private settings: SnackbarNotification;
55
constructor(snackbar: SnackbarNotification) {
66
super();
7+
console.log(snackbar);
78
this.settings = snackbar;
89
this.render();
910
}
@@ -44,11 +45,11 @@ export class SnackbarComponent extends HTMLElement {
4445
button.innerText = this.settings.buttons[i].label;
4546
button.dataset.index = `${i}`;
4647

47-
for (let k = 0; k < this.settings.buttons[i].classes.length; k++) {
48+
for (let k = 0; k < this.settings.buttons[i]?.classes?.length; k++) {
4849
button.classList.add(this.settings.buttons[i].classes[k]);
4950
}
5051

51-
if (this.settings.buttons[i].ariaLabel) {
52+
if (this.settings.buttons[i]?.ariaLabel) {
5253
button.setAttribute("aria-label", this.settings.buttons[i].ariaLabel);
5354
}
5455

src/toast-component.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ export class ToastComponent extends HTMLElement {
5656
button.innerText = this.settings.buttons[i].label;
5757
button.dataset.index = `${i}`;
5858

59-
for (let k = 0; k < this.settings.buttons[i].classes.length; k++) {
59+
for (let k = 0; k < this.settings.buttons[i]?.classes?.length; k++) {
6060
button.classList.add(this.settings.buttons[i].classes[k]);
6161
}
6262

63-
if (this.settings.buttons[i].ariaLabel) {
63+
if (this.settings.buttons[i]?.ariaLabel) {
6464
button.setAttribute("aria-label", this.settings.buttons[i].ariaLabel);
6565
}
6666

0 commit comments

Comments
 (0)