Skip to main content

Emotion’s Babel plugin or Emotion’s jsx function

To use Emotion's jsx function instead of the default React createElement function, you need to set up your environment to leverage Emotion's jsx pragma. This setup ensures that your css prop is correctly processed by Emotion. Here’s how you can achieve that:

Step-by-Step Setup

  1. Install Emotion Dependencies: Make sure you have Emotion installed in your project.

    npm install @emotion/react @emotion/babel-plugin
  2. Configure Babel: You need to configure Babel to use Emotion’s Babel plugin. This plugin transforms the css prop and other Emotion-specific features at compile time.

    Create or update your Babel configuration file (e.g., .babelrc or babel.config.json) with the following:

    {
    "presets": ["@babel/preset-react"],
    "plugins": ["@emotion"]
    }
  3. JSX Pragma: Use the /** @jsxImportSource @emotion/react */ pragma at the top of your JavaScript/TypeScript files. This pragma tells the compiler to use Emotion’s jsx function instead of React’s createElement.

    Here is an example component using the jsx pragma:

    /** @jsxImportSource @emotion/react */
    import { css } from '@emotion/react';

    const divStyle = (primary) => css`
    background-color: ${primary ? 'lightblue' : 'lightgray'};
    border: 2px solid ${primary ? 'darkblue' : 'darkgray'};
    border-radius: 10px;
    padding: 20px;
    margin: 10px;
    text-align: center;
    `;

    const App = () => (
    <div css={divStyle(true)}>
    This is a primary styled div.
    </div>
    );

    export default App;

Explanation

  1. JSX Pragma:

    • The /** @jsxImportSource @emotion/react */ pragma at the top of the file ensures that the Emotion jsx function is used instead of the default React createElement function. This allows Emotion to process the css prop and other Emotion-specific features.
  2. CSS Function:

    • The css function from Emotion is used to create a style definition. This function takes a template literal and returns a unique class name generated by Emotion.
  3. Usage in Component:

    • The css prop is used on the div element. Emotion processes this prop, generates the appropriate styles, and applies them to the element.

Without Babel Plugin (Alternative Approach)

If you prefer not to use the Babel plugin, you can manually use Emotion's jsx function by importing it directly in your components. Here’s how you can do that:

/** @jsxImportSource @emotion/react */
import { jsx, css } from '@emotion/react';

const divStyle = (primary) => css`
background-color: ${primary ? 'lightblue' : 'lightgray'};
border: 2px solid ${primary ? 'darkblue' : 'darkgray'};
border-radius: 10px;
padding: 20px;
margin: 10px;
text-align: center;
`;

const App = () => (
<div css={divStyle(true)}>
This is a primary styled div.
</div>
);

export default App;

In this setup:

  • You still need the /** @jsxImportSource @emotion/react */ pragma to ensure the correct jsx function is used.
  • Emotion processes the css prop, generating the required class names and styles.

Summary

  • Install Emotion: Ensure you have @emotion/react and @emotion/babel-plugin installed.
  • Babel Configuration: Configure Babel to use the Emotion plugin for seamless css prop handling.
  • JSX Pragma: Use /** @jsxImportSource @emotion/react */ to replace React’s createElement with Emotion’s jsx function.
  • Apply Styles: Use the css prop or Emotion’s styled components to apply styles.

By following these steps, you can effectively use Emotion’s jsx function to handle the css prop and other Emotion-specific features, enabling powerful and dynamic styling in your React applications.