-
-
Notifications
You must be signed in to change notification settings - Fork 143
Description
When using the Modal component, if a user starts a drag operation inside the modal (e.g., selecting text in an input field or dragging a range slider) and their mouse moves outside the modal boundary, the modal closes unexpectedly when they release the mouse button.
Reproduction steps
- Open a modal with an input field containing text
- Click and hold inside the input field to start selecting text
- While holding the mouse button, drag the cursor outside the modal boundary
- Release the mouse button
- Expected: Text selection completes, modal stays open
- Actual: Modal closes
The current backdropClose implementation closes the modal on any click event that occurs on the dialog backdrop. When a user starts their interaction inside the modal (mousedown inside) but finishes outside (mouseup on backdrop), the browser still fires a click event on the backdrop, triggering the modal to close.
I've successfully fixed this by extending the Modal controller and overriding the backdropClose method to track where the mousedown event started, and only close the modal if both the mousedown and click events occurred on the backdrop:
connect() {
super.connect();
this.mouseDownTarget = null;
}
backdropClose(event) {
// Track where mousedown occurred
if (event.type === 'mousedown') {
this.mouseDownTarget = event.target;
return;
}
// Only close if both mousedown and click happened on the backdrop
if (event.type === 'click') {
const isBackdrop = event.target.nodeName === 'DIALOG';
const mouseDownOnBackdrop = this.mouseDownTarget?.nodeName === 'DIALOG';
if (isBackdrop && mouseDownOnBackdrop) {
this.close();
}
this.mouseDownTarget = null;
}
}This also requires updating the modal template to listen for both events:
data-action="mousedown->modal#backdropClose click->modal#backdropClose"This prevents accidental closures while still allowing users to click the backdrop to dismiss the modal. I'm happy to submit a PR with this fix if you're open to it.