Skip to content

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

TypeScript

fix(core): a BottomSheet exit the engine retargets mid-flight keeps sliding off (#8349)

Christopher Chedeauba5866a50de3
diff --git a/.changeset/bottom-sheet-exit-retarget.md b/.changeset/bottom-sheet-exit-retarget.md
new file mode 100644
index 0000000000..2fcfc66b1e
--- /dev/null
+++ b/.changeset/bottom-sheet-exit-retarget.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] BottomSheet: a sheet whose height moves by a fraction of a pixel as it starts closing now slides off instead of vanishing. This happens in Chromium, for example, when a sheet that fits its content is dismissed by a drag. The browser replaces the exit's transform transition when that transition's end value changes mid-flight. The exit treated the cancelled transition as its end and closed the dialog before the sheet had moved. It now waits for the replacement transition to finish.
+
+@vjeux
diff --git a/packages/core/src/BottomSheet/BottomSheetPanel.test.tsx b/packages/core/src/BottomSheet/BottomSheetPanel.test.tsx
index 8ad6cb50b5..9e8674d505 100644
--- a/packages/core/src/BottomSheet/BottomSheetPanel.test.tsx
+++ b/packages/core/src/BottomSheet/BottomSheetPanel.test.tsx
@@ -157,6 +157,31 @@ describe('BottomSheetPanel', () => {
expect(onMotionComplete).toHaveBeenLastCalledWith('exiting');
});
+ it('keeps exiting when the engine replaces the exit transition mid-flight', () => {
+ const onMotionComplete = vi.fn();
+ renderPanel({kind: 'exiting'}, {onMotionComplete});
+ const panel = getPanel();
+ // Chromium cancels a running transition whose end value changes, and
+ // starts one from where it was. The exit's percentage transform changes
+ // when the closing frame moves the sheet's height by a fraction of a pixel.
+ panel.getAnimations = () =>
+ [{transitionProperty: 'transform', playState: 'running'}] as never;
+ fireEvent.transitionCancel(panel, {propertyName: 'transform'});
+ expect(onMotionComplete).not.toHaveBeenCalled();
+ panel.getAnimations = () => [];
+ fireEvent.transitionEnd(panel, {propertyName: 'transform'});
+ expect(onMotionComplete).toHaveBeenCalledWith('exiting');
+ });
+
+ it('ends the exit when its transition is cancelled with nothing running on', () => {
+ const onMotionComplete = vi.fn();
+ renderPanel({kind: 'exiting'}, {onMotionComplete});
+ const panel = getPanel();
+ panel.getAnimations = () => [];
+ fireEvent.transitionCancel(panel, {propertyName: 'transform'});
+ expect(onMotionComplete).toHaveBeenCalledWith('exiting');
+ });
+
it('completes immediately when the rendered transition is disabled', () => {
// The stylesheet decides reduced motion (its transform transition
// collapses under the preference); the gesture hook no longer pins an
diff --git a/packages/core/src/BottomSheet/BottomSheetPanel.tsx b/packages/core/src/BottomSheet/BottomSheetPanel.tsx
index 03188f7e7c..625403f815 100644
--- a/packages/core/src/BottomSheet/BottomSheetPanel.tsx
+++ b/packages/core/src/BottomSheet/BottomSheetPanel.tsx
@@ -418,6 +418,7 @@ function waitForTransition(
let done = false;
let timer: ReturnType<typeof setTimeout> | null = null;
+ let backstopMs = 0;
const finish = () => {
if (done) {
return;
@@ -431,9 +432,24 @@ function waitForTransition(
complete();
};
const handleTransitionEnd = (event: TransitionEvent) => {
- if (event.target === element && event.propertyName === propertyName) {
- finish();
+ if (event.target !== element || event.propertyName !== propertyName) {
+ return;
+ }
+ // The engine cancels a transition whose end value changes mid-flight. A
+ // percentage transform does when the closing frame moves the height by a
+ // fraction of a pixel. A replacement runs on from where it was. That one
+ // ends the motion, not the cancel.
+ if (
+ event.type === 'transitioncancel' &&
+ hasRunningTransition(element, propertyName)
+ ) {
+ if (timer != null) {
+ clearTimeout(timer);
+ timer = setTimeout(finish, backstopMs);
+ }
+ return;
}
+ finish();
};
element.addEventListener('transitionend', handleTransitionEnd);
element.addEventListener('transitioncancel', handleTransitionEnd);
@@ -484,7 +500,8 @@ function waitForTransition(
return () => {};
}
- timer = setTimeout(finish, transitionMs + TRANSITION_BACKSTOP_BUFFER_MS);
+ backstopMs = transitionMs + TRANSITION_BACKSTOP_BUFFER_MS;
+ timer = setTimeout(finish, backstopMs);
return () => {
if (timer != null) {
clearTimeout(timer);
@@ -494,6 +511,19 @@ function waitForTransition(
};
}
+// Whether a CSS transition of `propertyName` still runs on `element`.
+function hasRunningTransition(
+ element: HTMLElement,
+ propertyName: string,
+): boolean {
+ return (element.getAnimations?.() ?? []).some(
+ animation =>
+ (animation as Animation & {transitionProperty?: string})
+ .transitionProperty === propertyName &&
+ (animation.playState === 'running' || animation.playState === 'paused'),
+ );
+}
+
function parseTransitionTime(value: string): number | null {
const normalizedValue = value.trim();
if (!/^-?(?:\d+|\d*\.\d+)(?:ms|s)$/.test(normalizedValue)) {