Open bcbailey-torus opened 2 weeks ago
If I understand #279 correctly this change was necessary to be compatible with @storybook/preview-api
? My use case was to dynamically change the theme of the docs container in preview.tsx
:
// .storybook/preview.tsx
const Container = (props: DocsContainerProps) => (
<DocsContainer
{...props}
theme={{
...common,
...(useDarkMode() ? themes.dark : themes.light), // <<<
}}
/>
);
Unfortunately, this broke with storybook-dark-mode@4.0.2
. Does someone have an idea what to do about this? Is this hook simply no longer supported inside preview.tsx
? Had the terrible idea to use a MutationObserver
on the body class to work around this issue:
https://github.com/grubersjoe/react-activity-calendar/blob/main/.storybook/preview.tsx
I don't know why Storybook did these changes but they feel quite limiting =/.
We can also reproduce this runtime error. It completely breaks our storybook docs theming approach. Can someone please fix it, or maybe suggest another way to change docs theme?
Meanwhile downgraded to 4.0.1
due to this breaking change.
// preview.ts
...
import { MyDocsContainer2 } from './docsContainer';
const preview: Preview = {
parameters: {
docs: {
container: myDocsContainer
// myDocsContainer.tsx
...
import { useDarkMode } from 'storybook-dark-mode';
export const MyDocsContainer = (props: PropsWithChildren<DocsContainerProps>) => (
<DocsContainer context={props.context} theme={useDarkMode() ? themes.dark : themes.light}>
{props.children}
</DocsContainer>
);
Facing same problem for dark mode in docs
docs: {
container: (props) => {
const isDark = useDarkMode();
const currentProps = { ...props };
currentProps.theme = isDark ? themes.dark : themes.light;
return React.createElement(DocsContainer, currentProps);
},
},
Found workaround for dark mode
const preview: Preview = {
docs: {
container: (props: {
children: React.ReactNode;
context: DocsContextProps;
theme?: ThemeVars;
}) => {
// eslint-disable-next-line react-hooks/rules-of-hooks
const [isDark, setDark] = React.useState(true);
props.context.channel.on(DARK_MODE_EVENT_NAME, (state) =>
setDark(state)
);
const currentProps = { ...props };
currentProps.theme = isDark ? themes.dark : themes.light;
return <DocsContainer {...currentProps} />;
},
}
More a bit cleaner pragmatic solution in preview.tsx
const MyDocsContainer = (props: {
children: React.ReactNode;
context: DocsContextProps;
theme?: ThemeVars;
}) => {
const [isDark, setDark] = React.useState(true);
React.useEffect(() => {
props.context.channel.on(DARK_MODE_EVENT_NAME, setDark);
return () =>
props.context.channel.removeListener(DARK_MODE_EVENT_NAME, setDark);
}, [props.context.channel]);
return (
<DocsContainer
{...props}
theme={isDark ? themes.dark : themes.light}
/>
);
}
I mean it's a hack, but that's a lot better than what I came up with spontaneously :D. Thanks for sharing!
Summary
After upgrading to version 4.0.2 I am getting the error
Storybook preview hooks can only be called inside decorators and story functions.
Downgrading to 4.0.1 makes the error goes away.
This is likely related to the changes in #279
Is there something I need to change on my end?
Full error
Reference
My storybook
.storybook/preview.tsx
for reference:Versions
storybook: 8.1.10 storybook-dark-mode: 4.0.2