Skip to content
This repository has been archived by the owner on Oct 19, 2021. It is now read-only.

Commit

Permalink
fix(Tooltip): adds esc listener (#2248)
Browse files Browse the repository at this point in the history
* fix: adds esc listener

* fix: adds open

* fix: clean listener on unmount
  • Loading branch information
paschalidi authored and asudoh committed Apr 24, 2019
1 parent 01360a6 commit b6825cc
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/components/Tooltip/Tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,17 @@ class Tooltip extends Component {
requestAnimationFrame(() => {
this.getTriggerPosition();
});

document.addEventListener('keydown', this.handleEscKeyPress, false);
}

componentWillUnmount() {
if (this._debouncedHandleHover) {
this._debouncedHandleHover.cancel();
this._debouncedHandleHover = null;
}

document.removeEventListener('keydown', this.handleEscKeyPress, false);
}

static getDerivedStateFromProps({ open }, state) {
Expand Down Expand Up @@ -391,6 +395,13 @@ class Tooltip extends Component {
}
};

handleEscKeyPress = event => {
const { open } = this.state;
if (open && keyDownMatch(event, [keys.ESC, keyCodes.ESC, keyCodes.IEESC])) {
return this.setState({ open: false });
}
};

render() {
const {
triggerId = (this.triggerId =
Expand Down

0 comments on commit b6825cc

Please sign in to comment.