如何在NX的Storybook V6中添加材料UI V5 ThemeProvider
我目前正在使用nx.dev
作为monorepo
。我有多个React客户端。 多亏了NX,我有一个中央材料UI
配置(在LIB文件夹内)。
我正在尝试在该MUI文件夹中使用Storybook
。不幸的是,themeprovider
不干预。这意味着我的自定义调色板等将不会被接管。不幸的是,我不知道为什么Storybook
不接受themeprovider
从mui
。是因为NX吗?还是这与它无关?
我认为React 18
,Storybook 6
和MUI 5
之间存在一些问题。 但是必须有一个解决方案,因为我已经成功地建立了它,而没有NX和较低版本。 需要帮助!
在我的LIB文件夹中,有一个带有.StoryBook文件夹的MUI文件夹。
main.js
const rootMain = require('../../../.storybook/main');
module.exports = {
...rootMain,
core: { ...rootMain.core, builder: 'webpack5' },
stories: [
...rootMain.stories,
'../src/lib/**/*.stories.mdx',
'../src/lib/**/*.stories.@(js|jsx|ts|tsx)',
],
addons: [
...rootMain.addons,
'@nrwl/react/plugins/storybook',
],
webpackFinal: async (config, { configType }) => {
// apply any global webpack configs that might have been specified in .storybook/main.js
if (rootMain.webpackFinal) {
config = await rootMain.webpackFinal(config, { configType });
}
// add your own webpack tweaks if needed
return config;
},
};
preview.js
import React from 'react';
import { ThemeProvider, theme } from '../src';
export const parameters = {
actions: { argTypesRegex: '^on[A-Z].*' },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
},
};
export const decorators = [
(Story) => (
<ThemeProvider theme={theme}>
<Story />
</ThemeProvider>
),
];
尝试了
import React from 'react'
import { addDecorator } from '@storybook/react'
import { ThemeProvider } from '../src'
import { theme } from '../src'
export const parameters = {
actions: { argTypesRegex: '^on[A-Z].*' },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
},
}
addDecorator((story) => (
<ThemeProvider theme={theme}>
{story()}
</ThemeProvider>
))
i als在lib文件夹的src文件夹中
export * from './theme';
export * from '@mui/material';
一个index.ts文件,其中包含theme.ts.ts。
export const theme = createTheme({
palette,
spacing: [0, 4, 8, 16, 24, 32, 48, 56, 64, 80, 96],
components: {
...Buttons,
},
});
export default theme;
I am currently using nx.dev
as a monorepo
. I have multiple react clients.
Thanks to NX I have a central Material Ui
Configuration (inside the lib folder).
I'm trying to use Storybook
inside that mui folder. Unfortunately, the ThemeProvider
does not intervene. That means my custom palette etc. Will not be taken over. Unfortunately, I don't know why Storybook
is not accepting the Themeprovider
from MUI
. Is it because of NX? Or does this have nothing to do with it?
I think there are some issues between react 18
, Storybook 6
and MUI 5
.
But there must be a solution, cause I already build it successful, without NX and lower versions.
Need help please!
Inside my lib folder there is a mui folder with a .storybook folder.
main.js
const rootMain = require('../../../.storybook/main');
module.exports = {
...rootMain,
core: { ...rootMain.core, builder: 'webpack5' },
stories: [
...rootMain.stories,
'../src/lib/**/*.stories.mdx',
'../src/lib/**/*.stories.@(js|jsx|ts|tsx)',
],
addons: [
...rootMain.addons,
'@nrwl/react/plugins/storybook',
],
webpackFinal: async (config, { configType }) => {
// apply any global webpack configs that might have been specified in .storybook/main.js
if (rootMain.webpackFinal) {
config = await rootMain.webpackFinal(config, { configType });
}
// add your own webpack tweaks if needed
return config;
},
};
preview.js
import React from 'react';
import { ThemeProvider, theme } from '../src';
export const parameters = {
actions: { argTypesRegex: '^on[A-Z].*' },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
},
};
export const decorators = [
(Story) => (
<ThemeProvider theme={theme}>
<Story />
</ThemeProvider>
),
];
I als tried that in preview.js
import React from 'react'
import { addDecorator } from '@storybook/react'
import { ThemeProvider } from '../src'
import { theme } from '../src'
export const parameters = {
actions: { argTypesRegex: '^on[A-Z].*' },
controls: {
matchers: {
color: /(background|color)$/i,
date: /Date$/,
},
},
}
addDecorator((story) => (
<ThemeProvider theme={theme}>
{story()}
</ThemeProvider>
))
in the src folder of the lib folder folder there is an index.ts file containing
export * from './theme';
export * from '@mui/material';
theme.ts
export const theme = createTheme({
palette,
spacing: [0, 4, 8, 16, 24, 32, 48, 56, 64, 80, 96],
components: {
...Buttons,
},
});
export default theme;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我终于发现了这个问题
react故事书。
那对我有很大帮助。
I finally found that issue
react material-ui v5 theming doesnt work with storybook.
That helped me a lot.
@maxfromgermany。
我最近遇到了一个问题,并用此代码库解决了。将以下代码添加到“ .storybook/preview.js”中。
〜人才风暴
@maxfromgermany.
I had such a problem recently and solved with this codebase. Add the following code to the ".storybook/preview.js".
~ Storm In Talent