Skip to content

Commit 15a6369

Browse files
committed
Start rewriting toasts for PgOverlay system.
1 parent aa72100 commit 15a6369

File tree

4 files changed

+135
-9
lines changed

4 files changed

+135
-9
lines changed

src/pg/toast/README.md

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,42 @@
1-
# `<pg-toast>`
1+
# `PgToast`
22

3-
The `pg-toast` lives inside of the `pg-toasts` element.
3+
The `PgToast` utility will open a toast in the top right of the page.
44

55
```typescript
6-
import '@pictogrammers/components/pgToast.js';
6+
import PgToast from '@pictogrammers/components/pgToast';
77
```
88

9-
```html
10-
<pg-toast></pg-toast>
9+
```typescript
10+
PgToast.open({
11+
type: 'info',
12+
message: 'Loading...',
13+
loading: true
14+
});
15+
16+
// On Success
17+
this.handleToast.resolve({
18+
type: 'success',
19+
message: 'Saved',
20+
timeout: 5
21+
});
22+
// On Error
23+
this.handleToast.reject({
24+
type: 'error',
25+
message: 'Saved',
26+
timeout: 5
27+
});
28+
// Close open toast with no config
29+
disconnectedCallback() {
30+
this.handleToast?.resolve();
31+
}
1132
```
1233

13-
This is entirely a presentational component.
34+
| Prop | default | Description |
35+
| ---- | ------- | ----------- |
36+
| `type` | `info` | `info`, `warning`, `success`, `error` |
37+
| `message` | `''` | Message string. |
38+
| `timeout` | `undefined` | Timeout in seconds |
39+
40+
Toasts can be updated in place without adding to the queue by using the `handleToast` config.
41+
42+
Toasts with a `timeout` will automatically hide and any future `handleToast` executions will add to the queue as a new toast.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<div>
2+
<button part="toastInfo">Info</button>
3+
<button part="toastWarning">Warning</button>
4+
<button part="toastError">Error</button>
5+
<button part="toastLoading">Loading (mock 3 seconds)</button>
6+
<button part="toggleToast">Toggle Toast</button>
7+
</div>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import { Component, Part } from '@pictogrammers/element';
2+
import PgToast from '../../toast';
3+
4+
import template from './basic.html';
5+
6+
@Component({
7+
selector: 'x-pg-toasts-basic',
8+
template
9+
})
10+
export default class XPgToastsBasic extends HTMLElement {
11+
12+
@Part() $toastInfo: HTMLButtonElement;
13+
@Part() $toastWarning: HTMLButtonElement;
14+
@Part() $toastError: HTMLButtonElement;
15+
@Part() $toastLoading: HTMLButtonElement;
16+
@Part() $toastToggle: HTMLButtonElement;
17+
18+
connectedCallback() {
19+
this.$toastInfo.addEventListener('click', this.handleInfo.bind(this));
20+
this.$toastWarning.addEventListener('click', this.handleWarning.bind(this));
21+
this.$toastError.addEventListener('click', this.handleError.bind(this));
22+
this.$toastLoading.addEventListener('click', this.handleLoading.bind(this));
23+
this.$toastToggle.addEventListener('click', this.handleToggle.bind(this));
24+
}
25+
26+
handleInfo() {
27+
PgToast.open({
28+
type: 'info',
29+
message: 'Hello World! With really long content that wraps rows.',
30+
});
31+
}
32+
33+
handleWarning() {
34+
PgToast.open({
35+
type: 'warning',
36+
message: 'Hello World! Warning',
37+
});
38+
}
39+
40+
handleError() {
41+
PgToast.open({
42+
type: 'error',
43+
message: 'Hello World! Error',
44+
});
45+
}
46+
47+
async handleLoading() {
48+
const toast = await PgToast.open({
49+
type: 'info',
50+
message: 'Loading...',
51+
loading: true
52+
});
53+
setTimeout(() => {
54+
toast({
55+
type: 'success',
56+
message: 'Saved record.',
57+
timeout: 5
58+
});
59+
}, 3000);
60+
}
61+
62+
cacheToast: any;
63+
async handleToggle() {
64+
if (!this.cacheToast) {
65+
this.cacheToast = await PgToast.open({
66+
type: 'info',
67+
message: 'Loading...',
68+
loading: true
69+
});
70+
} else {
71+
this.cacheToast();
72+
this.cacheToast = null;
73+
}
74+
}
75+
76+
}

src/pg/toast/toast.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import { Component, Prop, Part } from '@pictogrammers/element';
22
import { uuid } from '../shared/uuid';
3-
import { removeToast } from '../shared/toast';
43

54
import template from './toast.html';
65
import style from './toast.css';
6+
import PgOverlay from '../overlay/overlay';
7+
8+
const toasts = [];
79

810
@Component({
911
selector: 'pg-toast',
1012
style,
1113
template
1214
})
13-
export default class PgToast extends HTMLElement {
15+
export default class PgToast extends PgOverlay {
1416
@Prop() loading: boolean = false;
1517
@Prop() message: string = '';
1618
@Prop() type: string = 'default';
@@ -22,11 +24,23 @@ export default class PgToast extends HTMLElement {
2224
@Part() $message: HTMLSpanElement;
2325
@Part() $loading: HTMLSpanElement;
2426

27+
static open(props: any = {}): Promise<any> {
28+
// ToDo: validate props
29+
super.open(props);
30+
const key = uuid();
31+
return Promise.resolve(function (config?: any) {
32+
const scopedKey = key;
33+
if (config === undefined) {
34+
this.toasts.find(t => t.key === scopedKey).remove();
35+
}
36+
});
37+
}
38+
2539
toasts: any[] = [];
2640

2741
connectedCallback() {
2842
this.$button.addEventListener('click', () => {
29-
removeToast(this.key);
43+
3044
});
3145
}
3246

0 commit comments

Comments
 (0)