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
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions v1/lib/core/Site.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,21 @@ class Site extends React.Component {
}}
/>
)}
{this.props.config.algolia && (
<script
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!

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 😄

search && search.focus();
}
});
`,
}}
/>
)}
{this.props.config.algolia &&
(this.props.config.algolia.algoliaOptions ? (
<script
Expand Down