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

[docs] Add search shortcut #10725

Merged
merged 1 commit into from
Mar 19, 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
55 changes: 40 additions & 15 deletions docs/src/modules/components/AppSearch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import React from 'react';
import keycode from 'keycode';
import compose from 'recompose/compose';
import pure from 'recompose/pure';
import EventListener from 'react-event-listener';
import PropTypes from 'prop-types';
import withWidth, { isWidthUp } from 'material-ui/utils/withWidth';
import SearchIcon from 'material-ui-icons/Search';
Expand Down Expand Up @@ -114,24 +116,47 @@ const styles = theme => ({
},
});

function AppSearch(props) {
const { classes, width } = props;
class AppSearch extends React.Component {
handleKeyDown = event => {
if (
['/', 's'].indexOf(keycode(event)) !== -1 &&
document.activeElement.nodeName.toLowerCase() === 'body' &&
document.activeElement !== this.input
) {
event.preventDefault();
this.input.focus();
}
};

if (!isWidthUp('sm', width)) {
removeDocsearch();
return null;
}
input = null;

render() {
const { classes, width } = this.props;

initDocsearch();
if (!isWidthUp('sm', width)) {
removeDocsearch();
return null;
}

initDocsearch();

return (
<div className={classes.root}>
<div className={classes.search}>
<SearchIcon />
</div>
<input id="docsearch-input" className={classes.input} />
</div>
);
return (
<EventListener target="window" onKeyDown={this.handleKeyDown}>
<div className={classes.root}>
<div className={classes.search}>
<SearchIcon />
</div>
<input
id="docsearch-input"
ref={node => {
this.input = node;
}}
className={classes.input}
/>
</div>
</EventListener>
);
}
}

AppSearch.propTypes = {
Expand Down