99 Notice ,
1010} from '@wordpress/components' ;
1111import { __ } from '@wordpress/i18n' ;
12- import { useCallback , useMemo , useEffect } from '@wordpress/element' ;
12+ import { useCallback , useMemo } from '@wordpress/element' ;
1313import { getValueFromVariable } from '@wordpress/global-styles-engine' ;
1414
1515/**
@@ -29,6 +29,7 @@ import {
2929 getMergedFontFamiliesAndFontFamilyFaces ,
3030 findNearestStyleAndWeight ,
3131} from './typography-utils' ;
32+ import { getFontStylesAndWeights } from '../../utils/get-font-styles-and-weights' ;
3233
3334const MIN_TEXT_COLUMNS = 1 ;
3435const MAX_TEXT_COLUMNS = 6 ;
@@ -196,15 +197,62 @@ export default function TypographyPanel( {
196197 const slug = fontFamilies ?. find (
197198 ( { fontFamily : f } ) => f === newValue
198199 ) ?. slug ;
199- onChange (
200- setImmutably (
201- value ,
202- [ 'typography' , 'fontFamily' ] ,
203- slug
204- ? `var:preset|font-family|${ slug } `
205- : newValue || undefined
206- )
200+ const newFontFamily = slug
201+ ? `var:preset|font-family|${ slug } `
202+ : newValue || undefined ;
203+
204+ // When font family changes, check if current font style/weight are available
205+ // in the new font family, and update to nearest available values if needed.
206+ const newFontFamilyFaces =
207+ fontFamilies ?. find ( ( { fontFamily : f } ) => f === newValue )
208+ ?. fontFace ?? [ ] ;
209+ const { fontStyles, fontWeights } =
210+ getFontStylesAndWeights ( newFontFamilyFaces ) ;
211+ const hasFontStyle = fontStyles ?. some (
212+ ( { value : fs } ) => fs === fontStyle
213+ ) ;
214+ const hasFontWeight = fontWeights ?. some (
215+ ( { value : fw } ) => fw ?. toString ( ) === fontWeight ?. toString ( )
207216 ) ;
217+
218+ let updatedValue = setImmutably (
219+ value ,
220+ [ 'typography' , 'fontFamily' ] ,
221+ newFontFamily
222+ ) ;
223+
224+ // Update font style/weight if they're not available in the new font family.
225+ if ( ! hasFontStyle || ! hasFontWeight ) {
226+ const { nearestFontStyle, nearestFontWeight } =
227+ findNearestStyleAndWeight (
228+ newFontFamilyFaces ,
229+ fontStyle ,
230+ fontWeight
231+ ) ;
232+ if ( nearestFontStyle || nearestFontWeight ) {
233+ // Update to the nearest available font style/weight in the new font family.
234+ updatedValue = {
235+ ...updatedValue ,
236+ typography : {
237+ ...updatedValue ?. typography ,
238+ fontStyle : nearestFontStyle || undefined ,
239+ fontWeight : nearestFontWeight || undefined ,
240+ } ,
241+ } ;
242+ } else if ( fontStyle || fontWeight ) {
243+ // Reset if no available styles/weights found.
244+ updatedValue = {
245+ ...updatedValue ,
246+ typography : {
247+ ...updatedValue ?. typography ,
248+ fontStyle : undefined ,
249+ fontWeight : undefined ,
250+ } ,
251+ } ;
252+ }
253+ }
254+
255+ onChange ( updatedValue ) ;
208256 } ;
209257 const hasFontFamily = ( ) => ! ! value ?. typography ?. fontFamily ;
210258 const resetFontFamily = ( ) => setFontFamily ( undefined ) ;
@@ -262,11 +310,6 @@ export default function TypographyPanel( {
262310 const hasFontWeights = settings ?. typography ?. fontWeight ;
263311 const fontStyle = decodeValue ( inheritedValue ?. typography ?. fontStyle ) ;
264312 const fontWeight = decodeValue ( inheritedValue ?. typography ?. fontWeight ) ;
265- const { nearestFontStyle, nearestFontWeight } = findNearestStyleAndWeight (
266- fontFamilyFaces ,
267- fontStyle ,
268- fontWeight
269- ) ;
270313 const setFontAppearance = useCallback (
271314 ( { fontStyle : newFontStyle , fontWeight : newFontWeight } ) => {
272315 // Only update the font style and weight if they have changed.
@@ -289,24 +332,6 @@ export default function TypographyPanel( {
289332 setFontAppearance ( { } ) ;
290333 } , [ setFontAppearance ] ) ;
291334
292- // Check if previous font style and weight values are available in the new font family.
293- useEffect ( ( ) => {
294- if ( nearestFontStyle && nearestFontWeight ) {
295- setFontAppearance ( {
296- fontStyle : nearestFontStyle ,
297- fontWeight : nearestFontWeight ,
298- } ) ;
299- } else {
300- // Reset font appearance if there are no available styles or weights.
301- resetFontAppearance ( ) ;
302- }
303- } , [
304- nearestFontStyle ,
305- nearestFontWeight ,
306- resetFontAppearance ,
307- setFontAppearance ,
308- ] ) ;
309-
310335 // Line Height
311336 const hasLineHeightEnabled = useHasLineHeightControl ( settings ) ;
312337 const lineHeight = decodeValue ( inheritedValue ?. typography ?. lineHeight ) ;
0 commit comments