Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

material ui Tooltip distance from the anchor

Is there a clear/easy way to control Tooltip's distance from the anchor element? The default positioning does not fit well for my case, the tooltip is too close to the anchor. I have checked all the props of it and PopperProps no visible option to do that.

like image 341
Yuki Avatar asked Aug 18 '19 01:08

Yuki


3 Answers

You can customize the tooltip's margin using withStyles.

In my case (Material-UI 3), the tooltip was too far away from the anchor.
Here is what I needed :

const StyledTooltip = withStyles({
  tooltipPlacementTop: {
    margin: "4px 0",
  },
})(Tooltip);

I targeted tooltipPlacementTop because it was the rule name when using the placement="top" prop.
You can find the adequate rule names in the Tooltip API documentation.

Last tip: I used the PopperProps={{ keepMounted: true }} prop to see in my navigator's inspector what CSS was applied to the tooltip.

Hope it helps.

like image 148
Hugo GEORGET Avatar answered Sep 18 '22 15:09

Hugo GEORGET


Follow up with Hugo's suggestion, since the tooltip position is absolute, instead of changing the margin I changed the anchor position by adjusting the properties right and top like so:

const StyledTooltip = withStyles({
  tooltipPlacementTop: {
    right: "1px",
    top: "8px",
  },
})(Tooltip);

It works as I expected. You can use left or right to adjust the tooltip horizontal position accordingly.

like image 20
Kelei Ren Avatar answered Sep 17 '22 15:09

Kelei Ren


I was using material ui styled to adjust my tooltip properties. I used the normal theme which is available in the MUI documentation.

const LightTooltip = styled(({ className, ...props }) => (
<Tooltip {...props} classes={{ popper: className }} />))(({ theme }) => ({
[`& .${tooltipClasses.tooltip}`]: {
    backgroundColor: theme.palette.common.white,
    color: 'rgba(0, 0, 0, 0.87)',
    boxShadow: theme.shadows[1],
    fontSize: 11
},}));

I tried to adjust the position property with Mui styled, but it wasn't working. I fixed it with my external style sheet.

.MuiTooltip-popper {
inset: -25px auto 0px auto;}
like image 36
Joshua Avatar answered Sep 17 '22 15:09

Joshua