Skip to content

Commit 46755a5

Browse files
committed
fix(Toast): a toast dismissed with no row on screen leaves the stack
A dismissed toast left the list only when its row's exit transition ended. A toast with no mounted row (hidden past maxVisible, or dismissed before it first rendered) has no transition: when it came back into view its row mounted already collapsed and never left, holding a visible slot and the Notifications landmark open with nothing in it. Such a toast now leaves the list at once, and a dismiss() before the first render still finds the toast and reports onHide once, even when it is dismissed twice before the viewport renders again.
1 parent e769940 commit 46755a5

3 files changed

Lines changed: 180 additions & 4 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[fix] A toast dismissed while it had no row on screen — hidden past `maxVisible`, or dismissed before it first rendered — now leaves the stack. Before, it stayed as an empty, invisible row that took a visible slot when it came back into view and kept the "Notifications" landmark (and its F6 stop) present after every real toast was gone.
6+
7+
@vjeux

‎packages/core/src/Toast/ToastViewport.test.tsx‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,143 @@ describe('ToastViewport visible limit', () => {
442442
});
443443
});
444444

445+
describe('dismissing a toast that has no mounted row', () => {
446+
function ShowAndKeepDismiss({
447+
options,
448+
label,
449+
onShown,
450+
}: {
451+
options: ToastOptions;
452+
label: string;
453+
onShown: (dismiss: () => void) => void;
454+
}) {
455+
const toast = useToast();
456+
return (
457+
<button type="button" onClick={() => onShown(toast(options))}>
458+
{label}
459+
</button>
460+
);
461+
}
462+
463+
it('reports a hidden toast once when it is dismissed twice before the viewport renders again', () => {
464+
const onHideA = vi.fn();
465+
let dismissA: (() => void) | null = null;
466+
render(
467+
<ToastViewport isTopLayer={false} maxVisible={1}>
468+
<ShowAndKeepDismiss
469+
options={{body: 'Toast A', isAutoHide: false, onHide: onHideA}}
470+
label="Show A"
471+
onShown={dismiss => {
472+
dismissA = dismiss;
473+
}}
474+
/>
475+
<ShowToastButton
476+
options={{body: 'Toast B', isAutoHide: false}}
477+
triggerLabel="Show B"
478+
/>
479+
</ToastViewport>,
480+
);
481+
act(() => {
482+
fireEvent.click(screen.getByText('Show A'));
483+
fireEvent.click(screen.getByText('Show B'));
484+
});
485+
expect(screen.queryByText('Toast A')).toBeNull();
486+
487+
// Both calls land in one batch, before the list renders without A.
488+
act(() => {
489+
dismissA?.();
490+
dismissA?.();
491+
});
492+
expect(onHideA).toHaveBeenCalledTimes(1);
493+
});
494+
495+
it('frees the slot of a toast dismissed while it was hidden past maxVisible', () => {
496+
const onHideA = vi.fn();
497+
let dismissA: (() => void) | null = null;
498+
render(
499+
<ToastViewport isTopLayer={false} maxVisible={2}>
500+
<ShowAndKeepDismiss
501+
options={{body: 'Toast A', isAutoHide: false, onHide: onHideA}}
502+
label="Show A"
503+
onShown={dismiss => {
504+
dismissA = dismiss;
505+
}}
506+
/>
507+
<ShowToastButton
508+
options={{body: 'Toast B', isAutoHide: false}}
509+
triggerLabel="Show B"
510+
/>
511+
<ShowToastButton
512+
options={{body: 'Toast C', isAutoHide: false}}
513+
triggerLabel="Show C"
514+
/>
515+
</ToastViewport>,
516+
);
517+
act(() => {
518+
fireEvent.click(screen.getByText('Show A'));
519+
fireEvent.click(screen.getByText('Show B'));
520+
fireEvent.click(screen.getByText('Show C'));
521+
});
522+
// Only the newest two are shown: A is hidden past the limit.
523+
expect(screen.queryByText('Toast A')).toBeNull();
524+
525+
act(() => {
526+
dismissA?.();
527+
});
528+
expect(onHideA).toHaveBeenCalledTimes(1);
529+
expect(onHideA).toHaveBeenCalledWith('manual');
530+
531+
// C leaves. A was dismissed, so it must not come back to hold the slot
532+
// as an empty row: B is the only toast left.
533+
const toastC = getToastWrapperByText('Toast C');
534+
act(() => {
535+
fireEvent.click(
536+
within(toastC).getByRole('button', {name: 'Dismiss notification'}),
537+
);
538+
});
539+
act(() => {
540+
completeExit(toastC.dataset.toastId ?? '');
541+
});
542+
expect(document.querySelectorAll('[data-toast-id]')).toHaveLength(1);
543+
expect(screen.getByText('Toast B')).toBeInTheDocument();
544+
545+
// Once B leaves too, nothing is left: no row, no empty landmark.
546+
const toastB = getToastWrapperByText('Toast B');
547+
act(() => {
548+
fireEvent.click(
549+
within(toastB).getByRole('button', {name: 'Dismiss notification'}),
550+
);
551+
});
552+
act(() => {
553+
completeExit(toastB.dataset.toastId ?? '');
554+
});
555+
expect(document.querySelectorAll('[data-toast-id]')).toHaveLength(0);
556+
expect(screen.queryByRole('region', {name: 'Notifications'})).toBeNull();
557+
expect(onHideA).toHaveBeenCalledTimes(1);
558+
});
559+
560+
it('never shows a toast dismissed before it rendered, and reports it once', () => {
561+
const onHide = vi.fn();
562+
render(
563+
<ToastViewport isTopLayer={false}>
564+
<ShowAndKeepDismiss
565+
options={{body: 'Toast gone', isAutoHide: false, onHide}}
566+
label="Show and dismiss"
567+
onShown={dismiss => dismiss()}
568+
/>
569+
</ToastViewport>,
570+
);
571+
act(() => {
572+
fireEvent.click(screen.getByText('Show and dismiss'));
573+
});
574+
575+
expect(document.querySelectorAll('[data-toast-id]')).toHaveLength(0);
576+
expect(screen.queryByRole('region', {name: 'Notifications'})).toBeNull();
577+
expect(onHide).toHaveBeenCalledTimes(1);
578+
expect(onHide).toHaveBeenCalledWith('manual');
579+
});
580+
});
581+
445582
describe('ToastViewport keyboard reach + focus', () => {
446583
it('F6 moves focus into the newest toast', () => {
447584
renderViewport(

‎packages/core/src/Toast/ToastViewport.tsx‎

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,9 @@ export function ToastViewport({
348348
// Toast ids whose exit has begun — guards onHide from double-firing (see
349349
// removeToast). Mirrors exitingIds state, readable synchronously.
350350
const exitingIdsRef = useRef<Set<string>>(new Set());
351+
// Toasts dispatched since the last commit, by id: a dismiss() called
352+
// before the toast first renders still finds it (and its onHide).
353+
const pendingEntriesRef = useRef<Map<string, ToastEntry>>(new Map());
351354
// The element that was focused before the user jumped into the viewport
352355
// (via F6). Used to restore focus once all toasts are dismissed so focus
353356
// never falls to <body>.
@@ -423,6 +426,16 @@ export function ToastViewport({
423426
// else to the polite region (role="status") — mirrors Toast.tsx.
424427
announce(text, entry.options.type === 'error' ? 'assertive' : 'polite');
425428
}
429+
const pending = pendingEntriesRef.current;
430+
if (uniqueID) {
431+
// An overwrite replaces a toast that has not rendered yet too.
432+
for (const [pendingId, pendingEntry] of pending) {
433+
if (pendingEntry.options.uniqueID === uniqueID) {
434+
pending.delete(pendingId);
435+
}
436+
}
437+
}
438+
pending.set(entry.id, entry);
426439
// An overwrite swaps a new entry, with a new id, into the replaced
427440
// toast's place, so the old row leaves the DOM without ever being
428441
// dismissed. Its settled id is dropped by the prune below, which covers
@@ -449,11 +462,15 @@ export function ToastViewport({
449462
if (exitingIdsRef.current.has(id)) {
450463
return;
451464
}
452-
exitingIdsRef.current.add(id);
453-
const entry = toastsRef.current.find(t => t.id === id);
454-
if (entry) {
455-
entry.options.onHide?.(reason);
465+
const entry =
466+
toastsRef.current.find(t => t.id === id) ??
467+
pendingEntriesRef.current.get(id);
468+
if (entry == null) {
469+
// Already gone: nothing to hide or report.
470+
return;
456471
}
472+
exitingIdsRef.current.add(id);
473+
entry.options.onHide?.(reason);
457474
// If focus currently lives inside the toast being dismissed, remember
458475
// that its removal must hand focus off to a neighbor (or the element
459476
// focused before the user entered the viewport) rather than <body>.
@@ -480,6 +497,20 @@ export function ToastViewport({
480497
pendingFocusRef.current = 'restore';
481498
}
482499
}
500+
if (dismissedNode == null) {
501+
// No row is mounted for it: it is hidden past `maxVisible`, or it has
502+
// not rendered yet. Nothing animates out, so it leaves the list now.
503+
// Waiting for its row to finish exiting would keep it forever: when it
504+
// surfaced, its row would mount already collapsed, with no transition
505+
// left to end, and hold a visible slot as an empty row.
506+
// It leaves the committed list now too, so a second dismissal before
507+
// the next render finds nothing to report.
508+
exitingIdsRef.current.delete(id);
509+
pendingEntriesRef.current.delete(id);
510+
toastsRef.current = toastsRef.current.filter(t => t.id !== id);
511+
setToasts(prev => prev.filter(t => t.id !== id));
512+
return;
513+
}
483514
setExitingIds(prev => {
484515
if (prev.has(id)) {
485516
return prev;
@@ -490,6 +521,7 @@ export function ToastViewport({
490521

491522
const handleExited = useCallback((id: string) => {
492523
exitingIdsRef.current.delete(id);
524+
pendingEntriesRef.current.delete(id);
493525
setExitingIds(withoutId(id));
494526
setToasts(prev => prev.filter(t => t.id !== id));
495527
}, []);

0 commit comments

Comments
 (0)