MUI v5 + Storybook:主题和字体系列在 Storybook 中不起作用

big*_*ato 9 reactjs material-ui

我遇到一个问题,MUI 主题在 Codesandbox 中有效,但在 Storybook 中无效

演示展示它在没有故事书的情况下工作: https://codesandbox.io/s/typescript-material-ui-textfield-ojk3h ?file=/src/App.tsx

演示显示它与故事书 Git 存储库不同:https://github.com/EdmundMai/mui-v5-storybook

这是我的组件:

import React from "react";
import styled from "styled-components";
import { createTheme, ThemeProvider } from "@mui/material/styles";
import MuiTextField from "@mui/material/TextField";

const StyledTextField = styled(MuiTextField)`
  width: 288px;
`;

const theme = createTheme({
  typography: {
    allVariants: {
      fontFamily: "serif",
      fontSize: "12px",
    },
  },
});

const ThemeProviderMine = () => (
  <ThemeProvider theme={theme}>
    <StyledTextField placeholder="Foobar" label={"Select a foobar"} />
  </ThemeProvider>
);

export default ThemeProviderMine;

Run Code Online (Sandbox Code Playgroud)

正如您所看到的,在codesandbox 上一切正常。我的自定义字体和字体大小都被使用:

在此输入图像描述

但是,当我使用故事书时,我的自定义字体和字体大小将被忽略:

在此输入图像描述

有谁知道为什么会这样?

MHa*_*ase 33

在我的例子中我所要做的只是添加:

features: {
  emotionAlias: false,
},
Run Code Online (Sandbox Code Playgroud)

.storybook/main.js


big*_*ato 1

天哪,我修好了。因此显然与 Storybook <6.3 不兼容,因为它依赖于 @emotion/styled v10,而 MUI v5 必须使用 v11。

https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#emotion11-quasi-compatibility

这就是我修复它的方法

react-storybook-typescript main % git diff
diff --git a/.storybook/main.js b/.storybook/main.js
index b53b802..bb5fb01 100644
--- a/.storybook/main.js
+++ b/.storybook/main.js
@@ -1,4 +1,8 @@
 module.exports = {
-    stories: ['../src/**/*.stories.tsx'],
-    addons: ['@storybook/addon-docs']
-};
\ No newline at end of file
+  stories: ["../src/**/*.stories.tsx"],
+  addons: ["@storybook/addon-docs"],
+  features: {
+    emotionAlias: false,
+  },
+};
+
diff --git a/package-lock.json b/package-lock.json
index 456a0ac..c066ac7 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -8,6 +8,8 @@
       "name": "cra-ts",
       "version": "0.1.0",
       "dependencies": {
+        "@emotion/react": "^11.7.0", // <--- not sure if this is required
+        "@emotion/styled": "^11.6.0", // <--- not sure if this is required
         "@mui/lab": "5.0.0-alpha.59",
         "@mui/material": "5.2.2",
         "@mui/styles": "^5.2.3",
@@ -29,8 +31,8 @@
       },
       "devDependencies": {
         "@babel/core": "^7.10.2",
-        "@storybook/addon-docs": "6.3.12",
-        "@storybook/react": "6.3.12",
+        "@storybook/addon-docs": "^6.4.8",
+        "@storybook/react": "^6.4.8",
         "babel-loader": "^8.1.0"
       }
     },

Run Code Online (Sandbox Code Playgroud)