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] Add repositionOnScroll prop #6515

Merged
merged 3 commits into from
Jan 10, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src-docs/src/views/tool_tip/tool_tip_example.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ const tooltipSnippet = [
import ToolTipComponent from './tool_tip_components';
const toolTipComponentSource = require('!!raw-loader!./tool_tip_components');

import ToolTipFixed from './tool_tip_fixed';
const toolTipFixedSource = require('!!raw-loader!./tool_tip_fixed');

import IconTip from './icon_tip';
const infoTipSource = require('!!raw-loader!./icon_tip');
const infoTipSnippet = `<EuiIconTip
Expand Down Expand Up @@ -146,6 +149,25 @@ export const ToolTipExample = {
props: { EuiToolTip },
demo: <ToolTipComponent />,
},
{
title: 'Tooltip on a fixed element',
text: (
<p>
Tooltips even work on <EuiCode>position: fixed;</EuiCode> elements.
Add the <EuiCode>repositionOnScroll</EuiCode> boolean prop to ensure
the tooltip realigns to the fixed anchor on scroll.
</p>
),
source: [
{
type: GuideSectionTypes.TSX,
code: toolTipFixedSource,
},
],

props: { EuiToolTip },
demo: <ToolTipFixed />,
},
{
title: 'IconTip',
text: (
Expand Down
38 changes: 38 additions & 0 deletions src-docs/src/views/tool_tip/tool_tip_fixed.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React, { useState, useRef } from 'react';

import { EuiToolTip, EuiButton } from '../../../../src';

export default () => {
const toggleRef = useRef<HTMLButtonElement | null>(null);
const fixedRef = useRef<HTMLButtonElement | null>(null);
const [isExampleShown, setIsExampleShown] = useState(false);
const toggleExample = () => {
setIsExampleShown((isExampleShown) => {
requestAnimationFrame(() => {
isExampleShown ? toggleRef.current?.focus() : fixedRef.current?.focus();
});
return !isExampleShown;
});
};

return (
<>
<EuiButton onClick={toggleExample} buttonRef={toggleRef}>
Toggle fixed example
</EuiButton>
{isExampleShown && (
<div style={{ position: 'fixed', bottom: 50, right: 50, zIndex: 10 }}>
<EuiToolTip
position="top"
content="This tooltip text scrolls with the fixed example button."
repositionOnScroll={true}
>
<EuiButton fill buttonRef={fixedRef} onClick={toggleExample}>
Toggle fixed example
</EuiButton>
</EuiToolTip>
</div>
)}
</>
);
};
25 changes: 25 additions & 0 deletions src/components/tool_tip/tool_tip.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,29 @@ describe('EuiToolTip', () => {

expect(container).toMatchSnapshot();
});

test('repositionOnScroll', () => {
const addEventSpy = jest.spyOn(window, 'addEventListener');
const removeEventSpy = jest.spyOn(window, 'removeEventListener');
const repositionFn = expect.any(Function);

const { rerender, unmount } = render(
<EuiToolTip content="content">
<button data-test-subj="trigger">Trigger</button>
</EuiToolTip>
);
expect(addEventSpy).not.toHaveBeenCalledWith('scroll', expect.anything());

// Should add a scroll event listener on mount and on update
rerender(
<EuiToolTip content="content" repositionOnScroll={true}>
<button data-test-subj="trigger">Trigger</button>
</EuiToolTip>
);
expect(addEventSpy).toHaveBeenCalledWith('scroll', repositionFn, true);

// Should remove the scroll event listener on unmount
unmount();
expect(removeEventSpy).toHaveBeenCalledWith('scroll', repositionFn, true);
});
});
21 changes: 21 additions & 0 deletions src/components/tool_tip/tool_tip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ export interface EuiToolTipProps {
* Suggested position. If there is not enough room for it this will be changed.
*/
position: ToolTipPositions;
/**
* When `true`, the tooltip's position is re-calculated when the user
* scrolls. This supports having fixed-position tooltip anchors.
*
* When nesting an `EuiTooltip` in a scrollable container, `repositionOnScroll` should be `true`
*/
repositionOnScroll?: boolean;

/**
* If supplied, called when mouse movement causes the tool tip to be
Expand Down Expand Up @@ -154,17 +161,30 @@ export class EuiToolTip extends Component<EuiToolTipProps, State> {

componentDidMount() {
this._isMounted = true;
if (this.props.repositionOnScroll) {
window.addEventListener('scroll', this.positionToolTip, true);
}
}

componentWillUnmount() {
this.clearAnimationTimeout();
this._isMounted = false;
window.removeEventListener('scroll', this.positionToolTip, true);
}

componentDidUpdate(prevProps: EuiToolTipProps, prevState: State) {
if (prevState.visible === false && this.state.visible === true) {
requestAnimationFrame(this.testAnchor);
}

// update scroll listener
if (prevProps.repositionOnScroll !== this.props.repositionOnScroll) {
if (this.props.repositionOnScroll) {
window.addEventListener('scroll', this.positionToolTip, true);
} else {
window.removeEventListener('scroll', this.positionToolTip, true);
}
}
}

testAnchor = () => {
Expand Down Expand Up @@ -293,6 +313,7 @@ export class EuiToolTip extends Component<EuiToolTipProps, State> {
title,
delay,
display,
repositionOnScroll,
...rest
} = this.props;

Expand Down
1 change: 1 addition & 0 deletions upcoming_changelogs/6515.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Added the `repositionOnScroll` prop to `EuiToolTip`