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 when is triggered by bs-toggle, to hide other open instances #34701

Merged
merged 3 commits into from
Aug 10, 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
7 changes: 7 additions & 0 deletions js/src/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const CLASS_NAME_FADE = 'fade'
const CLASS_NAME_SHOW = 'show'
const CLASS_NAME_STATIC = 'modal-static'

const OPEN_SELECTOR = '.modal.show'
const SELECTOR_DIALOG = '.modal-dialog'
const SELECTOR_MODAL_BODY = '.modal-body'
const SELECTOR_DATA_TOGGLE = '[data-bs-toggle="modal"]'
Expand Down Expand Up @@ -411,6 +412,12 @@ EventHandler.on(document, EVENT_CLICK_DATA_API, SELECTOR_DATA_TOGGLE, function (
})
})

// avoid conflict when clicking moddal toggler while another one is open
GeoSot marked this conversation as resolved.
Show resolved Hide resolved
const allReadyOpen = SelectorEngine.findOne(OPEN_SELECTOR)
GeoSot marked this conversation as resolved.
Show resolved Hide resolved
if (allReadyOpen) {
Modal.getInstance(allReadyOpen).hide()
}

const data = Modal.getOrCreateInstance(target)

data.toggle(this)
Expand Down
23 changes: 23 additions & 0 deletions js/tests/unit/modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,29 @@ describe('Modal', () => {

trigger.click()
})

it('should call hide first, if another modal is open', done => {
fixtureEl.innerHTML = [
'<button data-bs-toggle="modal" data-bs-target="#modal2"></button>',
'<div id="modal1" class="modal fade"><div class="modal-dialog"></div></div>',
'<div id="modal2" class="modal"><div class="modal-dialog"></div></div>'
].join('')

const trigger2 = fixtureEl.querySelector('button')
const modalEl1 = document.querySelector('#modal1')
const modalEl2 = document.querySelector('#modal2')
const modal1 = new Modal(modalEl1)

modalEl1.addEventListener('shown.bs.modal', () => {
trigger2.click()
})
modalEl1.addEventListener('hidden.bs.modal', () => {
expect(Modal.getInstance(modalEl2)).not.toBeNull()
expect(modalEl2.classList.contains('show')).toBeTrue()
done()
})
modal1.show()
})
})

describe('jQueryInterface', () => {
Expand Down
2 changes: 1 addition & 1 deletion site/content/docs/5.1/components/modal.md
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ Toggle between multiple modals with some clever placement of the `data-bs-target
Hide this modal and show the first with the button below.
</div>
<div class="modal-footer">
<button class="btn btn-primary" data-bs-target="#exampleModalToggle" data-bs-toggle="modal" data-bs-dismiss="modal">Back to first</button>
<button class="btn btn-primary" data-bs-target="#exampleModalToggle" data-bs-toggle="modal">Back to first</button>
</div>
</div>
</div>
Expand Down