How to style Material-UI's tooltip?

前端 未结 5 1903
栀梦
栀梦 2020-12-14 16:01

How can I style Material-UI\'s tooltip text? The default tooltip on hover comes out black with no text-wrap. Is it possible to change the background, color etc? Is this opti

相关标签:
5条回答
  • 2020-12-14 16:34

    If you want to change text color , font-size ... of Tooltip there is a simple way.

    You can insert a Tag inside Title of Martial Ui Tooltip for example :

    <Tooltip title={<span>YourText</span>}>
       <Button>Grow</Button>
    </Tooltip>
    

    then you can style your tag anyhow you want.

    check below Example :

    0 讨论(0)
  • 2020-12-14 16:35

    Another solution with HtmlTooltip

    I Use HtmlTooltip and add arrow: {color: '#f5f5f9',}, for the arrow tooltip style.

    And much more to the tooltip style itself.

    So I use ValueLabelComponent to control the label and put there a Tooltip from MaterialUI.

    Hopes it give another way to edit MaterialUI Tooltip :)

    const HtmlTooltip = withStyles((theme) => ({
      tooltip: {
        backgroundColor: 'var(--blue)',
        color: 'white',
        maxWidth: 220,
        fontSize: theme.typography.pxToRem(12),
        border: '1px solid #dadde9',
      },
      arrow: {
        color: '#f5f5f9',
      },
    }))(Tooltip);
    
    function ValueLabelComponent({ children, open, value }) {
      return (
        <HtmlTooltip arrow open={open} enterTouchDelay={0} placement="top" title={value}>
          {children}
        </HtmlTooltip>
      );
    }
    
    ...
    ...
    
    return (
        <div className={classes.root}>
          <Slider
            value={value}
            onChange={handleChange}
            onChangeCommitted={handleChangeCommitted}
            scale={(x) => convertRangeValueToOriginalValue(x, minMaxObj)}
            valueLabelDisplay="auto"
            valueLabelFormat={(x) => '$' + x}
            ValueLabelComponent={ValueLabelComponent}
            aria-labelledby="range-slider"
          />
        </div>
      );
    
    0 讨论(0)
  • 2020-12-14 16:38

    This answer is out of date. This answer was written in 2016 for the 0.x versions of Material-UI. Please see this answer for an approach that works with versions 3 and 4.

    well, you can change the text color and the element background customizing the mui theme.

    color - is the text color

    rippleBackgroundColor - is the tooltip bbackground

    Example: Using IconButton - but you could you the Tooltip directly..

    import React from 'react';
    import IconButton from 'material-ui/IconButton';
    import MuiThemeProvider from 'material-ui/lib/styles/MuiThemeProvider';
    import getMuiTheme from 'material-ui/lib/styles/getMuiTheme';
    
    const muiTheme = getMuiTheme({
      tooltip: {
        color: '#f1f1f1',
        rippleBackgroundColor: 'blue'
      },
    });
    
    const Example = () => (
      <div>
        <MuiThemeProvider muiTheme={muiTheme}>
            <IconButton iconClassName="muidocs-icon-custom-github" tooltip="test" />
        </MuiThemeProvider>
      </div>
    );
    

    You can also pass a style object for the Tooltip (in IconButton it's tooltipStyles) - but these styles will only be applied for the root element.

    It's not possible yet to change the label style to make it wrap in multiple lines.

    0 讨论(0)
  • 2020-12-14 16:47

    I ran into this issue as well, and want for anyone seeking to simply change the color of their tooltip to see this solution that worked for me:

    import React from 'react';
    import { makeStyles } from '@material-ui/core/styles';
    
    import Tooltip from '@material-ui/core/Tooltip';
    import Button from '@material-ui/core/Button';
    import DeleteIcon from '@material-ui/icons/Delete';
    
    const useStyles = makeStyles(theme => ({
      customTooltip: {
        // I used the rgba color for the standard "secondary" color
        backgroundColor: 'rgba(220, 0, 78, 0.8)',
      },
      customArrow: {
        color: 'rgba(220, 0, 78, 0.8)',
      },
    });
    
    export default TooltipExample = () => {
      const classes = useStyles();
    
      return (
        <>
          <Tooltip
            classes={{
              tooltip: classes.customTooltip,
              arrow: classes.customArrow
            }}
            title="Delete"
            arrow
          >
            <Button color="secondary"><DeleteIcon /></Button>
          </Tooltip>
        </>
      );
    };
    
    0 讨论(0)
  • 2020-12-14 16:50

    The other popular answer (by André Junges) on this question is for the 0.x versions of Material-UI. Below I've copied in my answer from Material UI's Tooltip - Customization Style which addresses this for v3 and v4.

    Below are examples of how to override all tooltips via the theme, or to just customize a single tooltip using withStyles (two different examples). The second approach could also be used to create a custom tooltip component that you could reuse without forcing it to be used globally.

    import React from "react";
    import ReactDOM from "react-dom";
    
    import {
      createMuiTheme,
      MuiThemeProvider,
      withStyles
    } from "@material-ui/core/styles";
    import Tooltip from "@material-ui/core/Tooltip";
    
    const defaultTheme = createMuiTheme();
    const theme = createMuiTheme({
      overrides: {
        MuiTooltip: {
          tooltip: {
            fontSize: "2em",
            color: "yellow",
            backgroundColor: "red"
          }
        }
      }
    });
    const BlueOnGreenTooltip = withStyles({
      tooltip: {
        color: "lightblue",
        backgroundColor: "green"
      }
    })(Tooltip);
    
    const TextOnlyTooltip = withStyles({
      tooltip: {
        color: "black",
        backgroundColor: "transparent"
      }
    })(Tooltip);
    
    function App(props) {
      return (
        <MuiThemeProvider theme={defaultTheme}>
          <div className="App">
            <MuiThemeProvider theme={theme}>
              <Tooltip title="This tooltip is customized via overrides in the theme">
                <div style={{ marginBottom: "20px" }}>
                  Hover to see tooltip customized via theme
                </div>
              </Tooltip>
            </MuiThemeProvider>
            <BlueOnGreenTooltip title="This tooltip is customized via withStyles">
              <div style={{ marginBottom: "20px" }}>
                Hover to see blue-on-green tooltip customized via withStyles
              </div>
            </BlueOnGreenTooltip>
            <TextOnlyTooltip title="This tooltip is customized via withStyles">
              <div>Hover to see text-only tooltip customized via withStyles</div>
            </TextOnlyTooltip>
          </div>
        </MuiThemeProvider>
      );
    }
    const rootElement = document.getElementById("root");
    ReactDOM.render(<App />, rootElement);
    

    Here is documentation on tooltip CSS classes available to control different aspects of tooltip behavior: https://material-ui.com/api/tooltip/#css

    Here is documentation on overriding these classes in the theme: https://material-ui.com/customization/components/#global-theme-override

    0 讨论(0)
提交回复
热议问题