[V5] Fix type when assigning SerializedStyles from emotion's css() to `sx` prop
See original GitHub issue- I have searched the issues of this repository and believe that this is not a duplicate.
Summary 💡
I noticed that the docs point us to do the following
<Box
sx={{
"& div": {
color: "hotpink",
},
}}
>
This also works when you import { css } from “@emotion/react”
<Box
sx={css`
& div {
color: hotpink;
}
`}
>
Although this works fine and the styling gets applied, there’s a Typescript warning:
(JSX attribute) sx?: SxProps<Theme> | undefined
No overload matches this call.
Overload 1 of 2, '(props: { component: ElementType<any>; } & { maxWidth?: ResponsiveStyleValue<MaxWidth<string | number> | (MaxWidth<string | number> | undefined)[] | undefined>; ... 63 more ...; textAlign?: ResponsiveStyleValue<...>; } & ... 4 more ... & Omit<...>): Element', gave the following error.
Type 'SerializedStyles' is not assignable to type 'SxProps<Theme> | undefined'.
Type 'SerializedStyles' is not assignable to type 'CSSSelectorObject<Theme>'.
Index signature is missing in type 'SerializedStyles'.
Overload 2 of 2, '(props: DefaultComponentProps<BoxTypeMap<{}, "div">>): Element', gave the following error.
Type 'SerializedStyles' is not assignable to type 'SxProps<Theme> | undefined'.ts(2769)
Box.d.ts(11, 7): The expected type comes from property 'sx' which is declared here on type 'IntrinsicAttributes & { css?: Interpolation<Theme>; } & { component: ElementType<any>; } & { maxWidth?: ResponsiveStyleValue<MaxWidth<string | number> | (MaxWidth<...> | undefined)[] | undefined>; ... 63 more ...; textAlign?: ResponsiveStyleValue<...>; } & ... 4 more ... & Omit<...>'
Box.d.ts(11, 7): The expected type comes from property 'sx' which is declared here on type 'IntrinsicAttributes & { css?: Interpolation<Theme>; } & { maxWidth?: ResponsiveStyleValue<MaxWidth<string | number> | (MaxWidth<string | number> | undefined)[] | undefined>; ... 63 more ...; textAlign?: ResponsiveStyleValue<...>; } & ... 4 more ... & Omit<...>'
Maybe we are simply not supposed to do this (despite the fact that it works?). Here are my dependencies:
“@material-ui/core”: “^5.0.0-alpha.35”, “@emotion/react”: “^11.4.0”,
Examples 🌈
N/A
Motivation 🔦
Some might prefer the string interpolation way of doing things rather than the direct JS object way.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:4
- Comments:5 (2 by maintainers)
Top Results From Across the Web
[V5] Fix type when assigning SerializedStyles from emotion's ...
Type 'SerializedStyles' is not assignable to type 'CSSSelectorObject<Theme>'. Index signature is missing in type 'SerializedStyles'.
Read more >Is there an analog of Emotions 'css' function in MUI 5?
There is css function in system and styled-engine but it does not seem to do the same, it returns SerializedStyles . The use...
Read more >How to use the sx prop in MUI v5. Still using makeStyles?
MUI's custom styling properties; how to nest CSS selectors inside the sx prop; using the sx prop for responsive design. Example demo: simple...
Read more >The css Prop - Emotion
The primary way to style elements with emotion is the css prop. It provides a concise and flexible API to style your components....
Read more >CHANGELOG.md - Material-UI - Fossies
[system] Fix executing server-side Emotion component as function interpolation 2 (#31024) @Andarist; [system] Fix sx prop types when CSS variables are ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
So I dug a little deeper and it turns out you can just use the css prop. I just had my babel config set incorrectly. Here are the settings in case anyone is interested:
presets
plugins
Now this works:
I really don’t know how I got into such a twist reading the docs but posting here helped me untangle, so thank you 😃
I guess the only loose end is why the
sx
prop also seems to workThis works for me:
import { SxProps } from ‘@mui/system’; import { Theme } from ‘@mui/material/styles’;
const style: SxProps<Theme> = { position: ‘absolute’, top: ‘50%’, left: ‘50%’, transform: ‘translate(-50%, -50%)’, width: ‘100%’, maxWidth: 900, bgcolor: ‘background.paper’, border: ‘1px solid #a7a7a7’, boxShadow: 24, p: 4, }