From 50ccbf4a1202b15b3f34a080826b5478a2ac8a73 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Mon, 30 Nov 2026 19:09:44 -0800 Subject: [PATCH 1/2] fix(Toast): a toast dismissed while it opens leaves the stack A row dismissed before its entrance starts, or as it starts, finds no collapse to run: the browser starts no transition, so no transitionend removes the row. Once a row is leaving, a layout effect now asks getAnimations() for its grid-template-rows transition and, with none, calls onExited at once. --- .../toast-dismissed-before-entrance-leaves.md | 7 ++ .../core/src/Toast/ToastViewport.test.tsx | 36 +++++++ packages/core/src/Toast/ToastViewport.tsx | 22 +++++ ...ssWhileOpening.interaction.browser.spec.ts | 95 +++++++++++++++++++ 4 files changed, 160 insertions(+) create mode 100644 .changeset/toast-dismissed-before-entrance-leaves.md create mode 100644 packages/core/src/Toast/__tests__/ToastDismissWhileOpening.interaction.browser.spec.ts diff --git a/.changeset/toast-dismissed-before-entrance-leaves.md b/.changeset/toast-dismissed-before-entrance-leaves.md new file mode 100644 index 0000000000000..de8b1a9eb0d87 --- /dev/null +++ b/.changeset/toast-dismissed-before-entrance-leaves.md @@ -0,0 +1,7 @@ +--- +'@astryxdesign/core': patch +--- + +[fix] Toast: a toast dismissed while it opens now leaves the stack. Dismissed before its entrance started, or as it started, the row could find no collapse to run: the browser started no transition, and the row waited for a transition end that never came, so an empty, zero-height row stayed in the viewport with the toast's text inside it. A row whose collapse the browser runs still waits for that collapse to end. + +@vjeux diff --git a/packages/core/src/Toast/ToastViewport.test.tsx b/packages/core/src/Toast/ToastViewport.test.tsx index 7272a82b219d4..2aed200f166e6 100644 --- a/packages/core/src/Toast/ToastViewport.test.tsx +++ b/packages/core/src/Toast/ToastViewport.test.tsx @@ -1953,6 +1953,42 @@ describe('Toast native motion contract', () => { }); expect(document.querySelector(`[data-toast-id="${id}"]`)).toBeNull(); }); + + it('removes a dismissed Toast at once when the browser runs no collapse for it', () => { + // A dismissal before the entrance starts finds the row collapsed, so the + // browser starts no transition for it. The stub reports no animation, as + // the browser does for that row. + const {wrapper} = renderMotionToast('bottomEnd'); + const id = wrapper.getAttribute('data-toast-id')!; + wrapper.getAnimations = () => []; + act(() => { + fireEvent.click( + within(wrapper).getByRole('button', {name: 'Dismiss notification'}), + ); + }); + + expect(document.querySelector(`[data-toast-id="${id}"]`)).toBeNull(); + }); + + it('keeps a dismissed Toast until its collapse ends when the browser runs one', () => { + const {wrapper} = renderMotionToast('bottomEnd'); + const id = wrapper.getAttribute('data-toast-id')!; + const collapse = { + transitionProperty: 'grid-template-rows', + } as unknown as Animation; + wrapper.getAnimations = () => [collapse]; + act(() => { + fireEvent.click( + within(wrapper).getByRole('button', {name: 'Dismiss notification'}), + ); + }); + + expect(wrapper).toBeInTheDocument(); + act(() => { + completeExit(id); + }); + expect(document.querySelector(`[data-toast-id="${id}"]`)).toBeNull(); + }); }); describe('Toast swipe dismissal', () => { diff --git a/packages/core/src/Toast/ToastViewport.tsx b/packages/core/src/Toast/ToastViewport.tsx index 45b90da091faf..aa463e85f21ba 100644 --- a/packages/core/src/Toast/ToastViewport.tsx +++ b/packages/core/src/Toast/ToastViewport.tsx @@ -329,8 +329,10 @@ function ToastRow({ // Each move between modal outlets renders the row and its card again. Their // starting styles must not apply then. const [hasEntryStarted, setHasEntryStarted] = useState(false); + const rowRef = useRef(null); // React's transition run and cancel events have no propertyName field. const watchEntry = useCallback((row: HTMLDivElement | null) => { + rowRef.current = row; if (row == null) { return; } @@ -350,8 +352,28 @@ function ToastRow({ return () => { row.removeEventListener('transitionrun', onTransition); row.removeEventListener('transitioncancel', onTransition); + rowRef.current = null; }; }, []); + // Syncs the exit with the browser's CSS transitions. A dismissal before the + // entrance starts finds the row collapsed. The browser then starts no + // transition, so no transitionend event comes. + useLayoutEffect(() => { + const row = rowRef.current; + if (!isExiting || row == null || typeof row.getAnimations !== 'function') { + return; + } + const collapses = row + .getAnimations() + .some( + animation => + 'transitionProperty' in animation && + animation.transitionProperty === 'grid-template-rows', + ); + if (!collapses) { + onExited(entry.id); + } + }, [entry.id, isExiting, onExited]); return (
{ + await page.evaluate(when => { + const dismissButton = (row: Element) => + [...row.querySelectorAll('button')].find( + button => button.getAttribute('aria-label') === 'Dismiss notification', + ); + const dismiss = (row: Element) => { + document.documentElement.dataset.toastDismissed = 'true'; + dismissButton(row)?.click(); + }; + const observer = new MutationObserver(() => { + const row = document.querySelector('[data-toast-id]'); + if (row == null || dismissButton(row) == null) { + return; + } + observer.disconnect(); + if (when === 'when its row mounts') { + dismiss(row); + return; + } + let isArmed = true; + row.addEventListener('transitionrun', event => { + if ( + !isArmed || + event.target !== row || + (event as TransitionEvent).propertyName !== 'grid-template-rows' + ) { + return; + } + isArmed = false; + if (when === 'as its entrance starts') { + dismiss(row); + return; + } + requestAnimationFrame(() => dismiss(row)); + }); + }); + observer.observe(document.body, {childList: true, subtree: true}); + }, moment); +} + +for (const reducedMotion of ['no-preference', 'reduce'] as const) { + for (const moment of [ + 'when its row mounts', + 'as its entrance starts', + 'one frame after its entrance starts', + ] as const) { + test(`a toast dismissed ${moment} leaves no row behind (reduced motion: ${reducedMotion})`, async ({ + page, + }) => { + await page.emulateMedia({reducedMotion}); + await openStory(page, storybook(), 'core-toast--error-persists'); + await dismissNextToast(page, moment); + await page.getByRole('button', {name: 'Trigger error'}).click(); + + await expect(page.locator('html')).toHaveAttribute( + 'data-toast-dismissed', + 'true', + ); + await expect(page.locator('[data-toast-id]')).toHaveCount(0); + }); + } +} From 1995aabec0b8e9c8fcae06f79f9ba60963b6be83 Mon Sep 17 00:00:00 2001 From: Christopher Chedeau Date: Tue, 1 Dec 2026 00:08:53 -0800 Subject: [PATCH 2/2] docs(Toast): verification map names the dismiss-while-opening spec FR5's verification row now names ToastDismissWhileOpening.interaction.browser.spec.ts. It dismisses a toast as its row mounts, as its entrance starts and one frame after, with and without reduced motion. --- docs/specs/components/Toast/Toast.spec.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/specs/components/Toast/Toast.spec.md b/docs/specs/components/Toast/Toast.spec.md index 1ef46cdeca449..8c769d10cfcc1 100644 --- a/docs/specs/components/Toast/Toast.spec.md +++ b/docs/specs/components/Toast/Toast.spec.md @@ -264,7 +264,7 @@ button), so no anatomy part can own the `toast` target. | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ---------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------- | | FR1, FR3 | `ToastViewport.test.tsx` layout, custom content, and onHide tests; `Layer/toastHost.test.tsx` and `toastHost.lateRegistration.test.tsx` toasts dismissed or replaced before they show | Default, custom content, double dismiss, a dismissal before the toast shows | Dropped controls or a repeated `onHide` fails. | `audit:Toast/behavior` | | FR2 | `ToastViewport.test.tsx` blur-pause, pause-source and raised-while-blurred tests | Window blur, toast raised while blurred, new toast during pause, overlapping pointer/focus/window holds, focus moving inside | A timer running while paused, or resuming while another hold remains, fails. | `audit:Toast/behavior` | -| FR4, FR5 | `ToastViewport.test.tsx` gesture and motion tests; `ToastSwipeScroll.interaction.browser.spec.ts` first move toward and away from the edge, slow and quick, from three offsets | Pen and touch thresholds, short drag, horizontal intent, second touch, interactive descendant, RTL | A dismissal from a rejected gesture, a lost snap-back, or off-axis drift fails. | `audit:Toast/behavior` | +| FR4, FR5 | `ToastViewport.test.tsx` gesture and motion tests; `ToastSwipeScroll.interaction.browser.spec.ts` first move toward and away from the edge, slow and quick, from three offsets; `ToastDismissWhileOpening.interaction.browser.spec.ts` dismissal as the row mounts, as its entrance starts and one frame after, with and without reduced motion | Pen and touch thresholds, short drag, horizontal intent, second touch, interactive descendant, RTL | A dismissal from a rejected gesture, a lost snap-back, or off-axis drift fails. | `audit:Toast/behavior` | | FR6–FR8 | `ToastViewport.test.tsx` placement, cap, and uniqueID tests, and the zero-inset cases (a zero on each edge replaces the provider's value on its own edge; a zero opposite the position writes nothing); `Layer/LayerProvider.test.tsx` inset tests; `ToastSafeArea.a11y.chromium.spec.ts` bottom gutter, under a finger and a mouse, with insets of 0, 34 and 48 px | Each position, cap 1 and an explicit cap, overwrite, ignore | Wrong order, cap, or collision, or a bottom gap other than 16 px beyond the inset, or beyond the larger of the inset and 34 px under a coarse pointer, fails. | `audit:Toast/layout` | | FR9–FR11 | `ToastViewport.test.tsx` announcement, F6, focus, landmark and empty-viewport tests; `ToastEmptyViewport.interaction.browser.spec.ts` no box once the last toast leaves | Info, error, StrictMode, nested viewports | A repeated or wrong-urgency announcement, or focus to the body after F6 entry, fails. | `audit:Toast/accessibility` | | FR2, FR8–FR10 | `ToastViewport.test.tsx` "keeps focus on a control inside a toast that an overwrite updates", "hands focus off when an overwrite removes the focused control", "updates a settled toast in place when an overwrite replaces it through uniqueID", "announces an overwrite once, through the viewport only", "auto-hides a control-free replacement after the update removes the focused control" | Overwrite with focus inside; overwrite that removes the focused control | A remounted card, or focus dropping to the page body, fails. | `audit:Toast/accessibility` |