Skip to content
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
1 change: 1 addition & 0 deletions src/components/toaster.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export const Toaster: React.FC<ToasterProps> = ({
onMouseLeave={handlers.endPause}
>
{toasts.map((t) => {
if (t.delayed && !t.visible) return null;
const toastPosition = t.position || position;
const offset = handlers.calculateOffset(t, {
reverseOrder,
Expand Down
53 changes: 46 additions & 7 deletions src/core/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export type Action =
}
| {
type: ActionType.UPDATE_TOAST;
toast: Partial<Toast>;
toast: Partial<Toast> & { id: string };
}
| {
type: ActionType.DISMISS_TOAST;
Expand Down Expand Up @@ -59,6 +59,8 @@ interface State {
[toasterId: string]: ToasterState;
}

const pendingDelayTimeouts = new Map<string, ReturnType<typeof setTimeout>>();

export const reducer = (state: ToasterState, action: Action): ToasterState => {
const { toastLimit } = state.settings;

Expand Down Expand Up @@ -86,9 +88,20 @@ export const reducer = (state: ToasterState, action: Action): ToasterState => {
toast,
});

case ActionType.DISMISS_TOAST:
case ActionType.DISMISS_TOAST: {
const { toastId } = action;

if (toastId) {
const timeoutId = pendingDelayTimeouts.get(toastId);
if (timeoutId) {
clearTimeout(timeoutId);
pendingDelayTimeouts.delete(toastId);
}
} else {
pendingDelayTimeouts.forEach((timeoutId) => clearTimeout(timeoutId));
pendingDelayTimeouts.clear();
}

return {
...state,
toasts: state.toasts.map((t) =>
Expand All @@ -101,17 +114,34 @@ export const reducer = (state: ToasterState, action: Action): ToasterState => {
: t
),
};
case ActionType.REMOVE_TOAST:
if (action.toastId === undefined) {
}

case ActionType.REMOVE_TOAST: {
const { toastId } = action;

if (toastId) {
const timeoutId = pendingDelayTimeouts.get(toastId);
if (timeoutId) {
clearTimeout(timeoutId);
pendingDelayTimeouts.delete(toastId);
}
} else {
pendingDelayTimeouts.forEach((timeoutId) => clearTimeout(timeoutId));
pendingDelayTimeouts.clear();
}

if (toastId === undefined) {
return {
...state,
toasts: [],
};
}

return {
...state,
toasts: state.toasts.filter((t) => t.id !== action.toastId),
toasts: state.toasts.filter((t) => t.id !== toastId),
};
}

case ActionType.START_PAUSE:
return {
Expand All @@ -130,6 +160,9 @@ export const reducer = (state: ToasterState, action: Action): ToasterState => {
pauseDuration: t.pauseDuration + diff,
})),
};

default:
return state;
}
};

Expand All @@ -144,7 +177,10 @@ const defaultToasterState: ToasterState = {
toastLimit: TOAST_LIMIT,
},
};
let memoryState: State = {};

let memoryState: State = {
[DEFAULT_TOASTER_ID]: defaultToasterState,
};

export const dispatch = (action: Action, toasterId = DEFAULT_TOASTER_ID) => {
memoryState[toasterId] = reducer(
Expand Down Expand Up @@ -172,6 +208,8 @@ export const createDispatch =
dispatch(action, toasterId);
};

export const getPendingDelayTimeouts = () => pendingDelayTimeouts;

export const defaultTimeouts: {
[key in ToastType]: number;
} = {
Expand Down Expand Up @@ -212,7 +250,8 @@ export const useStore = (
removeDelay:
t.removeDelay ||
toastOptions[t.type]?.removeDelay ||
toastOptions?.removeDelay,
toastOptions?.removeDelay ||
TOAST_EXPIRE_DISMISS_DELAY,
duration:
t.duration ||
toastOptions[t.type]?.duration ||
Expand Down
34 changes: 25 additions & 9 deletions src/core/toast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,34 @@ const createToast = (
id: opts?.id || genId(),
});

const createHandler =
(type?: ToastType): ToastHandler =>
(message, options) => {
const toast = createToast(message, type, options);
const pendingDelayTimeouts = new Map<string, ReturnType<typeof setTimeout>>();

const dispatch = createDispatch(
toast.toasterId || getToasterIdFromToastId(toast.id)
);
const createHandler = (type?: ToastType): ToastHandler => (message, options) => {
const toast = createToast(message, type, options);

const dispatch = createDispatch(toast.toasterId || getToasterIdFromToastId(toast.id));

if (toast.delay && toast.delay > 0) {
dispatch({
type: ActionType.UPSERT_TOAST,
toast: { ...toast, visible: false, delayed: true },
});

const timeoutId = setTimeout(() => {
dispatch({
type: ActionType.UPDATE_TOAST,
toast: { id: toast.id, visible: true, delayed: false },
});
pendingDelayTimeouts.delete(toast.id);
}, toast.delay);

pendingDelayTimeouts.set(toast.id, timeoutId);
} else {
dispatch({ type: ActionType.UPSERT_TOAST, toast });
return toast.id;
};
}

return toast.id;
};

const toast = (message: Message, opts?: ToastOptions) =>
createHandler('blank')(message, opts);
Expand Down
4 changes: 4 additions & 0 deletions src/core/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ export interface Toast {
pauseDuration: number;
position?: ToastPosition;
removeDelay?: number;
delay?: number;
delayed?: boolean;

ariaProps: {
role: 'status' | 'alert';
Expand Down Expand Up @@ -70,6 +72,8 @@ export type ToastOptions = Partial<
| 'iconTheme'
| 'toasterId'
| 'removeDelay'
| 'delay'
| 'delayed'
>
>;

Expand Down
32 changes: 32 additions & 0 deletions test/toast.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -532,3 +532,35 @@ describe('Multi-Toaster behavior', () => {
expect(screen.queryByText('Removable toast #2')).toBeInTheDocument();
});
});

describe('toast delay', () => {
test('shows toast only after delay', () => {
render(<Toaster />);

act(() => {
toast.success('Delayed toast', { delay: 500 });
});

expect(screen.queryByText('Delayed toast')).toBeNull();

act(() => {
jest.advanceTimersByTime(499);
});
expect(screen.queryByText('Delayed toast')).toBeNull();

act(() => {
jest.advanceTimersByTime(1);
});
expect(screen.getByText('Delayed toast')).toBeInTheDocument();

act(() => {
jest.advanceTimersByTime(2000);
});

act(() => {
jest.advanceTimersByTime(1500);
});

expect(screen.queryByText('Delayed toast')).toBeNull();
});
});