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
-
Install Emotion Dependencies: Make sure you have Emotion installed in your project.
npm install @emotion/react @emotion/babel-plugin
-
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
orbabel.config.json
) with the following:{
"presets": ["@babel/preset-react"],
"plugins": ["@emotion"]
} -
JSX Pragma: Use the
/** @jsxImportSource @emotion/react */
pragma at the top of your JavaScript/TypeScript files. This pragma tells the compiler to use Emotion’sjsx
function instead of React’screateElement
.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
-
JSX Pragma:
- The
/** @jsxImportSource @emotion/react */
pragma at the top of the file ensures that the Emotionjsx
function is used instead of the default ReactcreateElement
function. This allows Emotion to process thecss
prop and other Emotion-specific features.
- The
-
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.
- The
-
Usage in Component:
- The
css
prop is used on thediv
element. Emotion processes this prop, generates the appropriate styles, and applies them to the element.
- The
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 correctjsx
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’screateElement
with Emotion’sjsx
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.