-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add keyboard shortcut to focus search #1028
Conversation
Deploy preview for docusaurus-preview ready! Built with commit 1fba7f0 |
Also - are there any other inputs fields? This naive implementation is not checking to see if some other input is already focused. |
Thanks for the PR @third774! '/' is usually for searching so I have no issues with using this key for focusing on the search input. I'm not sure whether other sites with more dynamic content might break if we add this (for e.g. Reason, Babel and Prettier have a playground). I'll test it out with those sites first and report back. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks generally good to me, but we have to check that none of the inputs on the page are in focus before we change the focus.
dangerouslySetInnerHTML={{ | ||
__html: ` | ||
document.addEventListener('keyup', function(e) { | ||
// keyCode for '/' (slash) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add
if (e.target !== document.body) {
return;
}
here so that we only focus on the search bar when no other items on the page are in focus.
Try your current script vs the script with the e.target
checking on https://reasonml.github.io/en/try and you'll see why it's important to add that line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perfect - I thought that might be an issue. Thanks!
v1/lib/core/Site.js
Outdated
document.addEventListener('keyup', function(e) { | ||
// keyCode for '/' (slash) | ||
if (e.keyCode === 191) { | ||
var search = document.getElementById('search_input_react'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use const search
since it's not being reassigned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Out of curiosity - do these inlined scripts somehow get run through babel? I was surprised to see that const
is even supported in IE11 after you mentioned it, so it's probably fine if it doesn't?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope these scripts do not get processed through Babel, so we have to be careful about not using new language features. const
has actually existed since ES5 so we're fine 😄
@JoelMarcey @yangshun - requested changes have been pushed up! Thanks for the feedback! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM now. Thanks for the quick fix!
This PR adds a keyboard shortcut
/
for focusing the search input.Motivation
Was browsing the
draft-js
docs and found myself wishing this VIM keyboard shortcut existed since it is becoming more common across various websites. Not sure if where I've put this is the most appropriate place though.Have you read the Contributing Guidelines on pull requests?
Yep!
Test Plan
yarn start
fromv1/
Related PRs
I don't believe this PR is related to any others.