I'm learning Gatsby and attempting to build a few components using Material-UI and Styled Components and applying my theme. I was successful in applying a theme by following the Gatsby configurations for the material-ui plug-in explained here(https://www.gatsbyjs.org/packages/gatsby-plugin-material-ui/)
and also the Material-ui documentation example provided here(https://material-ui.com/customization/palette/)
My problem arrives when I install Styled Components, I can no longer apply my themes. It seems to be correctly installed as it does follow some other rules. Could someone tell me where I'm going wrong in accessing my theme properties?
This is my code, I placed the trail incase it's needed but nav.js is where my problem is.
gatsby-config.js
module.exports = {
plugins:[
<other plugins>,
{
resolve: 'gatsby-plugin-material-ui',
// If you want to use styled components you should change the injection order.
options: {
StylesProvider: {
injectFirst: true,
},
},
},
'gatsby-plugin-styled-components',
]
theme.js
import React from 'react'
import { createMuiTheme, colors, MuiThemeProvider, StylesProvider } from '@material-ui/core';
import { ThemeProvider } from 'styled-components';
import CssBaseline from '@material-ui/core/CssBaseline'
// A custom theme for this app
const theme = createMuiTheme({
palette: {
type : 'dark',
primary : { 500 : '#407855' },
secondary : { 500 : '#9c27b0' },
},
});
const ThemeWrapper = (props) => {
return (
<StylesProvider>
<MuiThemeProvider theme={theme}>
<ThemeProvider theme={theme}>
<CssBaseline />
{props.children}
</ThemeProvider>
</MuiThemeProvider>
</StylesProvider>
)
}
export default ThemeWrapper;
index.js
import React from 'react'
import ThemeWrapper from '../theme/theme'
import Layout from '../component/layout'
const IndexPage = ({ data }) => {
return (
<ThemeWrapper>
<Layout>
</Layout>
</ThemeWrapper>
)
}
export default IndexPage
layout.js
import React from 'react'
import NavBar from './nav'
import Footer from '../component/footer'
const Layout = (props) => {
return (
<div>
<div>
<NavBar />
{props.children}
</div>
<Footer />
</div>
)
}
export default Layout
nav.js
import React from 'react'
import styled from 'styled-components';
import { Link, graphql, useStaticQuery } from 'gatsby'
import { Paper, Tabs, Tab } from '@material-ui/core'
const StyledTabs = styled(Tabs)`
backgroundColor: transparent;
boxShadow: none;
textColor: primary !important; // Doesn't work
indicatorColor:${ props => props.data.theme.palette.secondary}; // Doesn't work
color : tomato; // Works
)`;
function Nav ( props ) {
return (
<Paper>
<StyledTabs value={0}>
<Tab label="Item One" />
<Tab label="Item Two" />
<Tab label="Item Three" />
</StyledTabs>
</Paper>
)
}
export default Nav
If I were to use the Tabs and Tab component instead of the StyledTabs it works
<Paper>
<Tabs textColor="primary" indicatorColor="secondary" value={0}> //works perfectly
<Tab label="Item One" />
<Tab label="Item Two" />
<Tab label="Item Three" />
</StyledTabs>
</Paper>