diff --git a/src/featherlight.js b/src/featherlight.js index 669bfef9..5a0c46f8 100644 --- a/src/featherlight.js +++ b/src/featherlight.js @@ -534,6 +534,20 @@ } }, + beforeOpen: function(_super, event) { + this._previouslyActive = document.activeElement; + document.activeElement.blur(); + return _super(event); + }, + + afterClose: function(_super, event) { + if (this._previouslyActive) { // Bulletproofing + this._previouslyActive.focus(); + this._previouslyActive = null; + } + return _super(event); + }, + onResize: function(_super, event){ this.resize(this.$content.naturalWidth, this.$content.naturalHeight); return _super(event); diff --git a/test/featherlight.html b/test/featherlight.html index cf191f8e..06f88368 100644 --- a/test/featherlight.html +++ b/test/featherlight.html @@ -60,6 +60,8 @@ Hello world + + diff --git a/test/featherlight_test.js b/test/featherlight_test.js index 11f4c8a5..95aad876 100644 --- a/test/featherlight_test.js +++ b/test/featherlight_test.js @@ -423,6 +423,14 @@ var stubAjaxLoad = function(content) { close.focus(); expect($(document.activeElement)).to.have.class('featherlight-close-icon'); }); + + it('removes focus and then resets it', function() { + $('input').focus(); + $.featherlight({text: 'Hello'}); + expect($(document.activeElement)).not.to.have.class('for-focus'); + $.featherlight.close(); + expect($(document.activeElement)).to.have.class('for-focus'); + }) }); }); }(jQuery));