Skip to content

Commit 3025868

Browse files
committed
feat(toast): handle onShow and onHide callbacks
1 parent 8f3e9af commit 3025868

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/providers/toast/provider.tsx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,17 @@ export function ToastProvider({
5353
payload: {
5454
id,
5555
component: options.component,
56+
onShow: options.onShow,
57+
onHide: options.onHide,
5658
},
5759
});
5860

5961
total.set((value) => value + 1);
6062

63+
if (options.onShow) {
64+
options.onShow();
65+
}
66+
6167
return id;
6268
},
6369
[total]
@@ -69,14 +75,28 @@ export function ToastProvider({
6975
const hide = useCallback(
7076
(ids?: string | string[]) => {
7177
if (ids === undefined) {
72-
// Hide all toasts
78+
// Hide all toasts - call onHide for each toast before hiding
79+
toasts.forEach((toast) => {
80+
if (toast.onHide) {
81+
toast.onHide();
82+
}
83+
});
7384
dispatch({ type: 'HIDE_ALL' });
7485
heights.set({});
7586
total.set(0);
7687
} else {
77-
// Hide specific toast(s)
88+
// Hide specific toast(s) - call onHide for each toast before hiding
7889
const idsArray = Array.isArray(ids) ? ids : [ids];
7990
const idsToRemove = idsArray;
91+
92+
// Find and call onHide callbacks before removing
93+
idsToRemove.forEach((id) => {
94+
const toast = toasts.find((t) => String(t.id) === String(id));
95+
if (toast?.onHide) {
96+
toast.onHide();
97+
}
98+
});
99+
80100
dispatch({
81101
type: 'HIDE',
82102
payload: { ids: idsArray },
@@ -94,7 +114,7 @@ export function ToastProvider({
94114
total.set((value) => value - 1);
95115
}
96116
},
97-
[total, heights]
117+
[total, heights, toasts]
98118
);
99119

100120
const contextValue = useMemo<ToasterContextValue>(

src/providers/toast/types.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@ export interface ToastShowOptions {
9898
* A function that receives toast props and returns a React element
9999
*/
100100
component: (props: ToastComponentProps) => React.ReactElement;
101+
/**
102+
* Callback function called when the toast is shown
103+
*/
104+
onShow?: () => void;
105+
/**
106+
* Callback function called when the toast is hidden
107+
*/
108+
onHide?: () => void;
101109
}
102110

103111
/**
@@ -112,6 +120,14 @@ export interface ToastItem {
112120
* A function that receives toast props and returns a React element
113121
*/
114122
component: (props: ToastComponentProps) => React.ReactElement;
123+
/**
124+
* Callback function called when the toast is shown
125+
*/
126+
onShow?: () => void;
127+
/**
128+
* Callback function called when the toast is hidden
129+
*/
130+
onHide?: () => void;
115131
}
116132

117133
/**

0 commit comments

Comments
 (0)