storybook: CSS Custom Properties (var(--foo)) in theme secondary colour causes fatal error
Describe the bug Using CSS variables in theme breaks storybook.
To Reproduce Use this theme:
import { create } from '@storybook/theming';
export default create({
base: 'light',
colorSecondary: 'var(--my-var, red)',
})
Expected behavior I expect the styles to be applied.
Code snippets
The above error occurred in the <Context.Consumer> component:
in Styled(a) (created by Link)
in Link (created by KnobPanel)
in div (created by Context.Consumer)
in Styled(div) (created by Placeholder)
in div (created by Context.Consumer)
in Styled(div) (created by Placeholder)
in Placeholder (created by KnobPanel)
in KnobPanel
in div (created by Context.Consumer)
in Styled(div)
in div (created by Context.Consumer)
in Styled(div)
in Unknown
in Unknown
in Unknown
in Unknown (created by Context.Consumer)
in _default (created by Layout)
in div (created by Context.Consumer)
in Styled(div) (created by Panel)
in Panel (created by Layout)
in div (created by Context.Consumer)
in Styled(div) (created by Main)
in div (created by Context.Consumer)
in Styled(div) (created by Main)
in Main (created by Layout)
in Layout (created by Context.Consumer)
in WithTheme(Layout)
in Unknown
in Unknown (created by ResizeDetector)
in ChildWrapper (created by ResizeDetector)
in ResizeDetector
in div (created by Context.Consumer)
in Styled(div)
in Unknown
in Unknown (created by Manager)
in ThemeProvider (created by Manager)
in Manager (created by Context.Consumer)
in Location (created by QueryLocation)
in QueryLocation (created by Root)
in LocationProvider (created by Root)
in HelmetProvider (created by Root)
in Root
React will try to recreate this component tree from scratch using the error boundary you provided, LocationProvider. vendors~main.5bfe49427a0ecbcae866.bundle.js:155228:5
The above error occurred in the <LocationProvider> component:
in LocationProvider (created by Root)
in HelmetProvider (created by Root)
in Root
Consider adding an error boundary to your tree to customize error handling behavior.
Visit https://fb.me/react-error-boundaries to learn more about error boundaries. vendors~main.5bfe49427a0ecbcae866.bundle.js:155228:5
Error: Couldn't parse the color string. Please provide the color as a string in hex, rgb, rgba, hsl or hsla notation.
vendors~main.5bfe49427a0ecbcae866.bundle.js:19244:14
Error: Couldn't parse the color string. Please provide the color as a string in hex, rgb, rgba, hsl or hsla notation.
System:
- OS: MacOS
- Device: Macbook Pro 2018
- Browser: Firefox, Chrome
- Framework: Web Components
- Addons: theming
- Version: 5.1.0-alpha.7
Additional context You can try this yourself just by setting the secondary color prop in react dev tools under ThemeProvider
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 4
- Comments: 42 (39 by maintainers)
CSS Custom Properties have been supported in browsers since 2014. It seems less than ideal to commit “Our CSS-in-JS library doesn’t support browser standards” to documentation.
This is fixed in 5.2!
I’m going to bow out of this thread, since it seems to me that the discussion isn’t progressing in a particularly productive direction. I really hope this fix gets in soon. Thanks all for your consideration.
That’s just not true. All the web platform fully works in your stories (just because they run in browser). Theming the Storybook UI itself is an additional feature which has its intrinsic limitations. For example, you can’t pass gradients as backgrounds. And gradients have been a part of web standarts for quite a while.
Ok, then the workaround above should work for you. The variables need to be in
preview-head.htmlbecause yourconfig.jsis executed in the context of preview iframeThanks for investigating @Armanio. Since it’s not used much we should be able to switch it out for something more permissive without too much hassle. But I’ll defer to @ndelangen and @domyen on theming decisions since they did the work, and will be maintaining our themes
Any PR is welcome. You can try to support CSS variables yourself. 🥇 Anywhere tomorrow i investigate this question.
Of course you are right. I will think about it. Maybe it is not as difficult as it seems.