简体   繁体   English

在每个故事的故事书中应用不同的主题

[英]Apply a different theme on a per story basis storybook

I am making a library of react components in storybook (v6.1.14)我正在故事书中制作一个反应组件库(v6.1.14)

I have two different themes, basically and light and dark version.我有两个不同的主题,基本上是浅色和深色版本。
I wrap all of my stories in the <ThemeProvider> like this:我将所有故事都包装在<ThemeProvider>中,如下所示:

import React from "react"
import {dark} from "../src/Themes/Theme";
import { ThemeProvider } from "styled-components";
import { GlobalStyles } from "../src/lib/Themes";

// apply our projects theme to our stories
const ThemeDecorator = storyFn => (
  <ThemeProvider theme={dark}>
        <GlobalStyles />
        {storyFn()}
    </ThemeProvider>
)

export default ThemeDecorator;

This is applied through the preview.js file like so:这是通过preview.js文件应用的,如下所示:

const { addDecorator } = require("@storybook/react");
import ThemeDecorator from "./themeDecorator"

// Add our project theme, add a global stylesheet
addDecorator(ThemeDecorator);

So now everything has the dark theme applied, however, for each component I want to show two stories: One for the light theme and one for the dark theme - how do I make a story take on a different theme to the one globally declared?所以现在一切都应用了深色主题,但是,对于每个组件,我想展示两个故事:一个用于浅色主题,一个用于深色主题 - 我如何让故事呈现与全局声明的主题不同的主题?

I had the same problem as well.我也有同样的问题。 From the Storybook docs, I found this answer .从 Storybook 文档中,我找到了这个答案

So basically, you can use decorators with your template and apply styling to it.所以基本上,您可以在模板中使用decorators并对其应用样式。

const Template: Story<LogoProps> = (args) => {
  const { version } = args;
  return <Logo {...args} version={version} />;
};

export const Light = Template.bind({});
Light.args = {
  version: 'light',
};
Light.decorators = [
  (LightLogo) => (
    <div style={{ backgroundColor: 'black', padding: '5px 7px' }}>
      <LightLogo />
    </div>
  ),
];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM