Material-ui: Tooltip - styled components styling not applied

Created on 18 May 2018  路  18Comments  路  Source: mui-org/material-ui

  • [x] I have searched the issues of this repository and believe that this is not a duplicate.

Expected Behavior

Turn Tooltip into styled component
-> use classes attribute to override tooltip class
-> styles should be applied to the element

Current Behavior

I can see that my new tooltip class is on the element itself but without styles.

Steps to Reproduce (for bugs)

https://codesandbox.io/s/yj7wjr753z

  1. Render Tooltip
  2. Implement styled components
  3. Override a specific class with the classes attribute
  4. Apply styling to the specified class

Your Environment

| Tech | Version |
|--------------|---------|
| Material-UI | latest |
| React | latest |

Tooltip question

Most helpful comment

Here is a working example following our guide, it can be further improved by adding muiTheme to the styled components theme.

capture d ecran 2018-09-23 a 22 33 25

import React from "react";
import styled from "styled-components";
import Button from "@material-ui/core/Button";
import Tooltip from "@material-ui/core/Tooltip";

const StyledTooltip = styled(props => (
  <Tooltip
    classes={{ popper: props.className, tooltip: "tooltip" }}
    {...props}
  />
))`
  & .tooltip {
    background-color: papayawhip;
    color: #000;
  }
`;

function StyledComponents() {
  return (
    <StyledTooltip title="Demo">
      <Button>Styled Components</Button>
    </StyledTooltip>
  );
}

export default StyledComponents;

https://codesandbox.io/s/8109v3n1x8


If you are wondering what classes you can use, you can find the information in the API docs page:

capture d ecran 2018-09-23 a 22 38 59
https://material-ui.com/api/tooltip/

or in the Dev Tool directly:

capture d ecran 2018-09-23 a 22 42 07

All 18 comments

This page might be of some use to you.

That is not the issue with this component, the styles are just not applied at all. You can try it yourself if you want with my codesandbox.

It works just fine for other material-ui components though.

This is workaround to you
https://codesandbox.io/s/lr20v47ql9

Yeah this is also effecting us. We cannot apply a style={{ attribute: 'value' }} to a <Tooltip /> component

const styles = theme =>({
 tooltip: {  backgroundColor: 'red' }
});
  <Tooltip  classes={{ tooltip: classes.tooltip }} >...

Greetings!
Am I right in thinking that the solutions provided here, don't really fix the problem @samueldepooter reported? I am experiencing this still. I am unable to override Tooltip style using styled-components like it says in This page. Any other component as far as I see works.

Overriding it via classes works sure, but that is not the point of the issue being open.

@aqueiros Do you have a reproduction example?

Hi @oliviertassinari , I did not provide one because @samueldepooter 's code sandbox depicts the problem pretty well.

@aqueiros Thanks for raising your voice. This process is part of who we move forward. The more people are complaining, the better SNR ratio we have.
The issue comes from the fact that the Tooltip has no root DOM element to apply the styled-component class name on. You even have a warning:

Warning: Failed prop type: The following properties are not supported: className. Please remove them.
in Tooltip (created by WithStyles(Tooltip))
in WithStyles(Tooltip) (created by Styled(WithStyles(Tooltip)))
in Styled(WithStyles(Tooltip)) (created by Index)
in Index

The best path going forward would be to spread the other properties on the Popper component and to deprecate PopperProps.
Do you want to work on it?

@oliviertassinari sure, I can take a look. But I will only be able to pick it up starting Monday next week.

Here is a working example following our guide, it can be further improved by adding muiTheme to the styled components theme.

capture d ecran 2018-09-23 a 22 33 25

import React from "react";
import styled from "styled-components";
import Button from "@material-ui/core/Button";
import Tooltip from "@material-ui/core/Tooltip";

const StyledTooltip = styled(props => (
  <Tooltip
    classes={{ popper: props.className, tooltip: "tooltip" }}
    {...props}
  />
))`
  & .tooltip {
    background-color: papayawhip;
    color: #000;
  }
`;

function StyledComponents() {
  return (
    <StyledTooltip title="Demo">
      <Button>Styled Components</Button>
    </StyledTooltip>
  );
}

export default StyledComponents;

https://codesandbox.io/s/8109v3n1x8


If you are wondering what classes you can use, you can find the information in the API docs page:

capture d ecran 2018-09-23 a 22 38 59
https://material-ui.com/api/tooltip/

or in the Dev Tool directly:

capture d ecran 2018-09-23 a 22 42 07

Thanks a lot for this fix! Has anyone managed to add custom width to the tooltip?
I've tried all combinations of maxWidth and/or width but haven't been able to make it work

  & .tooltip {
    background-color: papayawhip;
    maxWidth: 500;
    color: #000;

@pezzullig Did you check https://material-ui.com/demos/tooltips/#variable-width out?

Yes I did try that and it does work withStyles. I was just trying to get it to work in your codepen with the styled-components but I couldn't manage it. Am I right in saying that setting the maxWidth parameter in the .tooltip section should be equivalent to the withStyles method suggested?

@pezzullig maxWidth: 500; that's not a valid CSS property.

Ah yes you're right thanks @oliviertassinari ! max-width: 500px; works.

Hi @oliviertassinari . When I use your solution, it works as expected. But I tried to rewrite it by .attr and it doesn't work.
Can you explaine why? Thank you in advance.

const StyledTooltip = styled(Tooltip).attrs((props) => ({
  classes: { popper: props.className, tooltip: "tooltip" }
}))`
  & .tooltip {
    background-color: red;
    color: #000;
  }
`;

Was this page helpful?
0 / 5 - 0 ratings

Related issues

revskill10 picture revskill10  路  3Comments

TimoRuetten picture TimoRuetten  路  3Comments

ghost picture ghost  路  3Comments

FranBran picture FranBran  路  3Comments

newoga picture newoga  路  3Comments