Storybook in React app isn't loading emotion styles.
See original GitHub issueDescribe the bug Storybook in React app isn’t loading emotion styles.
To Reproduce Steps to reproduce the behavior:
- adding storybook to existing react app which uses emotion.
- set up webpack.config.js in ./storybook folder with code snippet below. Set up tsconfig.json with subsequent code snippet.
- npm run storybook
Expected behavior I expect that storybook will load my stories with styles. It loads the stories in the storybook but they don’t have styles associated.
Code snippets
for webpack.config.js in ./storybook
const path = require('path');
const SRC_PATH = path.join(__dirname, '../src');
module.exports = async ({ config, mode }) => {
config.module.rules.push({
test: /\.(ts|tsx)$/,
include: [SRC_PATH],
use: [
{
loader: require.resolve('awesome-typescript-loader'),
options: {
configFileName: './.storybook/tsconfig.json',
}
},
{ loader: require.resolve('react-docgen-typescript-loader') }
]
});
config.resolve.extensions.push('.ts', '.tsx');
return config;
};
for tsconfig.json in .storybook/
{
"compilerOptions": {
"baseUrl": "./",
"allowSyntheticDefaultImports": true,
"module": "es2015",
"target": "es5",
"lib": ["es6", "dom"],
"sourceMap": true,
"allowJs": false,
"jsx": "react",
"moduleResolution": "node",
"rootDir": "../",
"outDir": "dist",
"noImplicitReturns": true,
"noImplicitThis": true,
"noImplicitAny": true,
"strictNullChecks": true,
"declaration": true
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"build",
"dist",
"scripts",
"acceptance-tests",
"webpack",
"jest",
"src/setupTests.ts",
"**/*/*.test.ts",
"examples"
],
"awesomeTypescriptLoaderOptions": {
"useBabel": true,
"babelOptions": {
"babelrc": false, /* Important line */
"presets": ["@babel/preset-env", "@emotion/babel-preset-css-prop"],
"plugins": ["emotion"]
},
"babelCore": "@babel/core" // needed for Babel v7
}
}
System:
- OS: Windows
- Device: na
- Browser: Chrome
- Framework: React
- Addons: na
- Version: latest
Additional Context: When I inspect the markup I can see that styles with data-emotion attributes are in the file and a class is specified (Also I see my css selectors and css styles in these style tags), but the dom doesn’t associate to those styles with any class as if something fails in setting up the emotion connection between classes for styles and the markup…
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (4 by maintainers)
Top Results From Across the Web
javascript - Storybook not showing styles
Every component in React is self contained - your DialogModal component won't get styled because in Storybook it is not being rendered within ......
Read more >Theming
Reuse the theme variables above for a native Storybook developer experience. The theming engine relies on emotion, a CSS-in-JS library. // YourTheme.js ......
Read more >Use Emotion CSS Prop in Storybook - Duncan Leung
I was running into an issue where my Storybook webpack.config.js was not correctly parsing and evaluating Emotion styles passed via the css prop ......
Read more >[Solved]-Storybook not showing styles-Reactjs
You'll need to import any styles you use in App.js globally in storybook, by importing them in .storybook/preview.js (create the file if it...
Read more >Simple Storybook React Setup with Dark Mode Integration
We are going to use Emotion to style our components and also to manage the dark mode theme switching. You can use any...
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 FreeTop 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
Top GitHub Comments
In my project I was able to get Storybook to recognize css-prop (without using the jsx pragma) by adding this config to
/.storybook/webpack.config.js
.Line 17 - Line 19:
Also, I am including the
@emotion/babel-preset-css-prop
for.ts
and.tsx
filesLine 41 - Line 43:
I created a template to share my project config that uses Gatsby + TypeScript + Emotion + Storybook + React Intl + SVGR + Jest: https://github.com/duncanleung/gatsby-typescript-emotion-storybook
Here’s a link to my
webpack.config.js
that enables@emotion/babel-preset-css-prop
:Sorry for the late reply! WRT my use in the Docs page, the
/** @jsx */
pragma worked fine