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

Allow tooltip to be dismissed using Escape key #2914

Merged
merged 1 commit into from Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/component/Tooltip.tsx
Expand Up @@ -115,6 +115,8 @@ export class Tooltip<TValue extends ValueType, TName extends NameType> extends P
state = {
boxWidth: -1,
boxHeight: -1,
dismissed: false,
dismissedAtCoordinate: { x: 0, y: 0 },
};

private wrapperNode: HTMLDivElement;
Expand All @@ -128,7 +130,18 @@ export class Tooltip<TValue extends ValueType, TName extends NameType> extends P
}

updateBBox() {
const { boxWidth, boxHeight } = this.state;
const { boxWidth, boxHeight, dismissed } = this.state;
if (dismissed) {
this.wrapperNode.blur();
if (
this.props.coordinate.x !== this.state.dismissedAtCoordinate.x ||
this.props.coordinate.y !== this.state.dismissedAtCoordinate.y
) {
this.setState({ dismissed: false });
}
} else {
this.wrapperNode.focus();
}

if (this.wrapperNode && this.wrapperNode.getBoundingClientRect) {
const box = this.wrapperNode.getBoundingClientRect();
Expand Down Expand Up @@ -186,7 +199,7 @@ export class Tooltip<TValue extends ValueType, TName extends NameType> extends P
const { content, viewBox, coordinate, position, active, wrapperStyle } = this.props;
let outerStyle: CSSProperties = {
pointerEvents: 'none',
visibility: active && hasPayload ? 'visible' : 'hidden',
visibility: !this.state.dismissed && active && hasPayload ? 'visible' : 'hidden',
position: 'absolute',
top: 0,
left: 0,
Expand Down Expand Up @@ -246,6 +259,19 @@ export class Tooltip<TValue extends ValueType, TName extends NameType> extends P

return (
<div
tabIndex={0}
onKeyDown={event => {
if (event.key === 'Escape') {
this.setState({
dismissed: true,
dismissedAtCoordinate: {
...this.state.dismissedAtCoordinate,
x: this.props.coordinate.x,
y: this.props.coordinate.y,
},
});
}
}}
className={cls}
style={outerStyle}
ref={node => {
Expand Down