Skip to content

Commit

Permalink
Improve focus handling [#259]
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre committed Nov 16, 2016
1 parent 8f160d2 commit e7f71ad
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/featherlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions test/featherlight.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
Hello
<i>world</i>
</div>

<input class="for-focus">
</section>
</body>
</html>
Expand Down
8 changes: 8 additions & 0 deletions test/featherlight_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));

0 comments on commit e7f71ad

Please sign in to comment.