Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(docs): replace typefaces with fontsource #27313

Merged
merged 7 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions docs/docs/recipes/styling-css.md
Original file line number Diff line number Diff line change
Expand Up @@ -395,35 +395,41 @@ Hosting your own [Google Fonts](https://fonts.google.com/) locally within a proj

- A [Gatsby site](/docs/quick-start)
- The [Gatsby CLI](/docs/gatsby-cli/) installed
- Choosing a font package from [the typefaces project](https://github.com/KyleAMathews/typefaces)
- A chosen font package from [Fontsource](https://github.com/fontsource/fontsource)

### Directions

1. Run `npm install typeface-your-chosen-font`, replacing `your-chosen-font` with the name of the font you want to install from [the typefaces project](https://github.com/KyleAMathews/typefaces).
This example shows how to set up the [Open Sans](https://fonts.google.com/specimen/Open+Sans) font. If you have a different Google Font you want to use, you can find the corresponding package in [NPM](https://www.npmjs.com/search?q=fontsource) or the [packages directory in the Fontsource repository](https://github.com/fontsource/fontsource/tree/master/packages).

An example to load the popular 'Source Sans Pro' font would be: `npm install typeface-source-sans-pro`.
1. Run `npm install fontsource-open-sans` to download the necessary package files.

2. Add `import "typeface-your-chosen-font"` to a layout template, page component, or `gatsby-browser.js`.
2. Then within your app entry file or site component, import the font package. It is recommended you import it via the layout template (`layout.js`). However, importing via page component (`index.js`), or `gatsby-browser.js` are viable alternatives.

```jsx:title=src/components/layout.js
import "typeface-your-chosen-font"
import "fontsource-open-sans" // Defaults to weight 400 with all styles included.
```

If you wish to select a particular weight or style, you may specify it by changing the import path.

```jsx:title=src/components/layout.js
import "fontsource-open-sans/500.css" // Weight 500 with all styles included.
ayuhito marked this conversation as resolved.
Show resolved Hide resolved
import "fontsource-open-sans/900-normal.css" // Select either normal or italic.
```

**Note**: The range of supported weights and styles a font may support is shown in each package's README file.

3. Once it's imported, you can reference the font name in a CSS stylesheet, CSS Module, or CSS-in-JS.

```css:title=src/components/layout.css
body {
font-family: "Your Chosen Font";
font-family: "Open Sans";
}
```

_NOTE: So for the above example, the relevant CSS declaration would be `font-family: 'Source Sans Pro';`_

### Additional resources

- [Fontsource repo on GitHub](https://github.com/fontsource/fontsource)
- [Typography.js](/docs/typography-js/) - Another option for using Google fonts on a Gatsby site
- [The Typefaces Project Docs](https://github.com/KyleAMathews/typefaces/blob/master/README.md)
- [Live example on Kyle Mathews' blog](https://www.bricolage.io/typefaces-easiest-way-to-self-host-fonts/)

## Using Font Awesome

Expand Down
30 changes: 13 additions & 17 deletions docs/docs/using-web-fonts.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,32 @@ Some examples of web font services include [Google Fonts](https://fonts.google.c

### Using Google Fonts

The fastest way to get started using Google Fonts is by choosing a font from [the typefaces project](https://github.com/KyleAMathews/typefaces). This example shows how you can add Open Sans to your project.
The fastest way to get started using Google Fonts is by choosing a font from [Fontsource](https://github.com/fontsource/fontsource).
ayuhito marked this conversation as resolved.
Show resolved Hide resolved

First, install the typeface package with npm:
This example shows how to set up the [Open Sans](https://fonts.google.com/specimen/Open+Sans) font. If you have a different Google Font you want to use, you can find the corresponding package in [NPM](https://www.npmjs.com/search?q=fontsource) or the [packages directory in the Fontsource repository](https://github.com/fontsource/fontsource/tree/master/packages).

```bash
npm install --save typeface-open-sans
```
1. Run `npm install fontsource-open-sans` to download the necessary package files.

Or with yarn:
2. Then within your app entry file or site component, import the font package. It is recommended you import it via the layout template (`layout.js`). However, importing via page component (`index.js`), or `gatsby-browser.js` are viable alternatives.

```bash
yarn add typeface-open-sans
```jsx:title=src/components/layout.js
import "fontsource-open-sans" // Defaults to weight 400 with all styles included.
```

In your `layout.js` file, import the typeface.
If you wish to select a particular weight or style, you may specify it by changing the import path.

```jsx:title=src/components/layout.js
import "typeface-open-sans"
import "fontsource-open-sans/500.css" // Weight 500 with all styles included.
import "fontsource-open-sans/900-normal.css" // Select either normal or italic.
```

Next, add the typeface name to the appropriate place in your CSS. In this case, you will override the `body` element's `font-family` default values.
**Note**: The range of supported weights and styles a font may support is shown in each package's README file.

3. Once it's imported, you can reference the font name in a CSS stylesheet, CSS Module, or CSS-in-JS.

```css:title=src/components/layout.css
body {
color: hsla(0, 0%, 0%, 0.8);
// highlight-next-line
font-family: "Open Sans", georgia, serif;
font-weight: normal;
word-wrap: break-word;
font-kerning: normal;
font-family: "Open Sans";
}
```

Expand Down