{"size": 947, "ext": "js", "max_stars_repo_path": "lib/KeyDown.js", "max_stars_repo_name": "dsaprykin/react-keystack", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/KeyDown.js", "max_issues_repo_name": "dsaprykin/react-keystack", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/KeyDown.js", "max_forks_repo_name": "dsaprykin/react-keystack", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 22.5476190476, "max_line_length": 89, "alphanum_fraction": 0.7053854277}
var React = require('react');
var Children = React.Children;
var PropTypes = require('prop-types');
var keyStack = require('./keyStack');

function canUseDom() {
  return typeof window != 'undefined' && window.document;
}

class KeyDown extends React.PureComponent {
  // using componentWillMount gives us a predictable call direction vs. componentDidMount
  // see: https://github.com/facebook/react/issues/4752
  componentWillMount() {
    if (canUseDom()) {
      keyStack.stack(this)
    }
  }

  componentDidMount() {
    this.mounted = true;
  }

  // unpredictable call direction
  componentWillUnmount() {
    keyStack.remove(this);
    this.mounted = false;
  }

  render() {
    return Children.only(this.props.children);
  }
}

KeyDown.propTypes = {
  shortcut: PropTypes.string.isRequired,
  action: PropTypes.func.isRequired,
  children: PropTypes.element.isRequired,
  enableInTextField: PropTypes.bool
};

module.exports = KeyDown;
