首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何为我的自定义库组件正确加载字体系列?

如何为我的自定义库组件正确加载字体系列?
EN

Stack Overflow用户
提问于 2019-08-15 09:42:10
回答 1查看 885关注 0票数 1

我正在使用styled-componentsstyled-system编写一个新的React UI组件库。这个库将在一个副项目中使用,并且应该作为npm库发布。

我正在创建一个button组件,并且考虑到几乎每个组件都应该有一个这样的font-family Roboto

代码语言:javascript
复制
const BaseButton = styled.div`
  font-family: 'Roboto', sans-serif;
`;

考虑到每个组件都是相互独立的,那么为我的整个组件库设置一次默认font-family的最佳位置是哪里?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2019-08-15 12:29:57

如果你想让你所有的组件都使用Roboto,你应该全局设置@font-face up。

代码语言:javascript
复制
import { createGlobalStyle } from "styled-components";
import Roboto from './Roboto.otf';
import SecondaryFont from './SecondaryFont.otf';

const GlobalStyles = createGlobalStyle`
  @font-face {
    font-family: 'Roboto';
    src: url('${Roboto}') format('opentype');
  }
  @font-face {
    font-family: 'SecondaryFont';
    src: url('${SecondaryFont}') format('opentype');
  }

  body {
    font-family: 'Roboto', sans-serif;
  }
`

const YourCustomProvider = ({ children }) => (
  <>
    <GlobalStyles />
    {children}
  </>
)

// Inform users to wrap their app with your provider
const App = () => {
  return (
    <YourCustomProvider>
      // Their app
    </YourCustomProvider>
  )
}

然后在特定组件上使用辅助字体。

代码语言:javascript
复制
const RandomComponent = styled.div`
  font-family: "SecondaryFont"
`
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57503938

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档