storybook: Component-level css not loading into stories
I created a react app using create-react-app, and added a button component, a css for the button. When I load the story for the button, the styles are not loaded for the button. Pasting below, the related files. Is there any configuration that I am to do, to get this up and running with styles?
Component: index.js
import React, { Component } from 'react';
import styles from './style.css';
class CustomButton extends Component{
render(){
return (
<button className={styles.customButton}>Hello</button>
);
}
}
export default CustomButton;
style.css:
.customButton {
border: 1px solid red;
padding: 10px;
background-color: rgb(223, 19, 19);
}
Story file:
import React from 'react';
import CustomButton from '../../src/index';
import { storiesOf } from '@storybook/react';
const story = storiesOf("Custom button",module);
story.addWithJSX("simple",() => <CustomButton/>);
System info:
Environment Info:
System:
OS: Windows 7 6.1.7601
CPU: (2) x64 Intel(R) Core(TM) i3-2370M CPU @ 2.40GHz
Binaries:
Node: 10.16.0 - C:\Program Files\nodejs\node.EXE
Yarn: 1.22.4 - C:\Program Files (x86)\Yarn\bin\yarn.CMD
npm: 6.9.0 - C:\Program Files\nodejs\npm.CMD
Browsers:
Chrome: 84.0.4147.125
npmPackages:
@storybook/addon-info: ^5.3.19 => 5.3.19
@storybook/components: ^5.3.19 => 5.3.19
@storybook/preset-create-react-app: ^3.1.4 => 3.1.4
@storybook/react: ^5.3.19 => 5.3.19
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 14
- Comments: 34 (5 by maintainers)
I had the same issue where styles using SCSS in Vue Single File Components where ignored when building the Storybook using
build-storybook:When the style was marked as
cssit worked fine. I managed to fix it forscopedstyles, too, with the same solution.Note that my Vue components are in a Yarn workspace and they are split in several packages that are marked as ES Modules using
"type": "module"in theirpackage.json. They are also marked as"sideEffects": falseto enable tree-shaking.I managed to fix this by adding a new rule for
scssfiles and marking them as having side-effects. WithoutsideEffects: truethis didn’t work for me either.I have been encountering the same issue, only when building storybook and deploying it. Launching storybook in dev mode, it seems to load the styles correctly.
I still have this issue using vue 3 with vite and storybook 7. Should such things not work out of the box? I cant seem to find any documentation that would tell me to do something extra to load my component css.
Incase someone is still facing the issue in 2022. So I had similar issue that everything was working fine just component css module styles were not being picked. I installed these 2 packages
npm i --save-dev storybook-css-modules-preset&npm i --save @storybook/addon-postcss.Then in your .storybook folder main.js file inside the addons array ad them like this
`
` And it worked!
Hi there, I’ve landed on this thread after experiencing the same issue. That is, everything is fine for me in development, but when I build my Storybook instance, the
.scssfile that is imported in my main component file doesn’t seem to be gathered during the build, and my outputted build doesn’t have the, or any, expected styles with it.The project is open-source and is here. It is a very simple exported component. I build my Storybook instance to the
./docsfolder so it’s served with GitHub pages.I recently updated the Storybook instance to > 6, and that’s when the “css missing in production” issue started happening.
"typescript": "^4.2.2"To alleviate the issue for now, I’ve imported an already built version of my
.cssdirectly into a preview-head.html.Here are a few of the things I’ve tried:
@storybook/preset-scssinstead of my custommain.jsstyle handling.import './react-mailchimp-email-signup-form.scss';directly into my .storybook/preview.js as a few people suggest - and nothing, still no css in the final build.sass-loaderto"^10.1.1"from11like some threads suggested.I wanted to document this here because while I’m sure it’s a small thing, it’s kinda an important issue that needs to be sorting out. Hopefully the information provided can help track it down. Thank you.
If smb still has problem of using css-modules in storybook with this solved the issue for me - https://github.com/Negan1911/storybook-css-modules-preset
For me, putting
sideEffects: truesolved the issueWebpack
Versions
I can also confirm this behavior on a Vue-based project. Tried different things but it seems that any kind of scss is ignored when building the docs.
This works:
This doesn’t work:
Doesn’t work either:
@jonniebigodes we’re using scss and tailwind here… I can confirm that no scss imported in component level are considered… styles are completely ignored.
And I can’t even understand how can storybook load a jsx file and ignore scss that is imported within… but it is exactly what’s happening.
Same now, nothing above helped (Vue 2 + Storybook 6/7)