-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom theme not propagated within Storybook.js #24282
Comments
@robphoenix Thanks for the reproduction. As far as I can play with it, it's an issue with the version of emotion. Storybook uses emotion v10, Material UI v5 uses emotion v11. The context theme provider isn't resolved to the right version. MUI handles emotion as a peer-dependency and not a direct dependency because of the singletons. We have seen the same problem in the past in: I can solve the issue in the reproduction with this patch: diff --git a/package.json b/package.json
index 544c087..bb8d89b 100644
--- a/package.json
+++ b/package.json
@@ -68,6 +68,9 @@
"tslib": "^2.0.3",
"typescript": "^4.1.3"
},
+ "resolutions": {
+ "**/@emotion/styled": "^11.0.0"
+ },
"dependencies": {
"@emotion/react": "^11.1.2",
"@emotion/styled": "^11.0.0", Relevant line of codes:
A couple of ideas for solving this issue properly:
|
That addition to the package.json fixed it for me also, thanks! 🚀 🙏 Thanks for the swift, er..., resolution to this issue @oliviertassinari 😆 🙄 It's very much appreciated. |
Well, I haven't checked this exact case but it looks awfully like something that has already been reported to me and which also has been reported already several times to Storybook. The problem is that they enforce a resolution of They shouldn't apply any aliases like this at all. This just won't work - because even if they upgrade to Emotion 11 and force the imported I think you can fix that temporarily on your end by manipulating the Storybook's webpack config - but I'm not sure what exact changes are required. Maybe it's just a matter of removing that alias but that would have to be tested to see if Storybook works OK with such a config. |
@Andarist Do you know how Storybook force the resolution of |
I think they are having it as regular dep and force all references to I would advise against making it a dep for Material UI because package managers will just often install multiple copies of it and different parts of the module tree will resolve to different locations and thus to different created contexts etc and for things to work correctly you need to have a single Emotion context. Its pretty much the same reason why you need React itself as peer. |
I likely miss something. The theme and cache contexts seems to be coming from |
Ah, right - sorr, I've just generalized the problem, not realizing that you only ask about styled. Hm, it should be OK to put it as regular dep. You are still risking several copies being pulled into final bundles but thats just a bundlesize concern. If I find any other reason for you not doing that I will report back - you might also ping me in the PR introducing that change (if you gonna put one together) for taking a final look at this. |
@oliviertassinari @Andarist The issue isn't totally fixed by adding the resolutions field in the package.json; when adding components to the theme, any |
Just FYI - I might not be able to take a look at the provided repo in the nearest future so if somebody could pick this up it would be highly appreciated. I would just try to locate from what disk location particular contexts are coming from - it really has to be an issue with multiple instances of |
@robphoenix This is a different issue, looking at your code, it's very likely fixed by #24253. |
fixed with v5.0.0-alpha.23 thanks @oliviertassinari 🙏 |
At which point was the repro faulty? I just cloned it and button and box have the same color. |
@eps1lon The expected result is pink the actual result is blue Which color do you see rendered? |
Both pink. You're not using the latest version. I guess the I remove the The problem is caused by two issues:
|
sorry, I pushed the first fix as I thought the second issue I mentioned was connected.
import React from 'react'
import {ThemeProvider} from '@material-ui/core'
import {StylesProvider} from "@material-ui/core";
import theme from '../src/theme'
export const decorators = [
(Story) => (
<StylesProvider injectFirst>
<ThemeProvider theme={theme}>
<Story />
</ThemeProvider>
</StylesProvider >
),
]
https://storybook.js.org/docs/react/configure/overview#configure-story-rendering |
sx
prop when used within Storybook.js
Also struggling with this. I'm seeing partial updates from the provided theme, such as primary and secondary colors, but typography is not applied. Also, attempted to use the alpha release mentioned above, and received the following error:
// .storybook/main.js
module.exports = {
stories: ['../src/**/*.stories.mdx', '../src/**/*.stories.@(js|jsx|ts|tsx)'],
addons: ['@storybook/addon-links', '@storybook/addon-essentials', '@storybook/addon-a11y'],
} // .storybook/preview.js
import { CssBaseline } from '@material-ui/core'
import React from 'react'
import { StylesProvider, ThemeProvider } from '@material-ui/core/styles'
import { ThemeProvider as SCThemeProvider } from 'styled-components'
import { useDarkMode } from 'storybook-dark-mode'
import { lightTheme, darkTheme } from '../src/app/theme'
export const decorators = [
(Story, context) => {
// FIXME: no theme control set, use toolbars addon and retrieve state from context.globals once MUI issue resolved
const isDark = false
const theme = isDark ? darkTheme : lightTheme
return (
<StylesProvider injectFirst>
<CssBaseline />
<ThemeProvider theme={theme}>
<SCThemeProvider theme={theme}>
<Story {...context} />
</SCThemeProvider>
</ThemeProvider>
</StylesProvider>
)
},
]
export const parameters = {
actions: { argTypesRegex: '^on[A-Z].*' },
} I'm using this in nextJS, with styled-components, though I don't think this has any impact. All rendering with theme switching works perfectly in the app, styled-components work fine in storybook. |
@Pushplaybang This issue is specific about emotion. If you are using styled-components you can configure Material-UI to use it too. |
not sure if you misunderstood, adding the above in the hopes that it provides additional context. I'm experiencing the eact behaviour described by the OP.
Further, this issue is specifically about the latest version of MUI and how it's themes don't work in storybook, which seems to be a symptom of some issues regarding how each library is depending on emotion, as per the original post, and all of the subsequent conversation above. The fact that I'm also using styled-components, has no bearing in this instance, I have styled-components and MUI working perfectly in my app, with or without styled-components, the issue remains: MUI Custom themes don't currently seem to work in Storybook, for the latest stable release, and any subsequent release candidate. |
@Pushplaybang I meant configure Material-UI so you don't bundle emotion and styled-components in your app, only one of the two. We share a workaround in the previous comments. |
@oliviertassinari I've read this thread several times now, could you please quote the workaround you're speaking of? |
Maybe related to this. |
Any ETA for this? Or any alpha we could try and check? |
I didn't check it but this version might work: v6.5.0-alpha.31 |
I'm facing this exact issue!!!! The thing is that, the React component itself have no problem getting the theme, but the issue is with the story itself. There is a "workaround" (is in double quote because it caused other error!) if you render the story as a component (instead of story function, then it would be able to get the properties and get the theme. BUT, rendering the story as a component would ended up causing the following error in some other story!!!!
I am using pretty much everything latest:
Decorator in the
Where
Then in any story, I just try to grab the theme object and it simply returned
In addition, there is an "interesting" weird hack that could workaround the problem... is to add a decorator right before the story itself. And no, you cannot add the hack with
With that, the story would get the I'm opening a bug over at storybook as well. storybookjs/storybook#17668 |
Now using alpha storybook for emotion 11 support, which is required for MUI See mui/material-ui#24282 and storybookjs/storybook#17000
Wrapping with Emotion 10 theming provider works for me. |
Now using alpha storybook for emotion 11 support, which is required for MUI See mui/material-ui#24282 and storybookjs/storybook#17000
Now using alpha storybook for emotion 11 support, which is required for MUI See mui/material-ui#24282 and storybookjs/storybook#17000
If i could kiss you i would. Iv been trying to figure this out for over an hour and literally nothing helps. Not even the storybook MUI plugin (guessing it was abandoned from looking at the Git). you life saver. |
Now using alpha storybook for emotion 11 support, which is required for MUI See mui/material-ui#24282 and storybookjs/storybook#17000
I have now fixed the theme not being applied by adding the resolutions for emotion. I got this solution from mui/material-ui#24282 It isn[t a fix though, more a temporary resolution, we should keep an eye on this issue, see what happens or if there is a better option than what I've done here.
This works but you need to be on v 6.4! |
It seems to work only on canvas tab but doesn't on docs tab |
I had similar issue with Mui version 5 and storybook version 6.3.12 but worked fine after doing just |
I was on 6.4 version and had same issue. Update to 6.5 solved this bug |
I'm using
webpackFinal: async config => {
return {
...config,
resolve: {
...config.resolve,
alias: {
...config.resolve.alias,
'@mui/styled-engine': '@mui/styled-engine-sc',
},
},
};
}, |
The problem might be solved in Storybook v7.0.beta.0 per storybookjs/storybook#12262 (comment). |
Trying on a fresh setup with storybook 6.5.16 and none of the solutions worked. Tried The thing that does work, is adding the standard MUI ThemeProvider to a particular story - there it works without the needed of additional theme provider. Was testing overrides for a button - background, box shadow etc. Unless some other solution comes around, i suppose I'll need to wrap every story with my own component which provides the theme. Tried using v7 but it seems too buggy when trying to set it up using Yarn PnP setup. Recreated it from scratch 3 different ways and never got storybook to compile at all. More complicated when doing so in a monorepo |
I ended up trying to define a story wrapper within the same package as my components and import it into Storybook's preview.ts, and that seems to solve the issue, and doesn't require double-wrapping the theme. Here's what I'm using: Storybook's preview.tsx import StoryWrapper from '@monorepo/components/story-theme-wrapper';
import type { Story } from '@storybook/react';
function Theme(Story: Story) {
return (
<StoryWrapper>
<Story />
</StoryWrapper>
);
}
export const decorators = [Theme]; The StoryWrapper import React from 'react';
import CssBaseline from '@mui/material/CssBaseline';
import { ThemeProvider } from '@mui/material/styles';
import theme from '@monorepo/components/theme';
const StoryWrapper = ({ children }) => (
<ThemeProvider theme={theme}>
<CssBaseline />
{children}
</ThemeProvider>
);
export default StoryWrapper; I then removed My monorepo structure boils down to:
Edit: The nice thing about this approach is that I no longer need to declare mui, emotion and all other dependencies used in components in both packages. In a monorepo setup, to avoid it I'd need to either shared dependencies to root package.json so that they can be imported across all packages (especially important with yarn v3 and pnp which doesn't seem to allow it by default), or I'd need to create another directory level with package.json containing dependencies to be shared across N number of packages (but not to be shared by all packages). My storybook package.json now only contains storybook-related dependencies and nothing more. |
Optional
Get Outlook for Android<https://aka.ms/AAb9ysg>Optional
…________________________________
From: Maciej Gurban ***@***.***>
Sent: Saturday, March 25, 2023 11:43:52 PM
To: mui/material-ui ***@***.***>
Cc: Subscribed ***@***.***>
Subject: Re: [mui/material-ui] Custom theme not propagated within Storybook.js (#24282)
I ended up trying to define a story wrapper within the same package as my components and import it into Storybook's preview.ts, and that seems to solve the issue, and doesn't require double-wrapping the theme. Here's what I'm using:
Storybook's preview.tsx
import StoryWrapper from ***@***.***/components/story-theme-wrapper';
import type { Story } from ***@***.***/react';
function Theme(Story: Story) {
return (
<StoryWrapper>
<Story />
</StoryWrapper>
);
}
export const decorators = [Theme];
The StoryWrapper
import React from 'react';
import CssBaseline from ***@***.***/material/CssBaseline';
import { ThemeProvider } from ***@***.***/material/styles';
import theme from ***@***.***/components/theme';
const StoryWrapper = ({ children }) => (
<ThemeProvider theme={theme}>
<CssBaseline />
{children}
</ThemeProvider>
);
export default StoryWrapper;
I then removed emotionAlias: false from main.tsx and theme continues working as expected.
My monorepo structure boils down to:
packages/
components/
theme/
my-component/
my-component.tsx
my-component.story.tsx
storybook/
config/
preview.tsx
main.tsx
—
Reply to this email directly, view it on GitHub<#24282 (comment)>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/A2FFCYKVJZUH6FXOIOMLUC3W55YKRANCNFSM4VV7E4NA>.
You are receiving this because you are subscribed to this thread.Message ID: ***@***.***>
|
I believe that this issue is no longer exists with the storybook v7+ |
Current Behavior 😯
When using Material-UI v5 within Storybook, the
sx
prop is picking up the default theme rather than the custom theme.The theme logged to the console contains the custom theme colour (
#ff69b4
)However, the background colour of the
Box
component is#3f51b5
(indigo.500)Expected Behavior 🤔
Material UI components should pick up the custom theme when used within storybook.
Steps to Reproduce 🕹
Steps:
git clone https://github.com/robphoenix/mui-storybook
cd mui-storybook && yarn install
yarn storybook
Context 🔦
I'm building a UI component library. Everything is working fine with v5 when I use my custom theme/components within
create-react-app
, however I now want to develop the components outside of an app and am using storybook to do so. However this is not possible if the custom theme is not picked up. Apologies if I've overlooked something obvious, after a day of debugging I can't see the wood for the trees. 😄Your Environment 🌎
`npx @material-ui/envinfo`
The text was updated successfully, but these errors were encountered: