1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-09-02 01:42:36 +02:00

backport #29516: added animation when modal backdrop is static

This commit is contained in:
Johann-S
2019-10-28 16:28:11 +02:00
committed by XhmikosR
parent 29f585365f
commit dd96b832f7
5 changed files with 121 additions and 9 deletions

View File

@@ -38,6 +38,7 @@ const DefaultType = {
const Event = {
HIDE : `hide${EVENT_KEY}`,
HIDE_PREVENTED : `hidePrevented${EVENT_KEY}`,
HIDDEN : `hidden${EVENT_KEY}`,
SHOW : `show${EVENT_KEY}`,
SHOWN : `shown${EVENT_KEY}`,
@@ -56,7 +57,8 @@ const ClassName = {
BACKDROP : 'modal-backdrop',
OPEN : 'modal-open',
FADE : 'fade',
SHOW : 'show'
SHOW : 'show',
STATIC : 'modal-static'
}
const Selector = {
@@ -234,6 +236,29 @@ class Modal {
return config
}
_triggerBackdropTransition() {
if (this._config.backdrop === 'static') {
const hideEventPrevented = $.Event(Event.HIDE_PREVENTED)
$(this._element).trigger(hideEventPrevented)
if (hideEventPrevented.defaultPrevented) {
return
}
this._element.classList.add(ClassName.STATIC)
const modalTransitionDuration = Util.getTransitionDurationFromElement(this._element)
$(this._element).one(Util.TRANSITION_END, () => {
this._element.classList.remove(ClassName.STATIC)
})
.emulateTransitionEnd(modalTransitionDuration)
this._element.focus()
} else {
this.hide()
}
}
_showElement(relatedTarget) {
const transition = $(this._element).hasClass(ClassName.FADE)
const modalBody = this._dialog ? this._dialog.querySelector(Selector.MODAL_BODY) : null
@@ -303,8 +328,7 @@ class Modal {
if (this._isShown && this._config.keyboard) {
$(this._element).on(Event.KEYDOWN_DISMISS, (event) => {
if (event.which === ESCAPE_KEYCODE) {
event.preventDefault()
this.hide()
this._triggerBackdropTransition()
}
})
} else if (!this._isShown) {
@@ -362,11 +386,8 @@ class Modal {
if (event.target !== event.currentTarget) {
return
}
if (this._config.backdrop === 'static') {
this._element.focus()
} else {
this.hide()
}
this._triggerBackdropTransition()
})
if (animate) {

View File

@@ -833,4 +833,26 @@ $(function () {
})
.bootstrapModal('show')
})
QUnit.test('should not close modal when clicking outside of modal-content if backdrop = static', function (assert) {
assert.expect(1)
var done = assert.async()
var $modal = $('<div class="modal" data-backdrop="static"><div class="modal-dialog" /></div>').appendTo('#qunit-fixture')
$modal.on('shown.bs.modal', function () {
$modal.trigger('click')
setTimeout(function () {
var modal = $modal.data('bs.modal')
assert.strictEqual(modal._isShown, true)
done()
}, 10)
})
.on('hidden.bs.modal', function () {
assert.strictEqual(true, false, 'should not hide the modal')
})
.bootstrapModal({
backdrop: 'static'
})
})
})