-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't get emotion's css prop working inside storybook #7540
Comments
Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks! |
I'm seemingly bumping into this too. Also have tried all the suggested workarounds in the referenced issues. My CSS props work just fine outside of Storybook but once I am running in storybook my css props aren't working. Anything that's basically just a string works but any time I am referencing props in the |
Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks! |
I'm seeing this same issue in Has there been any traction on this issue? |
Also just upgraded to |
🙌 Based our now working config kinda on this: emotion-js/emotion#1359 (comment) but with some additional crap for resolving sass stuff (we're in the process of migrating and our storybook stuff stopped working without anyone noticing). Sorry for any inconvenience and thanks for the awesome library. |
@shilman in case you missed, I think this ticket is potentially resolvable but I'd ask @ryanlanciaux / @jaredh159 to verify |
@brekk I'm not clear exactly what the fix is you found, and where I would apply it. Could you send a PR to my simple repro repo linked in the OP? |
@jaredh159 @shilman I was able to track at least part of the problem down in the linked if (parent.isFunctionDeclaration() && parent.node && parent.node.id && parent.node.id.name) { With that one of the two components renders correctly. I'd be happy to help troubleshoot more but that's all I was able to uncover in that repo. |
I also ran into this issue with Babel7+Typescript+React+Emotion+Storybook setup. After hours of digging through docs and experiments I confirmed that const path = require("path");
module.exports = function({ config }) {
config.module.rules.push({
test: /\.(ts|tsx)$/,
loader: require.resolve("babel-loader"),
options: {
presets: [["react-app", { flow: false, typescript: true }], require.resolve("@emotion/babel-preset-css-prop")],
},
});
config.resolve.extensions.push(".ts", ".tsx");
return config;
}; works only when root .babelrc does not specify @emotion/babel-preset-css-prop plugin (which creates problems when transpiling for publish). Here is working {
"presets": [
"@babel/preset-env",
"@babel/preset-typescript"
// "@emotion/babel-preset-css-prop"
]
} Un-commenting I understand that ordering of presets is key here, since we want At the same time I don't completely understand how presets in root .babelrc interact with webpack babel-loader options. The observed behavior suggests that root presets are processed first, then loader option presets are processed and |
@illinar Have you tried the |
Thanks. This option seems to do the trick. |
Can somebody create a preset for this? https://storybook.js.org/docs/presets/writing-presets/#docs-content |
The OG post is unrelated to most of the discussion on this thread. I actually pulled his repo and fixed the error. He was exporting an anonymous component. Error message: Original CssPropButton.js: export default function() {
return (
<button
css={css`
color: hotpink;
`}
>
I should be hot pink!
</button>
);
} Fixed with no error: export default function CssPropButton() {
return (
<button
css={css`
color: hotpink;
`}
>
I should be hot pink!
</button>
);
} Unfortunately, this didn't solve the error I am having. His build set with the |
I cannot get this to work at all. The only way I can get this to work is if I use the jsx pragma, and then, it breaks storybook's ability to generate or infer prop descriptions for the docs. I'm not certain, but I'm assuming this is because both storybook's doc addon and emotion want to rewrite React.createElement? |
In my project I was able to get Storybook to recognize css-prop (without using the jsx pragma) by adding this config to Line 17 - Line 19: config.module.rules[0].use[0].options.presets = [
require.resolve('@babel/preset-react'),
require.resolve('@babel/preset-env'),
// Emotion preset must run BEFORE reacts preset to properly convert css-prop.
// Babel preset-ordering runs reversed (from last to first). Emotion has to be after React preset.
require.resolve('@emotion/babel-preset-css-prop'),
]; Also, I am including the Line 41 - Line 43: config.module.rules.push({
test: /\.(ts|tsx)$/,
loader: require.resolve('babel-loader'),
options: {
presets: [
['react-app', { flow: false, typescript: true }],
// Emotion preset must run BEFORE reacts preset to properly convert css-prop.
// Babel preset-ordering runs reversed (from last to first). Emotion has to be after React preset.
require.resolve('@emotion/babel-preset-css-prop'),
],
// other plugins here...
},
}); I created a template to share my project config that uses Gatsby + TypeScript + Emotion + Storybook + React Intl + SVGR + Jest: Here's a link to my |
@redshoga Your fix worked for me. This is the JavaScript version:
module.exports = {
webpackFinal: (config) => {
config.module.rules.push({
test: /\.(js|jsx)$/,
loader: require.resolve('babel-loader'),
options: {
cacheDirectory: true,
presets: [require.resolve('@emotion/babel-preset-css-prop')],
},
});
return config;
},
}; This project is brand new built with Emotion and Next.js just tonight. I'm surprised I had to add this hack for it to work. I have Storybook + Emotion playing nicely together without a hack on two separate projects at work, but I'm not sure why it works there and not here. A few things to note:
|
I fixed it with the settings below. pacakge.json "react": "^17.0.1",
"react-dom": "^17.0.1",
"@storybook/react": "^6.1.21",
"@emotion/react": "^11.1.2",
"typescript": "^4.1.3", .storybook/main.js module.exports = {
reactOptions: {
fastRefresh: true,
strictMode: true,
},
stories: ['../src/**/*.stories.@(ts|tsx|mdx)'],
addons: ['@storybook/addon-links', '@storybook/addon-essentials'],
webpackFinal: async (config) => {
config.module.rules[0].use[0].options.presets = [
require.resolve('@babel/preset-env'),
require.resolve('@babel/preset-typescript'),
[
require.resolve('@babel/preset-react'),
{
runtime: 'automatic',
importSource: '@emotion/react',
},
],
]
config.module.rules[0].use[0].options.plugins = [
...config.module.rules[0].use[0].options.plugins,
'@emotion/babel-plugin',
]
return config
},
} |
It'd be great to have this code encapsulated in a preset! |
@crushjz Thanks sir, now it's works! |
Removed because of storybook not loading when a @jsx pragma is used See storybookjs/storybook#7540
There are 2 solutions:
Both of these versions are super hacky and aren't the correct solution nor will they always be guaranteed to work through Storybook upgrades. Instead, forget the Webpack config altogether. Storybook has a native way of modifying the Babel config: The proper option is adding Babel overrides: // The location of your Babel config.
const babelConfig = require('../babel.config.js');
const storybookConfig = {
// ...
babel: async (
options
) => {
options
.overrides
.push({
...babelConfig,
test: '*', // This says "for all files, use this override".
})
return options;
},
// ...
}
module.exports = storybookConfig |
We've just encountered this issue. The project uses tsconfig.json has To fix it,
So far so good. |
With the solutions above I get this in the DOM:
Don't know what I'm missing. I'm using this same Babel configuration for the project build and it works fine. |
There are 2 babel configs you have to worry about. Depending on if you have JSX Pragma setup correctly and the latest babel-preset-react, it changes which you'll want to use.
/** @jsxRuntime classic @jsx jsx */
import {
css,
jsx,
} from '@emotion/react' ^^ You need this for Create-React-App. If you have the correct react babel preset with JSX pragma set to If you have an older React, you'll want to use I know this is confusing, and I don't fully understand it myself, but I've dealt with these issues on many projects. Getting Emotion working Next.js was probably one of the hardest, but I figured out a hack-fix to make it work. |
Hi everyone! Seems like there hasn't been much going on in this issue lately. If there are still questions, comments, or bugs, please feel free to continue the discussion. Unfortunately, we don't have time to get to every issue. We are always open to contributions so please send us a pull request if you would like to help. Inactive issues will be closed after 30 days. Thanks! |
For those people who want to use css prop in CRA based project with TS, modify your module.exports = {
stories: ['../src/**/*.stories.mdx', '../src/**/*.stories.@(js|jsx|ts|tsx)'],
addons: [
'@storybook/addon-links',
'@storybook/addon-essentials',
'@storybook/preset-create-react-app',
],
framework: '@storybook/react',
core: {
builder: 'webpack5',
},
webpackFinal: async (config, { configType }) => {
const oneOfRule = config.module.rules.find((rule) => rule.oneOf)
const babelRule = oneOfRule.oneOf.find((rule) =>
rule.loader?.includes('babel-loader')
)
babelRule.options.presets.push('@emotion/babel-preset-css-prop')
return config
},
} |
Thank you @velopert ! For people coming here using xstyled (might apply to other people too), you might also need to set // .storybook/main.js
module.exports = {
stories: ["../src/**/*.stories.mdx", "../src/**/*.stories.@(js|jsx|ts|tsx)"],
addons: [
"@storybook/addon-links",
"@storybook/addon-essentials",
"@storybook/addon-interactions",
"@storybook/preset-create-react-app",
"storybook-dark-mode",
],
framework: "@storybook/react",
core: {
builder: "webpack5",
},
features: {
emotionAlias: false, // <----------------------------- here
},
webpackFinal: async (config, { configType }) => {
const oneOfRule = config.module.rules.find((rule) => rule.oneOf);
const babelRule = oneOfRule.oneOf.find((rule) =>
rule.loader?.includes("babel-loader"),
);
babelRule.options.presets.push("@emotion/babel-preset-css-prop");
return config;
},
}; Works as in ├─ @xstyled/emotion@npm:3.5.1
├─ @emotion/babel-preset-css-prop@npm:11.2.0
├─ @emotion/react@npm:11.8.1
├─ @emotion/styled@npm:11.8.1
├─ @mdx-js/react@npm:1.6.22
├─ @storybook/addon-actions@npm:6.4.19
├─ @storybook/addon-docs@npm:6.4.19
├─ @storybook/addon-essentials@npm:6.4.19
├─ @storybook/addon-interactions@npm:6.4.19
├─ @storybook/addon-links@npm:6.4.19
├─ @storybook/builder-webpack5@npm:6.4.19
├─ @storybook/manager-webpack5@npm:6.4.19
├─ @storybook/node-logger@npm:6.4.19
├─ @storybook/preset-create-react-app@npm:4.0.1
├─ @storybook/react@npm:6.4.19
├─ @storybook/testing-library@npm:0.0.9
├─ react-dom@npm:17.0.2
├─ react-router-dom@npm:6.2.2
├─ react-scripts@npm:5.0.0
├─ react@npm:17.0.2 |
The emotionAlias should no longer be needed at all in the alpha version of storybook now. I think this issue can be closed It is fixed in 6.5.apha thanks to #17000 |
For projects using Vite instead of Wepack, see: storybookjs/builder-vite#210 |
If that helps anyone, here is what worked for me (using storybook 7): // .storybook/main.js
module.exports = {
// ...
babel: (options) => ({
...options,
presets: [...options.presets, '@emotion/babel-preset-css-prop'],
}),
} |
* chore: storybook emotion 설정 - storybookjs/storybook#7540 * feat: svg wrapper component * feat: arrow icon
* chore: storybook emotion 설정 - storybookjs/storybook#7540 * feat: svg wrapper component * feat: arrow icon * feat: typo theme * feat: emotion theme * feat: emotion faq example * feat: emotion theme * feat: emotion faq example * feat: typo theme * chore: vscode typescript 버전 세팅 * chore: rename FAQ -> FAQItem * feat: create FAQList * feat: create FAQList story * feat: li list style none * chore: mock 데이터 위치 수정
In case this helps anybody, I just upgraded to storybook 7 and the
|
Describe the bug
I can't seem to get emotion's
css
prop to work inside of Storybook. Seems like emotion is now the dominant css-in-js library, and thecss
prop is emotion's recommended primary way of styling components, so I thought I was choosing a well-travelled happy path here, but I'm stuck. I'm very open to the fact that I'm doing something dumb though, of course.In my actual project repo I'm getting the same thing described here and here. I tried all of the recommendations in both those threads, and still no joy -- and it looks like I’m not the only one.
So, I created a brand new React Storybook repo to just hello-world using emotion css prop and storybook. And I can't get that to work either! I'm wondering if someone could look at the super simple repro repo I made and tell me where I went wrong, here it is:
https://github.com/jaredh159/storybook-emotion-css
I'd be up for submitting a PR to the docs clarifying how to do this, if someone can show me where I messed up.
To Reproduce
Steps to reproduce the behavior:
git clone [email protected]:jaredh159/storybook-emotion-css.git
cd storybook-emotion-css && yarn && yarn storybook
TypeError: Cannot read property 'name' of null
Expand for full error message
Expected behavior
I expected to be able to use the
css
prop from emotion within Storybook.Code snippets
The repro repo does add a
.babelrc
inside thestories
dir as recommended in the emotion docs and on this similar issue, like so:I also tried this workaround, but no joy.
System:
The text was updated successfully, but these errors were encountered: