Last active
October 6, 2022 15:24
-
-
Save Rendez/fb31b0d765a58ab3c962372899cf3f45 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {useLayoutEffect, useState} from 'react'; | |
const fontFaces = [ | |
['tangerine', new FontFace('"Tangerine", serif', 'url(https://fonts.googleapis.com/css?family=Tangerine)')] | |
] as const; | |
export function useFontsLoaded(types = ['tangerine']) { | |
const [fontsLoaded, setFontsLoaded] = useState(false); | |
const depTypes = types.join(' '); | |
useLayoutEffect(() => { | |
const typesList = depTypes.split(' '); | |
Promise.all( | |
fontFaces.map(([type, fontFace]) => { | |
if (typesList.includes(type)) { | |
return fontFace.load(); | |
} | |
return Promise.resolve(); | |
}) | |
) | |
.then(() => { | |
setFontsLoaded(true); | |
}) | |
.catch((e) => { | |
console.error('Fonts Loaded Hook: error loading one or more fonts.', e); | |
}); | |
}, [depTypes]); | |
return fontsLoaded; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment