From 5d160e6237c2b8a70cd0399c7a908c63802d18f1 Mon Sep 17 00:00:00 2001 From: Emil Kowalski <36730035+emilkowalski@users.noreply.github.com> Date: Thu, 21 Mar 2024 09:53:52 +0100 Subject: [PATCH] allow to pass jsx to actions (#379) * allow to pass jsx to actions * Allow to pass jsx as actions * update docs --- .github/pull-request-template.md | 6 ++--- src/index.tsx | 45 ++++++++++++++++++++------------ src/types.ts | 20 ++++++++------ website/src/pages/toast.mdx | 18 ++++++++++++- 4 files changed, 61 insertions(+), 28 deletions(-) diff --git a/.github/pull-request-template.md b/.github/pull-request-template.md index 342c0cd..b16f62d 100644 --- a/.github/pull-request-template.md +++ b/.github/pull-request-template.md @@ -1,11 +1,11 @@ -### Issue 😱: +### Issue: Closes https://github.com/emilkowalski/sonner/issues/ -### What has been done ✅: +### What has been done: - [ ] -### Screenshots/Videos 🎥: +### Screenshots/Videos: N/A diff --git a/src/index.tsx b/src/index.tsx index 65501fb..a76b552 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -3,12 +3,20 @@ import React from 'react'; import ReactDOM from 'react-dom'; -import DOMPurify from "dompurify"; +import DOMPurify from 'dompurify'; import { getAsset, Loader } from './assets'; import { useIsDocumentHidden } from './hooks'; import { toast, ToastState } from './state'; import './styles.css'; -import type { ExternalToast, HeightT, ToasterProps, ToastProps, ToastT, ToastToDismiss } from './types'; +import { + isAction, + type ExternalToast, + type HeightT, + type ToasterProps, + type ToastProps, + type ToastT, + type ToastToDismiss, +} from './types'; // Visible toasts amount const VISIBLE_TOASTS_AMOUNT = 3; @@ -56,7 +64,7 @@ const Toast = (props: ToastProps) => { descriptionClassName = '', duration: durationFromToaster, position, - gap = GAP, + gap, loadingIcon: loadingIconProp, expandByDefault, classNames, @@ -375,16 +383,16 @@ const Toast = (props: ToastProps) => { <> {toastType || toast.icon || toast.promise ? (
- {toast.promise || (toast.type === 'loading' && !toast.icon) - ? toast.icon || getLoadingIcon() - : null} + {toast.promise || (toast.type === 'loading' && !toast.icon) ? toast.icon || getLoadingIcon() : null} {toast.type !== 'loading' ? toast.icon || icons?.[toastType] || getAsset(toastType) : null}
) : null}
-
{toast.description ? (
{ >
) : null}
- {toast.cancel ? ( + {React.isValidElement(toast.cancel) ? ( + toast.cancel + ) : toast.cancel && isAction(toast.cancel) ? ( ) : null} - {toast.action ? ( + {React.isValidElement(toast.action) ? ( + toast.action + ) : toast.action && isAction(toast.action) ? ( , +}); +``` + ### Cancel Renders a secondary button, clicking it will close the toast and run the callback passed via `onClick`. @@ -81,6 +89,14 @@ toast('My cancel toast', { }); ``` +You can also render jsx as your action. + +```jsx +toast('My cancel toast', { + action: , +}); +``` + ### Promise Starts in a loading state and will update automatically after the promise resolves or fails.