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

[EuiToolTip] Improve render performance #3596

Merged
merged 8 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions src/components/tool_tip/_tool_tip.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,10 @@

.euiToolTip {
@include euiToolTipStyle;
@include euiToolTipAnimation('top');
@include euiToolTipAnimation('top', 0ms);
position: absolute;
opacity: 0;

// Animation delays
&.euiToolTip--delayLong {
animation-delay: $euiAnimSpeedNormal * 5;
}

// Custom sizing
$arrowSize: $euiSizeM;
$arrowPlusSize: (($arrowSize/2) + 1px) * -1; /* 1 */
Expand Down
4 changes: 3 additions & 1 deletion src/components/tool_tip/tool_tip.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
requiredProps,
findTestSubject,
takeMountedSnapshot,
sleep,
} from '../../test';
import { EuiToolTip } from './tool_tip';

Expand All @@ -41,7 +42,7 @@ describe('EuiToolTip', () => {
expect(component).toMatchSnapshot();
});

test('shows tooltip on focus', () => {
test('shows tooltip on focus', async () => {
const component = mount(
<EuiToolTip title="title" id="id" content="content" {...requiredProps}>
<button data-test-subj="trigger">Trigger</button>
Expand All @@ -50,6 +51,7 @@ describe('EuiToolTip', () => {

const trigger = findTestSubject(component, 'trigger');
trigger.simulate('focus');
await sleep(260); // wait for showToolTip setTimout
expect(takeMountedSnapshot(component)).toMatchSnapshot();
});
});
23 changes: 21 additions & 2 deletions src/components/tool_tip/tool_tip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { keysOf } from '../common';
import { EuiPortal } from '../portal';
import { EuiToolTipPopover } from './tool_tip_popover';
import { findPopoverPosition, htmlIdGenerator, keys } from '../../services';
import { enqueueStateChange } from '../../services/react';

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

Expand All @@ -53,6 +54,11 @@ const delayToClassNameMap: { [key in ToolTipDelay]: string | null } = {
long: 'euiToolTip--delayLong',
thompsongl marked this conversation as resolved.
Show resolved Hide resolved
};

const delayToMsMap: { [key in ToolTipDelay]: number } = {
regular: 250,
long: 250 * 5,
};

export const DELAY = keysOf(delayToClassNameMap);

interface ToolTipStyles {
Expand Down Expand Up @@ -126,6 +132,7 @@ export class EuiToolTip extends Component<Props, State> {
_isMounted = false;
anchor: null | HTMLElement = null;
popover: null | HTMLElement = null;
private timeoutId?: ReturnType<typeof setTimeout>;

state: State = {
visible: false,
Expand All @@ -140,7 +147,14 @@ export class EuiToolTip extends Component<Props, State> {
delay: 'regular',
};

clearAnimationTimeout = () => {
if (this.timeoutId) {
this.timeoutId = clearTimeout(this.timeoutId) as undefined;
}
};

componentDidMount() {
this.clearAnimationTimeout();
chandlerprall marked this conversation as resolved.
Show resolved Hide resolved
this._isMounted = true;
}

Expand Down Expand Up @@ -184,7 +198,11 @@ export class EuiToolTip extends Component<Props, State> {
};

showToolTip = () => {
this.setState({ visible: true });
if (!this.timeoutId) {
this.timeoutId = setTimeout(() => {
enqueueStateChange(() => this.setState({ visible: true }));
}, delayToMsMap[this.props.delay]);
}
};

positionToolTip = () => {
Expand Down Expand Up @@ -232,8 +250,9 @@ export class EuiToolTip extends Component<Props, State> {
};

hideToolTip = () => {
this.clearAnimationTimeout();
if (this._isMounted) {
this.setState({ visible: false });
enqueueStateChange(() => this.setState({ visible: false }));
}
};

Expand Down
6 changes: 2 additions & 4 deletions src/components/tool_tip/tool_tip_popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { CommonProps } from '../common';

type Props = CommonProps &
Omit<HTMLAttributes<HTMLDivElement>, 'title'> & {
positionToolTip: (rect: ClientRect | DOMRect) => void;
positionToolTip: () => void;
children?: ReactNode;
title?: ReactNode;
popoverRef?: (ref: HTMLDivElement) => void;
Expand All @@ -36,7 +36,7 @@ export class EuiToolTipPopover extends Component<Props> {
requestAnimationFrame(() => {
// Because of this delay, sometimes `positionToolTip` becomes unavailable.
if (this.popover) {
this.props.positionToolTip(this.popover.getBoundingClientRect());
this.props.positionToolTip();
}
});
};
Expand All @@ -50,8 +50,6 @@ export class EuiToolTipPopover extends Component<Props> {

componentDidMount() {
document.body.classList.add('euiBody-hasPortalContent');

this.updateDimensions();
window.addEventListener('resize', this.updateDimensions);
}

Expand Down
2 changes: 1 addition & 1 deletion src/global_styling/mixins/_tool_tip.scss
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,5 @@
}

@mixin euiToolTipAnimation($side: 'top', $delay: $euiAnimSpeedNormal) {
animation: #{map-get($euiTooltipAnimations, $side)} $euiAnimSpeedSlow ease-out $euiAnimSpeedNormal forwards;
animation: #{map-get($euiTooltipAnimations, $side)} $euiAnimSpeedSlow ease-out $delay forwards;
thompsongl marked this conversation as resolved.
Show resolved Hide resolved
}