-
-
Notifications
You must be signed in to change notification settings - Fork 9.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
Addon-Docs: Changing the theme via api doesn't persist in Docs #10523
Comments
In 6.0 we've moved to a parallel theming structure for docs: |
@shilman this instruction set the theme in the initial state. Is there a way to change at run time? I want to change the theme of the Storybook, Docs and Design System at once: |
I believe docs is dynamically themable but Storybook UI is not cc @ndelangen |
I'm looking for this same thing too for https://github.com/hipstersmoothie/storybook-dark-mode/. I need a way to dynamically set the theme for the |
@hipstersmoothie OK I misunderstood the issue. In @tmeasday is it possible to update parameters dynamically, or perhaps we should consider moving to |
setting the theme is possible at runtime for the manager: storybook/lib/api/src/modules/layout.ts Lines 223 to 262 in a14bf2a
|
@shilman This is how I have always set the theme for storybook https://github.com/hipstersmoothie/storybook-dark-mode/blob/master/src/Tool.tsx#L90 I'm not sure that theming ever worked with the docs tab (hipstersmoothie/storybook-dark-mode#70) for this addon.
This is exactly what I need. I saw the globalArg function but it seems like it was for the new |
No? I haven't really been following the discussion above but the obvious answer to that question is of course not. That's exactly why we made global args. |
So the question remains: How can I dynamically theme |
I think it should use global args then? |
Yeah, I guess? I think we’ll likely keep running into challenges deciding if things should be configured via parameters vs global args. Perhaps we should make a pattern where certain things are can be configured both ways. What do you think folks? |
Yeah that seems very very likely to me too tom.. |
hey @shilman, are you planning to include this soon? it would be great! 🙏 |
Bump on this one - would love to see dynamic theming for docs! |
Temp workround - just refresh the iframe: |
@shilman @tmeasday @ndelangen I apologize for mentioning, but have you come to any solution? Prior to version 6.4 there was a workaround with dynamically installing the theme through the substitution of DocsContainer |
Any update on this? |
@shilman could this be looked at prior to v7 final release? I think there's some machinery missing to get docs to update in tandem with the set storybook theme. Not all the same initialization code seems to be run when loading a docs page vs a component story when it comes to addons? |
@probablyup we'll take a look at things this week. i don't think we'll block the release for it, but depending on how broken it is it's possibly something that we could address in a patch release during stabilization. |
Thanks @shilman. I think I was able to narrow down the issue to the useParameter() hook from api. It seems to never get the initialized parameters for doc stories, at least until you’ve switched to a component story and then back. |
Hello everyone. Having this exact issue. We have managed to integrate all of Storybook's theming with our own theming system ("natively"), but not the docs page (workaround below). We actually have a custom docs container component, but it gets wrapped with some SB stuff including a background. While you guys are at this, please consider removing any wrapping when a custom docs container is being used. Our workaround have been to manually override the background as follows: const useStyles = createUseStyles(({palette}) => ({
'@global': {
'.sbdocs-wrapper': {
// TODO: https://github.com/storybookjs/storybook/issues/10523
backgroundColor: 'transparent!important'
},
html: {
backgroundColor: palette.background.normal
},
body: {
backgroundColor: palette.background.normal
}
}
}));
const Background: FC = () => {
useStyles();
return null;
};
const DocsContainer: FC<DocsContainerProps> = (props) => {
const themeRef = useRef(DEFAULT_THEME_PACK.themes[DEFAULT_THEME_TYPE]);
const {context} = props;
const stories = context.componentStories();
const {globals} = context.getStoryContext(stories[0]);
const theme = useMemo(() => {
if (
(globals as IGlobals).themePackKey &&
(globals as IGlobals).themeType
) {
themeRef.current = getThemeFromGlobals(globals as IGlobals);
}
return themeRef.current;
}, [globals]);
return (
<Unstyled>
<UDSProvider>
<ThemeStore theme={theme}>
<NotificationsServiceStore>
<PromptsServiceStore>
<Background />
<DocsContainerInternal {...props} />
<NotificationsServiceManager />
<PromptsServiceManager />
</PromptsServiceStore>
</NotificationsServiceStore>
</ThemeStore>
</UDSProvider>
</Unstyled>
);
}; In another related topic, we do something similar in stories decorators (to change the background of the stories automatically based on theme). We want everything to adjust automatically based on selected theme in toolbar and we have managed to do so, using the latest version of Storybook. The only remaining issue so far is that (for stories and docs) since the injection of the styles take place "locally", there is always a quick flash when loading stories and docs (from default/initial background). Your suggested solution should fix this for docs, since the correct value will be used from the beginning, during first render. But stories will continue to have the issue unless you map stories backgrounds to the theme too. We are currently using the same pattern your background add-on uses. |
My solution for those in Storybook 7.6. First create a themed docs container import { DocsContainer } from '@storybook/blocks'; // !! Not from @storybook/addon-doc
import React, { useEffect, useState } from 'react';
import { getUserPreferedColorTheme } from '../theme';
export const ThemedDocsContainer = ({ children, context, ...rest }) => {
// getUserPreferedColorTheme : Use localstorage and prefer dark media query to guess the user theme
const [theme, setTheme] = useState(getUserPreferedColorTheme());
const handler = () => {
setTheme(getUserPreferedColorTheme());
};
useEffect(() => {
window.addEventListener('storage', handler);
return function cleanup() {
window.removeEventListener('storage', handler);
};
});
return (
<DocsContainer context={context} {...rest} theme={theme}>
{children}
</DocsContainer>
);
}; Register a new addon that will listen to globals changes. import { addons } from '@storybook/manager-api';
import { GLOBALS_UPDATED } from '@storybook/core-events';
addons.setConfig({
theme: getUserPreferedColorTheme(),
});
addons.register('auto-theme-switcher', (api) => {
let currTheme = undefined;
function updateTheme() {
const theme = getUserPreferedColorTheme();
if (theme && currTheme !== theme) {
currTheme = theme;
api.setOptions({ theme }); // change theme of manager
// addons.getChannel().emit(FORCE_RE_RENDER);
}
}
api.on(GLOBALS_UPDATED, ({ globals }) => {
if (globals.theme) {
setUserPreferedColorTheme(globals.theme); // Store the value in localstorage
updateTheme();
}
});
queryWindowMatchMedia().addEventListener('change', () => {
updateTheme();
});
}); In const preview: Preview = {
globalTypes: {
theme: {
description: 'Global theme',
defaultValue: getUserPreferedColorTheme().name, // My ThemeVars have a `name` property
toolbar: {
title: 'Theme',
items: [
{ value: 'my-light', right: 'my-light', title: 'Light', icon: 'sun' },
{ value: 'my-dark', right: 'my-dark', title: 'Dark', icon: 'moon' },
],
dynamicTitle: true, // Change title based on selected value
},
},
},
parameters: {
docs: {
container: ThemedDocsContainer, // our custom container
}
} |
Describe the bug
I have an addon tool that handles the storybook theme through the api:
The Storybook matches, but Docs don't change.
Docs works with the dark theme just by adding through addParametes:
Expected behavior
That when changing the theme through the storybook API, it trigger the Docs theme
Screenshots
System:
Environment Info:
System:
OS: macOS 10.15.4
CPU: (4) x64 Intel(R) Core(TM) i5-5257U CPU @ 2.70GHz
Binaries:
Node: 10.16.0 - /usr/local/bin/node
Yarn: 1.9.4 - /usr/local/bin/yarn
npm: 6.13.7 - /usr/local/bin/npm
Browsers:
Chrome: 81.0.4044.122
Firefox: 74.0
Safari: 13.1
npmPackages:
@storybook/addon-a11y: ^5.3.18 => 5.3.18
@storybook/addon-actions: ^5.3.18 => 5.3.18
@storybook/addon-docs: ^5.3.18 => 5.3.18
@storybook/addon-knobs: ^5.3.18 => 5.3.18
@storybook/addon-storysource: ^5.3.18 => 5.3.18
@storybook/addon-viewport: ^5.3.18 => 5.3.18
@storybook/addons: ^5.3.18 => 5.3.18
@storybook/react: ^5.3.18 => 5.3.18
@storybook/storybook-deployer: ^2.8.3 => 2.8.5
The text was updated successfully, but these errors were encountered: