【问题标题】:Is it possible to overwrite gatsby-theme-blog's fonts?是否可以覆盖 gatsby-theme-blog 的字体?
【发布时间】:2020-07-25 17:56:29
【问题描述】:

我正在使用 gatsby-theme-blog,并且想使用默认的“Montserrat”以外的字体,它带有“Wordpress 2016”主题。

如果可能,我想将字体更改为“Work Sans”,“fairyGate”主题用于标题。

来自Shadowing in Gatsby Themes 我知道如何在我的项目下创建文件来替换原始文件。但是,所有方法都失败了,帖子页面中的标题仍然是Montserrat

这是我尝试过的:

  1. 安装主题及其依赖字体:
// package.json
{
  "dependencies": {
    "@theme-ui/typography": "^0.3.0", // to use `toTheme` method
    "typeface-work-sans": "^0.0.72", // the font I'd like to use
    "typography-theme-fairy-gates": "^0.16.19"
  }
}
  1. 创建文件以隐藏原始排版:
// my-project/src/gatsby-theme-blog/gatsby-plugin-theme-ui/typography.ts

import "typeface-work-sans"

import { toTheme } from "@theme-ui/typography"
import fairyGates from "typography-theme-fairy-gates"

export default toTheme(fairyGates)

// my-project/src/gatsby-theme-blog/gatsby-plugin-theme-ui/index.ts


/* I duplicate most codes in original gatsby-plugin-theme-ui/index.js 
   intentionally to avoid other possible failing factors */

import merge from "deepmerge"
import typography from "gatsby-theme-blog/src/gatsby-plugin-theme-ui/typography"
import colors from "gatsby-theme-blog/src/gatsby-plugin-theme-ui/colors"
import styles from "gatsby-theme-blog/src/gatsby-plugin-theme-ui/styles"
import prism from "gatsby-theme-blog/src/gatsby-plugin-theme-ui/prism"

export default merge(typography, {
  initialColorMode: `light`,
  colors,
  fonts: {
    heading: `Work Sans, sans-serif`, // <- Changing font here doesn't work as expected!
    monospace: `Consolas, Menlo, Monaco, source-code-pro, Courier New, monospace`,
  },
  sizes: {
    container: 672,
  },
  styles,
  prism,
})

欢迎提出任何建议!

【问题讨论】:

    标签: gatsby theme-ui


    【解决方案1】:
    1. 创建一个布局文件并将所有页面包含在其中。 像this 这样的东西可以工作
    2. 使用CSS in JS 确定您的样式或类似的范围,您可以指定字体样式并以范围和模块化的方式覆盖css 样式
    3. 使用Gatsby plugin google fonts 提供您选择的字体

    【讨论】:

    • 谢谢! 1. 和 2 是我所做的(很抱歉没有在帖子中澄清这一点),但我不知道有 Gatsby 插件谷歌字体。我去试试,谢谢!
    • 是的,我绝对推荐 Gatsby Google Fonts Plugin,它非常方便。如果对您有帮助,请接受此作为答案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-10
    • 1970-01-01
    • 2012-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-28
    相关资源
    最近更新 更多