Skip to content

Commit

Permalink
Don't hide modal when keyboard is set to false in modal's configurati…
Browse files Browse the repository at this point in the history
…on (#32179)

* Don't hide modal when config.keyboard is false

* Update unit test

- Modal should not be closed when pressing esc key if keyboard = false
  and backdrop is 'static'

Co-authored-by: XhmikosR <xhmikosr@gmail.com>
  • Loading branch information
rohit2sharma95 and XhmikosR committed Nov 20, 2020
1 parent 2630b05 commit 0839cbf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 29 deletions.
52 changes: 26 additions & 26 deletions js/src/modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,11 @@ class Modal {
return
}

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

if (animate) {
Expand Down Expand Up @@ -404,35 +408,31 @@ class Modal {
}

_triggerBackdropTransition() {
if (this._config.backdrop === 'static') {
const hideEvent = EventHandler.trigger(this._element, EVENT_HIDE_PREVENTED)
if (hideEvent.defaultPrevented) {
return
}
const hideEvent = EventHandler.trigger(this._element, EVENT_HIDE_PREVENTED)
if (hideEvent.defaultPrevented) {
return
}

const isModalOverflowing = this._element.scrollHeight > document.documentElement.clientHeight
const isModalOverflowing = this._element.scrollHeight > document.documentElement.clientHeight

if (!isModalOverflowing) {
this._element.style.overflowY = 'hidden'
}

this._element.classList.add(CLASS_NAME_STATIC)
const modalTransitionDuration = getTransitionDurationFromElement(this._dialog)
EventHandler.off(this._element, TRANSITION_END)
EventHandler.one(this._element, TRANSITION_END, () => {
this._element.classList.remove(CLASS_NAME_STATIC)
if (!isModalOverflowing) {
this._element.style.overflowY = 'hidden'
EventHandler.one(this._element, TRANSITION_END, () => {
this._element.style.overflowY = ''
})
emulateTransitionEnd(this._element, modalTransitionDuration)
}

this._element.classList.add(CLASS_NAME_STATIC)
const modalTransitionDuration = getTransitionDurationFromElement(this._dialog)
EventHandler.off(this._element, TRANSITION_END)
EventHandler.one(this._element, TRANSITION_END, () => {
this._element.classList.remove(CLASS_NAME_STATIC)
if (!isModalOverflowing) {
EventHandler.one(this._element, TRANSITION_END, () => {
this._element.style.overflowY = ''
})
emulateTransitionEnd(this._element, modalTransitionDuration)
}
})
emulateTransitionEnd(this._element, modalTransitionDuration)
this._element.focus()
} else {
this.hide()
}
})
emulateTransitionEnd(this._element, modalTransitionDuration)
this._element.focus()
}

// ----------------------------------------------------------------------
Expand Down
5 changes: 2 additions & 3 deletions js/tests/unit/modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,12 +595,11 @@ describe('Modal', () => {
modal.show()
})

it('should not close modal when escape key is pressed with keyboard = false and backdrop = static', done => {
fixtureEl.innerHTML = '<div class="modal" data-bs-backdrop="static" data-bs-keyboard="false"><div class="modal-dialog"></div>'
it('should not close modal when escape key is pressed with keyboard = false', done => {
fixtureEl.innerHTML = '<div class="modal"><div class="modal-dialog"></div></div>'

const modalEl = fixtureEl.querySelector('.modal')
const modal = new Modal(modalEl, {
backdrop: 'static',
keyboard: false
})

Expand Down

0 comments on commit 0839cbf

Please sign in to comment.