Skip to content

Commit

Permalink
search: add shortcuts to focus
Browse files Browse the repository at this point in the history
This cribs from Racket's documentation site [1], where pressing "s" will
focus the search bar. This is a modified version of [2], which I choose
to receive and use under the MIT license [3], [4], which is compatible
with MIT-LICENSE.txt.

There's a probably a good way to steer users into this, but "/" is
common thanks to Vim and many websites; "s" is natural (at least to me,
having used Racket's docs so long).

There's probably a "jQuery" way to do this, but I didn't look very hard.

[1]: https://docs.racket-lang.org
[2]: https://github.com/racket/scribble/blob/02ecb223c8e0e4dda3c00739338c96dcd1d5bac6/scribble-lib/scribble/scribble-common.js#L145C2-L152C13
[3]: https://github.com/racket/scribble/blob/02ecb223c8e0e4dda3c00739338c96dcd1d5bac6/LICENSE.txt
[4]: https://github.com/racket/racket/blob/240aa086c548440e0b11641c40cf79e393a560a4/racket/src/LICENSE-MIT.txt
  • Loading branch information
benknoble committed Jul 26, 2024
1 parent fb88c03 commit 96c138c
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ var Search = {
Search.observeFocus();
Search.observeTextEntry();
Search.observeResultsClicks();
Search.listenForKeys();
},

observeFocus: function() {
Expand Down Expand Up @@ -149,6 +150,14 @@ var Search = {
});
},

listenForKeys: function() {
window.addEventListener("keyup", function(e) {
if ((e.key === 's' || e.key === 'S' || e.key === '/') && e.target === document.body) {
$('form#search input').focus();
}
}, false);
},

runSearch: function() {
var term = $('#search-text').val();
if(term.length < 2) { return false };
Expand Down

0 comments on commit 96c138c

Please sign in to comment.