storybook: Addon background light / dark doesn't work in Docs when using MDX
Describe the bug With 6.1.8 addon background doesn’t work when using Docs + MDX. It does work with DocsPage but doesn’t work when using MDX. With MDX the background color only switch when shifting to Canvas then back to Docs.
Example of a story definition in MDX that doesn’t work:
<Canvas>
<Story name="default">
<Button>Cutoff</Button>
</Story>
</Canvas>
Video that show the problem:

To Reproduce Steps to reproduce the behavior:
- Go to ‘…’
- Click on ‘…’
- Scroll down to ‘…’
- See error
Expected behavior A clear and concise description of what you expected to happen.
Screenshots If applicable, add screenshots to help explain your problem.
Code snippets If applicable, add code samples to help explain your problem.
System
Please paste the results of npx sb@next info here.
Additional context Add any other context about the problem here.
About this issue
- Original URL
- State: open
- Created 4 years ago
- Reactions: 9
- Comments: 27 (16 by maintainers)
Workaround until this is fixed properly:
Beware! this hacks the global CSS scope to target parent elements!
I was surprised to find this bug, and even more surprised to find it’s been a known bug for a year and a half.
We want to address this in 6.3. If you want to contribute to Storybook, we’ll prioritize PR review for any fixes here. And if you’d like any help getting started, please jump into the #support channel on our Discord: https://discord.gg/storybook
@dmackerman https://storybook.js.org/docs/react/writing-stories/decorators#global-decorators
A workaround for both the background and the grid is to find the ID of the story in the inspector and add it to the canvas element, like
<Canvas id="anchor--foundation-color--color">. (No need for the CSS hack mentioned above.)Same here @tremby cannot believe that if I use mdx all these functionalities are not working.
With this horrible decorator, inspired in @isc30 decorator I’ve enabled all the options meanwhile an official solution appears 😃
.storybook/preview.js
Just chiming in with a use case I have… We have several styles of buttons, one of which is “white.” It’s more useful to preview that against a dark background. With CSF, I could set the default background to “dark.” Doesn’t work in MDX.
Don’t worry I didn’t read it like that! My English is probably more limited than yours :p
If you could do a minimal experiment in the repo I might be able to help! I’m a bit busy these days but if you set the motion I can help you out! Feel free to reach me on discord as well