Giter VIP home page Giter VIP logo

Comments (5)

jeslage avatar jeslage commented on June 6, 2024 2

@christineurban I published a new version of the add-on. you can change the version to 2.2.0 in your package.json.

The provider key inside the themePlayground parameters returns the following props: children, theme and name. I introduced the name prop in the new version, it will return the current theme name. The theme prop will return the current theme object and children is the same as the <Story /> component in a normal decorator.

The following code should work in your case, but I couldn't test it properly

const withThemeProviderAndFonts = ({ children, theme, name }) => {
  let globalStyles = "";
  let fontFaces = "";
  let tenantMock;

  switch (name) {
    case themeNames.ThemeOne:
      globalStyles = generateGlobalStyles(themes.ThemeOne);
      fontFaces = generateFontFaces(fonts.ThemeOneFonts);
      tenantMock = tenantMocks.ThemeOneTenantMock;
      break;
    case themeNames.ThemeTwo:
      globalStyles = generateGlobalStyles(themes.ThemeTwo);
      fontFaces = generateFontFaces(fonts.ThemeTwoFonts);
      tenantMock = tenantMocks.ThemeTwoTenantMock;
      break;
    case themeNames.ThemeThree:
      globalStyles = generateGlobalStyles(themes.ThemeThree);
      fontFaces = generateFontFaces(fonts.ThemeThreeFonts);
      tenantMock = tenantMocks.ThemeThreeTenantMock;
      break;
    default:
      globalStyles = generateGlobalStyles(themes.Default);
      tenantMock = tenantMocks.DefaultTenantMock;
  }

  handleSetStyles(GLOBAL_STYLE_ELEMENT_ID, globalStyles);
  handleSetStyles(FONTS_ELEMENT_ID, fontFaces);

  return (
    <AppContext.Provider
      value={{
        featureFlags: [],
        tenant: tenantMock,
      }}
    >
      <ThemeProvider theme={theme}>
        <div id="__next">
          <div className="pageLayout">
            {children}
          </div>
        </div>
      </ThemeProvider>
    </AppContext.Provider>
  );
};

export const parameters = {
  themePlayground: {
    theme: [
      { name: themeNames.ThemeOne, theme: themes.ThemeOne },
      { name: themeNames.ThemeTwo, theme: themes.ThemeTwo },
      { name: themeNames.ThemeThree, theme: themes.ThemeThree },
      { name: themeNames.Default, theme: themes.Default},
    ],
    provider: withThemeProviderAndFonts
  }
};

Let me now if this works for you or if you have any other issues.

from storybook-addon-theme-playground.

jeslage avatar jeslage commented on June 6, 2024

Hey @christineurban,
currently its not possible to get the current theme name inside the decorator. But I have an idea how to implement it and will publish a new version soon. I will let you know, when its published.
Hope that helps!

from storybook-addon-theme-playground.

christineurban avatar christineurban commented on June 6, 2024

Hi @jeslage thank you so much for working on this, much appreciated!

from storybook-addon-theme-playground.

christineurban avatar christineurban commented on June 6, 2024

Wonderful! I will work on implementing this today, thanks again @jeslage 🙏🏽

from storybook-addon-theme-playground.

christineurban avatar christineurban commented on June 6, 2024

Hi @jeslage, this works perfectly in preview.tsx 🙌🏽 I have realized I have another use case where I would like to know the theme in the story context, to apply props specific to a story and a theme. Here is an example of what was doing before with context.globals.theme:

export const Footer: ComponentStory<typeof FooterComponent> = (
  _args,
  { globals: { theme } }
) => {
  switch (theme) {
    case themeNames.ThemeOne:
      return <FooterComponent key="1" {...props.ThemeOneProps} />;
    case themeNames.ThemeTwo:
      return <FooterComponent key="2" {...props.ThemeTwoProps} />;
    case themeNames.ThemeThree:
      return <FooterComponent key="3" {...props.ThemeThreeProps} />;
    default:
      return <FooterComponent key="4" {...props.DefaultProps} />;
  }
};

Is there possibly some way to also get the theme from the story? Apologies for not realizing this sooner and thank you for time!

from storybook-addon-theme-playground.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.