我目前正在开发 Next.js 网站,并使用样式组件和 Storybook。虽然我已将所有内容配置为与 Storybook 配合良好,但我遇到了 ThemeProvider 的问题。
以前,一切正常,但将项目从 Create React App (CRA) 迁移到 Next.js 13 后,ThemeProvider 似乎没有按预期进行合作。
你们能帮我解决这个问题吗?
这是
.storybook/main.ts
。
import * as path from 'path';
import type { StorybookConfig } from '@storybook/nextjs';
const config: StorybookConfig = {
stories: [
'../stories/**/*.mdx',
'../stories/**/*.stories.@(js|jsx|mjs|ts|tsx)',
],
addons: [
'@storybook/addon-onboarding',
'@storybook/addon-links',
'@storybook/addon-essentials',
'@chromatic-com/storybook',
'@storybook/addon-interactions',
],
framework: {
name: '@storybook/nextjs',
options: {
image: {
loading: 'eager',
},
nextConfigPath: path.resolve(__dirname, '../next.config.js'),
},
},
docs: {
autodocs: 'tag',
},
staticDirs: ['../public'],
};
export default config;
这是
.storybook/preview.ts
。
import type { Preview } from '@storybook/react';
import { ThemeProvider } from 'styled-components';
import { theme } from '../styles/theme';
import { GlobalStyles } from '../styles/GlobalStyles';
import { withThemeFromJSXProvider } from '@storybook/addon-styling';
const preview: Preview = {
parameters: {
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/i,
},
},
nextjs: {
appDirectory: true,
},
},
};
export default preview;
export const decorators = [
withThemeFromJSXProvider({
themes: { theme },
Provider: ThemeProvider,
GlobalStyles,
}),
];
我已经尝试了一切..
import type { Preview } from '@storybook/react';
import { ThemeProvider } from 'styled-components';
import { theme } from '../styles/theme';
import { GlobalStyles } from '../styles/GlobalStyles';
import { withThemeFromJSXProvider } from '@storybook/addon-styling';
const preview: Preview = {
decorators: [
(Story) => (
<ThemeProvider theme={theme}>
<GlobalStyles theme={theme} />
<Story />
</ThemeProvider>
),
],
parameters: {
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/i,
},
},
nextjs: {
appDirectory: true,
},
},
};
export default preview;