Material UI主题调色板未将主色应用于主按钮背景色。

huangapple go评论65阅读模式
英文:

material ui theme palette does not apply the primary color to the primary button background color

问题

我想设置主颜色,这是所有 Material UI 控件的背景颜色

const theme = createTheme({
  palette: {
    primary: {
      main: '#00629A'
    },
  },
});

发生的情况是按钮的背景始终是透明的,文本颜色应用了主值!我做错了什么?

Material UI主题调色板未将主色应用于主按钮背景色。

当我检查该网站:https://zenoo.github.io/mui-theme-creator/#Buttons
并更改主颜色时,它会影响所有控件的背景颜色!


<details>
<summary>英文:</summary>

I want to set the primary color which is the ****background** color** for all material ui controls

    const theme = createTheme({
      palette: {
        primary: {
          main: &#39;#00629A&#39;
        },
      },
    });

     &lt;ThemeProvider theme={theme}&gt;
       &lt;Button&gt;Import&lt;/Button&gt;
     &lt;/ThemeProvider&gt;

What happens is that the button background is always transparent and the **text color** is getting the main value applied!?

What do I wrong?

[![enter image description here][1]][1]

When I check that Site: https://zenoo.github.io/mui-theme-creator/#Buttons
and I change the primary color it affects all controls background color!




  [1]: https://i.stack.imgur.com/GmnJ8.png

</details>


# 答案1
**得分**: 0

根据[文档][1]:

> 基本按钮
> 
> 按钮有三个变种:文本(默认)、包含和轮廓。

> 文本按钮
> 
> 文本按钮通常用于不太显著的操作,包括位于:对话框中,卡片中。在卡片中,文本按钮有助于保持对卡片内容的强调。

> 包含按钮
> 
> 包含按钮是高强调的,通过其使用高程和填充来区分。它们包含对您的应用程序至关重要的操作。


所以您的代码按预期运行。

如果您需要将背景颜色更改为主要颜色,您应该将 `variant` 设置为 `contained`,如下所示。

```jsx
<Button variant="contained">导入</Button>

如果您需要更改所有按钮,您可以使用 components 属性。

例如:

const theme = createTheme({
  palette: {
    primary: {
      main: "#00629A"
    }
  },
  components: {
    MuiButton: {
      defaultProps: {
        variant: "contained"
      }
    }
  }
});

您可以在这里看到整个示例。

英文:

According to documentation:

> Basic button
>
> The Button comes with three variants: text (default), contained, and
> outlined.

> Text button
>
> Text buttons are typically used for less-pronounced
> actions, including those located: in dialogs, in cards. In cards, text
> buttons help maintain an emphasis on card content.

> Contained button
>
> Contained buttons are high-emphasis, distinguished by
> their use of elevation and fill. They contain actions that are primary
> to your app.

So your code is working as expected.

If you need to change background color to primary color, you should set variant to contained like this.

&lt;Button variant=&quot;contained&quot;&gt;Import&lt;/Button&gt;

If you need to change all buttons, you can use components prop.

For example:

const theme = createTheme({
  palette: {
    primary: {
      main: &quot;#00629A&quot;
    }
  },
  components: {
    MuiButton: {
      defaultProps: {
        variant: &quot;contained&quot;
      }
    }
  }
});

You can see the whole example here.

huangapple
  • 本文由 发表于 2023年7月27日 22:23:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76780703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定