diff --git a/src/components/views/elements/Tooltip.tsx b/src/components/views/elements/Tooltip.tsx index 6e7ba994dc..47b81c7a2e 100644 --- a/src/components/views/elements/Tooltip.tsx +++ b/src/components/views/elements/Tooltip.tsx @@ -98,6 +98,8 @@ export default class Tooltip extends React.Component { }); } + // Add the parent's position to the tooltips, so it's correctly + // positioned, also taking into account any window zoom private updatePosition(style: CSSProperties) { const parentBox = this.parent.getBoundingClientRect(); let offset = 0; @@ -155,11 +157,12 @@ export default class Tooltip extends React.Component { } private renderTooltip = () => { - // Add the parent's position to the tooltips, so it's correctly - // positioned, also taking into account any window zoom - // NOTE: The additional 6 pixels for the left position, is to take account of the - // tooltips chevron - const style = this.updatePosition({}); + let style: CSSProperties = {}; + // When the tooltip is hidden, no need to thrash the DOM with `style` + // attribute updates (performance) + if (this.props.visible) { + style = this.updatePosition({}); + } // Hide the entire container when not visible. This prevents flashing of the tooltip // if it is not meant to be visible on first mount. style.display = this.props.visible ? "block" : "none"; diff --git a/src/components/views/elements/TooltipTarget.tsx b/src/components/views/elements/TooltipTarget.tsx index 9ccd572ea8..5d30bf2539 100644 --- a/src/components/views/elements/TooltipTarget.tsx +++ b/src/components/views/elements/TooltipTarget.tsx @@ -44,6 +44,19 @@ const TooltipTarget: React.FC = ({ const show = () => setIsVisible(true); const hide = () => setIsVisible(false); + // No need to fill up the DOM with hidden tooltip elements. Only add the + // tooltip when we're hovering over the item (performance) + const tooltip = isVisible && ; + return (
= ({ {...rest} > { children } - + { tooltip }
); }; diff --git a/test/components/views/elements/TooltipTarget-test.tsx b/test/components/views/elements/TooltipTarget-test.tsx index f81445994d..cdb550a6d0 100644 --- a/test/components/views/elements/TooltipTarget-test.tsx +++ b/test/components/views/elements/TooltipTarget-test.tsx @@ -39,7 +39,9 @@ describe('', () => { afterEach(() => { // clean up visible tooltips const tooltipWrapper = document.querySelector('.mx_Tooltip_wrapper'); - document.body.removeChild(tooltipWrapper); + if (tooltipWrapper) { + document.body.removeChild(tooltipWrapper); + } }); it('renders container', () => { diff --git a/test/components/views/elements/__snapshots__/TooltipTarget-test.tsx.snap b/test/components/views/elements/__snapshots__/TooltipTarget-test.tsx.snap index 9d8516af77..d0d01f5380 100644 --- a/test/components/views/elements/__snapshots__/TooltipTarget-test.tsx.snap +++ b/test/components/views/elements/__snapshots__/TooltipTarget-test.tsx.snap @@ -22,8 +22,5 @@ exports[` renders container 1`] = ` child -
`;