@@ -6,7 +6,6 @@ import { i18n } from 'stackable'
66/**
77 * Internal dependencies
88 */
9- import fonts from './google-fonts.json'
109import {
1110 loadGoogleFont ,
1211 MODERN_FONT_STACKS ,
@@ -18,20 +17,32 @@ import {
1817 */
1918import { __ } from '@wordpress/i18n'
2019import { applyFilters } from '@wordpress/hooks'
21- import { useMemo } from '@wordpress/element'
20+ import {
21+ useMemo , useState , useEffect ,
22+ } from '@wordpress/element'
2223import AdvancedAutosuggestControl from '../advanced-autosuggest-control'
2324
2425import { select } from '@wordpress/data'
2526
26- const fontOptions = fonts . map ( font => {
27- return { label : font . family , value : font . family }
28- } )
27+ const loadGoogleFonts = async ( ) => {
28+ const { default : fonts } =
29+ await import ( /* webpackChunkName: "data/google-fonts" */ './google-fonts.json' )
30+ return fonts . map ( font => ( { label : font . family , value : font . family } ) )
31+ }
2932
3033const FontFamilyControl = props => {
3134 const {
3235 loadingThemeFont, themeFonts, themeFontOptions,
3336 } = select ( 'stackable/theme-fonts' ) . getThemeFonts ( )
3437
38+ const [ googleFontOptions , setGoogleFontOptions ] = useState ( [ ] )
39+
40+ useEffect ( ( ) => {
41+ loadGoogleFonts ( ) . then ( fontOptions => {
42+ setGoogleFontOptions ( fontOptions )
43+ } )
44+ } , [ ] )
45+
3546 const options = useMemo ( ( ) => {
3647 const allFontOptions = [
3748 {
@@ -53,7 +64,7 @@ const FontFamilyControl = props => {
5364 {
5465 id : 'google-fonts' ,
5566 title : __ ( 'Google Fonts' , i18n ) ,
56- options : fontOptions ,
67+ options : googleFontOptions ,
5768 } ,
5869 ]
5970 if ( themeFonts . length ) {
@@ -64,7 +75,7 @@ const FontFamilyControl = props => {
6475 } )
6576 }
6677 return applyFilters ( 'stackable.font-family-control.options' , allFontOptions )
67- } , [ loadingThemeFont ] )
78+ } , [ loadingThemeFont , googleFontOptions ] )
6879
6980 return (
7081 < AdvancedAutosuggestControl
@@ -74,7 +85,7 @@ const FontFamilyControl = props => {
7485 onChange = { fontFamily => {
7586 if ( ! themeFonts . includes ( fontFamily ) ) {
7687 // Load font if it's a Google font.
77- fontOptions . some ( font => {
88+ googleFontOptions . some ( font => {
7889 if ( font . value === fontFamily ) {
7990 loadGoogleFont ( fontFamily )
8091 return true
0 commit comments