@@ -8,12 +8,14 @@ interface Props {
88 fontNames : string [ ]
99 interval : number
1010 timeout : number
11+ scope : string
1112}
1213
1314export 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
0 commit comments