Skip to content

Commit b513a19

Browse files
authored
Fix prevented show event disables modals with fade class from being displayed again (#34085)
Fix modal, in case is faded, a prevented show event can cause show method to not be executed again.
1 parent 1366659 commit b513a19

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

js/src/modal.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,20 @@ class Modal extends BaseComponent {
108108
return
109109
}
110110

111-
if (this._isAnimated()) {
112-
this._isTransitioning = true
113-
}
114-
115111
const showEvent = EventHandler.trigger(this._element, EVENT_SHOW, {
116112
relatedTarget
117113
})
118114

119-
if (this._isShown || showEvent.defaultPrevented) {
115+
if (showEvent.defaultPrevented) {
120116
return
121117
}
122118

123119
this._isShown = true
124120

121+
if (this._isAnimated()) {
122+
this._isTransitioning = true
123+
}
124+
125125
scrollBarHide()
126126

127127
document.body.classList.add(CLASS_NAME_OPEN)

js/tests/unit/modal.spec.js

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,43 @@ describe('Modal', () => {
203203
modal.show()
204204
})
205205

206+
it('should be shown after the first call to show() has been prevented while fading is enabled ', done => {
207+
fixtureEl.innerHTML = '<div class="modal fade"><div class="modal-dialog"></div></div>'
208+
209+
const modalEl = fixtureEl.querySelector('.modal')
210+
const modal = new Modal(modalEl)
211+
212+
let prevented = false
213+
modalEl.addEventListener('show.bs.modal', e => {
214+
if (!prevented) {
215+
e.preventDefault()
216+
prevented = true
217+
218+
setTimeout(() => {
219+
modal.show()
220+
})
221+
}
222+
})
223+
224+
modalEl.addEventListener('shown.bs.modal', () => {
225+
expect(prevented).toBeTrue()
226+
expect(modal._isAnimated()).toBeTrue()
227+
done()
228+
})
229+
230+
modal.show()
231+
})
232+
206233
it('should set is transitioning if fade class is present', done => {
207234
fixtureEl.innerHTML = '<div class="modal fade"><div class="modal-dialog"></div></div>'
208235

209236
const modalEl = fixtureEl.querySelector('.modal')
210237
const modal = new Modal(modalEl)
211238

212239
modalEl.addEventListener('show.bs.modal', () => {
213-
expect(modal._isTransitioning).toEqual(true)
240+
setTimeout(() => {
241+
expect(modal._isTransitioning).toEqual(true)
242+
})
214243
})
215244

216245
modalEl.addEventListener('shown.bs.modal', () => {

0 commit comments

Comments
 (0)