Mousetrap takes precedence over React's keyboard events?
Author: ben-zCreated Jan 26, 2018Updated Oct 12, 2023
Consider this snippet:
class TestComponent extends React.Component {
handleInputKeyDown(e) {
e.stopPropagation();
console.log("input key down", e.key);
}
handleWrapperKeyDown(e) {
e.stopPropagation();
console.log("wrapper key down", e.key);
}
componentDidMount() {
Mousetrap(this.wrapper).bind('esc', this.handleWrapperKeyDown, 'keydown');
}
componentWillUnmount() {
Mousetrap(this.wrapper).unbind('esc');
}
render() {
return (
<div ref={(elem) => this.wrapper = elem}>
<input onKeyDown={this.handleInputKeyDown} />
</div>
)
}
}Since the <input /> exists inside the <div />, I would expect that handleInputKeyDown is called before handleWrapperKeyDown when focused on the input element. But instead, handleWrapperKeyDown is called. Is this a know issue/feature? This can make incrementally converting a large codebase to Mousetrap difficult.
Here's a demonstration of the issue: https://jsfiddle.net/k7uuofaL/15/
Source: ccampbell/mousetrap