【问题标题】:Access Button Primary Color In ReactReact中的访问按钮原色
【发布时间】:2020-10-30 23:41:03
【问题描述】:

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

CODESANDBOX 链接 CLICK HERE

主题/覆盖/MuiButton.js

import palette from '../palette';

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

主题/覆盖/index.js

import MuiButton from "./MuiButton";

export default {
  MuiButton
};

主题/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;

【问题讨论】:

  • 你在 CSS 中没有 SCSS 语法...
  • @DennisVash 他正在使用 Material Ui,所以不管他是否安装了 sass。
  • 谁说你在材质 UI 样式中有 scss?你有资源吗? - NVM 这里有一个例子:material-ui.com/styles/basics/#nesting-selectors,好像 MUI 使用 JSS 作为源
  • @DennisVash。我正在使用 JSS
  • 请展示您如何尝试利用这些样式。理想情况下,包括一个 code sandbox 来展示您的问题。

标签: css reactjs material-ui jss react-material


【解决方案1】:

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

来自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,
    },
  },
};

【讨论】:

猜你喜欢
  • 2020-10-06
  • 1970-01-01
  • 2016-08-27
  • 2015-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-21
  • 1970-01-01
相关资源
最近更新 更多