React 中的访问按钮原色

Access Button Primary Color In React

我正在尝试在主题中为包含的、主要的和悬停的按钮提供覆盖。我试过了,但没用

CODESANDBOX LINK CLICK HERE

theme/overrides/MuiButton.js

import palette from '../palette';

export default {
  contained: {
    backgroundColor: '#FFFFFF',
    '&.primary': {
      '&:hover': {
        backgroundColor: palette.primary.dark,
      },
    },
  },
};

theme/overrides/index.js

import MuiButton from "./MuiButton";

export default {
  MuiButton
};

theme/index.js

import { createMuiTheme } from "@material-ui/core";

import palette from "./palette";
import typography from "./typography";
import overrides from "./overrides";

const theme = createMuiTheme({
  palette,
  typography,
  overrides,
  zIndex: {
    appBar: 1200,
    drawer: 1100
  }
});

export default theme;

确定如何适当地覆盖主题中的默认样式的最佳资源是查看默认样式的定义方式。

来自https://github.com/mui-org/material-ui/blob/v4.11.0/packages/material-ui/src/Button/Button.js#L138

  containedPrimary: {
    color: theme.palette.primary.contrastText,
    backgroundColor: theme.palette.primary.main,
    '&:hover': {
      backgroundColor: theme.palette.primary.dark,
      // Reset on touch devices, it doesn't add specificity
      '@media (hover: none)': {
        backgroundColor: theme.palette.primary.main,
      },
    },
  },

将此方法转换为您问题中的代码如下所示:

import palette from '../palette';

export default {
  containedPrimary: {
    backgroundColor: '#FFFFFF',
    '&:hover': {
      backgroundColor: palette.primary.dark,
    },
  },
};