Closed martinburford closed 1 year ago
Here is an example of it not working when using globalTypes
:
To achieve this, I've setup global types like this (within preview.tsx
):
// Create a custom toolbar in Storybook, so that light and dark mode can be switched on-demand (AND persist between docs and canvas blocks)
export const globalTypes = {
dataTheme: {
defaultValue: "light",
toolbar: {
dynamicTitle: true,
items: [
{ icon: "moon", value: "dark", title: "Dark mode" },
{ icon: "sun", value: "light", title: "Light mode" },
]
}
},
};
I also use this in combination with storybook-addon-data-theme-switcher
which is referenced within main.ts
:
const config: StorybookConfig = {
stories: ["components/**/*.stories.mdx", "../components/**/*.stories.mdx"],
addons: [
"@storybook/addon-links",
"@storybook/addon-essentials",
"@storybook/addon-onboarding",
"@storybook/addon-interactions",
"storybook-addon-data-theme-switcher",
],
...
Since I use a data attribute on the html
node of either data-theme="light"
or data-theme="dark"
, I'd expect this to be being updated every time the toolbar selection is changed, but it's not.
Is there something I'm doing wrong? Sometimes it does work, but it's 50/50. Sometimes it does, sometimes it doesn't, and just gets stuck on the the same thing, regardless of the selection you make (as per the video in this comment).
Any help with this would be appreciated.
Many thanks Martin
Hi @martinburford, the behaviour you described -- context resetting between pages -- is the intended behaviour indeed.
The globalTypes
approach should worked, though. Have you tried @storybook/addon-themes
instead? If that still doesn't work, please feel free to report back!
Hi there! Thank you for opening this issue, but it has been marked as stale
because we need more information to move forward. Could you please provide us with the requested reproduction or additional information that could help us better understand the problem? We'd love to resolve this issue, but we can't do it without your help!
I'm afraid we need to close this issue for now, since we can't take any action without the requested reproduction or additional information. But please don't hesitate to open a new issue if the problem persists – we're always happy to help. Thanks so much for your understanding.
I am using MDX for stories within Storybook 7.3.2. I have found the
globalTypes
to be incredibly flakey, often not working at all, so I decided to add my own React Context Provider withinpreview.tsx
...This is a super simple provider
Within a component shown within EVERY storyfile, I then do the following:
When the above functionality is executed (via a simple button click) ... sure enough, the context is updated, so that the contexts
theme
property is eitherlight
ordark
.What I'm finding however is that whenever a new story is changed OR the
docs
page is viewed for any story, the default value oflight
is restored, presumably because the provider is also re-generated as part of aniframe
re-generation / refresh, wiping out any previous context values.As mentioned, I'm only using this approach as using
globalTypes
hasn't really worked for me. I used it absolutely as per the Storybook docs, and I'd say it worked 40-60% of the time, sometimes switching the global type, other times, it was ignored. If this approach worked, I'd happily use it, but it looks a bit buggy from what I've seen. I'm not sure if this is a known bug, but it's this reason why I've looked to alternative solutions.Here is a video demonstrating the issue:
https://youtu.be/ObOm67mWTZU
To Reproduce
The Storybook instance where this is happening can be found here:
https://storybook.martinburford.co.uk/?path=/docs/components-atoms-card--docs
With the code being here:
https://github.com/martinburford/www-nextjs13
System
With the log file showing the following: