简体   繁体   中英

How to style material-ui components with emotion

I want to style material Ui Tooltip component and I want to target it's tooltip and arrow classes, how I should add styles to them with emotion?

I tried to follow this guide: https://github.com/mui-org/material-ui/issues/11467#issuecomment-423845900 But I need to target css classes.

This is what I tried:

import Tooltip, { TooltipProps } from '@material-ui/core/Tooltip';
import { experimentalStyled } from '@material-ui/core/styles';
import { customThemeOptions } from '../utils/globalStyles';
import { Global, css } from '@emotion/react';
const PtMuiTooltip = experimentalStyled(
      ({ className, title, children, ...other }: TooltipProps) => (
        <Tooltip
          title={title}
          style={{
            '& .MuiTooltip-arrow': {
              color: `${customThemeOptions.pt.palette.primary.main}`,
            },
          }}
          {...other}
        >
          {children}
        </Tooltip>
      ),
    )`
      background-color: ${customThemeOptions.pt.palette.primary.main};
      box-shadow: '0px 1px 3px rgba(0, 0, 0, 0.1), 0px 0px 10px rgba(0, 0, 0, 0.06)';
      padding: '1.5rem';
      border-radius: '0';
    `;

All I want is to create my custom component from material ui tooltip and add styles to tooltip bakcground and arrow color. How I should achieve it with emotion and material-ui?

This example should work

    import Tooltip, { TooltipProps } from '@material-ui/core/Tooltip';
    
    import { experimentalStyled } from '@material-ui/core/styles';
    import { customThemeOptions } from '../utils/globalStyles';
    
    const PtMuiTooltip = experimentalStyled(
      ({ className, title, children, ...other }: TooltipProps) => (
        <Tooltip
          title={title}
          classes={{ popper: className, arrow: 'arrow', tooltip: 'tooltip' }}
          {...other}
        >
          {children}
        </Tooltip>
      ),
    )`
      & .tooltip {
        background-color: ${customThemeOptions.pt.palette.primary.main};
        box-shadow: '0px 1px 3px rgba(0, 0, 0, 0.1), 0px 0px 10px rgba(0, 0, 0, 0.06)';
        padding: '1.5rem';
        border-radius: '0';
      }
      & .arrow {
        color: ${customThemeOptions.pt.palette.primary.main};
      }
    `;
    
    export default PtMuiTooltip;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM