Skip to content

Commit 84db69d

Browse files
feat(core): add ability to close toasts via event incl. a result (#596)
* feat(core): add ability to close toasts via event incl. a result * chore: cleanup
1 parent d1c956b commit 84db69d

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

core/src/components/cat-notification/cat-notification.tsx

+27-11
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@ import { catI18nRegistry as i18n } from '../cat-i18n/cat-i18n-registry';
44
interface ToastRef {
55
toast?: {
66
showToast: () => void;
7-
hideToast: () => void;
7+
hideToast: (result?: any) => void;
88
};
99
}
1010

11+
type ToastResultRef = ToastRef & { result?: any };
12+
1113
export interface ToastOptions {
1214
/** The appearance mode of the notification. (Default: `dark`) */
1315
mode: 'dark' | 'light';
@@ -27,8 +29,8 @@ export interface ToastOptions {
2729
onAction: (toast: ToastRef) => void;
2830
/** Callback executed when the notification is clicked. Receives a reference to the notification as first argument. */
2931
onClick: (toast: ToastRef) => void;
30-
/** Callback executed when the notification is dismissed. Receives a reference to the notification as first argument. */
31-
onDismiss: (toast: ToastRef) => void;
32+
/** Callback executed when the notification is dismissed. Receives a reference to the notification as first argument and an optional result as second argument. */
33+
onDismiss: (toast: ToastRef, result?: any) => void;
3234
}
3335

3436
/**
@@ -49,17 +51,23 @@ export class CatNotificationService {
4951
return CatNotificationService.instance;
5052
}
5153

52-
show(content: string | Node, options?: Partial<ToastOptions>): () => void {
53-
const ref: ToastRef = {};
54+
show(content: string | Node, options?: Partial<ToastOptions>): (result?: any) => void {
55+
const ref: ToastResultRef = {};
5456
const toastContent = this.getNode(content, ref, options);
5557
const toastOptions = this.getOptions(toastContent, ref, options);
5658
const toast = Toastify(toastOptions);
57-
ref.toast = toast;
58-
toast.showToast();
59-
return () => toast.hideToast();
59+
ref.toast = {
60+
showToast: () => toast.showToast(),
61+
hideToast: (result?: any) => {
62+
ref.result = result;
63+
toast.hideToast();
64+
}
65+
};
66+
ref.toast.showToast();
67+
return (result?: any) => ref.toast?.hideToast(result);
6068
}
6169

62-
private getNode(content: string | Node, ref: ToastRef, options?: Partial<ToastOptions>): HTMLElement {
70+
private getNode(content: string | Node, ref: ToastResultRef, options?: Partial<ToastOptions>): HTMLElement {
6371
const template = document.createElement('template');
6472
template.innerHTML = `<div class="cat-toastify-wrapper">
6573
${options?.icon ? `<cat-icon class="cat-toastify-icon" icon="${options.icon}" size="l"></cat-icon>` : ''}
@@ -94,10 +102,15 @@ export class CatNotificationService {
94102
}
95103
}
96104

105+
node.addEventListener('cat-close', event => {
106+
const { detail } = (event as CustomEvent) || {};
107+
ref.toast?.hideToast(detail);
108+
});
109+
97110
return node;
98111
}
99112

100-
private getOptions(node: Node, ref: ToastRef, options?: Partial<ToastOptions>): Options {
113+
private getOptions(node: Node, ref: ToastResultRef, options?: Partial<ToastOptions>): Options {
101114
const [gravity, position] = (options?.placement?.split('-') ?? ['bottom', 'left']) as [
102115
Options['gravity'],
103116
Options['position']
@@ -112,7 +125,10 @@ export class CatNotificationService {
112125
className: options?.mode === 'light' ? 'cat-toastify' : 'cat-toastify cat-toastify-dark',
113126
stopOnFocus: true,
114127
onClick: () => options?.onClick?.(ref),
115-
callback: () => options?.onDismiss?.(ref),
128+
callback: () => {
129+
const { toast, result } = ref;
130+
options?.onDismiss?.({ toast }, result);
131+
},
116132
offset: {
117133
x: '1rem',
118134
y: '1rem'

0 commit comments

Comments
 (0)