From 6daae47cc08342a7302b32f29654ac281298f498 Mon Sep 17 00:00:00 2001 From: Giovanni Mendoza Date: Fri, 10 Jan 2020 11:06:12 +0200 Subject: [PATCH] Backport #29886 Close modal with keyboard=true & backdrop=static --- js/src/modal.js | 7 +++-- js/tests/unit/modal.js | 49 +++++++++++++++++++++++++++++++ site/docs/4.4/components/modal.md | 8 ++--- 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/js/src/modal.js b/js/src/modal.js index ad925f6ff4..faaa8f1083 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 b1ddc8512f..14cebe84f3 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 = $('