Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(Toast): the card's shadow is no longer clipped away (#5547)

freddymetac68e4c2575e1
diff --git a/.changeset/toast-shadow-clipped.md b/.changeset/toast-shadow-clipped.md
new file mode 100644
index 0000000000..f27346928f
--- /dev/null
+++ b/.changeset/toast-shadow-clipped.md
@@ -0,0 +1,47 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Toast: the card's shadow is no longer clipped away.
+
+Each toast's grid row used `overflow: hidden` throughout its lifetime. The clip
+is load-bearing while the row opens and closes — it makes the toast read as
+folding into the stack — but at rest it hugs the card's border box with zero
+slack on every side and cuts off every shadow the card casts. Astryx's own
+`--shadow-med` was declared and invisible: against a white page, every sampled
+pixel below a stock toast was pure white.
+
+The row now keeps its cross-engine `overflow: hidden` boundary during entry and
+exit, and releases it to `overflow: visible` only after the opening transition
+settles. Dismissal restores the clip synchronously. The wrapper keeps its
+ordinary pointer boundary, so a second click while the toast is still visible
+is absorbed by the toast rather than falling through to an obscured control
+underneath.
+
+This avoids `overflow-clip-margin`, which WebKit 26.5 does not support, while
+preserving the exact paint boundary the exit shipped with before this fix.
+
+Settled state is held on the mounted row rather than in a set of toast ids on
+the viewport, so it cannot outlive the row it describes. A row leaves the DOM
+by more paths than dismissal — `maxVisible` evicts the oldest when a newer
+toast arrives, and a `uniqueID` overwrite swaps a new entry into a replaced
+toast's place — and on neither path does anything on the dismissal path run.
+An evicted toast that resurfaces once the stack drains therefore mounts clipped
+and runs its own entry transition, instead of releasing the clip over a row
+that is still opening.
+
+One further lifecycle guard: only the row's own transition is read, since
+`grid-template-rows` is not private to the wrapper and `transitionend` bubbles
+from any descendant animating its own grid. Reading a descendant's event as the
+row's own releases the clip before the row has finished opening, and during
+exit it unmounts the toast mid-collapse.
+
+Below a settled stock toast on a white page, sampling straight down from the
+card's bottom border box: `255,255,255` at every offset before; after, the
+shadow paints `223` at +0px and fades `237 → 243 → 247 → 250 → 253 → 254`,
+reaching white again at +12px. During exit the row clips again, so anything
+outside the shrinking row is neither painted nor hit-testable — while the
+wrapper itself keeps the ordinary pointer boundary it has always had, and
+still absorbs a click aimed at a toast that is still on screen.
+
+@freddymeta
diff --git a/packages/core/src/Toast/ToastViewport.test.tsx b/packages/core/src/Toast/ToastViewport.test.tsx
index d45ae9b1e4..0045d751cc 100644
--- a/packages/core/src/Toast/ToastViewport.test.tsx
+++ b/packages/core/src/Toast/ToastViewport.test.tsx
@@ -643,6 +643,183 @@ describe('Toast native motion contract', () => {
).toBe('hidden');
});
+ it('releases the shadow clip only while the toast is settled', () => {
+ const {wrapper} = renderMotionToast('bottomEnd', 'Shadow lifecycle');
+ const inner = wrapper.firstElementChild as HTMLElement;
+
+ // Entry starts with main's original clip boundary.
+ expect(getComputedStyle(inner).overflow).toBe('hidden');
+
+ act(() => {
+ fireEvent.transitionEnd(wrapper, {propertyName: 'grid-template-rows'});
+ });
+ expect(getComputedStyle(inner).overflow).toBe('visible');
+
+ act(() => {
+ fireEvent.click(
+ within(wrapper).getByRole('button', {name: 'Dismiss notification'}),
+ );
+ });
+ // Dismissal restores main's original clip boundary synchronously. The
+ // wrapper keeps its normal hit boundary, so a second click while the toast
+ // is still visible is absorbed by the toast rather than falling through
+ // to an obscured control underneath.
+ expect(getComputedStyle(inner).overflow).toBe('hidden');
+ expect(getComputedStyle(wrapper).pointerEvents).toBe('auto');
+ });
+
+ it('settles a toast that replaced another through uniqueID', () => {
+ // An overwrite unmounts the replaced row and mounts a new one with a new
+ // id, so the new row runs its own entry transition and has to release the
+ // clip on its own. The replaced id is dropped at the same time: nothing
+ // dismisses it, so neither removeToast nor handleExited would ever prune
+ // it, and a viewport overwriting one uniqueID on a schedule would collect
+ // a dead id per update for as long as it lives.
+ render(
+ <ToastViewport isTopLayer={false} position="bottomEnd" maxVisible={3}>
+ <ShowToastButton
+ options={{uniqueID: 'save', body: 'Saving changes'}}
+ triggerLabel="Show v1"
+ />
+ <ShowToastButton
+ options={{uniqueID: 'save', body: 'Changes saved'}}
+ triggerLabel="Show v2"
+ />
+ </ToastViewport>,
+ );
+
+ act(() => {
+ fireEvent.click(screen.getByText('Show v1'));
+ });
+ const first = getToastWrapperByText('Saving changes');
+ const firstId = first.getAttribute('data-toast-id');
+ act(() => {
+ fireEvent.transitionEnd(first, {propertyName: 'grid-template-rows'});
+ });
+ expect(
+ getComputedStyle(first.firstElementChild as HTMLElement).overflow,
+ ).toBe('visible');
+
+ act(() => {
+ fireEvent.click(screen.getByText('Show v2'));
+ });
+ const second = getToastWrapperByText('Changes saved');
+ // A different row, so the settled state of the one it replaced says
+ // nothing about it: it starts clipped.
+ expect(second.getAttribute('data-toast-id')).not.toBe(firstId);
+ expect(
+ getComputedStyle(second.firstElementChild as HTMLElement).overflow,
+ ).toBe('hidden');
+
+ act(() => {
+ fireEvent.transitionEnd(second, {propertyName: 'grid-template-rows'});
+ });
+ expect(
+ getComputedStyle(second.firstElementChild as HTMLElement).overflow,
+ ).toBe('visible');
+ });
+
+ it('drops settled state when a toast is evicted and later resurfaces', () => {
+ // A toast beyond `maxVisible` unmounts without ever being dismissed, so
+ // neither removeToast nor handleExited runs for it. When the stack drains
+ // and it resurfaces, it mounts a fresh row that has to run its own entry
+ // transition from `@starting-style` — a settled id left over from its
+ // previous life would release the clip immediately, painting the card's
+ // shadow (and taking pointer input) outside the row that is still opening.
+ render(
+ <ToastViewport isTopLayer={false} position="bottomEnd" maxVisible={1}>
+ <ShowToastButton
+ options={{body: 'First', isAutoHide: false}}
+ triggerLabel="Show first"
+ />
+ <ShowToastButton
+ options={{body: 'Second', isAutoHide: false}}
+ triggerLabel="Show second"
+ />
+ </ToastViewport>,
+ );
+
+ act(() => {
+ fireEvent.click(screen.getByText('Show first'));
+ });
+ const first = getToastWrapperByText('First');
+ act(() => {
+ fireEvent.transitionEnd(first, {propertyName: 'grid-template-rows'});
+ });
+ expect(
+ getComputedStyle(first.firstElementChild as HTMLElement).overflow,
+ ).toBe('visible');
+
+ // Evicted: still in the list, no longer rendered.
+ act(() => {
+ fireEvent.click(screen.getByText('Show second'));
+ });
+ expect(screen.queryByText('First')).not.toBeInTheDocument();
+
+ // Drain the stack so it comes back.
+ const second = getToastWrapperByText('Second');
+ act(() => {
+ fireEvent.transitionEnd(second, {propertyName: 'grid-template-rows'});
+ fireEvent.click(
+ within(second).getByRole('button', {name: 'Dismiss notification'}),
+ );
+ });
+ act(() => {
+ fireEvent.transitionEnd(second, {propertyName: 'grid-template-rows'});
+ });
+
+ const resurfaced = getToastWrapperByText('First');
+ expect(
+ getComputedStyle(resurfaced.firstElementChild as HTMLElement).overflow,
+ ).toBe('hidden');
+
+ // And it settles again on its own transition, rather than being stuck.
+ act(() => {
+ fireEvent.transitionEnd(resurfaced, {propertyName: 'grid-template-rows'});
+ });
+ expect(
+ getComputedStyle(resurfaced.firstElementChild as HTMLElement).overflow,
+ ).toBe('visible');
+ });
+
+ it('ignores a grid-template-rows transition bubbling from a descendant', () => {
+ // `grid-template-rows` is not private to the wrapper: any descendant may
+ // animate its own grid, and transitionend bubbles. Reading a descendant's
+ // event as the row's own releases the clip before the row has opened, and
+ // during exit it unmounts the toast mid-collapse.
+ const {wrapper} = renderMotionToast('bottomEnd', 'Bubbled transition');
+ const inner = wrapper.firstElementChild as HTMLElement;
+ expect(getComputedStyle(inner).overflow).toBe('hidden');
+
+ act(() => {
+ fireEvent.transitionEnd(inner, {propertyName: 'grid-template-rows'});
+ });
+ expect(getComputedStyle(inner).overflow).toBe('hidden');
+
+ // The wrapper's own transition still settles it.
+ act(() => {
+ fireEvent.transitionEnd(wrapper, {propertyName: 'grid-template-rows'});
+ });
+ expect(getComputedStyle(inner).overflow).toBe('visible');
+
+ // Same guard on the exit path: a descendant's event must not cut the
+ // collapse short and unmount the toast early.
+ act(() => {
+ fireEvent.click(
+ within(wrapper).getByRole('button', {name: 'Dismiss notification'}),
+ );
+ });
+ act(() => {
+ fireEvent.transitionEnd(inner, {propertyName: 'grid-template-rows'});
+ });
+ expect(screen.getByText('Bubbled transition')).toBeInTheDocument();
+
+ act(() => {
+ fireEvent.transitionEnd(wrapper, {propertyName: 'grid-template-rows'});
+ });
+ expect(screen.queryByText('Bubbled transition')).not.toBeInTheDocument();
+ });
+
it('keeps reduced motion transitions eventful for exit cleanup', () => {
const toastSource = readFileSync(
'packages/core/src/Toast/Toast.tsx',
diff --git a/packages/core/src/Toast/ToastViewport.tsx b/packages/core/src/Toast/ToastViewport.tsx
index cb121c7d6c..62dbde6729 100644
--- a/packages/core/src/Toast/ToastViewport.tsx
+++ b/packages/core/src/Toast/ToastViewport.tsx
@@ -137,11 +137,16 @@ const styles = stylex.create({
maxWidth: '100%',
minWidth: 0,
minHeight: 0,
- // Required for the 1fr -> 0fr exit collapse to hide the shrinking Toast.
- // This preserves main's paint containment; browser-chrome behavior is not
- // changed or claimed by this responsive-layout fix.
+ // Clip while the grid row opens and closes so the toast reads as folding
+ // into the stack. Once the entry transition settles, the companion style
+ // below releases the clip so the card's shadow can paint. `hidden` works
+ // in every engine and restores the exact exit paint/hit boundary that the
+ // viewport shipped with before the shadow fix.
overflow: 'hidden',
},
+ toastWrapperInnerSettled: {
+ overflow: 'visible',
+ },
});
// Flatten a toast's rendered content (title, description, etc.) to the plain
@@ -181,6 +186,109 @@ export interface ToastViewportProps {
children?: React.ReactNode;
}
+/**
+ * Drop an id from a Set of ids, keeping the Set's identity when the id is not
+ * in it so React can skip the re-render. The viewport tracks two of these
+ * (exiting rows, settled rows) and each has to be pruned from more than one
+ * place.
+ */
+const withoutId =
+ (id: string) =>
+ (prev: Set<string>): Set<string> => {
+ if (!prev.has(id)) {
+ return prev;
+ }
+ const next = new Set(prev);
+ next.delete(id);
+ return next;
+ };
+
+interface ToastRowProps {
+ entry: ToastEntry;
+ isExiting: boolean;
+ /** Top-anchored stacks grow downward and flip the row's drift and gap. */
+ isReversed: boolean;
+ onExited: (id: string) => void;
+ onDismiss: (id: string, reason: ToastDismissReason) => void;
+}
+
+/**
+ * One row of the stack: the grid wrapper that opens and collapses, and the
+ * clip that releases once it is open so the card's elevation can paint.
+ *
+ * Settled state lives here, on the mounted row, rather than in a Set of ids on
+ * the viewport. A row leaves the DOM by more paths than dismissal — `maxVisible`
+ * evicts the oldest when a newer toast arrives, and a uniqueID overwrite swaps
+ * a new entry into a replaced toast's place — and on those paths nothing on the
+ * dismissal path runs to prune it. Holding the flag on the row makes every one
+ * of them the same event: the row unmounts, the flag goes with it, and a toast
+ * that resurfaces when the stack drains mounts clipped and runs its own entry
+ * transition. Re-clipping on dismissal is the same rule read forward, since the
+ * clip is released only while the row is settled *and* not exiting.
+ */
+function ToastRow({
+ entry,
+ isExiting,
+ isReversed,
+ onExited,
+ onDismiss,
+}: ToastRowProps) {
+ const o = entry.options;
+ const type = o.type ?? 'info';
+ const isAutoHide = o.isAutoHide ?? (type === 'error' ? false : true);
+ const dur = o.autoHideDuration ?? 5000;
+ const [isSettled, setIsSettled] = useState(false);
+
+ return (
+ <div
+ data-toast-id={entry.id}
+ {...stylex.props(
+ styles.toastWrapper,
+ isReversed ? styles.toastWrapperFromTop : styles.toastWrapperFromBottom,
+ isReversed ? styles.toastWrapperGapReversed : styles.toastWrapperGap,
+ isExiting && styles.toastWrapperExiting,
+ )}
+ onTransitionEnd={(e: React.TransitionEvent) => {
+ // `grid-template-rows` is not private to this wrapper — any descendant
+ // may animate its own grid, and transitionend bubbles. Only the row's
+ // own transition says the row has finished opening or collapsing.
+ if (e.target !== e.currentTarget) {
+ return;
+ }
+ if (e.propertyName !== 'grid-template-rows') {
+ return;
+ }
+ if (isExiting) {
+ onExited(entry.id);
+ return;
+ }
+ // The row is fully open now. Release only the paint clip; the wrapper
+ // still owns pointer events and restores the clip synchronously when
+ // dismissal starts.
+ setIsSettled(true);
+ }}>
+ <div
+ {...stylex.props(
+ styles.toastWrapperInner,
+ isSettled && !isExiting && styles.toastWrapperInnerSettled,
+ )}>
+ <ToastSurface
+ type={type}
+ body={o.body}
+ endContent={o.endContent}
+ isAutoHide={isAutoHide}
+ autoHideDuration={dur}
+ isExiting={isExiting}
+ gestureDirection={isReversed ? -1 : 1}
+ onDismiss={reason => onDismiss(entry.id, reason)}
+ renderContent={o.renderContent}
+ />
+ </div>
+ </div>
+ );
+}
+ToastRow.displayName = 'ToastRow';
+
/**
* Container that renders and manages toast notifications. Place at the root
* of your app to enable useToast(). Toasts stack with enter/exit
@@ -286,6 +394,10 @@ export function ToastViewport({
// else to the polite region (role="status") — mirrors Toast.tsx.
announce(text, entry.options.type === 'error' ? 'assertive' : 'polite');
}
+ // An overwrite swaps a new entry, with a new id, into the replaced
+ // toast's place, so the old row leaves the DOM without ever being
+ // dismissed. Its settled id is dropped by the prune below, which covers
+ // every silent exit with one rule rather than one branch per path.
setToasts(prev => {
if (uniqueID) {
const existing = prev.find(t => t.options.uniqueID === uniqueID);
@@ -349,14 +461,7 @@ export function ToastViewport({
const handleExited = useCallback((id: string) => {
exitingIdsRef.current.delete(id);
- setExitingIds(prev => {
- if (!prev.has(id)) {
- return prev;
- }
- const next = new Set(prev);
- next.delete(id);
- return next;
- });
+ setExitingIds(withoutId(id));
setToasts(prev => prev.filter(t => t.id !== id));
}, []);
@@ -485,12 +590,6 @@ export function ToastViewport({
? styles.bottomStart
: styles.bottomEnd;
const isReversed = position === 'topEnd' || position === 'topStart';
- const toastWrapperPositionStyle = isReversed
- ? styles.toastWrapperFromTop
- : styles.toastWrapperFromBottom;
- const gapStyle = isReversed
- ? styles.toastWrapperGapReversed
- : styles.toastWrapperGap;
return (
<ToastContext value={contextValue}>
@@ -509,47 +608,16 @@ export function ToastViewport({
style: Object.keys(insetStyle).length > 0 ? insetStyle : undefined,
},
)}>
- {visibleToasts.map(entry => {
- const o = entry.options;
- const type = o.type ?? 'info';
- const isAutoHide = o.isAutoHide ?? (type === 'error' ? false : true);
- const dur = o.autoHideDuration ?? 5000;
- const isExiting = exitingIds.has(entry.id);
- return (
- <div
- key={entry.id}
- data-toast-id={entry.id}
- {...stylex.props(
- styles.toastWrapper,
- toastWrapperPositionStyle,
- gapStyle,
- isExiting && styles.toastWrapperExiting,
- )}
- onTransitionEnd={
- isExiting
- ? (e: React.TransitionEvent) => {
- if (e.propertyName === 'grid-template-rows') {
- handleExited(entry.id);
- }
- }
- : undefined
- }>
- <div {...stylex.props(styles.toastWrapperInner)}>
- <ToastSurface
- type={type}
- body={o.body}
- endContent={o.endContent}
- isAutoHide={isAutoHide}
- autoHideDuration={dur}
- isExiting={isExiting}
- gestureDirection={isReversed ? -1 : 1}
- onDismiss={reason => removeToast(entry.id, reason)}
- renderContent={o.renderContent}
- />
- </div>
- </div>
- );
- })}
+ {visibleToasts.map(entry => (
+ <ToastRow
+ key={entry.id}
+ entry={entry}
+ isExiting={exitingIds.has(entry.id)}
+ isReversed={isReversed}
+ onExited={handleExited}
+ onDismiss={removeToast}
+ />
+ ))}
</div>
</ToastContext>
);