Skip to content
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

Merged
merged 3 commits into from
Oct 12, 2018

Conversation

third774
Copy link
Contributor

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

  • Tested locally running yarn start from v1/
  • Would love some suggestions on where/how to add unit tests

demo

Related PRs

I don't believe this PR is related to any others.

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Oct 11, 2018
@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented Oct 11, 2018

Deploy preview for docusaurus-preview ready!

Built with commit 1fba7f0

https://deploy-preview-1028--docusaurus-preview.netlify.com

@JoelMarcey
Copy link
Contributor

@endiliey @yangshun - do you see any reason why we would want to save a / press for anything besides this?

@third774
Copy link
Contributor Author

Also - are there any other inputs fields? This naive implementation is not checking to see if some other input is already focused.

@yangshun
Copy link
Contributor

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.

Copy link
Contributor

@yangshun yangshun left a 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)
Copy link
Contributor

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.

Copy link
Contributor Author

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!

document.addEventListener('keyup', function(e) {
// keyCode for '/' (slash)
if (e.keyCode === 191) {
var search = document.getElementById('search_input_react');
Copy link
Contributor

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.

Copy link
Contributor Author

@third774 third774 Oct 12, 2018

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?

Copy link
Contributor

@yangshun yangshun Oct 12, 2018

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 😄

@third774
Copy link
Contributor Author

third774 commented Oct 12, 2018

@JoelMarcey @yangshun - requested changes have been pushed up! Thanks for the feedback!

Copy link
Contributor

@yangshun yangshun left a 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!

@yangshun yangshun merged commit e880756 into facebook:master Oct 12, 2018
@third774 third774 deleted the add-focus-search-shortcut branch October 12, 2018 12:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants