From 1cd343396aeada9b1f8056931105ed248b66f0ab Mon Sep 17 00:00:00 2001 From: Brendan Falkowski Date: Mon, 18 Feb 2019 16:11:32 -0800 Subject: [PATCH 01/15] =?UTF-8?q?Add=20=E2=80=9C$modal-close-icon-svg?= =?UTF-8?q?=E2=80=9D=20variable=20to=20SCSS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- css/src/_modal.scss | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/css/src/_modal.scss b/css/src/_modal.scss index d349858..c4ede60 100644 --- a/css/src/_modal.scss +++ b/css/src/_modal.scss @@ -14,8 +14,9 @@ $modal-backdrop-opacity: 0.65 !default; $modal-backdrop-transition-duration: 300ms !default; -$modal-close-button-size: 28px !default; -$modal-close-icon-size: 24px !default; +$modal-close-button-size: 28px !default; +$modal-close-icon-size: 24px !default; +$modal-close-icon-svg: 'remove' !default; $modal-dialog-transition-duration: 500ms !default; $modal-dialog-translate-y: -250px !default; // Animate from this offset to final position @@ -216,7 +217,7 @@ $z-modal-dialog: 901 !default; } &::before { - @include svg (remove); + @include svg ($modal-close-icon-svg); content: ''; position: absolute; top: 50%; From 9daf779a54a47882acc79b885a5c6458089fca3a Mon Sep 17 00:00:00 2001 From: Brendan Falkowski Date: Thu, 21 Feb 2019 16:40:10 -0800 Subject: [PATCH 02/15] #4: close existing modals before creating a new modal --- js/src/modal.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/js/src/modal.js b/js/src/modal.js index 6de86a2..5d774bf 100644 --- a/js/src/modal.js +++ b/js/src/modal.js @@ -41,6 +41,7 @@ function Modal (config) { Modal.prototype = { init: function (element) { this.saveFocus(); + this.closeExistingModals(); this.createModal(); this.addEvents(); this.openModal(); @@ -79,6 +80,33 @@ Modal.prototype = { } }, + closeExistingModals: function () { + // Find modals except those closing or closed, but not yet removed from the DOM. + var modals = document.querySelectorAll('.modal:not([data-modal-state="closing"]):not([data-modal-state="closed"])'); + + for (var i = 0; i < modals.length; i++) { + var modal = modals[i]; + var closeButton = modal.querySelector('[data-modal-close]'); + + if (closeButton) { + // Close gracefully + closeButton.click(); + } else { + // Close by force + modal.setAttribute('data-modal-state', 'closing'); + + window.setTimeout(function () { + modal.setAttribute('data-modal-state', 'closed') + + var event = new CustomEvent('modal-closed', {'bubbles': true}); + modal.dispatchEvent(event); + + modal.parentNode.removeChild(modal); + }, this.config.transitionEndTime); + } + } + }, + closeModal: function () { var _this = this; From 925ac23fa47ac273f8275603b74881eeb359e544 Mon Sep 17 00:00:00 2001 From: Brendan Falkowski Date: Thu, 21 Feb 2019 16:41:20 -0800 Subject: [PATCH 03/15] Restore focus immediately on close (not waiting for close animations to finish) --- js/src/modal.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/js/src/modal.js b/js/src/modal.js index 5d774bf..a106b55 100644 --- a/js/src/modal.js +++ b/js/src/modal.js @@ -121,6 +121,8 @@ Modal.prototype = { _this.destroyModal(); }, this.config.transitionEndTime); + + this.restoreFocus(); }, createModal: function () { @@ -158,7 +160,6 @@ Modal.prototype = { destroyModal: function () { this.$modal.parentNode.removeChild(this.$modal); - this.restoreFocus(); }, openModal: function () { From 4b22ecdd676eec924a807a784a122a9d6f516ab3 Mon Sep 17 00:00:00 2001 From: Brendan Falkowski Date: Thu, 21 Feb 2019 16:42:58 -0800 Subject: [PATCH 04/15] =?UTF-8?q?Remove=20value=20from=20=E2=80=9Cdata-mod?= =?UTF-8?q?al-close=E2=80=9D=20attribute?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- js/src/modal.js | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 642ad44..5a042ab 100644 --- a/README.md +++ b/README.md @@ -99,7 +99,7 @@ Add modal triggers and content to your HTML: Save - diff --git a/js/src/modal.js b/js/src/modal.js index a106b55..5ccdaf9 100644 --- a/js/src/modal.js +++ b/js/src/modal.js @@ -130,9 +130,8 @@ Modal.prototype = { var widthClass = 'modal_dialog--' + this.config.width; if (this.config.addCloseButton) { - // Add close button to markup closeButton = [ - '' ]; @@ -154,7 +153,7 @@ Modal.prototype = { this.$modal = document.getElementById(this.config.id); this.$backdrop = this.$modal.querySelector('.modal_backdrop'); - this.$closeButtons = this.$modal.querySelectorAll('[data-modal-close="true"]'); + this.$closeButtons = this.$modal.querySelectorAll('[data-modal-close]'); this.$dialog = this.$modal.querySelector('.modal_dialog'); }, From e8ba8babdc1958f4fd7cc1393599093753670f2b Mon Sep 17 00:00:00 2001 From: Brendan Falkowski Date: Thu, 21 Feb 2019 16:45:34 -0800 Subject: [PATCH 05/15] =?UTF-8?q?Remove=20event=20listener=20for=20click?= =?UTF-8?q?=20on=20=E2=80=9C.modal=5Fbackdrop=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use existing event listener for “data-modal-close” attribute --- js/src/modal.js | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/js/src/modal.js b/js/src/modal.js index 5ccdaf9..78d7136 100644 --- a/js/src/modal.js +++ b/js/src/modal.js @@ -28,7 +28,7 @@ function Modal (config) { // Selectors this.$backdrop = null; - this.$closeButtons = null; + this.$closeTriggers = null; this.$dialog = null; this.$modal = null; @@ -50,13 +50,6 @@ Modal.prototype = { addEvents: function () { var _this = this; - // Click on backdrop to close - if (this.config.allowBackdropClose) { - this.$backdrop.addEventListener('click', function (e) { - _this.closeModal(); - }); - } - if (this.config.allowEscapeClose) { document.addEventListener('keydown', function (e) { // Press "TAB" trap @@ -71,9 +64,9 @@ Modal.prototype = { }); } - // Click on "close buttons" to close - for (var i = 0; i < this.$closeButtons.length; i++) { - this.$closeButtons[i].addEventListener('click', function (e) { + // Click on "data-modal-close" elements to close + for (var i = 0; i < this.$closeTriggers.length; i++) { + this.$closeTriggers[i].addEventListener('click', function (e) { e.preventDefault(); _this.closeModal(); }); @@ -128,6 +121,7 @@ Modal.prototype = { createModal: function () { var closeButton = []; var widthClass = 'modal_dialog--' + this.config.width; + var backdropCloseAttribute = (this.config.allowBackdropClose) ? 'data-modal-close' : ''; if (this.config.addCloseButton) { closeButton = [ @@ -143,7 +137,7 @@ Modal.prototype = { this.config.content, closeButton.join(''), '', - '