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

Remove tooltip focus on mousemove #3335

Merged
merged 14 commits into from
May 28, 2020
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
- Added `displayName` to components using `React.forwardRef` ([#3451](https://github.com/elastic/eui/pull/3451))
- Added event target checker for `EuiOverlayMask`'s `onClick` prop ([#3462](https://github.com/elastic/eui/pull/3462))

**Bug Fixes**

- Fixed issue where multiple `EuiToolTip` components could be visible when element was focused ([#3335](https://github.com/elastic/eui/pull/3335))

## [`24.0.0`](https://github.com/elastic/eui/tree/v24.0.0)

- Added `null` as acceptable `icon` prop for `EuiCard` ([#3470](https://github.com/elastic/eui/pull/3470))
Expand Down
39 changes: 23 additions & 16 deletions src/components/tool_tip/tool_tip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import { EuiPortal } from '../portal';
import { EuiToolTipPopover } from './tool_tip_popover';
import { findPopoverPosition, htmlIdGenerator } from '../../services';

import { TAB } from '../../services/key_codes';

import { EuiResizeObserver } from '../observer/resize_observer';

export type ToolTipPositions = 'top' | 'right' | 'bottom' | 'left';
Expand Down Expand Up @@ -115,7 +117,6 @@ export interface Props {

interface State {
visible: boolean;
hasFocus: boolean;
calculatedPosition: ToolTipPositions;
toolTipStyles: ToolTipStyles;
arrowStyles: undefined | { left: number; top: number };
Expand All @@ -129,7 +130,6 @@ export class EuiToolTip extends Component<Props, State> {

state: State = {
visible: false,
hasFocus: false,
calculatedPosition: this.props.position,
toolTipStyles: DEFAULT_TOOLTIP_STYLES,
arrowStyles: undefined,
Expand All @@ -147,6 +147,7 @@ export class EuiToolTip extends Component<Props, State> {

componentWillUnmount() {
this._isMounted = false;
window.addEventListener('mousemove', this.hasFocusMouseMoveListener);
adfaris marked this conversation as resolved.
Show resolved Hide resolved
}

componentDidUpdate(prevProps: Props, prevState: State) {
Expand Down Expand Up @@ -237,30 +238,35 @@ export class EuiToolTip extends Component<Props, State> {
}
};

onFocus = () => {
this.setState({
hasFocus: true,
});
this.showToolTip();
hasFocusMouseMoveListener = () => {
this.hideToolTip();
window.removeEventListener('mousemove', this.hasFocusMouseMoveListener);
adfaris marked this conversation as resolved.
Show resolved Hide resolved
};

onBlur = () => {
this.setState({
hasFocus: false,
});
this.hideToolTip();
onKeyUp = (e: { keyCode: number }) => {
if (e.keyCode === TAB) {
window.addEventListener('mousemove', this.hasFocusMouseMoveListener);
}
};

hideOnTab = (e: { keyCode: number; }) => {
if(e.keyCode === TAB) {
this.hideToolTip()
}
}
onMouseOver = () => {
window.addEventListener('keyup', this.hideOnTab);
this.showToolTip();
}

onMouseOut = (e: ReactMouseEvent<HTMLSpanElement, MouseEvent>) => {
// Prevent mousing over children from hiding the tooltip by testing for whether the mouse has
// left the anchor for a non-child.
if (
this.anchor === e.relatedTarget ||
(this.anchor != null && !this.anchor.contains(e.relatedTarget as Node))
) {
if (!this.state.hasFocus) {
this.hideToolTip();
}
}

if (this.props.onMouseOut) {
Expand Down Expand Up @@ -317,8 +323,9 @@ export class EuiToolTip extends Component<Props, State> {
<span
ref={anchor => (this.anchor = anchor)}
className={anchorClasses}
onMouseOver={this.showToolTip}
onMouseOut={this.onMouseOut}>
onMouseOver={this.onMouseOver}
onMouseOut={this.onMouseOut}
onKeyUp={e => this.onKeyUp(e)}>
{/**
* Re: jsx-a11y/mouse-events-have-key-events
* We apply onFocus, onBlur, etc to the children element because that's the element
Expand Down