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

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

Override Material UI 5 Button Text in my theme

问题

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

theme.jsx

  1. import { createTheme } from "@mui/material/styles";
  2. export const theme = createTheme({
  3. palette: {
  4. mode: "light",
  5. primary: {
  6. main: "#E9531E",
  7. },
  8. secondary: {
  9. main: "#4357AD",
  10. },
  11. },
  12. typography: {
  13. fontFamily: "Roboto",
  14. textTransform: "none",
  15. },...
  16. overrides: {
  17. MuiButton: {
  18. root: {
  19. background: "linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)",
  20. border: 0,
  21. borderRadius: 3,
  22. boxShadow: "0 3px 5px 2px rgba(255, 105, 135, .3)",
  23. color: "white",
  24. height: 48,
  25. padding: "0 30px",
  26. textTransform: "none",
  27. },
  28. },

App.jsx

  1. import Navbar from "./components/Navbar";
  2. import Login from "./pages/Login";
  3. function App() {
  4. return (
  5. <div className="App">
  6. <Navbar />
  7. <Login />
  8. </div>
  9. );
  10. }
  11. export default App;

index.jsx

  1. import React from "react";
  2. import ReactDOM from "react-dom/client";
  3. import "./index.css";
  4. import App from "./App";
  5. import { ThemeProvider } from "@mui/material";
  6. import { theme } from "./theme";
  7. const root = ReactDOM.createRoot(document.getElementById("root"));
  8. root.render(
  9. <ThemeProvider theme={theme}>
  10. <App />
  11. </ThemeProvider>
  12. );

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

我尝试过使用 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

  1. import { createTheme, } from &quot;@mui/material/styles&quot;;
  2. export const theme = createTheme({
  3. palette: {
  4. mode: &quot;light&quot;,
  5. primary: {
  6. main: &quot;#E9531E&quot;,
  7. },
  8. secondary: {
  9. main: &quot;#4357AD&quot;,
  10. },
  11. },
  12. typography: {
  13. fontFamily: &quot;Roboto&quot;,
  14. textTransform: &quot;none&quot;,
  15. },...
  16. overrides: {
  17. MuiButton: {
  18. root: {
  19. background: &quot;linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)&quot;,
  20. border: 0,
  21. borderRadius: 3,
  22. boxShadow: &quot;0 3px 5px 2px rgba(255, 105, 135, .3)&quot;,
  23. color: &quot;white&quot;,
  24. height: 48,
  25. padding: &quot;0 30px&quot;,
  26. textTransform: &quot;none&quot;,
  27. },
  28. },

App.jsx

  1. import Navbar from &quot;./components/Navbar&quot;;
  2. import Login from &quot;./pages/Login&quot;;
  3. function App() {
  4. return (
  5. &lt;div className=&quot;App&quot;&gt;
  6. &lt;Navbar /&gt;
  7. &lt;Login /&gt;
  8. &lt;/div&gt;
  9. );
  10. }
  11. export default App;

index.jsx

  1. import React from &quot;react&quot;;
  2. import ReactDOM from &quot;react-dom/client&quot;;
  3. import &quot;./index.css&quot;;
  4. import App from &quot;./App&quot;;
  5. import { ThemeProvider } from &quot;@mui/material&quot;;
  6. import { theme } from &quot;./theme&quot;;
  7. const root = ReactDOM.createRoot(document.getElementById(&quot;root&quot;));
  8. root.render(
  9. &lt;ThemeProvider theme={theme}&gt;
  10. &lt;App /&gt;
  11. &lt;/ThemeProvider&gt;
  12. );

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

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

  1. const theme = createTheme({
  2. ...,
  3. components: {
  4. MuiButton: {
  5. styleOverrides: {
  6. root: {
  7. background: "linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)",
  8. border: 0,
  9. borderRadius: 3,
  10. boxShadow: "0 3px 5px 2px rgba(255, 105, 135, .3)",
  11. color: "white",
  12. height: 48,
  13. padding: "0 30px",
  14. textTransform: "none",
  15. },
  16. },
  17. },
  18. },
  19. });

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

英文:

You need to change the style overrides to this:

  1. const theme = createTheme({
  2. ...,
  3. components: {
  4. MuiButton: {
  5. styleOverrides: {
  6. root: {
  7. background: &quot;linear-gradient(45deg, #FE6B8B 30%, #FF8E53 90%)&quot;,
  8. border: 0,
  9. borderRadius: 3,
  10. boxShadow: &quot;0 3px 5px 2px rgba(255, 105, 135, .3)&quot;,
  11. color: &quot;white&quot;,
  12. height: 48,
  13. padding: &quot;0 30px&quot;,
  14. textTransform: &quot;none&quot;,
  15. },
  16. },
  17. },
  18. },
  19. });

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:

确定