Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tooltip] How to customize the tooltip? #11467

Closed
1 task done
samueldepooter opened this issue May 18, 2018 · 21 comments
Closed
1 task done

[Tooltip] How to customize the tooltip? #11467

samueldepooter opened this issue May 18, 2018 · 21 comments
Labels
component: tooltip This is the name of the generic UI component, not the React module! support: question Community support but can be turned into an improvement

Comments

@samueldepooter
Copy link

samueldepooter commented May 18, 2018

  • 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
@pelotom
Copy link
Member

pelotom commented May 18, 2018

See https://next.material-ui.com/guides/interoperability/#portals

@pelotom pelotom added the support: question Community support but can be turned into an improvement label May 18, 2018
@samueldepooter

This comment has been minimized.

@oliviertassinari oliviertassinari added the component: tooltip This is the name of the generic UI component, not the React module! label May 26, 2018
@artalar

This comment has been minimized.

@andrewmclagan
Copy link

andrewmclagan commented Jun 6, 2018

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

@oky1

This comment has been minimized.

@oliviertassinari oliviertassinari self-assigned this Jun 22, 2018
@oliviertassinari oliviertassinari added docs Improvements or additions to the documentation ok ✅ and removed support: question Community support but can be turned into an improvement labels Jul 3, 2018
@aqueiros

This comment has been minimized.

@oliviertassinari

This comment has been minimized.

@aqueiros

This comment has been minimized.

@oliviertassinari

This comment has been minimized.

@oliviertassinari

This comment has been minimized.

@oliviertassinari oliviertassinari added new feature New feature or request good first issue Great for first contributions. Enable to learn the contribution process. and removed docs Improvements or additions to the documentation labels Jul 24, 2018
@aqueiros

This comment has been minimized.

@oliviertassinari oliviertassinari removed their assignment Jul 24, 2018
@oliviertassinari oliviertassinari added the support: question Community support but can be turned into an improvement label Sep 23, 2018
@oliviertassinari oliviertassinari removed new feature New feature or request good first issue Great for first contributions. Enable to learn the contribution process. labels Sep 23, 2018
@oliviertassinari
Copy link
Member

oliviertassinari commented Sep 23, 2018

Here is a working example following our guide, it can be further improved by adding theme 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 }} {...props} />
))`
  & .MuiTooltip-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?file=/StyledComponentsButton.js


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

@pezzullig

This comment has been minimized.

@oliviertassinari
Copy link
Member

@pezzullig
Copy link

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?

@oliviertassinari
Copy link
Member

oliviertassinari commented Oct 25, 2018

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

@pezzullig
Copy link

pezzullig commented Oct 26, 2018

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

@lalinchik
Copy link

lalinchik commented Oct 8, 2020

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;
  }
`;

@BeKnowDo
Copy link

BeKnowDo commented May 5, 2021

2021 and I can't get this example to work :-/

@oliviertassinari oliviertassinari changed the title Tooltip - styled components styling not applied [Tooltip] styled components styling not applied May 5, 2021
@oliviertassinari
Copy link
Member

oliviertassinari commented May 5, 2021

@BeKnowDo I have updated my initial comment to be more up to date. This case is even in the docs now: https://next.material-ui.com/guides/interoperability/#portals.

The customization demos cover it too: https://next.material-ui.com/components/tooltips/#customized-tooltips.

@malavshah9
Copy link

Thanks @oliviertassinari . This helped me a lot.

@oliviertassinari oliviertassinari changed the title [Tooltip] styled components styling not applied [Tooltip] How to customize the tooltip Jul 16, 2021
@oliviertassinari oliviertassinari changed the title [Tooltip] How to customize the tooltip [Tooltip] How to customize the tooltip? Jul 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: tooltip This is the name of the generic UI component, not the React module! support: question Community support but can be turned into an improvement
Projects
None yet
Development

No branches or pull requests