question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[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:closed
  • Created 2 years ago
  • Reactions:4
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
bobbyconnollycommented, Jun 7, 2021

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

                [
                    "@babel/preset-react",
                    {
                        runtime: "automatic",
                        importSource: "@emotion/react",
                    },
                ],

plugins

                [
                    "@emotion/babel-plugin",
                    {
                        // sourceMap is on by default but source maps are dead code eliminated in production
                        sourceMap: true,
                        autoLabel: "dev-only",
                        labelFormat: "[local]",
                        cssPropOptimization: true,
                    },
                ],

Now this works:

import { css } from "@emotion/react"

...

                <Box
                    css={css`
                        & div {
                            color: hotpink;
                        }
                    `}
                >

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 work

0reactions
claudiusnascimentocommented, Jan 24, 2022

This 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, }

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found