Skip to content

Commit

Permalink
[Tooltip] Fix popper.js re-instantiation (#19304)
Browse files Browse the repository at this point in the history
  • Loading branch information
netochaves authored and oliviertassinari committed Jan 20, 2020
1 parent 609a15f commit 80d9d7c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
22 changes: 14 additions & 8 deletions packages/material-ui/src/Tooltip/Tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,19 @@ const Tooltip = React.forwardRef(function Tooltip(props, ref) {
}
}

// Avoid the creation of a new Popper.js instance at each render.
const popperOptions = React.useMemo(
() => ({
modifiers: {
arrow: {
enabled: Boolean(arrowRef),
element: arrowRef,
},
},
}),
[arrowRef],
);

return (
<React.Fragment>
{React.cloneElement(children, { ref: handleRef, ...childrenProps })}
Expand All @@ -495,14 +508,7 @@ const Tooltip = React.forwardRef(function Tooltip(props, ref) {
open={childNode ? open : false}
id={childrenProps['aria-describedby']}
transition
popperOptions={{
modifiers: {
arrow: {
enabled: Boolean(arrowRef),
element: arrowRef,
},
},
}}
popperOptions={popperOptions}
{...interactiveWrapperListeners}
{...PopperProps}
>
Expand Down
18 changes: 17 additions & 1 deletion packages/material-ui/src/Tooltip/Tooltip.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,10 @@ describe('<Tooltip />', () => {
it('should use hysteresis with the enterDelay', () => {
const { container } = render(
<Tooltip
{...defaultProps}
enterDelay={111}
leaveDelay={5}
TransitionProps={{ timeout: 6 }}
{...defaultProps}
/>,
);
const children = container.querySelector('#testChild');
Expand Down Expand Up @@ -466,4 +466,20 @@ describe('<Tooltip />', () => {
);
});
});

it('should use the same popper.js instance between two renders', () => {
const popperRef = React.createRef();
const { forceUpdate } = render(
<Tooltip
{...defaultProps}
open
PopperProps={{
popperRef,
}}
/>,
);
const firstPopperInstance = popperRef.current;
forceUpdate();
expect(firstPopperInstance).to.equal(popperRef.current);
});
});
9 changes: 9 additions & 0 deletions test/utils/createClientRender.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ function clientRender(element, options = {}) {
return result;
};

result.forceUpdate = function forceUpdate() {
result.rerender(
React.cloneElement(element, {
'data-force-update': String(Math.random()),
}),
);
return result;
};

return result;
}

Expand Down

0 comments on commit 80d9d7c

Please sign in to comment.