Skip to content

Commit 5c78461

Browse files
committed
feat(toast): handle persistent duration
1 parent adfedec commit 5c78461

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

example/src/app/(home)/components/toast.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ const MyToast = (props: ToastComponentProps) => {
8181
return (
8282
<Toast
8383
variant="accent"
84-
placement="bottom"
84+
placement="top"
85+
duration="persistent"
8586
className="flex-row items-center gap-3"
8687
{...props}
8788
>

src/components/toast/toast.hooks.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ import { useEffect, useRef } from 'react';
33
/**
44
* Hook to handle automatic toast dismissal based on duration
55
*
6-
* @param duration - Duration in milliseconds before the toast automatically disappears
6+
* @param duration - Duration in milliseconds before the toast automatically disappears, or 'persistent' to prevent auto-hide
77
* @param id - The unique ID of the toast
88
* @param hide - Function to hide the toast
99
*
1010
* @example
1111
* ```tsx
1212
* useToastDuration(4000, toastId, hide);
13+
* useToastDuration('persistent', toastId, hide);
1314
* ```
1415
*/
1516
export function useToastDuration(
16-
duration: number | null | undefined,
17+
duration: number | 'persistent' | undefined,
1718
id: string | undefined,
1819
hide: ((ids?: string | string[]) => void) | undefined
1920
): void {
@@ -26,10 +27,16 @@ export function useToastDuration(
2627
timeoutRef.current = null;
2728
}
2829

29-
// Only set timeout if duration is valid and id/hide are available
30+
// Handle immediate dismissal
31+
if (duration === 0 && id && hide) {
32+
hide(id);
33+
return;
34+
}
35+
36+
// Only set timeout if duration is a valid positive number and id/hide are available
37+
// Skip timeout if duration is 'persistent', undefined, or invalid (treats as persistent)
3038
if (
31-
duration !== null &&
32-
duration !== undefined &&
39+
typeof duration === 'number' &&
3340
!isNaN(duration) &&
3441
duration > 0 &&
3542
duration !== Infinity &&

src/components/toast/toast.types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ export interface ToastRootProps
122122
placement?: ToastPlacement;
123123
/**
124124
* Duration in milliseconds before the toast automatically disappears
125-
* Set to `null` or `Infinity` to prevent auto-hide
125+
* Set to `'persistent'` to prevent auto-hide (toast will remain until manually dismissed)
126126
* @default 4000
127127
*/
128-
duration?: number | null;
128+
duration?: number | 'persistent';
129129
/**
130130
* Additional CSS class for the toast container
131131
*/

0 commit comments

Comments
 (0)