New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to use Material-UI theme with styled-components? #10098
Comments
This comment has been minimized.
This comment has been minimized.
👋 Thanks for using Material-UI! We use the issue tracker exclusively for bug reports and feature requests, however, If you would like to link from here to your question on SO, it will help others find it. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
The recommended approach is to use two theme provider: import React from 'react';
import styled, { ThemeProvider } from 'styled-components';
import {
StylesProvider,
ThemeProvider as MuiThemeProvider,
createMuiTheme,
darken,
fade,
} from '@material-ui/core';
const myTheme = createMuiTheme();
const StyledButton = styled.button`
${({ theme }) => `
padding: ${theme.spacing(1, 2)};
border: 1px solid;
cursor: pointer;
outline: 0;
border-radius: ${theme.shape.borderRadius}px;
color: ${theme.palette.primary.contrastText};
background-color: ${theme.palette.primary.main};
border-color: ${theme.palette.primary.main};
transition: ${theme.transitions.create(['background-color', 'box-shadow'])};
&:hover {
background-color: ${darken(theme.palette.primary.main, 0.1)};
border-color: ${darken(theme.palette.primary.main, 0.2)};
}
font-size: 18px;
${theme.breakpoints.up('md')} {
font-size: 16px;
}
`}
`;
export default function App() {
return (
<StylesProvider injectFirst>
<MuiThemeProvider theme={myTheme}>
<ThemeProvider theme={myTheme}>
<StyledButton>Styled Components</StyledButton>
</ThemeProvider>
</MuiThemeProvider>
</StylesProvider>
);
} |
@oliviertassinari will this approach cause performance issues? |
@malik-sahab None that I'm aware of. |
@oliviertassinari is there any way to use single theme provider? I would like to have determenistic classnames |
@R-Oscar the class names of styled-components aren't deterministic, there is no way to make them so. Stick to JSS in this case. |
@oliviertassinari I mean classes of @material-ui/core (such as |
#10098 (comment) will do the task. |
How can I access the mui theme with the example shown in the documentation on how to use the styled components API?
The text was updated successfully, but these errors were encountered: