Skip to content

Commit 525ff66

Browse files
committed
feat: add removedByUser for onClose callback
- solve: #1000, #716 - breaking: children props not passed down to `onClose` and `onOpen` callbacks
1 parent 2c9138a commit 525ff66

File tree

5 files changed

+18
-15
lines changed

5 files changed

+18
-15
lines changed

src/components/CloseButton.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Default } from '../utils';
33
import { Theme, TypeOptions } from '../types';
44

55
export interface CloseButtonProps {
6-
closeToast: (e: React.MouseEvent<HTMLElement>) => void;
6+
closeToast: (removedByUser: boolean) => void;
77
type: TypeOptions;
88
ariaLabel?: string;
99
theme: Theme;
@@ -16,7 +16,7 @@ export function CloseButton({ closeToast, theme, ariaLabel = 'close' }: CloseBut
1616
type="button"
1717
onClick={e => {
1818
e.stopPropagation();
19-
closeToast(e);
19+
closeToast(true);
2020
}}
2121
aria-label={ariaLabel}
2222
>

src/core/containerObserver.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ReactElement, cloneElement, isValidElement } from 'react';
1+
import { cloneElement, isValidElement, ReactElement } from 'react';
22
import {
33
Id,
44
NotValidatedToastProps,
@@ -59,7 +59,7 @@ export function createContainerObserver(
5959

6060
const toggle = (v: boolean, id?: Id) => {
6161
toasts.forEach(t => {
62-
if (id == null || id === t.props.toastId) isFn(t.toggle) && t.toggle(v);
62+
if (id == null || id === t.props.toastId) t.toggle?.(v);
6363
});
6464
};
6565

@@ -74,24 +74,25 @@ export function createContainerObserver(
7474
};
7575

7676
const addActiveToast = (toast: ActiveToast) => {
77-
const { toastId, onOpen, updateId, children } = toast.props;
77+
const { toastId, updateId } = toast.props;
7878
const isNew = updateId == null;
7979

8080
if (toast.staleId) toasts.delete(toast.staleId);
8181

8282
toasts.set(toastId, toast);
83-
activeToasts = [...activeToasts, toast.props.toastId].filter(v => v !== toast.staleId);
83+
activeToasts = [...activeToasts, toastId].filter(v => v !== toast.staleId);
8484
notify();
8585
dispatchChanges(toToastItem(toast, isNew ? 'added' : 'updated'));
8686

87-
if (isNew && isFn(onOpen)) onOpen(isValidElement(children) && children.props);
87+
if (isNew) toast.props.onOpen?.();
8888
};
8989

9090
const buildToast = <TData = unknown>(content: ToastContent<TData>, options: NotValidatedToastProps) => {
9191
if (shouldIgnoreToast(options)) return;
9292

9393
const { toastId, updateId, data, staleId, delay } = options;
94-
const closeToast = () => {
94+
const closeToast = (removedByUser?: true) => {
95+
toasts.get(toastId)!.removedByUser = removedByUser;
9596
removeToast(toastId);
9697
};
9798

@@ -115,9 +116,8 @@ export function createContainerObserver(
115116
autoClose: options.isLoading ? false : getAutoCloseDelay(options.autoClose, props.autoClose),
116117
deleteToast() {
117118
const toastToRemove = toasts.get(toastId)!;
118-
const { onClose, children } = toastToRemove.props;
119-
if (isFn(onClose)) onClose(isValidElement(children) && children.props);
120119

120+
toastToRemove.props.onClose?.(toastToRemove.removedByUser);
121121
dispatchChanges(toToastItem(toastToRemove, 'removed'));
122122
toasts.delete(toastId);
123123

src/hooks/useToast.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ export function useToast(props: ToastProps) {
141141
drag.canDrag = false;
142142
if (Math.abs(drag.delta) > drag.removalDistance) {
143143
setPreventExitTransition(true);
144-
props.closeToast();
144+
props.closeToast(true);
145145
props.collapseAll();
146146
return;
147147
}
@@ -168,7 +168,7 @@ export function useToast(props: ToastProps) {
168168
if (closeOnClick) {
169169
eventHandlers.onClick = (e: React.MouseEvent) => {
170170
onClick && onClick(e);
171-
drag.canCloseOnClick && closeToast();
171+
drag.canCloseOnClick && closeToast(true);
172172
};
173173
}
174174

src/types.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,12 @@ export interface ToastOptions<Data = unknown> extends CommonOptions {
174174
/**
175175
* Called when toast is mounted.
176176
*/
177-
onOpen?: <T = {}>(props: T) => void;
177+
onOpen?: () => void;
178178

179179
/**
180180
* Called when toast is unmounted.
181181
*/
182-
onClose?: <T = {}>(props: T) => void;
182+
onClose?: (removedByUser: true | undefined) => void;
183183

184184
/**
185185
* An optional inline style to apply.
@@ -282,7 +282,7 @@ export interface ToastProps extends ToastOptions {
282282
toastId: Id;
283283
key: Id;
284284
transition: ToastTransition;
285-
closeToast: () => void;
285+
closeToast: (removedByUser?: boolean) => void;
286286
position: ToastPosition;
287287
children?: ToastContent;
288288
draggablePercent: number;
@@ -311,6 +311,7 @@ export interface Toast {
311311
content: ToastContent;
312312
props: ToastProps;
313313
toggle?: (v: boolean) => void;
314+
removedByUser?: true | undefined;
314315
}
315316

316317
export type ToastItemStatus = 'added' | 'removed' | 'updated';
@@ -325,6 +326,7 @@ export interface ToastItem<Data = {}> {
325326
data: Data;
326327
icon?: ToastIcon;
327328
status: ToastItemStatus;
329+
removedByUser?: true;
328330
}
329331

330332
export type OnChangeCallback = (toast: ToastItem) => void;

src/utils/mapper.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export function toToastItem(toast: Toast, status: ToastItemStatus): ToastItem {
1111
data: toast.props.data || {},
1212
isLoading: toast.props.isLoading,
1313
icon: toast.props.icon,
14+
removedByUser: toast.removedByUser,
1415
status
1516
}
1617
: // monkey patch for now

0 commit comments

Comments
 (0)