Skip to content

Commit

Permalink
Focus on search input with ctrl + s
Browse files Browse the repository at this point in the history
  • Loading branch information
snyaggarwal committed Jan 6, 2023
1 parent b8af10c commit 64c4fd5
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/components/search/SearchInput.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class SearchInput extends React.Component {
constructor(props){
super(props);

this.inputRef = React.createRef();

this.state = {
siteTitle: getSiteTitle(),
input: undefined,
Expand All @@ -28,7 +30,21 @@ class SearchInput extends React.Component {
return queryParams.get(attr, defaultValue || '')
}

_listenKey = event => {
const isCtrlS = event.keyCode === 83 && event.ctrlKey;
if (!isCtrlS) {
return;
}

this.inputRef.current.focus();
};

componentWillUnmount() {
document.body.removeEventListener("keydown", this._listenKey);
}

componentDidMount() {
document.body.addEventListener("keydown", this._listenKey);
this.setState({
input: this.getValueFromURL('q'), exactMatch: this.getValueFromURL('exactMatch')
})
Expand Down Expand Up @@ -121,6 +137,7 @@ class SearchInput extends React.Component {
fullWidth
onChange={this.handleInputChange}
onKeyPress={this.handleKeyPress}
inputRef={this.inputRef}
/>
{
input &&
Expand Down

0 comments on commit 64c4fd5

Please sign in to comment.