reactjs - 如何设置 Material UI Tooltip 样式?

标签 reactjs tooltip material-ui formsy-material-ui

如何设置 Material UI 工具提示标题的样式?悬停时的默认工具提示显示为黑色,没有文本换行。是否可以更改背景、颜色等?这个选项可用吗?

最佳答案

关于这个问题的另一个流行答案(由 André Junges 提出)是针对 Material-UI 的 0.x 版本。下面我复制了 Material UI's Tooltip - Customization Style 的答案它解决了 v3 和 v4 的问题。再往下,我添加了使用 v5 的示例版本。

下面是如何通过主题覆盖所有工具提示,或仅使用 withStyles 自定义单个工具提示的示例(两个不同的示例)。第二种方法还可用于创建自定义工具提示组件,您可以重复使用该组件,而无需强制在全局范围内使用它。

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);

Edit Tooltip customization

以下是有关工具提示 CSS 类的文档,可用于控制工具提示行为的不同方面:https://material-ui.com/api/tooltip/#css

以下是有关覆盖主题中这些类的文档:https://material-ui.com/customization/components/#global-theme-override

<小时/>

这是一个类似的示例,但已更新为可与 Material-UI v5 配合使用(请注意,它适用于 some fixes 之后的 5.0.3 及更高版本)。它包括通过主题进行自定义、使用 styled 进行自定义以及使用 sx 属性进行自定义。所有这些自定义都以“工具提示槽”为目标,以便将 CSS 应用于控制工具提示视觉外观的元素。

import React from "react";
import ReactDOM from "react-dom";

import { createTheme, ThemeProvider, styled } from "@mui/material/styles";
import Tooltip from "@mui/material/Tooltip";

const defaultTheme = createTheme();
const theme = createTheme({
  components: {
    MuiTooltip: {
      styleOverrides: {
        tooltip: {
          fontSize: "2em",
          color: "yellow",
          backgroundColor: "red"
        }
      }
    }
  }
});
const BlueOnGreenTooltip = styled(({ className, ...props }) => (
  <Tooltip {...props} componentsProps={{ tooltip: { className: className } }} />
))(`
    color: lightblue;
    background-color: green;
    font-size: 1.5em;
`);

const TextOnlyTooltip = styled(({ className, ...props }) => (
  <Tooltip {...props} componentsProps={{ tooltip: { className: className } }} />
))(`
    color: black;
    background-color: transparent;
`);

function App(props) {
  return (
    <ThemeProvider theme={defaultTheme}>
      <div className="App">
        <ThemeProvider 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>
        </ThemeProvider>
        <BlueOnGreenTooltip title="This tooltip is customized via styled">
          <div style={{ marginBottom: "20px" }}>
            Hover to see blue-on-green tooltip customized via styled
          </div>
        </BlueOnGreenTooltip>
        <TextOnlyTooltip title="This tooltip is customized via styled">
          <div style={{ marginBottom: "20px" }}>
            Hover to see text-only tooltip customized via styled
          </div>
        </TextOnlyTooltip>
        <Tooltip
          title="This tooltip is customized via the sx prop"
          componentsProps={{
            tooltip: {
              sx: {
                color: "purple",
                backgroundColor: "lightblue",
                fontSize: "2em"
              }
            }
          }}
        >
          <div>
            Hover to see purple-on-blue tooltip customized via the sx prop
          </div>
        </Tooltip>
      </div>
    </ThemeProvider>
  );
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

Edit Tooltip customization

有关 v4 和 v5 之间主题结构更改的文档:https://mui.com/guides/migration-v4/#theme

Material-UI 文档中的工具提示自定义示例:https://mui.com/components/tooltips/#customization

关于reactjs - 如何设置 Material UI Tooltip 样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36759985/

相关文章:

reactjs - react + redux 应用程序中的 agGrid 正在修改底层数据

jquery - 使用 jQuery 计算文本像素宽度

java - 如何在 JLayer 覆盖的 JComponent 上激活工具提示?

javascript - 如何从下拉边框底部删除边框颜色?

reactjs - 如何修复 material-ui-dropzone 中的 theme.spacing.unit 警告?

javascript - React 中输入元素的怪癖

css - React Native splinter 的边框

jquery - 使用 qTip2 在 javascript 中定义样式?

reactjs - Material UI 和网格系统

reactjs - 选择,具有收缩属性的 OutlineInput 标签在空时与 TextField 不同