diff --git a/packages/edit-site/src/components/global-styles/screen-theme-font-faces.js b/packages/edit-site/src/components/global-styles/screen-theme-font-faces.js index 2fb8ede17a75b7..53b79552479b1f 100644 --- a/packages/edit-site/src/components/global-styles/screen-theme-font-faces.js +++ b/packages/edit-site/src/components/global-styles/screen-theme-font-faces.js @@ -3,7 +3,12 @@ */ import { __ } from '@wordpress/i18n'; import { Icon, check } from '@wordpress/icons'; -import { Tooltip, Button } from '@wordpress/components'; +import { + Tooltip, + Button, + __experimentalUseNavigator as useNavigator, +} from '@wordpress/components'; +import { useMemo, useEffect } from '@wordpress/element'; /** * Internal dependencies @@ -14,9 +19,28 @@ import { useFontFamilies } from './hooks'; import FontFaceItem from './font-face-item'; function ScreenThemeFontFacesList( { themeFontSelected } ) { + const { goBack } = useNavigator(); const { fontFamilies, handleRemoveFontFace, sortFontFaces } = useFontFamilies(); const font = fontFamilies[ themeFontSelected ]; + const getFontFaces = ( fontFamily ) => { + if ( ! fontFamily || ! Array.isArray( fontFamily.fontFace ) ) { + return []; + } + return sortFontFaces( + font.fontFace.filter( ( fontFace ) => ! fontFace.shouldBeRemoved ) + ); + }; + const fontFaces = useMemo( () => getFontFaces( font ), [ fontFamilies ] ); + + useEffect( () => { + // Go Back if no font faces are available + // This can happen if all font faces are flagged as shouldBeRemoved and the change is saved. + // After the change is saved, the font faces are removed from the font family and the font faces are no longer available. + if ( ! fontFaces.length ) { + goBack(); + } + }, [ fontFamilies ] ); return ( <> @@ -43,40 +67,31 @@ function ScreenThemeFontFacesList( { themeFontSelected } ) { { ! font &&
{ __( 'No font faces available' ) }
} - { font && - Array.isArray( font.fontFace ) && - sortFontFaces( font.fontFace ).map( ( fontFace, i ) => { - return ( - ! fontFace.shouldBeRemoved && ( -