使用 CRA/样式组件在 Storybook 中应用自定义字体?
你好,我正在尝试将故事书与 React 结合使用。 目前正在使用由 create-react-app 构建并使用样式组件的应用程序。 现在我一直在将自定义字体导入故事书预览页面。
有趣的是,自定义字体在 safari 中工作正常,但在 chrome 中却不行。但我想知道如何在 chrome 中使用它,因为我们大部分时间都使用 chrome。 我在样式组件中有 grobalStyles ,包括字体。尝试了我在网上找到的一些解决方案,例如尝试将一些代码放入 preview-head.html
和 manager-head.html
中,但在 Chrome 中都不起作用。它适用于 google font API 的链接,包括 *-head.html 中的链接,但我需要使用本地的自定义字体。
有什么建议吗?
package.json
"storybook": "start-storybook -p 6006 -s ./src",
...
文件夹结构
|- .storybook
|- ...
|- src
|- ...
|- assets
|- ...
|- fonts
|- NotoSansJP-Regular.ttf
|- ... .ttf
|- NotoSansJP.css
|- lib
|- ...
|- styled-components
|- grobalStyles.ts
.storybook/preview.js
import React from 'react';
import { ThemeProvider } from 'styled-components';
import { addDecorator } from '@storybook/react';
import { theme } from '../src/lib/styled-components/theme';
import { GlobalStyle } from '../src/lib/styled-components/globalStyle';
export const parameters = {
actions: { argTypesRegex: '^on[A-Z].*' },
};
addDecorator((storyFn) => (
<ThemeProvider theme={theme}>
<GlobalStyle />
{storyFn()}
</ThemeProvider>
));
.storybook/webpack.config.js
const path = require('path');
module.exports = {
module: {
loaders: [
{
test: /\.(png|woff|woff2|eot|ttf|svg)$/,
loaders: ['file-loader'],
include: path.resolve(__dirname, '../'),
},
],
},
};
GrobalStyle.js
import { createGlobalStyle } from 'styled-components';
import '../../assets/fonts/NotoSansJP-Regular.ttf';
import '../../assets/fonts/NotoSansJP-Bold.ttf';
import '../../assets/fonts/NotoSansJP-Light.ttf';
export const GlobalStyle = createGlobalStyle`
@font-face {
font-family: "Noto Sans JP";
src: local("Noto Sans JP"),
url("./assets/fonts/NotoSansJP-Regular.ttf") format("truetype");
font-weight: normal;
}
@font-face {
font-family: "Noto Sans JP";
src: local("Noto Sans JP"),
url("./assets/fonts/NotoSansJP-Bold.ttf") format("truetype");
font-weight: bold;
}
@font-face {
font-family: "Noto Sans JP";
src: local("Noto Sans JP"),
url("./assets/fonts/NotoSansJP-Bold.ttf") format("truetype");
font-weight: 700;
}
@font-face {
font-family: "Noto Sans JP";
src: local("Noto Sans JP"),
url("./assets/fonts/NotoSansJP-Light.ttf") format("truetype");
font-weight: light;
}
* {
font-family: "Noto Sans JP";
font-size: 16px;
}
...
`
还尝试过: preview-head.html
和 manager-head.html
<link rel="stylesheet" href="./assets/fonts/NotoSansJP.css" />
<style type="text/css">
* {
font-family: 'Noto Sans JP';
font-size: 16px;
}
</style>
Hi I'm trying to use storybook with React.
Currently working with an app built by create-react-app and using styled-components.
Now I am stuck with importing custom fonts into storybook preview page.
Interestingly, the custom fonts work fine in safari but not in chrome. But I'd like to know how to use it with chrome since we work with chrome most of the time.
I have grobalStyles in styled-components including fonts. Tried some solutions I found online Like tried to put some codes in preview-head.html
and manager-head.html
but none worked in chrome. It works with links for google font API including in *-head.html but I need to use custom fonts from my local.
Any advice?
package.json
"storybook": "start-storybook -p 6006 -s ./src",
...
folder structure
|- .storybook
|- ...
|- src
|- ...
|- assets
|- ...
|- fonts
|- NotoSansJP-Regular.ttf
|- ... .ttf
|- NotoSansJP.css
|- lib
|- ...
|- styled-components
|- grobalStyles.ts
.storybook/preview.js
import React from 'react';
import { ThemeProvider } from 'styled-components';
import { addDecorator } from '@storybook/react';
import { theme } from '../src/lib/styled-components/theme';
import { GlobalStyle } from '../src/lib/styled-components/globalStyle';
export const parameters = {
actions: { argTypesRegex: '^on[A-Z].*' },
};
addDecorator((storyFn) => (
<ThemeProvider theme={theme}>
<GlobalStyle />
{storyFn()}
</ThemeProvider>
));
.storybook/webpack.config.js
const path = require('path');
module.exports = {
module: {
loaders: [
{
test: /\.(png|woff|woff2|eot|ttf|svg)$/,
loaders: ['file-loader'],
include: path.resolve(__dirname, '../'),
},
],
},
};
GrobalStyle.js
import { createGlobalStyle } from 'styled-components';
import '../../assets/fonts/NotoSansJP-Regular.ttf';
import '../../assets/fonts/NotoSansJP-Bold.ttf';
import '../../assets/fonts/NotoSansJP-Light.ttf';
export const GlobalStyle = createGlobalStyle`
@font-face {
font-family: "Noto Sans JP";
src: local("Noto Sans JP"),
url("./assets/fonts/NotoSansJP-Regular.ttf") format("truetype");
font-weight: normal;
}
@font-face {
font-family: "Noto Sans JP";
src: local("Noto Sans JP"),
url("./assets/fonts/NotoSansJP-Bold.ttf") format("truetype");
font-weight: bold;
}
@font-face {
font-family: "Noto Sans JP";
src: local("Noto Sans JP"),
url("./assets/fonts/NotoSansJP-Bold.ttf") format("truetype");
font-weight: 700;
}
@font-face {
font-family: "Noto Sans JP";
src: local("Noto Sans JP"),
url("./assets/fonts/NotoSansJP-Light.ttf") format("truetype");
font-weight: light;
}
* {
font-family: "Noto Sans JP";
font-size: 16px;
}
...
`
Also tried: preview-head.html
and manager-head.html
<link rel="stylesheet" href="./assets/fonts/NotoSansJP.css" />
<style type="text/css">
* {
font-family: 'Noto Sans JP';
font-size: 16px;
}
</style>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试此代码,也可以使用
webfontloader
其出色的字体自由度。You can try this code or you can use
webfontloader
its awsome libery for fonts.