From 65961a02df9841b836c0c80eb38aceac0dd66022 Mon Sep 17 00:00:00 2001 From: Marius Olbertz Date: Tue, 1 Dec 2015 20:09:39 +0100 Subject: [PATCH 1/2] Fixed interaction with key events and animated modals caused by the fact that the modal's elements aren't visible when it opens. --- js/foundation.reveal.js | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/js/foundation.reveal.js b/js/foundation.reveal.js index 87ed493bf9..c0db5ed316 100644 --- a/js/foundation.reveal.js +++ b/js/foundation.reveal.js @@ -274,10 +274,12 @@ if(_this.options.overlay){ Foundation.Motion.animateIn(_this.$overlay, 'fade-in', function(){ Foundation.Motion.animateIn(_this.$element, _this.options.animationIn, function(){ + _this.focusableElements = Foundation.Keyboard.findFocusable(_this.$element); }); }); }else{ Foundation.Motion.animateIn(_this.$element, _this.options.animationIn, function(){ + _this.focusableElements = Foundation.Keyboard.findFocusable(_this.$element); }); } }else{ @@ -316,10 +318,7 @@ */ Reveal.prototype._extraHandlers = function(){ var _this = this; - var visibleFocusableElements = this.$element.find('a[href], area[href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), button:not([disabled]), iframe, object, embed, *[tabindex], *[contenteditable]').filter(function() { - if (!$(this).is(':visible') || $(this).attr('tabindex') < 0){ return false; }//only have visible elements and those that have a tabindex greater or equal 0 - return true; - }); + this.focusableElements = Foundation.Keyboard.findFocusable(this.$element); if(!this.options.overlay && this.options.closeOnClick && !this.options.fullScreen){ $('body').on('click.zf.reveal', function(e){ @@ -329,9 +328,6 @@ } if(this.options.closeOnEsc){ $(window).on('keydown.zf.reveal', function(e){ - if (visibleFocusableElements.length === 0) { // no focusable elements inside the modal at all, prevent tabbing in general - e.preventDefault(); - } Foundation.Keyboard.handleKey(e, _this, { close: function() { if (this.options.closeOnEsc) { @@ -339,6 +335,9 @@ } } }); + if (_this.focusableElements.length === 0) { // no focusable elements inside the modal at all, prevent tabbing in general + e.preventDefault(); + } }); } @@ -348,19 +347,19 @@ // handle keyboard event with keyboard util Foundation.Keyboard.handleKey(e, _this, { tab_forward: function() { - if (this.$element.find(':focus').is(visibleFocusableElements.eq(-1))) { // left modal downwards, setting focus to first element - visibleFocusableElements.eq(0).focus(); + if (this.$element.find(':focus').is(_this.focusableElements.eq(-1))) { // left modal downwards, setting focus to first element + _this.focusableElements.eq(0).focus(); e.preventDefault(); } }, tab_backward: function() { - if (this.$element.find(':focus').is(visibleFocusableElements.eq(0)) || this.$element.is(':focus')) { // left modal upwards, setting focus to last element - visibleFocusableElements.eq(-1).focus(); + if (this.$element.find(':focus').is(_this.focusableElements.eq(0)) || this.$element.is(':focus')) { // left modal upwards, setting focus to last element + _this.focusableElements.eq(-1).focus(); e.preventDefault(); } }, open: function() { - if ($target.is(visibleFocusableElements)) { // dont't trigger if acual element has focus (i.e. inputs, links, ...) + if ($target.is(_this.focusableElements)) { // dont't trigger if acual element has focus (i.e. inputs, links, ...) this.open(); } }, @@ -370,9 +369,6 @@ } } }); - if (visibleFocusableElements.length === 0) { // no focusable elements inside the modal at all, prevent tabbing in general - e.preventDefault(); - } }); }; From 15d7156ef4d8d3c8dc56bbf6582796eeca6e83dd Mon Sep 17 00:00:00 2001 From: Marius Olbertz Date: Tue, 1 Dec 2015 20:58:37 +0100 Subject: [PATCH 2/2] Focus is now shifted back to the anchor when modal is closed using the keyboard. --- js/foundation.reveal.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/js/foundation.reveal.js b/js/foundation.reveal.js index c0db5ed316..c86d24bcc5 100644 --- a/js/foundation.reveal.js +++ b/js/foundation.reveal.js @@ -332,6 +332,7 @@ close: function() { if (this.options.closeOnEsc) { this.close(); + this.$anchor.focus(); } } }); @@ -359,13 +360,18 @@ } }, open: function() { - if ($target.is(_this.focusableElements)) { // dont't trigger if acual element has focus (i.e. inputs, links, ...) + if (_this.$element.find(':focus').is(_this.$element.find('[data-close]'))) { + setTimeout(function() { // set focus back to anchor if close button has been activated + _this.$anchor.focus(); + }, 1); + } else if ($target.is(_this.focusableElements)) { // dont't trigger if acual element has focus (i.e. inputs, links, ...) this.open(); } }, close: function() { if (this.options.closeOnEsc) { this.close(); + this.$anchor.focus(); } } });