Skip to content

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

TypeScript

fix(Dialog, BottomSheet): a dismissed file picker inside no longer closes them (#7851)

Christopher Chedeau79f4fc70d4a4
diff --git a/.changeset/dialog-sheet-ignore-descendant-cancel.md b/.changeset/dialog-sheet-ignore-descendant-cancel.md
new file mode 100644
index 0000000000..cde34cd795
--- /dev/null
+++ b/.changeset/dialog-sheet-ignore-descendant-cancel.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Dialog, BottomSheet and BottomSheetSwitcher stay open when a file picker inside them is dismissed. A file input fires a bubbling `cancel` event when its picker closes without a choice, and all three took that event for a close request of their own and closed (a switcher closed its whole flow). They now act only on a `cancel` aimed at their own `<dialog>`, so Escape and platform close requests (the Android back gesture) behave as before. No API changes.
+
+@vjeux
diff --git a/packages/core/src/BottomSheet/BottomSheet.test.tsx b/packages/core/src/BottomSheet/BottomSheet.test.tsx
index 9a93ed0235..6652d30386 100644
--- a/packages/core/src/BottomSheet/BottomSheet.test.tsx
+++ b/packages/core/src/BottomSheet/BottomSheet.test.tsx
@@ -498,6 +498,35 @@ describe('BottomSheet', () => {
expect(dialog).toHaveStyle({'--_sheet-scrim-opacity': '1'});
});
+ it('stays open when a file picker inside it is dismissed, and still closes on its own close request', () => {
+ // A file input fires a bubbling `cancel` at itself when its picker is
+ // dismissed. It reaches the <dialog> like a close request but is not one.
+ const onOpenChange = vi.fn();
+ render(
+ <BottomSheet
+ isOpen
+ purpose="form"
+ onOpenChange={onOpenChange}
+ label="Upload">
+ <input type="file" aria-label="Picture" />
+ </BottomSheet>,
+ );
+
+ const fromPicker = new Event('cancel', {bubbles: true, cancelable: true});
+ fireEvent(screen.getByLabelText('Picture'), fromPicker);
+
+ expect(onOpenChange).not.toHaveBeenCalled();
+ expect(fromPicker.defaultPrevented).toBe(false);
+
+ fireEvent(
+ screen.getByRole('dialog'),
+ new Event('cancel', {cancelable: true}),
+ );
+
+ expect(onOpenChange).toHaveBeenCalledTimes(1);
+ expect(onOpenChange).toHaveBeenCalledWith(false);
+ });
+
it('keeps a standalone modal sheet presented through its exit animation', () => {
render(<ExitHarness />);
const dialog = screen.getByRole('dialog', {name: 'Filters'});
diff --git a/packages/core/src/BottomSheet/BottomSheet.tsx b/packages/core/src/BottomSheet/BottomSheet.tsx
index 921e42c43f..c091002eab 100644
--- a/packages/core/src/BottomSheet/BottomSheet.tsx
+++ b/packages/core/src/BottomSheet/BottomSheet.tsx
@@ -361,12 +361,16 @@ function StandaloneBottomSheet({
});
// The native `cancel` event is a close request the stack never saw a press
- // for (Android back, the platform close watcher). Always preventDefault so
- // the browser cannot close the controlled <dialog>, then answer with the
- // stack's rules: only the top-most layer dismisses, and nothing dismisses
- // during an IME composition.
+ // for (Android back, the platform close watcher). One that bubbles up from
+ // inside is not: a file input fires it when its picker is dismissed, so it is
+ // left alone. Otherwise always preventDefault so the browser cannot close the
+ // controlled <dialog>, then answer with the stack's rules: only the top-most
+ // layer dismisses, and nothing dismisses during an IME composition.
const handleCancel = useCallback(
(event: React.SyntheticEvent<HTMLDialogElement>) => {
+ if (event.target !== event.currentTarget) {
+ return;
+ }
event.preventDefault();
if (shouldDismissOnCloseRequest()) {
dismissOnEscape();
diff --git a/packages/core/src/BottomSheet/BottomSheetSwitcher.test.tsx b/packages/core/src/BottomSheet/BottomSheetSwitcher.test.tsx
index e5e4900beb..0e2f01d7c9 100644
--- a/packages/core/src/BottomSheet/BottomSheetSwitcher.test.tsx
+++ b/packages/core/src/BottomSheet/BottomSheetSwitcher.test.tsx
@@ -708,6 +708,32 @@ describe('BottomSheetSwitcher', () => {
expect(onActiveSheetChange).toHaveBeenNthCalledWith(2, null);
});
+ it('keeps the flow open when a file picker inside a step is dismissed, and still closes on its own close request', () => {
+ // A file input fires a bubbling `cancel` at itself when its picker is
+ // dismissed. A step renders no dialog, so it reaches the switcher's.
+ const onActiveSheetChange = vi.fn();
+ render(
+ <BottomSheetSwitcher
+ activeSheet="upload"
+ onActiveSheetChange={onActiveSheetChange}>
+ <BottomSheet sheetId="upload" label="Upload" purpose="form">
+ <input type="file" aria-label="Picture" />
+ </BottomSheet>
+ </BottomSheetSwitcher>,
+ );
+
+ const fromPicker = new Event('cancel', {bubbles: true, cancelable: true});
+ fireEvent(screen.getByLabelText('Picture'), fromPicker);
+
+ expect(onActiveSheetChange).not.toHaveBeenCalled();
+ expect(fromPicker.defaultPrevented).toBe(false);
+
+ fireEvent(getSharedDialog(), new Event('cancel', {cancelable: true}));
+
+ expect(onActiveSheetChange).toHaveBeenCalledTimes(1);
+ expect(onActiveSheetChange).toHaveBeenCalledWith(null);
+ });
+
it('honors purpose=required for a switcher-managed sheet', () => {
const onActiveSheetChange = vi.fn();
render(
diff --git a/packages/core/src/BottomSheet/BottomSheetSwitcher.tsx b/packages/core/src/BottomSheet/BottomSheetSwitcher.tsx
index 8106421861..3b4df57075 100644
--- a/packages/core/src/BottomSheet/BottomSheetSwitcher.tsx
+++ b/packages/core/src/BottomSheet/BottomSheetSwitcher.tsx
@@ -591,8 +591,14 @@ export function BottomSheetSwitcher({
? undefined
: sheetLabels.get(visibleTransition.retainedSheet));
+ // A `cancel` that bubbles up from a step is not a close request for the
+ // flow: a file input fires one when its picker is dismissed, and a step
+ // renders no dialog of its own, so it lands here. It is left alone.
const handleCancel = useCallback(
(event: SyntheticEvent<HTMLDialogElement>) => {
+ if (event.target !== event.currentTarget) {
+ return;
+ }
event.preventDefault();
if (shouldDismissOnCloseRequest()) {
dismissOnEscape();
diff --git a/packages/core/src/Dialog/Dialog.test.tsx b/packages/core/src/Dialog/Dialog.test.tsx
index 5828d165db..b676be0215 100644
--- a/packages/core/src/Dialog/Dialog.test.tsx
+++ b/packages/core/src/Dialog/Dialog.test.tsx
@@ -286,6 +286,45 @@ describe('Dialog', () => {
});
});
+ describe('a cancel from inside the dialog', () => {
+ // A file input fires a bubbling `cancel` at itself when its picker is
+ // dismissed (Escape, or Cancel in the chooser). It reaches the <dialog>
+ // like a close request but is not one.
+ function DialogWithFilePicker({onOpenChange}: {onOpenChange: () => void}) {
+ return (
+ <Dialog isOpen onOpenChange={onOpenChange} aria-label="Upload">
+ <input type="file" aria-label="Picture" />
+ </Dialog>
+ );
+ }
+
+ it('stays open when a file picker inside it is dismissed', () => {
+ const onOpenChange = vi.fn();
+ render(<DialogWithFilePicker onOpenChange={onOpenChange} />);
+
+ const fromPicker = new Event('cancel', {bubbles: true, cancelable: true});
+ screen.getByLabelText('Picture').dispatchEvent(fromPicker);
+
+ expect(onOpenChange).not.toHaveBeenCalled();
+ expect(fromPicker.defaultPrevented).toBe(false);
+ });
+
+ it('still closes on its own close request', () => {
+ const onOpenChange = vi.fn();
+ render(<DialogWithFilePicker onOpenChange={onOpenChange} />);
+
+ screen
+ .getByLabelText('Picture')
+ .dispatchEvent(new Event('cancel', {bubbles: true, cancelable: true}));
+ screen
+ .getByRole('dialog')
+ .dispatchEvent(new Event('cancel', {cancelable: true}));
+
+ expect(onOpenChange).toHaveBeenCalledTimes(1);
+ expect(onOpenChange).toHaveBeenCalledWith(false);
+ });
+ });
+
describe('IME composition', () => {
// A CJK user presses Escape to cancel a half-formed character several times
// a sentence. jsdom models neither composition nor the close watcher, so
diff --git a/packages/core/src/Dialog/Dialog.tsx b/packages/core/src/Dialog/Dialog.tsx
index 0803c1cfa4..397cbd028d 100644
--- a/packages/core/src/Dialog/Dialog.tsx
+++ b/packages/core/src/Dialog/Dialog.tsx
@@ -643,11 +643,16 @@ export function Dialog({
// Escape presses the stack owns never arrive here — it preventDefault()s
// those, which suppresses the close watcher.
//
- // Always preventDefault so the browser cannot close a controlled <dialog>
- // behind React's back, then answer the request with the stack's own rules:
- // only the top-most layer dismisses, a `required` dialog swallows it, and
- // nothing dismisses while an IME composition is in progress.
+ // A `cancel` that bubbles up from inside is not a close request for this
+ // dialog: a file input fires one when its picker is dismissed. It is left
+ // alone. Otherwise always preventDefault so the browser cannot close a
+ // controlled <dialog> behind React's back, then answer the request with the
+ // stack's own rules: only the top-most layer dismisses, a `required` dialog
+ // swallows it, and nothing dismisses while an IME composition is in progress.
const handleCancel = (event: React.SyntheticEvent<HTMLDialogElement>) => {
+ if (event.target !== event.currentTarget) {
+ return;
+ }
event.preventDefault();
if (!shouldDismissOnCloseRequest()) {
return;