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 all commits
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
18 changes: 18 additions & 0 deletions v1/lib/core/Site.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,24 @@ class Site extends React.Component {
}}
/>
)}
{this.props.config.algolia && (
<script
dangerouslySetInnerHTML={{
__html: `
document.addEventListener('keyup', function(e) {
if (e.target !== document.body) {
return;
}
// 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) {
const search = document.getElementById('search_input_react');
search && search.focus();
}
});
`,
}}
/>
)}
{this.props.config.algolia &&
(this.props.config.algolia.algoliaOptions ? (
<script
Expand Down