Skip to content

An opinionated toast component for React.

refactor toast to accept jsx as first argument

emilkowalski869184dd8fd1
diff --git a/src/index.tsx b/src/index.tsx
index a2796e7..a61a8ca 100644
--- a/src/index.tsx
+++ b/src/index.tsx
@@ -311,8 +311,8 @@ const Toast = (props: ToastProps) => {
</svg>
</button>
) : null}
- {toast.jsx ? (
- toast.jsx
+ {toast.jsx || React.isValidElement(toast.title) ? (
+ toast.jsx || toast.title
) : (
<>
{toastType || toast.icon || toast.promise ? (
diff --git a/src/state.ts b/src/state.ts
index 0655d08..4c187b0 100644
--- a/src/state.ts
+++ b/src/state.ts
@@ -29,19 +29,19 @@ class Observer {
return id;
};
- message = (message: string, data?: ExternalToast) => {
+ message = (message: string | React.ReactNode, data?: ExternalToast) => {
const id = toastsCounter++;
this.publish({ ...data, id, title: message });
return id;
};
- error = (message: string, data?: ExternalToast) => {
+ error = (message: string | React.ReactNode, data?: ExternalToast) => {
const id = toastsCounter++;
this.publish({ ...data, id, type: 'error', title: message });
return id;
};
- success = (message: string, data?: ExternalToast) => {
+ success = (message: string | React.ReactNode, data?: ExternalToast) => {
const id = toastsCounter++;
this.publish({ ...data, id, type: 'success', title: message });
return id;
@@ -63,7 +63,7 @@ class Observer {
export const ToastState = new Observer();
// bind this to the toast function
-const toastFunction = (message: string, data?: ExternalToast) => {
+const toastFunction = (message: string | React.ReactNode, data?: ExternalToast) => {
const id = toastsCounter++;
ToastState.publish({
title: message,
diff --git a/src/types.ts b/src/types.ts
index a109b80..e29d80e 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -12,7 +12,7 @@ export type PromiseT = Promise<any> | (() => Promise<any>);
export interface ToastT {
id: number;
- title?: string;
+ title?: string | React.ReactNode;
type?: ToastTypes;
icon?: React.ReactNode;
jsx?: React.ReactNode;
diff --git a/website/src/components/Types/Types.tsx b/website/src/components/Types/Types.tsx
index 33804d4..5aeeae6 100644
--- a/website/src/components/Types/Types.tsx
+++ b/website/src/components/Types/Types.tsx
@@ -8,9 +8,7 @@ export const Types = () => {
return (
<div>
<h2>Types</h2>
- <p>
- You can customize the type of toast you want to render, and pass an options object as the second argument.
- </p>
+ <p>You can customize the type of toast you want to render, and pass an options object as the second argument.</p>
<div className="buttons">
{allTypes.map((type) => (
<button
@@ -91,7 +89,7 @@ toast.promise(promise, {
},
{
name: 'Custom',
- snippet: `toast.custom(() => <div>This is a custom component</div>)`,
- action: () => toast.custom(() => <div>This is a custom component</div>),
+ snippet: `toast(<div>A custom toast with default styling</div>)`,
+ action: () => toast(<div>A custom toast with default styling</div>),
},
];