Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add an option to dismiss toasts on click #520

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const Toast = (props: ToastProps) => {
className = '',
descriptionClassName = '',
duration: durationFromToaster,
dismissibleByClick = false,
position,
gap,
loadingIcon: loadingIconProp,
Expand Down Expand Up @@ -330,6 +331,14 @@ const Toast = (props: ToastProps) => {

toastRef.current?.style.setProperty('--swipe-amount', `${swipeAmount}px`);
}}
onClick={
!dismissibleByClick || disabled || !dismissible || !(toasts.length === 1 || expanded)
? () => {}
: () => {
deleteToast();
toast.onDismiss?.(toast);
}
}
>
{closeButton && !toast.jsx ? (
<button
Expand Down Expand Up @@ -481,6 +490,7 @@ const Toaster = forwardRef<HTMLElement, ToasterProps>(function Toaster(props, re
theme = 'light',
richColors,
duration,
dismissibleByClick,
style,
visibleToasts = VISIBLE_TOASTS_AMOUNT,
toastOptions,
Expand Down Expand Up @@ -722,6 +732,7 @@ const Toaster = forwardRef<HTMLElement, ToasterProps>(function Toaster(props, re
toast={toast}
defaultRichColors={richColors}
duration={toastOptions?.duration ?? duration}
dismissibleByClick={dismissibleByClick}
className={toastOptions?.className}
descriptionClassName={toastOptions?.descriptionClassName}
invert={invert}
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export interface ToasterProps {
richColors?: boolean;
expand?: boolean;
duration?: number;
dismissibleByClick?: boolean;
gap?: number;
visibleToasts?: number;
closeButton?: boolean;
Expand Down Expand Up @@ -155,6 +156,7 @@ export interface ToastProps {
cancelButtonStyle?: React.CSSProperties;
actionButtonStyle?: React.CSSProperties;
duration?: number;
dismissibleByClick?: boolean;
className?: string;
unstyled?: boolean;
descriptionClassName?: string;
Expand Down
1 change: 1 addition & 0 deletions website/src/pages/toaster.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ Changes the directionality of the toast's text.
| pauseWhenPageIsHidden | Pauses toast timers when the page is hidden, e.g., when the tab is backgrounded, the browser is minimized, or the OS is locked. | `false` |
| icons | Changes the default icons | `-` |
| cn | Custom function for constructing/merging classes. | `classes.filter(Boolean).join(' ')` |
| dismissibleByClick | Allows users to dismiss toasts by clicking on them. Only works for `dismissible` toasts. | `false` |
Loading