Skip to content

Commit 2e88b4b

Browse files
authored
Merge pull request #4 from codeAdrian/feature/docs-update-and-cleanup
Docs update & cleanup
2 parents 7d0d2f6 + e3fe333 commit 2e88b4b

File tree

3 files changed

+83
-13
lines changed

3 files changed

+83
-13
lines changed

README.md

Lines changed: 75 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,33 @@
1-
# Gatsby Omni Font Loader
2-
3-
Performant asynchronous font loading plugin for Gatsby.
1+
<div align="center">
2+
<img src="https://res.cloudinary.com/dazdt97d3/image/upload/c_scale,q_auto:best,w_200/v1606558223/omni-logo.jpg" alt="Omni font loader logo">
3+
<br/><br/>
4+
<h1>Gatsby Omni Font Loader</h1>
5+
6+
Performant asynchronous font loading & Flash Of Unstyled Text (FOUT) handling plugin for Gatsby.
7+
8+
<br/>
9+
<img src="https://badgen.net/github/tag/codeAdrian/gatsby-omni-font-loader" />
10+
&nbsp;
11+
<img src="https://badgen.net/npm/dt/gatsby-omni-font-loader" />
12+
&nbsp;
13+
<img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg" />
14+
<br/><br/>
15+
16+
<img src="https://badgen.net/github/stars/codeAdrian/gatsby-omni-font-loader" />
17+
&nbsp;
18+
<img src="https://badgen.net/github/open-issues/codeAdrian/gatsby-omni-font-loader" />
19+
&nbsp;
20+
<img src="https://badgen.net/github/closed-issues/codeAdrian/gatsby-omni-font-loader" />
21+
&nbsp;
22+
<img src="https://badgen.net/github/last-commit/codeAdrian/gatsby-omni-font-loader/main" />
23+
&nbsp;
24+
<img src="https://badgen.net/github/license/codeAdrian/gatsby-omni-font-loader" />
25+
&nbsp;
26+
<img src="https://badgen.net/packagephobia/install/gatsby-omni-font-loader" />
27+
</div>
28+
<br/><br/>
29+
30+
## Features
431

532
* Supports web fonts & self-hosted fonts
633
* Preloads the files & preconnects to the URL
@@ -63,6 +90,50 @@ Add the following snippet to `gatsby-config.js` plugins array.
6390
}
6491
```
6592

93+
## Options
94+
95+
<table>
96+
<thead>
97+
<tr>
98+
<th>Option</th>
99+
<th>Description</th>
100+
<th>Default</th>
101+
</tr>
102+
</thead>
103+
<tbody>
104+
<tr>
105+
<td>enableListener</td>
106+
<td>Enable font loading listener to handle Flash Of Unstyled Text. If enabled, CSS classes will be applied to HTML once each font has finished loading.</td>
107+
<td>false</td>
108+
</tr>
109+
<tr>
110+
<td>preconnect</td>
111+
<td>URLs used for preconnect meta. Base URL where <strong>font files</strong> are hosted.</td>
112+
<td>[]</td>
113+
</tr>
114+
<tr>
115+
<td>interval</td>
116+
<td>Font listener interval (in ms). Default is 300ms. Recommended: >=300ms. </td>
117+
<td>300</td>
118+
</tr>
119+
<tr>
120+
<td>timeout</td>
121+
<td>Font listener timeout value (in ms). Default is 30s (30000ms). Listener will no longer check for loaded fonts after timeout, fonts will still be loaded and displayed, but without handling FOUT.</td>
122+
<td>30000</td>
123+
</tr>
124+
<tr>
125+
<td>web</td>
126+
<td>Self-hosted fonts config. Add font files and font CSS files to "static" folder. Array of <code>{name: "Font name", file: "https://url-to-font-css.path"}</code> objects.</td>
127+
<td>[]</td>
128+
</tr>
129+
<tr>
130+
<td>custom</td>
131+
<td>Web fonts config. File link should point to font CSS file. Array of <code>{name: "Font name", file: "https://url-to-font-css.path"}</code> objects.</td>
132+
<td>[]</td>
133+
</tr>
134+
<tbody>
135+
</table>
136+
66137
## Handling FOUT with Font loading listener
67138

68139
When loading fonts asynchronously, Flash Of Unstyled Text (FOUT) might happen because fonts load few moments later after page is displayed to the user.
@@ -83,7 +154,6 @@ Here is the example of how `body` element will look like after all fonts are bei
83154

84155
<img alt="FOUT example" src="https://res.cloudinary.com/dazdt97d3/image/upload/v1604140006/fouc.gif">
85156

86-
87157
## Issues and Contributions
88158

89159
Feel free to [report issues](https://github.com/codeAdrian/gatsby-omni-font-loader/issues) you find and feel free to contribute to the project by creating Pull Requests.
@@ -94,4 +164,4 @@ Contributions are welcome and appreciated!
94164

95165
The project is created and maintained by [Adrian Bece](https://codeadrian.github.io/) with the generous help of community contributors. If you have used the plugin and would like to contribute, feel free to [Buy Me A Coffee](https://www.buymeacoffee.com/ubnZ8GgDJ).
96166

97-
<a href="https://www.buymeacoffee.com/ubnZ8GgDJ" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-red.png" alt="Buy Me A Coffee" style="height: 51px !important;width: 217px !important;" ></a>
167+
<a href="https://www.buymeacoffee.com/ubnZ8GgDJ"><img src="https://img.buymeacoffee.com/button-api/?text=Support this project&emoji=&slug=ubnZ8GgDJ&button_colour=BD5FFF&font_colour=ffffff&font_family=Bree&outline_colour=000000&coffee_colour=FFDD00"></a>

components/FontListener.tsx

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React, { useEffect, useMemo, useRef, useState } from "react"
22
import { Helmet } from "react-helmet"
3+
import { kebabCase } from "../utils"
34

45
declare var document: { fonts: any }
56

@@ -52,14 +53,6 @@ export const FontListener: React.FC<Props> = ({
5253
</Helmet>
5354
)
5455

55-
function kebabCase(str) {
56-
return str
57-
.match(/[A-Z]{2,}(?=[A-Z][a-z0-9]*|\b)|[A-Z]?[a-z0-9]*|[A-Z]|[0-9]+/g)
58-
.filter(Boolean)
59-
.map(x => x.toLowerCase())
60-
.join("-")
61-
}
62-
6356
function getLoadedFontClassNames() {
6457
return Boolean(loadedFonts.length)
6558
? loadedFonts

utils/utils.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,10 @@ export const getFontNames = (allFonts: { name: string }[]) => {
88

99
export const getFontFiles = (allFonts: { file: string }[]) =>
1010
allFonts.map(({ file }) => file)
11+
12+
export const kebabCase = str =>
13+
str
14+
.match(/[A-Z]{2,}(?=[A-Z][a-z0-9]*|\b)|[A-Z]?[a-z0-9]*|[A-Z]|[0-9]+/g)
15+
.filter(Boolean)
16+
.map(x => x.toLowerCase())
17+
.join("-")

0 commit comments

Comments
 (0)