From 1d459291dba32abf3588d1682ce2a48fee72fad6 Mon Sep 17 00:00:00 2001 From: Maharshi Alpesh Date: Thu, 20 Feb 2025 14:48:07 +0530 Subject: [PATCH 1/4] fix: maxVisibleToasts functionality --- packages/components/toast/src/toast-region.tsx | 7 ++++++- packages/components/toast/src/use-toast.ts | 6 +++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/components/toast/src/toast-region.tsx b/packages/components/toast/src/toast-region.tsx index 41a212ece7..54c568661a 100644 --- a/packages/components/toast/src/toast-region.tsx +++ b/packages/components/toast/src/toast-region.tsx @@ -74,7 +74,11 @@ export function ToastRegion({ return null; } - if (total - index <= 4 || (isHovered && total - index <= maxVisibleToasts + 1)) { + if ( + disableAnimation || + total - index <= 4 || + (isHovered && total - index <= maxVisibleToasts + 1) + ) { return ( ({ heights={heights} index={index} isRegionExpanded={isHovered || isTouched} + maxVisibleToasts={maxVisibleToasts} placement={placement} setHeights={setHeights} toastOffset={toastOffset} diff --git a/packages/components/toast/src/use-toast.ts b/packages/components/toast/src/use-toast.ts index a442678767..20d19ed317 100644 --- a/packages/components/toast/src/use-toast.ts +++ b/packages/components/toast/src/use-toast.ts @@ -119,6 +119,7 @@ interface Props extends Omit, "title">, ToastProps { isRegionExpanded: boolean; placement?: ToastPlacement; toastOffset?: number; + maxVisibleToasts: number; } export type UseToastProps = Props & @@ -158,6 +159,7 @@ export function useToast(originalProps: UseToastProps) icon, onClose, severity, + maxVisibleToasts, ...otherProps } = props; @@ -486,7 +488,8 @@ export function useToast(originalProps: UseToastProps) "data-drag-value": number; className: string; } => { - const isCloseToEnd = total - index - 1 <= 2; + const comparingValue = isRegionExpanded ? maxVisibleToasts - 1 : 2; + const isCloseToEnd = total - index - 1 <= comparingValue; const dragDirection = placement === "bottom-center" || placement === "top-center" ? "y" : "x"; const dragConstraints = {left: 0, right: 0, top: 0, bottom: 0}; const dragElastic = getDragElasticConstraints(placement); @@ -593,6 +596,7 @@ export function useToast(originalProps: UseToastProps) shouldCloseToast, slots, toastOffset, + maxVisibleToasts, ], ); From a66aabe550ac05717d6b8d8e5c62d90c0d8d5c44 Mon Sep 17 00:00:00 2001 From: Maharshi Alpesh Date: Thu, 20 Feb 2025 14:51:58 +0530 Subject: [PATCH 2/4] chore: adding the changeset --- .changeset/slow-dogs-travel.md | 5 +++++ packages/components/toast/src/use-toast.ts | 4 +++- .../toast/stories/toast.stories.tsx | 22 +++++++++++++------ 3 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 .changeset/slow-dogs-travel.md diff --git a/.changeset/slow-dogs-travel.md b/.changeset/slow-dogs-travel.md new file mode 100644 index 0000000000..048b8c00fc --- /dev/null +++ b/.changeset/slow-dogs-travel.md @@ -0,0 +1,5 @@ +--- +"@heroui/toast": patch +--- + +fixing maxVisibleToast functionality in toast (#4870) diff --git a/packages/components/toast/src/use-toast.ts b/packages/components/toast/src/use-toast.ts index 20d19ed317..9d022b46b2 100644 --- a/packages/components/toast/src/use-toast.ts +++ b/packages/components/toast/src/use-toast.ts @@ -488,7 +488,9 @@ export function useToast(originalProps: UseToastProps) "data-drag-value": number; className: string; } => { - const comparingValue = isRegionExpanded ? maxVisibleToasts - 1 : 2; + const comparingValue = isRegionExpanded + ? maxVisibleToasts - 1 + : Math.min(2, maxVisibleToasts - 1); const isCloseToEnd = total - index - 1 <= comparingValue; const dragDirection = placement === "bottom-center" || placement === "top-center" ? "y" : "x"; const dragConstraints = {left: 0, right: 0, top: 0, bottom: 0}; diff --git a/packages/components/toast/stories/toast.stories.tsx b/packages/components/toast/stories/toast.stories.tsx index b501f18789..586be77fb3 100644 --- a/packages/components/toast/stories/toast.stories.tsx +++ b/packages/components/toast/stories/toast.stories.tsx @@ -49,6 +49,9 @@ export default { "top-center", ], }, + maxVisibleToasts: { + control: {type: "number"}, + }, hideCloseButton: { control: { type: "boolean", @@ -80,7 +83,7 @@ const defaultProps = { const Template = (args: ToastProps) => { return ( <> - +