]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
fix(modal): change the order we return from show() method (#34087)
authoralpadev <2838324+alpadev@users.noreply.github.com>
Sun, 25 Jul 2021 17:36:11 +0000 (19:36 +0200)
committerGitHub <noreply@github.com>
Sun, 25 Jul 2021 17:36:11 +0000 (20:36 +0300)
In case of a modal with fading enabled, a prevented show event can cause show to not showing the modal anymore.

See #34055

Co-authored-by: XhmikosR <xhmikosr@gmail.com>
js/src/modal.js
js/tests/unit/modal.js

index 2e3017024ef4f75a637e930f7fdac5bfe2d607a8..a23bfc1ba6d0344dd8d993f4fa56fedb89e9274d 100644 (file)
@@ -104,22 +104,22 @@ class Modal {
       return
     }
 
-    if ($(this._element).hasClass(CLASS_NAME_FADE)) {
-      this._isTransitioning = true
-    }
-
     const showEvent = $.Event(EVENT_SHOW, {
       relatedTarget
     })
 
     $(this._element).trigger(showEvent)
 
-    if (this._isShown || showEvent.isDefaultPrevented()) {
+    if (showEvent.isDefaultPrevented()) {
       return
     }
 
     this._isShown = true
 
+    if ($(this._element).hasClass(CLASS_NAME_FADE)) {
+      this._isTransitioning = true
+    }
+
     this._checkScrollbar()
     this._setScrollbar()
 
index a46b3d2deffebb115643ae36e8c8864d07aa00f0..f2c60684b979d24d746049dd5cb95a5b95740f2c 100644 (file)
@@ -102,6 +102,32 @@ $(function () {
       .bootstrapModal('show')
   })
 
+  QUnit.test('should be shown after the first call to show() has been prevented while fading is enabled', function (assert) {
+    assert.expect(2)
+    var done = assert.async()
+
+    var $el = $('<div class="modal fade"><div class="modal-dialog" style="transition-duration: 20ms;"/></div>').appendTo('#qunit-fixture')
+
+    var prevented = false
+    $el
+      .on('show.bs.modal', function (e) {
+        if (!prevented) {
+          e.preventDefault()
+          prevented = true
+
+          setTimeout(function () {
+            $el.bootstrapModal('show')
+          })
+        }
+      })
+      .on('shown.bs.modal', function () {
+        assert.ok(prevented, 'show prevented')
+        assert.ok($el.hasClass('fade'))
+        done()
+      })
+      .bootstrapModal('show')
+  })
+
   QUnit.test('should hide modal when hide is called', function (assert) {
     assert.expect(3)
     var done = assert.async()