Skip to content

Commit 23fb7a7

Browse files
authored
Fix modal event-listeners during dismiss click (#36863)
ref: #36855
1 parent 9494569 commit 23fb7a7

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

.bundlewatch.config.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
},
3535
{
3636
"path": "./dist/js/bootstrap.bundle.js",
37-
"maxSize": "43.0 kB"
37+
"maxSize": "43.25 kB"
3838
},
3939
{
4040
"path": "./dist/js/bootstrap.bundle.min.js",
@@ -50,7 +50,7 @@
5050
},
5151
{
5252
"path": "./dist/js/bootstrap.js",
53-
"maxSize": "28.5 kB"
53+
"maxSize": "28.75 kB"
5454
},
5555
{
5656
"path": "./dist/js/bootstrap.min.js",

js/src/modal.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const EVENT_HIDDEN = `hidden${EVENT_KEY}`
3030
const EVENT_SHOW = `show${EVENT_KEY}`
3131
const EVENT_SHOWN = `shown${EVENT_KEY}`
3232
const EVENT_RESIZE = `resize${EVENT_KEY}`
33+
const EVENT_CLICK_DISMISS = `click.dismiss${EVENT_KEY}`
3334
const EVENT_MOUSEDOWN_DISMISS = `mousedown.dismiss${EVENT_KEY}`
3435
const EVENT_KEYDOWN_DISMISS = `keydown.dismiss${EVENT_KEY}`
3536
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
@@ -222,18 +223,21 @@ class Modal extends BaseComponent {
222223
})
223224

224225
EventHandler.on(this._element, EVENT_MOUSEDOWN_DISMISS, event => {
225-
if (event.target !== event.currentTarget) { // click is inside modal-dialog
226-
return
227-
}
228-
229-
if (this._config.backdrop === 'static') {
230-
this._triggerBackdropTransition()
231-
return
232-
}
233-
234-
if (this._config.backdrop) {
235-
this.hide()
236-
}
226+
EventHandler.one(this._element, EVENT_CLICK_DISMISS, event2 => {
227+
// a bad trick to segregate clicks that may start inside dialog but end outside, and avoid listen to scrollbar clicks
228+
if (this._dialog.contains(event.target) || this._dialog.contains(event2.target)) {
229+
return
230+
}
231+
232+
if (this._config.backdrop === 'static') {
233+
this._triggerBackdropTransition()
234+
return
235+
}
236+
237+
if (this._config.backdrop) {
238+
this.hide()
239+
}
240+
})
237241
})
238242
}
239243

js/tests/unit/modal.spec.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,9 @@ describe('Modal', () => {
644644
const mouseDown = createEvent('mousedown')
645645

646646
modalEl.dispatchEvent(mouseDown)
647+
modalEl.click()
647648
modalEl.dispatchEvent(mouseDown)
649+
modalEl.click()
648650

649651
setTimeout(() => {
650652
expect(spy).toHaveBeenCalledTimes(1)
@@ -719,9 +721,11 @@ describe('Modal', () => {
719721
const mouseDown = createEvent('mousedown')
720722

721723
dialogEl.dispatchEvent(mouseDown)
724+
modalEl.click()
722725
expect(spy).not.toHaveBeenCalled()
723726

724727
modalEl.dispatchEvent(mouseDown)
728+
modalEl.click()
725729
expect(spy).toHaveBeenCalled()
726730
resolve()
727731
})

0 commit comments

Comments
 (0)