Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix modal event-listeners during dismiss click #36863

Merged
merged 3 commits into from Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .bundlewatch.config.json
Expand Up @@ -34,7 +34,7 @@
},
{
"path": "./dist/js/bootstrap.bundle.js",
"maxSize": "43.0 kB"
"maxSize": "43.25 kB"
},
{
"path": "./dist/js/bootstrap.bundle.min.js",
Expand All @@ -50,7 +50,7 @@
},
{
"path": "./dist/js/bootstrap.js",
"maxSize": "28.5 kB"
"maxSize": "28.75 kB"
},
{
"path": "./dist/js/bootstrap.min.js",
Expand Down
28 changes: 16 additions & 12 deletions js/src/modal.js
Expand Up @@ -30,6 +30,7 @@ const EVENT_HIDDEN = `hidden${EVENT_KEY}`
const EVENT_SHOW = `show${EVENT_KEY}`
const EVENT_SHOWN = `shown${EVENT_KEY}`
const EVENT_RESIZE = `resize${EVENT_KEY}`
const EVENT_CLICK_DISMISS = `click.dismiss${EVENT_KEY}`
const EVENT_MOUSEDOWN_DISMISS = `mousedown.dismiss${EVENT_KEY}`
const EVENT_KEYDOWN_DISMISS = `keydown.dismiss${EVENT_KEY}`
const EVENT_CLICK_DATA_API = `click${EVENT_KEY}${DATA_API_KEY}`
Expand Down Expand Up @@ -222,18 +223,21 @@ class Modal extends BaseComponent {
})

EventHandler.on(this._element, EVENT_MOUSEDOWN_DISMISS, event => {
if (event.target !== event.currentTarget) { // click is inside modal-dialog
return
}

if (this._config.backdrop === 'static') {
this._triggerBackdropTransition()
return
}

if (this._config.backdrop) {
this.hide()
}
EventHandler.one(this._element, EVENT_CLICK_DISMISS, event2 => {
// a bad trick to segregate clicks that may start inside dialog but end outside, and avoid listen to scrollbar clicks
if (this._dialog.contains(event.target) || this._dialog.contains(event2.target)) {
return
}

if (this._config.backdrop === 'static') {
this._triggerBackdropTransition()
return
}

if (this._config.backdrop) {
this.hide()
}
})
})
}

Expand Down
4 changes: 4 additions & 0 deletions js/tests/unit/modal.spec.js
Expand Up @@ -644,7 +644,9 @@ describe('Modal', () => {
const mouseDown = createEvent('mousedown')

modalEl.dispatchEvent(mouseDown)
modalEl.click()
modalEl.dispatchEvent(mouseDown)
modalEl.click()

setTimeout(() => {
expect(spy).toHaveBeenCalledTimes(1)
Expand Down Expand Up @@ -719,9 +721,11 @@ describe('Modal', () => {
const mouseDown = createEvent('mousedown')

dialogEl.dispatchEvent(mouseDown)
modalEl.click()
expect(spy).not.toHaveBeenCalled()

modalEl.dispatchEvent(mouseDown)
modalEl.click()
expect(spy).toHaveBeenCalled()
resolve()
})
Expand Down