Turn Tooltip into styled component
-> use classes attribute to override tooltip
class
-> styles should be applied to the element
I can see that my new tooltip class is on the element itself but without styles.
https://codesandbox.io/s/yj7wjr753z
| Tech | Version |
|--------------|---------|
| Material-UI | latest
|
| React | latest
|
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.
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:
https://material-ui.com/api/tooltip/
or in the Dev Tool directly:
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;
}
`;
Most helpful comment
Here is a working example following our guide, it can be further improved by adding
muiTheme
to the styled components theme.https://codesandbox.io/s/8109v3n1x8
If you are wondering what classes you can use, you can find the information in the API docs page:
https://material-ui.com/api/tooltip/
or in the Dev Tool directly: