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.

Component not render correctly using only emotion CSS props

See original GitHub issue

Bug Report

Given the following basic component code

// @flow
import React from 'react'
import { css } from '@emotion/core'

const base = css`
  color: red;
`

export const Button = ({ children }) => <button css={base}>{children}</button>

.babelrc

{
  "presets": ["@babel/preset-flow", "@emotion/babel-preset-css-prop"]
}

Expected behavior

It should render a button with red color on it. But instead the css is not loaded correctly

Additional context/Screenshots docz example flow 2018-12-29 20-12-20 docz example flow 2018-12-29 20-12-53

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
ticidesigncommented, Mar 6, 2019

I had the same problem and @mitchellhamilton one of the maintainer of Emotion helped me.

Docz appends its babel preset to the user provided config so since its babel preset includes @babel/preset-react which includes the react jsx transform which @emotion/babel-preset-css-prop also uses. So Docz’s usage of the react JSX transform is used by default.

To fix this, we’re including the JSX transform plugin in the plugins in our doczrc.js file. This works because plugins in the config take precedence over plugins in presets.

plugins: [
    createPlugin({
      modifyBabelRc(babelrc) {
        return {
          ...babelrc,
          plugins: [
            ...babelrc.plugins,
            [
              '@babel/plugin-transform-react-jsx',
              { pragma: '___EmotionJSX', pragmaFrag: 'React.Fragment' },
            ],
          ],
        };
      },
    }),
  ],

Credits to @mitchellhamilton

2reactions
nathanmsmithcommented, Jan 8, 2019

I’m getting issue with both the pragma and the babel preset – my issue seems to be that I’m using TypeScript, since it works fine for me just using .js files. If it helps, I can set up a demo repo.

Read more comments on GitHub >

github_iconTop Results From Across the Web

emotion css feature is not rendering the styles (React)
I can create a styled component and use it. However, if I try to use the native css`` feature it renders the object...
Read more >
The css Prop
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 >
How to Make a List Component with Emotion
This looks pretty weird to me because, not only are we writing styles for the ... to make sure your component is rendering...
Read more >
Frequently Asked Questions - Material UI
As a rule of thumb, only use inline-styles for dynamic style properties. ... All MUI components that should render something in the DOM...
Read more >
The best styling options for Next.js
As a result, you can start writing CSS right away with no setup required. ... styles/globals.css' function MyApp({ Component, pageProps }) ...
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