storybook: [Bug]: Storybook docs CSS cascading into our components
Describe the bug
Any “inherited” CSS defined in the storybook docs page leaks into our components. In my case, font
. I was asked to file a bug here via discord, in order to gather suggested solutions/alternatives.
As of storybook 7 beta 20, the storybook emotion style blocks define:
.css-1j5dgab {
font-family: "Nunito Sans",-apple-system,".SFNSText-Regular","San Francisco",BlinkMacSystemFont,"Segoe UI","Helvetica Neue",Helvetica,Arial,sans-serif;
}
Which applies to the element <div class="sbdocs sbdocs-content css-1j5dgab">
The problem is that many of our components styles inherit a global font. We declare font: 400 10px/1 verdana, sans-serif
in our primary applications css file. Even though that file gets imported into storybook, the storybook CSS overrides it.
This means that our components actually have the storybook font styles applied.
For now, I redeclare our font in every component’s CSS file.
Given this is how CSS was designed to work, I only see a few options:
- Limit selectors to inner SB elements so they can’t leak into non-SB elements
- Use an iframe or some context to wrap components so they don’t inherit at all from SB
- Maybe I can override the SB style via a theme?
To Reproduce
No response
System
Environment Info:
System:
OS: Windows 10 10.0.22000
CPU: (16) x64 11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz
Binaries:
Node: 18.9.1 - C:\Program Files\nodejs\node.EXE
Yarn: 3.3.1 - C:\Program Files\nodejs\yarn.CMD
npm: 8.19.1 - C:\Program Files\nodejs\npm.CMD
Browsers:
Edge: Spartan (44.22000.120.0), Chromium (108.0.1462.76)
npmPackages:
@storybook/addon-docs: 7.0.0-beta.20 => 7.0.0-beta.20
@storybook/addon-essentials: 7.0.0-beta.20 => 7.0.0-beta.20
@storybook/addon-interactions: 7.0.0-beta.20 => 7.0.0-beta.20
@storybook/addon-links: 7.0.0-beta.20 => 7.0.0-beta.20
@storybook/react: 7.0.0-beta.20 => 7.0.0-beta.20
@storybook/react-vite: 7.0.0-beta.20 => 7.0.0-beta.20
@storybook/testing-library: 0.0.14-next.1 => 0.0.14-next.1
Additional context
No response
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 13
- Comments: 28 (11 by maintainers)
I’m trying to style the entire storybook (v7) with my company’s design system (based on CSS variables defined in :root) and I’ve been struggling for hours. I had to add many CSS overrides in
preview-head.html
and I’m in the middle of a CSS specificity war because it now messes with our components…For example here’s a MDX documentation page where I’m using
<Unstyled>
, but I still get inherited styles like font-family or font-size in my lists:And it gets worse for stories:
Would it make sense to consider some option to opt-out from storybook’s styling for everything related to the preview pane (mdx / stories)? Being allowed to start from a blank slate ?
In the meantime, the approach taken by @jtiala worked for us to re-set the necessary styles (in our case,
font-family
)I’m curious, though – when would a user not want to wrap their story in
Unstyled
? Are there cases where the Storybook styles bleeding into the stories themselves desirable?Facing this with the latest storybook version still. The only workaround is adding
<Unstyled />
to specific components on each mdx doc. Any way we can globally override these styles or unstyle it globally for mdx docs?I will see if I can put an app together, but I am still curious if there is a way (or can be a way) to “unstyle” all stories globally instead of having to modify every story component that we have. I appreciate your assistance with this.
I’m struggling to think of any cases where you wouldn’t want isolated story styles.
@JReinhold does this need to be on the burndown?
I’m having a similar yet opposite issue: my component styles are bleeding into the Docs styles. The styles most effected are where I’ve styles the element itself and those styles are being used by the html in the Docs, e.i.
<div>, <button>, <input>
.This bug still persist in 7.2 version with MDX stories…
The only way to overwrite this:
is something like that in preview-head.html:
We’re running into this as well, also with auto-docs; we don’t have a place to render
Unstyled
explicitly. Maybe it should be applied for auto-docs automatically?We’ve added a new doc block
Unstyled
that is suppose to help mitigate this problem. You can use that to wrap any part of your docs page that you don’t want the global styles to bleed in to.The documentation for this is not quite ready yet, but you can see the migration notes about it here: https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#default-docs-styles-will-leak-into-non-story-user-components
And here’s an example of what it does in action: https://next--63591cc9eaf4e8caec2bb5e9.chromatic.com/?path=/docs/blocks-unstyled--docs
Does that help you @viveleroi ? It’s unclear to me whether you are experiencing this in AutoDocs or in your manually written docs pages.