Skip to content

An opinionated toast component for React.

Add custom id (#51)

Emil Kowalski5d289d424098
diff --git a/src/state.ts b/src/state.ts
index 50f8a06..94c17ec 100644
--- a/src/state.ts
+++ b/src/state.ts
@@ -27,7 +27,7 @@ class Observer {
this.toasts = [...this.toasts, data];
};
- dismiss = (id?: number) => {
+ dismiss = (id?: number | string) => {
if (!id) {
this.toasts.forEach((toast) => {
this.subscribers.forEach((subscriber) => subscriber({ id: toast.id, dismiss: true }));
@@ -39,32 +39,32 @@ class Observer {
};
message = (message: string | React.ReactNode, data?: ExternalToast) => {
- const id = toastsCounter++;
+ const id = data?.id || toastsCounter++;
this.publish({ ...data, id, title: message });
return id;
};
error = (message: string | React.ReactNode, data?: ExternalToast) => {
- const id = toastsCounter++;
+ const id = data?.id || toastsCounter++;
this.publish({ ...data, id, type: 'error', title: message });
return id;
};
success = (message: string | React.ReactNode, data?: ExternalToast) => {
- const id = toastsCounter++;
+ const id = data?.id || toastsCounter++;
this.publish({ ...data, id, type: 'success', title: message });
return id;
};
promise = (promise: PromiseT, data?: PromiseData) => {
- const id = toastsCounter++;
+ const id = data?.id || toastsCounter++;
this.publish({ ...data, promise, id });
return id;
};
// We can't provide the toast we just created as a prop as we didn't creat it yet, so we can create a default toast object, I just don't know how to use function in argument when calling()?
- custom = (jsx: (id: number) => React.ReactElement, data?: ExternalToast) => {
- const id = toastsCounter++;
+ custom = (jsx: (id: number | string) => React.ReactElement, data?: ExternalToast) => {
+ const id = data?.id || toastsCounter++;
this.publish({ jsx: jsx(id), id, ...data });
};
}
@@ -73,7 +73,8 @@ export const ToastState = new Observer();
// bind this to the toast function
const toastFunction = (message: string | React.ReactNode, data?: ExternalToast) => {
- const id = toastsCounter++;
+ const id = data?.id || toastsCounter++;
+
ToastState.publish({
title: message,
...data,
diff --git a/src/types.ts b/src/types.ts
index d13d4d3..305d0f5 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -11,7 +11,7 @@ export type PromiseData = ExternalToast & {
};
export interface ToastT {
- id: number;
+ id: number | string;
title?: string | React.ReactNode;
type?: ToastTypes;
icon?: React.ReactNode;
@@ -38,7 +38,7 @@ export interface ToastT {
export type Position = 'top-left' | 'top-right' | 'bottom-left' | 'bottom-right' | 'top-center' | 'bottom-center';
export interface HeightT {
height: number;
- toastId: number;
+ toastId: number | string;
}
interface ToastOptions {
@@ -72,8 +72,10 @@ export enum SwipeStateTypes {
export type Theme = 'light' | 'dark';
export interface ToastToDismiss {
- id: number;
+ id: number | string;
dismiss: boolean;
}
-export type ExternalToast = Omit<ToastT, 'id' | 'type' | 'title'>;
+export type ExternalToast = Omit<ToastT, 'id' | 'type' | 'title'> & {
+ id?: number | string;
+};