Skip to content
Merged
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
7 changes: 7 additions & 0 deletions .changeset/toast-dismissed-before-entrance-leaves.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion docs/specs/components/Toast/Toast.spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
36 changes: 36 additions & 0 deletions packages/core/src/Toast/ToastViewport.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
22 changes: 22 additions & 0 deletions packages/core/src/Toast/ToastViewport.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLDivElement | null>(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;
}
Expand All @@ -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 (
<div
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright (c) Meta Platforms, Inc. and affiliates.

/**
* @file ToastDismissWhileOpening.interaction.browser.spec.ts
* @input The ErrorPersists story, whose toast stays until it is dismissed, on
* a phone page, in Chromium and WebKit, with and without reduced motion.
* @output Tests that a toast dismissed while its row opens leaves no row
* behind: when the row mounts, as its entrance starts, and one frame after.
* @position A dismissal during the entrance can find the row still collapsed.
* The browser then starts no collapse, so the row cannot wait for one.
*/
import {expect, test, type Page} from '@astryxdesign/a11y-spec/playwright';
import {openStory, withStorybook} from '@astryxdesign/interaction-harness';

const storybook = withStorybook(test);

test.use({
viewport: {width: 390, height: 844},
isMobile: true,
hasTouch: true,
});

type Moment =
| 'when its row mounts'
| 'as its entrance starts'
| 'one frame after its entrance starts';

/**
* Dismisses the next toast at a moment of its entrance. A mutation observer
* runs before the next frame, so a dismissal when the row mounts comes before
* the entrance starts.
*/
async function dismissNextToast(page: Page, moment: Moment): Promise<void> {
await page.evaluate(when => {
const dismissButton = (row: Element) =>
[...row.querySelectorAll<HTMLElement>('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);
});
}
}
Loading