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

Commit b6825cc

Browse files
paschalidiasudoh
authored andcommitted
fix(Tooltip): adds esc listener (#2248)
* fix: adds esc listener * fix: adds open * fix: clean listener on unmount
1 parent 01360a6 commit b6825cc

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

src/components/Tooltip/Tooltip.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,17 @@ class Tooltip extends Component {
266266
requestAnimationFrame(() => {
267267
this.getTriggerPosition();
268268
});
269+
270+
document.addEventListener('keydown', this.handleEscKeyPress, false);
269271
}
270272

271273
componentWillUnmount() {
272274
if (this._debouncedHandleHover) {
273275
this._debouncedHandleHover.cancel();
274276
this._debouncedHandleHover = null;
275277
}
278+
279+
document.removeEventListener('keydown', this.handleEscKeyPress, false);
276280
}
277281

278282
static getDerivedStateFromProps({ open }, state) {
@@ -391,6 +395,13 @@ class Tooltip extends Component {
391395
}
392396
};
393397

398+
handleEscKeyPress = event => {
399+
const { open } = this.state;
400+
if (open && keyDownMatch(event, [keys.ESC, keyCodes.ESC, keyCodes.IEESC])) {
401+
return this.setState({ open: false });
402+
}
403+
};
404+
394405
render() {
395406
const {
396407
triggerId = (this.triggerId =

0 commit comments

Comments
 (0)