@@ -5,28 +5,37 @@ import * as Content from "./Views/content";
55import { generateNumberByStep } from "~/common/utilities" ;
66
77export const useFont = ( { url, name } , deps ) => {
8- const [ loading , setLoading ] = React . useState ( false ) ;
8+ const [ fetchState , setFetchState ] = React . useState ( { loading : false , error : null } ) ;
99 const prevName = React . useRef ( name ) ;
1010
1111 if ( ! window . $SLATES_LOADED_FONTS ) window . $SLATES_LOADED_FONTS = [ ] ;
1212 const alreadyLoaded = window . $SLATES_LOADED_FONTS . includes ( name ) ;
1313
1414 React . useEffect ( ( ) => {
15- if ( alreadyLoaded ) return ;
15+ if ( alreadyLoaded ) {
16+ setFetchState ( ( prev ) => ( { ...prev , error : null } ) ) ;
17+ return ;
18+ }
1619
17- setLoading ( true ) ;
20+ setFetchState ( ( prev ) => ( { ... prev , error : null , loading : true } ) ) ;
1821 const customFonts = new FontFace ( name , `url(${ url } )` ) ;
19- customFonts . load ( ) . then ( ( loadedFont ) => {
20- document . fonts . add ( loadedFont ) ;
21- prevName . current = name ;
22- setLoading ( false ) ;
22+ customFonts
23+ . load ( )
24+ . then ( ( loadedFont ) => {
25+ document . fonts . add ( loadedFont ) ;
26+ prevName . current = name ;
2327
24- window . $SLATES_LOADED_FONTS . push ( name ) ;
25- } ) ;
28+ setFetchState ( ( prev ) => ( { ...prev , loading : false } ) ) ;
29+ window . $SLATES_LOADED_FONTS . push ( name ) ;
30+ } )
31+ . catch ( ( err ) => {
32+ setFetchState ( { loading : false , error : err } ) ;
33+ } ) ;
2634 } , deps ) ;
2735
2836 return {
29- isFontLoading : loading ,
37+ isFontLoading : fetchState . loading ,
38+ error : fetchState . error ,
3039 // NOTE(Amine): show previous font while we load the new one.
3140 fontName : alreadyLoaded ? name : prevName . current ,
3241 } ;
0 commit comments