Skip to content

Commit f950670

Browse files
author
Francis Champagne
committed
Revert to classnames
1 parent c82a5fb commit f950670

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed

components/FontListener.tsx

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ interface Props {
88
fontNames: string[]
99
interval: number
1010
timeout: number
11+
scope: string
1112
}
1213

1314
export const FontListener: React.FC<Props> = ({
1415
fontNames,
1516
interval,
1617
timeout,
18+
scope,
1719
}) => {
1820
const [hasLoaded, setHasLoaded] = useState<Boolean>(false)
1921
const [loadedFonts, setLoadedFonts] = useState<string[]>([])
@@ -25,7 +27,9 @@ export const FontListener: React.FC<Props> = ({
2527
[loadedFonts, fontNames]
2628
)
2729

28-
const loadedAttrName = useMemo(getLoadedFontAttrNames, [loadedFonts])
30+
const classnameScope = useMemo(() => scope, [])
31+
const loadedClassname = useMemo(getLoadedFontClassNames, [loadedFonts])
32+
const targetElement = classnameScope === "html" ? "documentElement" : "body"
2933

3034
const apiAvailable = "fonts" in document
3135

@@ -46,17 +50,18 @@ export const FontListener: React.FC<Props> = ({
4650
clearInterval(intervalId)
4751
}
4852
}, [hasLoaded, intervalId])
53+
54+
useEffect(() => {
55+
document[targetElement].className += " " + loadedClassname
56+
}, [loadedClassname])
4957

50-
return (
51-
<Helmet>
52-
<html {...loadedAttrName} />
53-
</Helmet>
54-
)
58+
return null
5559

56-
function getLoadedFontAttrNames() {
60+
function getLoadedFontClassNames() {
5761
return Boolean(loadedFonts.length)
5862
? loadedFonts
59-
.reduce((acc, fontName) => ({ ...acc, ...({[`wf-${kebabCase(fontName)}`]: "loaded"}) }), {})
63+
.map(fontName => `wf-${kebabCase(fontName)}--loaded`)
64+
.join(" ")
6065
: ""
6166
}
6267

consts/defaults.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ export const INTERVAL_DEFAULT = 300
33
export const TIMEOUT_DEFAULT = 30000
44

55
export const MODE_DEFAULT = "async"
6+
7+
export const SCOPE_DEFAULT = "body"

gatsby-browser.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "react"
22
import { AsyncFonts, FontListener } from "./components"
3-
import { INTERVAL_DEFAULT, MODE_DEFAULT, TIMEOUT_DEFAULT } from "./consts"
3+
import { INTERVAL_DEFAULT, MODE_DEFAULT, TIMEOUT_DEFAULT, SCOPE_DEFAULT } from "./consts"
44
import { getFontFiles, getFontNames } from "./utils"
55

66
export const wrapRootElement = (
@@ -11,6 +11,7 @@ export const wrapRootElement = (
1111
enableListener,
1212
interval = INTERVAL_DEFAULT,
1313
timeout = TIMEOUT_DEFAULT,
14+
scope = SCOPE_DEFAULT,
1415
mode = MODE_DEFAULT,
1516
}
1617
) => {
@@ -22,7 +23,7 @@ export const wrapRootElement = (
2223
const fontFiles = getFontFiles(allFonts)
2324
const fontNames = getFontNames(allFonts)
2425

25-
const listenerProps = { fontNames, interval, timeout }
26+
const listenerProps = { fontNames, interval, timeout, scope }
2627

2728
const hasFontFiles = Boolean(fontFiles.length)
2829
const hasFontNames = Boolean(fontNames.length)

0 commit comments

Comments
 (0)