Search code examples
reactjsmaterial-uistyled-components

How do I access Material-ui's theme in Styled Component


I'm using CRA with Material-ui and Styled Components type of styling. When building my CSS I want to access Material-ui's default theme.

part of package.json:

  "dependencies": {
    "react": "^16.8.6",
    "react-dom": "^16.8.6",
    "react-scripts": "3.0.1",
    "@material-ui/core": "^4.2.1",
    "@material-ui/icons": "^4.2.1",
    "@material-ui/styles": "^4.2.1",
    "styled-components": "^4.3.2"
  }

When I try the below theme exists on props but is an empty object.

StyledApp.js:

import styled from "styled-components";
import Button from "@material-ui/core/Button";

export const StyledButtonUsingTheme = styled(Button)`
  //Below will give "Cannot read property 'error' of undefined" 
  background-color: ${props => props.theme.palette.error.light};
`;

App.js:

import React from "react";
import "./App.css";

import { StylesProvider, ThemeProvider } from "@material-ui/styles";
import { createMuiTheme } from "@material-ui/core/styles";

import { StyledButtonUsingTheme } from "./StyledApp";

function App() {
  const defaultTheme = createMuiTheme();

  window.console.log("Default theme passing to ThemeProvider", defaultTheme);

  return (
    <StylesProvider injectFirst>
      <ThemeProvider theme={defaultTheme}>
        <div className="App">
          <StyledButtonUsingTheme variant="outlined">
            Styled Button Using Theme
          </StyledButtonUsingTheme>
        </div>
      </ThemeProvider>
    </StylesProvider>
  );
}

export default App;

The console.log in App.js shows the whole theme object, and that's what I pass to ThemesProvider. Interestingly props.theme is there! but sadly with no values.


Solution

  • As Horyd in the comment says, using the ThemeProvider from Styled-Components will give you access to the theme properties inside your styled component. But Material-UI doesn't apply that theme anymore to its own components.

    The workaround I found is as ugly as it is simple: Use both Themeproviders. So Material-UI applies the theme to its components and you can access the theme in your styled components.

    import { ThemeProvider } from "styled-components";
    import { MuiThemeProvider,StylesProvider } from "@material-ui/core/styles";
    
    ReactDOM.render(
    
      //Make sure the Material stylesheet is placed above your own 
      //styles so you can overwrite them
      <StylesProvider injectFirst> 
    
        //Use the theme in the ThemeProvider for Material-UI so
        //styles are applied to the Material-UI components
        <MuiThemeProvider theme={theme}>
    
          //Use also the ThemeProvider for Styled-Components so 
          //you can access the theme in your own css
          <ThemeProvider theme={theme}>
    
            //Include your app and you have acces to everything 
            <App />
    
          </ThemeProvider>
    
        </MuiThemeProvider>
    
      </StylesProvider>,
    
    document.getElementById("app"));