diff --git a/.changeset/shiny-knives-tell.md b/.changeset/shiny-knives-tell.md new file mode 100644 index 0000000000..72388dc93d --- /dev/null +++ b/.changeset/shiny-knives-tell.md @@ -0,0 +1,5 @@ +--- +'@emotion/react': patch +--- + +Add a dev-only warning about styles created with `css` from `@emotion/react` being passed to `cx` from ``. diff --git a/packages/react/__tests__/warnings.js b/packages/react/__tests__/warnings.js index 7f2a04a9ee..9ffadafe0a 100644 --- a/packages/react/__tests__/warnings.js +++ b/packages/react/__tests__/warnings.js @@ -1,7 +1,7 @@ // @flow /** @jsx jsx */ import 'test-utils/next-env' -import { jsx, css, Global, keyframes } from '@emotion/react' +import { jsx, css, Global, keyframes, ClassNames } from '@emotion/react' import renderer from 'react-test-renderer' import { render } from '@testing-library/react' @@ -259,3 +259,30 @@ test('`css` opaque object passed in as `className` prop', () => { `) }) + +test('`css` opaque object passed to `cx` from ', () => { + render( + + {({ cx }) => ( +
+ )} + + ) + + expect((console.error: any).mock.calls).toMatchInlineSnapshot(` +Array [ + Array [ + "You have passed styles created with \`css\` from \`@emotion/react\` package to the \`cx\`. +\`cx\` is meant to compose class names (strings) so you should convert those styles to a class name by passing them to the \`css\` received from component.", + ], +] +`) +}) diff --git a/packages/react/src/class-names.js b/packages/react/src/class-names.js index 6931adc6c6..650028a5d7 100644 --- a/packages/react/src/class-names.js +++ b/packages/react/src/class-names.js @@ -30,6 +30,16 @@ let classnames = (args: Array): string => { if (Array.isArray(arg)) { toAdd = classnames(arg) } else { + if ( + process.env.NODE_ENV !== 'production' && + arg.styles !== undefined && + arg.name !== undefined + ) { + console.error( + 'You have passed styles created with `css` from `@emotion/react` package to the `cx`.\n' + + '`cx` is meant to compose class names (strings) so you should convert those styles to a class name by passing them to the `css` received from component.' + ) + } toAdd = '' for (const k in arg) { if (arg[k] && k) {