【问题标题】:Use custom color schema in MUI theme在 MUI 主题中使用自定义颜色模式
【发布时间】:2021-10-11 11:36:58
【问题描述】:

我想在我的主题中配置一些颜色,供我的应用程序中的某些类别使用​​。

所以我设置了一个主题并在我的组件中使用它。

theme.tsx

import { createTheme, Theme } from '@mui/material/styles'
import { red } from '@mui/material/colors'

export const theme: Theme = createTheme({
  palette: {
    primary: {
      main: blue[800]
    },
    secondary: {
      main: '#19857b'
    },
    cat1: {
      main: red[700]
    }
  }
})

circle.tsx

import CircleIcon from '@mui/icons-material/FiberManualRecord'
import {theme} from '/shared/theme'

export function Circle() {
  return (
    <>
    <CircleIcon style={{ color: theme.palette.cat1.main }} />
    <CircleIcon style={{ color: theme.palette.cat1[200] }} />
    </>
  )
}

我试图实现的是以动态方式设置某些元素的颜色。因此,第 1 类中的圆将变为红色。所有需要的颜色都被导入到主题中。我不想在组件本身中导入所有可能的颜色。

但我也想根据这个计算另一个元素的颜色。在上面的示例中,我想获得 200 个红色。

【问题讨论】:

  • 您想通过只将red 对象传递给主题来使用颜色的所有形状吗?
  • @NearHuscarl 我不知道推荐的方式。最后,我只需要两三个等级的颜色。但我想计算它而不是通过硬编码所有可能性来污染主题配置......

标签: javascript reactjs material-ui


【解决方案1】:

首先,不要使用styles,它更难覆盖,MUI 有更好的替代方案(sx prop/styled 函数)在作为回调传递时为您提供theme 对象,所以将您的代码更改为:

<CircleIcon sx={{ color: theme => theme.palette.cat1.main }} />

其次,如果您想访问颜色的其他变体,例如使用primarysecondary,请使用augmentColor() 函数告诉MUI 生成dark/light/contrastText 颜色自动为您服务:

import {
  createTheme,
  ThemeProvider,
  Theme,
  lighten,
  darken
} from "@mui/material/styles";

const { palette } = createTheme();
const { augmentColor } = palette;
createTheme({
  palette: {
    // this will tell MUI to calculate the main, dark, light and contrastText
    // variants based on the red[500], and then merge the new properties with
    // the color object itself. The end result will be something like:
    // cat1: { '100': ..., '900': ..., light: ..., dark: ..., contrastText: ... }
    cat1: augmentColor({ color: red }),

    // this will tell MUI to calculate the main, dark, light and contrastText
    // variants based on the red[100], no other shades are passed unlike the above.
    cat2: augmentColor({ color: { main: red[100] } })

    // light and dark variants are generated in augmentColor using lighten() and
    // darken() function, if you want even more control, override the light and
    // dark properties yourself like this:
    cat3: augmentColor({
      color: {
        dark: darken(red[300], 0.6),
        main: red[300],
        light: lighten(red[300], 0.6)
      }
    })
  }
})

现场演示

【讨论】:

  • 非常有帮助。感谢那。如果需要另一个渐变,我该如何使用theme.palette.cat1.main?假设有两个图标:第一个应该是 cat1 的主颜色(红色),第二个应该是红色 [200]。
  • @user3142695 我的回答中的第一种方法将让您访问原始颜色的所有阴影,因为您传递了整个对象:theme.palette.cat1.maintheme.palette.cat1[500] 相同,您还可以访问其他阴影喜欢:theme.palette.cat1[200].
  • darken() 的计算结果如何?变暗总是+300或类似的东西吗?我的意思是如果主要是红色[500],那么是暗红色[300]?
  • @user3142695 我不这么认为,MUI 的颜色对象是手工制作的 (source)。使用lightendarken 根据您的需要定制更多内容。
  • @user3142695 关于implementation,他们只是修改了颜色的r、g、b值。例如,如果您增加darken() 的第二个参数值。它只是从那些 r,g,b 值中减去更多,直到它完全变暗 (#000000)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-06
  • 1970-01-01
  • 2023-01-28
  • 2022-12-20
  • 2015-10-04
  • 2017-11-29
相关资源
最近更新 更多