Skip to content

Commit

Permalink
fix(modal): fix modal event-listeners on dismiss click
Browse files Browse the repository at this point in the history
ref: #36855
  • Loading branch information
GeoSot committed Sep 7, 2022
1 parent 9494569 commit 7f39709
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
32 changes: 18 additions & 14 deletions js/src/modal.js
Expand Up @@ -30,7 +30,8 @@ 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_MOUSEDOWN_DISMISS = `mousedown.dismiss${EVENT_KEY}`
const EVENT_CLICK_DISMISS = `click.dismiss${EVENT_KEY}`
const EVENT_MOUSDOWN_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 @@ -221,19 +222,22 @@ 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.on(this._element, EVENT_MOUSDOWN_DISMISS, event => {
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

0 comments on commit 7f39709

Please sign in to comment.