Gib*_*boK 13 javascript reactjs material-ui jss
目前我使用以下代码使用jss为元素添加颜色.
const styleSheet = theme => ({
root: {
backgroundColor: theme.colors.red,
},
})Run Code Online (Sandbox Code Playgroud)
我想知道是否存在一个基于颜色添加不透明度的函数theme.colors.red.
smt的例子如:
backgroundColor: color(theme.colors.red, .05),
Cra*_*les 29
材质UI包含一个colorManipulator 实用程序文件,其中包含一个fade方法:
用法:
import { fade } from '@material-ui/core/styles/colorManipulator';
/**
* Set the absolute transparency of a color.
* Any existing alpha values are overwritten.
*
* @param {string} color - CSS color, i.e. one of: #nnn, #nnnnnn, rgb(), rgba(), hsl(), hsla()
* @param {number} value - value to set the alpha channel to in the range 0 -1
* @returns {string} A CSS color string. Hex input values are returned as rgb
*/
{
backgroundColor: fade(theme.colors.red, 0.5)
}
Run Code Online (Sandbox Code Playgroud)
该色库也可能是有用的:
import Color from 'color';
Run Code Online (Sandbox Code Playgroud)
然后,您可以在代码中引用它:
{
backgroundColor: Color(theme.colors.red).alpha(0.5).string()
}
Run Code Online (Sandbox Code Playgroud)
对于 MUI v5 这似乎有效:
import { alpha } from '@mui/material';
Run Code Online (Sandbox Code Playgroud)
...
MuiContainer: {
styleOverrides: {
root: {
'&.MuiContainer-asideWithImage': {
backgroundColor: alpha(MY_COLOR, 0.78),
},
},
},
},
Run Code Online (Sandbox Code Playgroud)
...
或者,您可以使用Material UI Next中提供的淡入淡出功能.
import {fade} from 'material-ui/styles/colorManipulator';
const theme = createMuiTheme({
overrides: {
MuiButton: {
root: {
boxShadow: `0 4px 8px 0 ${fade(defaultTheme.palette.primary[500], 0.18)}`,
}
},
}
});
export default theme;
Run Code Online (Sandbox Code Playgroud)
以下是它的工作原理:https://github.com/mui-org/material-ui/blob/v1-beta/src/styles/colorManipulator.js#L157-L164
另一种解决方案可能是使用https://github.com/styled-components/polished中的类似颜色函数
假设您还没有在颜色中定义 alpha 通道,您还可以执行以下操作:
backgroundColor: theme.colors.red + '00'
Run Code Online (Sandbox Code Playgroud)
这会将 alpha 通道设置为 0,因此是透明的。您可以在'00'到之间附加任何值'ff'
我找到了一个解决方案
backgroundColor: theme.utils.rgba(theme.axColor.black, 0.7),
Run Code Online (Sandbox Code Playgroud)