Comments (5)
@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.
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.
Hi @jeslage thank you so much for working on this, much appreciated!
from storybook-addon-theme-playground.
Wonderful! I will work on implementing this today, thanks again @jeslage 🙏🏽
from storybook-addon-theme-playground.
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)
- Missing properties from Type ThemePlaygroundProps - overrides, config HOT 3
- Theme doesn't persist when changing story
- Configured theme does not persist between changing story HOT 4
- Theme object throws error HOT 4
- Storyshots Invalid hook call when using addon-theme-playground HOT 4
- Panel not loading HOT 3
- React 18 Update HOT 13
- Fails to import `lodash` with Yarn 2 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from storybook-addon-theme-playground.