MUI - 更改主题中的按钮文本颜色

IT技术 reactjs button colors material-ui themes
2021-05-09 05:15:45

我在直接在 MUI 主题中更改按钮文本颜色时遇到问题。更改原色 + 按钮字体大小效果很好,所以问题不在于传递主题。这是我的代码:

import React from 'react';
import { MuiThemeProvider, createMuiTheme } from 'material-ui/styles';
import { lightBlue } from 'material-ui/colors';
import styled from 'styled-components';

const theme = createMuiTheme({
  palette: {
    primary: lightBlue, // works
  },
  raisedButton: {
    color: '#ffffff', // doesn't work
  },
  typography: {
    button: {
      fontSize: 20, // works
      color: '#ffffff' // doesn't work
    }
  }
});

const App = ({ user, pathname, onToggleDrawer }) => (
  <MuiThemeProvider theme={theme}>
    ...
  </MuiThemeProvider>
);

我也尝试使用导入的颜色而不是#ffffff,但这也没有效果。

有人有任何想法吗?

4个回答

解决了!这终于成功了:

const theme = createMuiTheme({
  palette: {
    primary: lightBlue,
  },
  overrides: {
    MuiButton: {
      raisedPrimary: {
        color: 'white',
      },
    },
  }
});

因此,您只需要使用“覆盖”并明确说明要更改的组件的确切类型。

这对我有用。我们选择的颜色决定使用深色按钮对比色,但白色作为对比色看起来可以说更好:

const theme = createMuiTheme({
  palette: {
    primary: {
      main: "#46AD8D",
      contrastText: "#fff" //button text white instead of black
    },
    background: {
      default: "#394764"
    }
  }
});

这个解决方案对我有用

const theme = createMuiTheme({
  overrides: {
    MuiButton: {
      label: {
        color: "#f1f1f1",
      },
    },
  },

https://github.com/mui-org/material-ui/blob/master/src/styles/getMuiTheme.js#L200你可以看到可以在主题中为各种组件设置什么,raisedButton你会看到color实际上是按钮背景,要设置文本颜色,您需要更改textColor属性。

const theme = getMuiTheme({
  raisedButton: {
    textColor: '#ffffff', // this should work
    primaryTextColor: '#ffffff' // or this if using `primary` style
  }
});

鉴于 CSScolor影响文本而不是背景,它并不完全直观,它甚至与具有props的组件本身的属性不匹配http://www.material-ui.com/#/components/raised-button因为backgroundColorlabelColor相反!!!