Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference to theme's primary color instead of a specific color in MUI

Tags:

Using ReactJS and MUI, I have a project in which I have changed the theme colors.

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(         <ThemeProvider theme={newTheme}>             <Project />         </ThemeProvider>     )   } 

Everything works as expected. Certain components, like buttons have the ability to set the color based on the primary prop. However, I have a component that uses an icon that needs the primary color. I can import the color and set it directly:

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} />         )     } } 

Is there some way to reference the theme's primary color, rather than setting the color in each component individually? Something like:

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} />         )     } } 
like image 401
DataSwede Avatar asked Apr 07 '17 14:04

DataSwede


People also ask

How do you use primary light color in MUI?

Make a class that specifies the colour you want and provide the hex colour code as the background colour. (also not ideal). Make a JSX class using makeStyles that takes the app's theme as an argument and provide the primary. light colour directly from your theme.

How do you use palette color in MUI?

You can explore the default values of the palette using the theme explorer or by opening the dev tools console on this page ( window.theme.palette ). The default palette uses the shades prefixed with A ( A200 , etc.) for the secondary palette color, and the un-prefixed shades for the other palette colors.


1 Answers

If you're using React v16.8.0 and Material-UI v3.5.0 or greater, you can utilize the useTheme() hook:

import { useTheme } from '@material-ui/core/styles';  function LockIcon = () => {   const theme = useTheme();    return (     <ActionLock color={theme.palette.primary1Color} /> } 
like image 148
jdoroy Avatar answered Oct 12 '22 05:10

jdoroy