Using the css Prop of @emotion/react and apply babel plugin
See original GitHub issueI am just submitting this issue so that it can be searched by others.
- Install these packages:
yarn add @emotion/babel-plugin @emotion/react
- Modify your vite.config.ts
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
react({
jsxImportSource: "@emotion/react",
babel: {
plugins: ["@emotion/babel-plugin"],
},
}),
],
});
- Modify tsconfig.json
"compilerOptions": {
"types": ["vite/client", "@emotion/react/types/css-prop"]
- Modify .story/book/main.js
const react = require("@vitejs/plugin-react");
module.exports = {
stories: ["../src/**/*.stories.mdx", "../src/**/*.stories.@(js|jsx|ts|tsx)"],
addons: ["@storybook/addon-links", "@storybook/addon-essentials"],
framework: "@storybook/react",
core: {
builder: "storybook-builder-vite",
},
async viteFinal(config) {
config.plugins = config.plugins.filter(
(plugin) =>
!(Array.isArray(plugin) && plugin[0]?.name.includes("vite:react"))
);
config.plugins.push(
react({
exclude: [/\.stories\.(t|j)sx?$/, /node_modules/],
jsxImportSource: "@emotion/react",
babel: {
plugins: ["@emotion/babel-plugin"],
},
})
);
console.log(config.plugins);
return config;
},
};
That’s it! Happy coding!!
Issue Analytics
- State:
- Created 2 years ago
- Reactions:27
- Comments:8 (1 by maintainers)
Top Results From Across the Web
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 >What is the proper way to enable the css prop in Emotion 11 ...
Here is a working config with emotion and Next.js npm install @emotion/react @emotion/styled && npm i --save-dev @emotion/babel-plugin
Read more >@emotion/babel-plugin | Yarn - Package Manager
The css prop doesn't work via the babel plugin. jsx can be manually imported from @emotion/core (which can be automated with eslint-plugin-emotion) or...
Read more >How To Use Emotion for Styling in React | DigitalOcean
emotion provides a css prop that can accept nested selectors and media queries. It can support an object or a tagged template literal....
Read more >gatsby-plugin-emotion
Provide support for using the css-in-js library Emotion including server side rendering. This plugin supports Emotion v11+. Older versions should use ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@terenceodonoghue - I was having the same problem and errors trying to get MDX working along side my stories. I saw your solution but I didn’t really want to add that pragma to every file in our repo. I ended up trying out the MDX2 Opt-in feature, and the MDX stories started building without issues! Shout out to @IanVS for all the help on Discord.
Here is my entire
storybook/main.js
@bestickley I know it’s been awhile since you asked, but in answer to your question: since storybook already defines the react plugin for vite, you have to remove it first, and then re-add it with a custom configuration.
If you do not omit the plugin that storybook provides, then vite complains about conflicting plugins.