Skip to content

fix: Ignore presses retargeted to the document element in dismissable modals - #10511

Open
FrancoKaddour wants to merge 1 commit into
adobe:mainfrom
FrancoKaddour:fix/modal-dismiss-retargeted-press
Open

fix: Ignore presses retargeted to the document element in dismissable modals#10511
FrancoKaddour wants to merge 1 commit into
adobe:mainfrom
FrancoKaddour:fix/modal-dismiss-retargeted-press

Conversation

@FrancoKaddour

Copy link
Copy Markdown
Contributor

Closes #10510

🧢 Your Project

N/A — fixing a bug reported by a user of react-aria-components.

📝 Description

When the element being pressed inside a dismissable ModalOverlay is removed from the DOM mid-press (e.g. a row button swapped out when an async mutation resolves), the browser retargets the press events: both pointerdown and click arrive with the document element as their target. isValidEvent in useInteractOutside then treats the press as a genuine outside interaction:

  • the not-in-document guard passes, because documentElement.contains(documentElement) is true, and
  • event.composedPath() for a root-targeted event never includes the overlay ref.

Both events "look outside", so the modal dismisses under the user's pointer.

This PR scopes the fix to useModalOverlay: a modal's underlay covers the viewport, so the document element is never a legitimate outside-press target for a modal — it only shows up as one via mid-press retargeting. The hook now composes shouldCloseOnInteractOutside to reject the document element before delegating to a user-provided predicate (so the existing escape hatch keeps working).

The check deliberately does not live in useInteractOutside/isValidEvent: non-modal overlays must keep treating the document element as a valid outside target, because on pages with a short <body>, presses below the body target the document element and should still dismiss a popover — that's the reason the guard was widened from body to documentElement in #1369 (fixing #1367). This change leaves popovers untouched.

🧪 Test plan

Two new tests in useModalOverlay.test.js, parameterized over the existing Mouse/Pointer/Touch event matrix (6 cases):

  1. A press fully retargeted to the document element does not close the modal.
  2. It does not close even when a user-provided shouldCloseOnInteractOutside returns true.

Both fail on main (the modal closes) and pass with this change. No regressions: packages/react-aria/test/overlays/ (197 tests) and react-aria-components Dialog/Popover suites (27 tests) pass.

🚀 How to test locally

yarn jest packages/react-aria/test/overlays/useModalOverlay.test.js

✅ PR Checklist

  • Included link to corresponding issue
  • Added/updated unit tests
  • Filled out test instructions (above)
  • Updated documentation (no doc changes needed — no API change)

@snowystinger snowystinger left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR
Looks like lint is failing

expect(onOpenChange).not.toHaveBeenCalled();
});

it('should not hide the overlay when a press is retargeted to the document element', function () {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move these tests up to the RAC level and demonstrate the use case you brought up?

Comment on lines +77 to +79
pressStart(document.documentElement);
pressEnd(document.documentElement);
fireEvent.click(document.documentElement);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how is this pressing an element that is swapped mid press? it's clicking the documentElement in every event

// would close the modal under the user's pointer. Non-modal overlays must keep
// treating it as valid: on pages with a short body, presses below the body
// target the document element and should still dismiss (see #1367).
if (element === element.ownerDocument.documentElement) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happened to the isConnected check you proposed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Dismissable modal closes when you press a button the app removes mid-press

2 participants