add util to download and set the --font-family to the downloaded font

This commit is contained in:
Julien Calixte
2024-04-06 14:10:52 +02:00
parent 458ac16d73
commit 809b376430

View File

@@ -0,0 +1,22 @@
import FontFaceObserver from 'fontfaceobserver'
export const downloadGoogleFont = async (font: string): Promise<void> => {
const href = assembleFontLink(font)
const link = document.createElement('link')
link.href = href
link.rel = 'stylesheet'
document.head.appendChild(link)
await new FontFaceObserver(font).load()
document.documentElement.style.setProperty('--font-family', font)
}
const assembleFontLink = (font: string) => {
return `https://fonts.googleapis.com/css2?display=swap&family=${font.replaceAll(
' ',
'+'
)}`
}