覆盖 Material UI 5 主题中的按钮文本

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

Override Material UI 5 Button Text in my theme

问题

我正在尝试将 MuiButton 文本改为小写,但我找不到方法。
我分享代码和截图。

theme.jsx

import { createTheme } from "@mui/material/styles";

export const theme = createTheme({
  palette: {
    mode: "light",
    primary: {
      main: "#E9531E",
    },
    secondary: {
      main: "#4357AD",
    },
  },
  typography: {
    fontFamily: "Roboto",
    textTransform: "none",
  },...

overrides: {
    MuiButton: {
      root: {
        background: "linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)",
        border: 0,
        borderRadius: 3,
        boxShadow: "0 3px 5px 2px rgba(255, 105, 135, .3)",
        color: "white",
        height: 48,
        padding: "0 30px",
        textTransform: "none",
      },
    },

App.jsx

import Navbar from "./components/Navbar";
import Login from "./pages/Login";

function App() {
  return (
    <div className="App">
      <Navbar />
      <Login />
    </div>
  );
}

export default App;

index.jsx

import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import App from "./App";
import { ThemeProvider } from "@mui/material";
import { theme } from "./theme";

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  
  <ThemeProvider theme={theme}>
    
    <App />
    
  </ThemeProvider>
);

全局文本样式更改,但按钮样式不更改。

我尝试过使用 styled,但我不知道如何正确使用它们。

英文:

I'm trying to change the MuiButton text to lowercase but I can't find a way.
I share code and screenshot.

theme.jsx

import { createTheme, } from &quot;@mui/material/styles&quot;;

export const theme = createTheme({
  palette: {
    mode: &quot;light&quot;,
    primary: {
      main: &quot;#E9531E&quot;,
    },
    secondary: {
      main: &quot;#4357AD&quot;,
    },
  },
  typography: {
    fontFamily: &quot;Roboto&quot;,
    textTransform: &quot;none&quot;,
  },...

overrides: {
    MuiButton: {
      root: {
        background: &quot;linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)&quot;,
        border: 0,
        borderRadius: 3,
        boxShadow: &quot;0 3px 5px 2px rgba(255, 105, 135, .3)&quot;,
        color: &quot;white&quot;,
        height: 48,
        padding: &quot;0 30px&quot;,
        textTransform: &quot;none&quot;,
      },
    },

App.jsx

import Navbar from &quot;./components/Navbar&quot;;
import Login from &quot;./pages/Login&quot;;

function App() {
  return (
    &lt;div className=&quot;App&quot;&gt;
      &lt;Navbar /&gt;
      &lt;Login /&gt;
    &lt;/div&gt;
  );
}

export default App;

index.jsx

import React from &quot;react&quot;;
import ReactDOM from &quot;react-dom/client&quot;;
import &quot;./index.css&quot;;
import App from &quot;./App&quot;;
import { ThemeProvider } from &quot;@mui/material&quot;;
import { theme } from &quot;./theme&quot;;

const root = ReactDOM.createRoot(document.getElementById(&quot;root&quot;));
root.render(
  
  &lt;ThemeProvider theme={theme}&gt;
    
    &lt;App /&gt;
    
  &lt;/ThemeProvider&gt;
);

The global text style changes but the button style does not.

I have tried styled but I don't know how to use them well.

答案1

得分: 0

你需要将样式覆盖更改为以下内容:

const theme = createTheme({
  ...,
  components: {
    MuiButton: {
      styleOverrides: {
        root: {
          background: "linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)",
          border: 0,
          borderRadius: 3,
          boxShadow: "0 3px 5px 2px rgba(255, 105, 135, .3)",
          color: "white",
          height: 48,
          padding: "0 30px",
          textTransform: "none",
        },
      },
    },
  },
});

查看文档以获取更多信息:
https://mui.com/material-ui/customization/theme-components/#theme-style-overrides

英文:

You need to change the style overrides to this:

const theme = createTheme({
  ...,
  components: {
    MuiButton: {
      styleOverrides: {
        root: {
          background: &quot;linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)&quot;,
          border: 0,
          borderRadius: 3,
          boxShadow: &quot;0 3px 5px 2px rgba(255, 105, 135, .3)&quot;,
          color: &quot;white&quot;,
          height: 48,
          padding: &quot;0 30px&quot;,
          textTransform: &quot;none&quot;,
        },
      },
    },
  },
});

Check out the docs for more information:
https://mui.com/material-ui/customization/theme-components/#theme-style-overrides

huangapple
  • 本文由 发表于 2023年8月9日 18:40:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76866962.html
匿名

发表评论

匿名网友

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

确定