diff --git a/js/src/modal.js b/js/src/modal.js
index ad925f6ff..faaa8f108 100644
--- a/js/src/modal.js
+++ b/js/src/modal.js
@@ -325,9 +325,12 @@ class Modal {
}
_setEscapeEvent() {
- if (this._isShown && this._config.keyboard) {
+ if (this._isShown) {
$(this._element).on(Event.KEYDOWN_DISMISS, (event) => {
- if (event.which === ESCAPE_KEYCODE) {
+ if (this._config.keyboard && event.which === ESCAPE_KEYCODE) {
+ event.preventDefault()
+ this.hide()
+ } else if (!this._config.keyboard && event.which === ESCAPE_KEYCODE) {
this._triggerBackdropTransition()
}
})
diff --git a/js/tests/unit/modal.js b/js/tests/unit/modal.js
index b1ddc8512..14cebe84f 100644
--- a/js/tests/unit/modal.js
+++ b/js/tests/unit/modal.js
@@ -855,4 +855,53 @@ $(function () {
backdrop: 'static'
})
})
+
+ QUnit.test('should close modal when escape key is pressed with keyboard = true and backdrop is static', function (assert) {
+ assert.expect(1)
+ var done = assert.async()
+ var $modal = $('
').appendTo('#qunit-fixture')
+
+ $modal.on('shown.bs.modal', function () {
+ $modal.trigger($.Event('keydown', {
+ which: 27
+ }))
+
+ setTimeout(function () {
+ var modal = $modal.data('bs.modal')
+
+ assert.strictEqual(modal._isShown, false)
+ done()
+ }, 10)
+ })
+ .bootstrapModal({
+ backdrop: 'static',
+ keyboard: true
+ })
+ })
+
+ QUnit.test('should not close modal when escape key is pressed with keyboard = false and backdrop = static', function (assert) {
+ assert.expect(1)
+ var done = assert.async()
+ var $modal = $('').appendTo('#qunit-fixture')
+
+ $modal.on('shown.bs.modal', function () {
+ $modal.trigger($.Event('keydown', {
+ which: 27
+ }))
+
+ setTimeout(function () {
+ var modal = $modal.data('bs.modal')
+
+ assert.strictEqual(modal._isShown, true)
+ done()
+ }, 10)
+ })
+ .on('hidden.bs.modal', function () {
+ assert.strictEqual(false, true, 'should not hide the modal')
+ })
+ .bootstrapModal({
+ backdrop: 'static',
+ keyboard: false
+ })
+ })
})
diff --git a/site/docs/4.4/components/modal.md b/site/docs/4.4/components/modal.md
index 6bd573d7a..aebfd9e10 100644
--- a/site/docs/4.4/components/modal.md
+++ b/site/docs/4.4/components/modal.md
@@ -139,7 +139,7 @@ Toggle a working modal demo by clicking the button below. It will slide down and
When backdrop is set to static, the modal will not close when clicking outside it. Click the button below to try it.
-