Skip to content
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

Hardcoded theme imports in Storybook makes adding a new theme difficult #82

Open
kyh opened this issue Aug 10, 2020 · 0 comments
Open
Labels
kit: Storybook Concerning Storybook status: needs triage These issues haven't been looked at yet by a maintainer.

Comments

@kyh
Copy link

kyh commented Aug 10, 2020

When using this template for a project I noticed that the hardcoded theme imports in storybook makes updating the theme, particularly adding, removing, or editing the theme name a very manual process.

Proposal:

Dynamically generate previews based on number of themes

export const withTheme = (storyFn: any) => {
  const themes = Object.keys(theme);
  return (
    <>
      {themes.map((t) => (
        <ThemeProvider theme={theme[t]}>
          ...
        </ThemeProvider>
      ))}
    </>
  );
};

In the .stories.mdx where the theme is imported, we render the content using the useTheme hook instead:

export const Palette = () => {
  const theme = useTheme();

  const palette = Object.keys(theme.colors).map((colorKey) => ({
    title: colorKey,
    subtitle: `theme.colors.${colorKey}`,
    colors: Object.values(theme.colors[colorKey]),
  }));

  return (
    <ColorPalette>
      {palette.map((colorItem) => (
        <ColorItem key={colorItem.title} {...colorItem} />
      ))}
    </ColorPalette>
  );
};

<Preview withSource="none">
  <Story name="Colors">
    <Palette />
  </Story>
</Preview>
@t-schemmer t-schemmer added kit: Storybook Concerning Storybook status: needs triage These issues haven't been looked at yet by a maintainer. labels Aug 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kit: Storybook Concerning Storybook status: needs triage These issues haven't been looked at yet by a maintainer.
Projects
None yet
Development

No branches or pull requests

2 participants