在Material UI中引用主题的主要颜色而不是特定颜色

Dat*_*ede 7 reactjs material-ui

使用ReactJS和Material UI,我有一个项目,我在其中更改了主题颜色.

const newTheme = getMuiTheme({
    fontFamily: 'Roboto, sans-serif',
    palette: {
        primary1Color: cyan500,
        primary2Color: cyan700,
        primary3Color: grey400,
        accent1Color: amberA400,
        accent2Color: grey100,
        accent3Color: grey500,
        textColor: darkBlack,
        alternateTextColor: white,
        canvasColor: white,
        borderColor: grey300,
        disabledColor: fade(darkBlack, 0.3),
        pickerHeaderColor: cyan500,
        clockCircleColor: fade(darkBlack, 0.07),
        shadowColor: fullBlack,
    },
});

  // App class
  render() {
    return(
        <MuiThemeProvider muiTheme={newTheme}>
            <Project />
        </MuiThemeProvider>
    )
  }
Run Code Online (Sandbox Code Playgroud)

一切都按预期工作.某些组件(如按钮)可以根据主要道具设置颜色.但是,我有一个使用需要主色的图标的组件.我可以导入颜色并直接设置它:

import React from 'react';
import ActionLock from 'material-ui/svg-icons/action/lock';
import {cyan500} from 'material-ui/styles/colors';

export default class LockIcon extends React.Component {
    render() {
        return(
            <ActionLock color={cyan500} />
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法引用主题的主要颜色,而不是单独设置每个组件的颜色?就像是:

import React from 'react';
import ActionLock from 'material-ui/svg-icons/action/lock';
import {primary1Color} from 'somewhere';

export default class LockIcon extends React.Component {
    render() {
        return(
            <ActionLock color={primary1Color} />
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

jdo*_*roy 22

如果您使用的是 React v16.8.0 和 Material-UI v3.5.0 或更高版本,则可以使用useTheme()钩子:

import { useTheme } from '@material-ui/core/styles';

function LockIcon = () => {
  const theme = useTheme();

  return (
    <ActionLock color={theme.palette.primary1Color} />
}
Run Code Online (Sandbox Code Playgroud)

  • 正如下面的答案所说,使用 MUI v5 `import { useTheme } from "@mui/material/styles";` 然后 `const theme = useTheme();` 然后 `color={theme.palette.primary.main}` (3认同)

Nea*_*arl 15

如果您使用系统属性,您可以将对象的字符串路径定义Palette为颜色值,如下所示:

<Box sx={{ color: "primary.main" }}>primary.main</Box>
<Box sx={{ color: "secondary.main" }}>secondary.main</Box>
<Box sx={{ color: "error.main" }}>error.main</Box>
<Box sx={{ color: "warning.main" }}>warning.main</Box>
<Box sx={{ color: "info.main" }}>info.main</Box>
<Box sx={{ color: "success.main" }}>success.main</Box>
<Box sx={{ color: "text.primary" }}>text.primary</Box>
<Box sx={{ color: "text.secondary" }}>text.secondary</Box>
<Box sx={{ color: "text.disabled" }}>text.disabled</Box>
Run Code Online (Sandbox Code Playgroud)

上面的内容等同于:

<Box sx={{ color: theme => theme.palette.primary.main }}>primary.main</Box>
<Box sx={{ color: theme => theme.palette.secondary.main }}>secondary.main</Box>
<Box sx={{ color: theme => theme.palette.error.main }}>error.main</Box>
<Box sx={{ color: theme => theme.palette.warning.main }}>warning.main</Box>
<Box sx={{ color: theme => theme.palette.info.main }}>info.main</Box>
<Box sx={{ color: theme => theme.palette.success.main }}>success.main</Box>
<Box sx={{ color: theme => theme.palette.text.primary }}>text.primary</Box>
<Box sx={{ color: theme => theme.palette.text.secondary }}>text.secondary</Box>
<Box sx={{ color: theme => theme.palette.text.disabled }}>text.disabled</Box>
Run Code Online (Sandbox Code Playgroud)

使用回调的示例更加冗长,但类型安全,只有字符串的较短示例在原型设计时更快且效果更好,但您可能希望将字符串存储在常量中以防止任何拼写错误并帮助 IDE 重构您的代码更好的。

现场演示

编辑43281020/引用主题主色而不是材质中的特定颜色


yko*_*ach 10

是的,你有!使用muiThemeable ..

import muiThemeable from 'material-ui/styles/muiThemeable';
class LockIcon extends React.Component {
    render() {
        return(
            <ActionLock color={this.props.muiTheme.palette.primary1Color} />
        )
    }
}
        export default muiThemeable()(LockIcon)
Run Code Online (Sandbox Code Playgroud)

来自material-ui docs


che*_*nop 10

添加如何在material-ui v1.0.0(当前为beta)中访问主题颜色使用withTheme组件.
另请参阅以下示例.

import React, {Component} from 'react';
import { withTheme } from 'material-ui/styles';

class WithThemeExample extends Component {
    render() {
        const { theme } = props;
        const {primary, secondary} = theme.palette.text;

        return (
            <div>
                <div style={{color: primary}}>Hi in Primary color</div>
                <div style={{color: secondary}}>Bye in Secondary color</div>
            </div>
        );
    }
}

export default withTheme()(WithThemeExample);
Run Code Online (Sandbox Code Playgroud)


vin*_*ess 6

您可以使用 useTheme() 挂钩并访问颜色,如下例所示。还有一些其他颜色变体。

在此输入图像描述

梅5:

import * as React from 'react';
import PropTypes from 'prop-types';
import { NavLink } from "react-router-dom";
import { useTheme } from "@mui/material/styles";

function VinNavLink(props) {
  const theme = useTheme();

  return (
    <NavLink {...props} style={({ isActive }) => isActive ? { textDecoration: "underline", color: theme.palette.primary.main} : undefined}>{props.children}</NavLink>
  );
}

export default VinNavLink;
Run Code Online (Sandbox Code Playgroud)


sha*_*rma 5

好的,如果您使用的是大于 4 的 material-ui 版本,那么上述解决方案可能不适合您。按照下面的代码

import { withTheme } from '@material-ui/core/styles';

function DeepChildRaw(props) {
  return <span>{`spacing ${props.theme.spacing}`}</span>;
}

const DeepChild = withTheme(DeepChildRaw);
Run Code Online (Sandbox Code Playgroud)

参考:https : //material-ui.com/styles/advanced/


Ham*_*eed 5

有了 React hooks,现在你不需要在withTheme中扭曲组件,只需使用useTheme

import { useTheme } from '@material-ui/core/styles';

export default function MyComponent() {
    const theme = useTheme();
    
    return <span>{`spacing ${theme.spacing}`}</span>;
}
Run Code Online (Sandbox Code Playgroud)