Skip to content

Fix prevented show event disables modals with fade class from being displayed again #34085

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

Merged
merged 3 commits into from
May 24, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions js/src/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,20 @@ class Modal extends BaseComponent {
return
}

if (this._isAnimated()) {
this._isTransitioning = true
}

const showEvent = EventHandler.trigger(this._element, EVENT_SHOW, {
relatedTarget
})

if (this._isShown || showEvent.defaultPrevented) {
if (showEvent.defaultPrevented) {
return
}

this._isShown = true

if (this._isAnimated()) {
this._isTransitioning = true
}

scrollBarHide()

document.body.classList.add(CLASS_NAME_OPEN)
Expand Down
31 changes: 30 additions & 1 deletion js/tests/unit/modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,43 @@ describe('Modal', () => {
modal.show()
})

it('should be shown after the first call to show() has been prevented while fading is enabled ', done => {
fixtureEl.innerHTML = '<div class="modal fade"><div class="modal-dialog"></div></div>'

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

let prevented = false
modalEl.addEventListener('show.bs.modal', e => {
if (!prevented) {
e.preventDefault()
prevented = true

setTimeout(() => {
modal.show()
})
}
})

modalEl.addEventListener('shown.bs.modal', () => {
expect(prevented).toBeTrue()
expect(modal._isAnimated()).toBeTrue()
done()
})

modal.show()
})

it('should set is transitioning if fade class is present', done => {
fixtureEl.innerHTML = '<div class="modal fade"><div class="modal-dialog"></div></div>'

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

modalEl.addEventListener('show.bs.modal', () => {
expect(modal._isTransitioning).toEqual(true)
setTimeout(() => {
expect(modal._isTransitioning).toEqual(true)
})
})

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