Replies: 1 comment 8 replies
-
After more trial and error and stumbling across this page in the documentation I found a way to use the material-UI theme with the CSS API without using the UseTheme hook: import {
Theme as EmotionTheme
} from "@emotion/react/macro";
import {
Theme as MaterialUITheme,
} from "@material-ui/core";
// Re-declare the emotion theme to have the properties of the MaterialUiTheme
declare module "@emotion/react" {
export interface Theme extends MaterialUITheme {}
}
// Define the button css
const ButtonStyle = (theme: MaterialUITheme) => css`
background: ${theme.palette.primary.main};
`; An example of this solution and all the other possible solutions can be found in the code-sandbox. I'm still curious if this is the current recommended solution for using the material-UI theme with emotion V11 or a shorter way of achieving the desired result? |
Beta Was this translation helpful? Give feedback.
8 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm currently switching from Material-UI v4 to v5. As a result, I also switched to using emotion.sh instead of JSS. I have to say I'm delighted with this styling library as it allows me to work with CSS as it was designed and makes my code cleaner 🚀 . I, however, encountered one problem when trying to translate one of my components to use emotion. This component contains a styled div that is styled based on colours defined in the material-theme. I use the solution given by @Andarist in #2252 to achieve this:
I then use the Material-ui (The Emotion ThemeProvider is redundant in Material-UI v5) to forward the theme object to the CSS API. I then use the following code to style the material-ui button:
As can be seen in this sandbox this seems to work fine, but it keeps throwing typescript errors.
I think this has something to do with the CSS API not having the right type definition for the material-UI theme prop. I'm not sure if this is an emotion.sh related Type definition problem or a material-UI Type definition problem. I tried several typescript definitions for the CSS arrow function but I haven't been able to find the right one. I also tried using the UseTheme hook to forward the material-UI theme, which seems to work without any problems. As I would like to use the shorter syntax, I was wondering if somebody else has found the right typescript syntax that makes typescript happy. Thanks a lot in advance!
What I already tried
Code sandbox
Possible related issues
theme
prop #1434Beta Was this translation helpful? Give feedback.
All reactions