From 2ea71a9e782000a5abe7916204ab694d993a9415 Mon Sep 17 00:00:00 2001 From: Johann-S Date: Sun, 28 Jul 2019 11:49:44 +0200 Subject: [PATCH] allow dynamic modal body for scrollable modals --- js/src/modal/modal.js | 5 +++-- js/src/modal/modal.spec.js | 21 +++++++++++++++++++-- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/js/src/modal/modal.js b/js/src/modal/modal.js index a0e460f228..5d74249374 100644 --- a/js/src/modal/modal.js +++ b/js/src/modal/modal.js @@ -243,6 +243,7 @@ class Modal { _showElement(relatedTarget) { const transition = this._element.classList.contains(ClassName.FADE) + const modalBody = SelectorEngine.findOne(Selector.MODAL_BODY, this._dialog) if (!this._element.parentNode || this._element.parentNode.nodeType !== Node.ELEMENT_NODE) { @@ -254,8 +255,8 @@ class Modal { this._element.removeAttribute('aria-hidden') this._element.setAttribute('aria-modal', true) - if (this._dialog.classList.contains(ClassName.SCROLLABLE)) { - SelectorEngine.findOne(Selector.MODAL_BODY, this._dialog).scrollTop = 0 + if (this._dialog.classList.contains(ClassName.SCROLLABLE) && modalBody) { + modalBody.scrollTop = 0 } else { this._element.scrollTop = 0 } diff --git a/js/src/modal/modal.spec.js b/js/src/modal/modal.spec.js index ed881f1682..5111f00a9a 100644 --- a/js/src/modal/modal.spec.js +++ b/js/src/modal/modal.spec.js @@ -396,8 +396,6 @@ describe('Modal', () => { const modalBody = modalEl.querySelector('.modal-body') const modal = new Modal(modalEl) - spyOn(modal, 'hide').and.callThrough() - modalEl.addEventListener('shown.bs.modal', () => { expect(modalBody.scrollTop).toEqual(0) done() @@ -406,6 +404,25 @@ describe('Modal', () => { modal.show() }) + it('should set .modal\'s scroll top to 0 if .modal-dialog-scrollable and modal body do not exists', done => { + fixtureEl.innerHTML = [ + '' + ].join('') + + const modalEl = fixtureEl.querySelector('.modal') + const modal = new Modal(modalEl) + + modalEl.addEventListener('shown.bs.modal', () => { + expect(modalEl.scrollTop).toEqual(0) + done() + }) + + modal.show() + }) + it('should not enforce focus if focus equal to false', done => { fixtureEl.innerHTML = '