Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use available font weights and styles in FontAppearanceControl #61915

Merged
merged 46 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
707ee73
Pass active font faces to appearance control component
mikachan May 23, 2024
c733432
Add formatFontWeight function
mikachan May 23, 2024
42811bd
Use only available font weights in FontAppearanceControl
mikachan May 23, 2024
a4ad7f9
Refactor weight and style array fallbacks
mikachan May 23, 2024
ead32c3
Rename normal to regular in styles list
mikachan May 23, 2024
861cd72
Make font weight labels translatable
mikachan May 23, 2024
af44839
Handle system fonts
mikachan May 25, 2024
af005a5
Add comment for font weight and style options
mikachan May 25, 2024
52802aa
Check against fontFamily rather than name
mikachan May 25, 2024
2811262
Handle font style names and values similar to font weights
mikachan May 25, 2024
a1bc596
Use some() rather than findIndex()
mikachan May 25, 2024
ca26c60
Merge branch 'trunk' into fix/font-weight-list
mikachan May 29, 2024
2d42dc4
Add getMergedFontFamiliesAndFontFamilyFaces function
mikachan May 30, 2024
b4fa5eb
Add tests for getMergedFontFamiliesAndFontFamilyFaces
mikachan May 30, 2024
672ae3a
Merge common case statements
mikachan May 30, 2024
dc60306
Remove toUpperCase() on font style names
mikachan May 30, 2024
3ba9ce8
Attempt to fix variable fonts options
mikachan May 30, 2024
15739d8
Fix formatFontStyle test
mikachan May 30, 2024
7677b53
Trim any surrounding whitespace from fontweight string
mikachan May 31, 2024
362d1c7
Add tests for normal and bold font weights
mikachan May 31, 2024
aba764d
Trim font weight before checking for spaces
mikachan May 31, 2024
e6d2e67
Create getFontStylesAndWeights function
mikachan May 31, 2024
7bd07ab
Move trim into if statement and improve comment
mikachan May 31, 2024
48eac26
Allow all uncombined weights and styles to be returned from getFontSt…
mikachan Jun 3, 2024
8ffd9b2
Make option key consistent with combined result
mikachan Jun 4, 2024
6b36ff0
Move combined option logic into getFontStylesAndWeights()
mikachan Jun 4, 2024
1ca996b
Swap ABeeZee test for Piazzolla example
mikachan Jun 4, 2024
ac2991f
Merge branch 'trunk' into fix/font-weight-list
mikachan Jun 16, 2024
29f6fcb
Fix isSystemFont logic
mikachan Jun 18, 2024
7a96507
Apply available font styles and weights when fontFamily changes
mikachan Jun 21, 2024
19a1e11
Merge branch 'trunk' into fix/font-weight-list
mikachan Jun 21, 2024
150e3a9
Swap isSystemFont logic around
mikachan Jun 21, 2024
f0b23cf
Merge branch 'trunk' into fix/font-weight-list
mikachan Jun 24, 2024
a55bf4f
Merge branch 'trunk' into fix/font-weight-list
mikachan Jun 24, 2024
97cbbad
Export isSystemFont and isVariableFont from getFontStylesAndWeights
mikachan Jun 24, 2024
ec2cf86
Use getFontStylesAndWeights to compare font face values
mikachan Jun 24, 2024
fa55510
Merge branch 'trunk' into fix/font-weight-list
mikachan Jun 25, 2024
ce08121
Improve comments for getFontStylesAndWeights
mikachan Jun 26, 2024
541010c
Improve test wording
mikachan Jun 26, 2024
da9f221
Add todo item
mikachan Jun 26, 2024
d359d9e
Include faux bold and italic as options
mikachan Jun 28, 2024
0ece8f5
Handle variable font weights
mikachan Jun 28, 2024
ad00060
Try to set the nearest available font weight
mikachan Jun 28, 2024
d7bba77
Only adds faux bold if a weight of 600 or above is not already available
creativecoder Jun 28, 2024
f6aef40
Updates tests with new faux bold logic
creativecoder Jun 28, 2024
f871c01
Merge branch 'trunk' into fix/font-weight-list
creativecoder Jun 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,57 +3,12 @@
*/
import { CustomSelectControl } from '@wordpress/components';
import { useMemo } from '@wordpress/element';
import { __, _x, sprintf } from '@wordpress/i18n';

const FONT_STYLES = [
{
name: _x( 'Regular', 'font style' ),
value: 'normal',
},
{
name: _x( 'Italic', 'font style' ),
value: 'italic',
},
];

const FONT_WEIGHTS = [
{
name: _x( 'Thin', 'font weight' ),
value: '100',
},
{
name: _x( 'Extra Light', 'font weight' ),
value: '200',
},
{
name: _x( 'Light', 'font weight' ),
value: '300',
},
{
name: _x( 'Regular', 'font weight' ),
value: '400',
},
{
name: _x( 'Medium', 'font weight' ),
value: '500',
},
{
name: _x( 'Semi Bold', 'font weight' ),
value: '600',
},
{
name: _x( 'Bold', 'font weight' ),
value: '700',
},
{
name: _x( 'Extra Bold', 'font weight' ),
value: '800',
},
{
name: _x( 'Black', 'font weight' ),
value: '900',
},
];
import { __, sprintf } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { getFontStylesAndWeights } from '../../utils/get-font-styles-and-weights';

/**
* Adjusts font appearance field label in case either font styles or weights
Expand All @@ -76,7 +31,7 @@ const getFontAppearanceLabel = ( hasFontStyles, hasFontWeights ) => {
};

/**
* Control to display unified font style and weight options.
* Control to display font style and weight options of the active font.
*
* @param {Object} props Component props.
*
Expand All @@ -87,6 +42,7 @@ export default function FontAppearanceControl( props ) {
onChange,
hasFontStyles = true,
hasFontWeights = true,
fontFamilyFaces,
value: { fontStyle, fontWeight },
...otherProps
} = props;
Expand All @@ -97,43 +53,22 @@ export default function FontAppearanceControl( props ) {
name: __( 'Default' ),
style: { fontStyle: undefined, fontWeight: undefined },
};
const { fontStyles, fontWeights, combinedStyleAndWeightOptions } =
getFontStylesAndWeights( fontFamilyFaces );

// Combines both font style and weight options into a single dropdown.
// Generates select options for combined font styles and weights.
const combineOptions = () => {
const combinedOptions = [ defaultOption ];

FONT_STYLES.forEach( ( { name: styleName, value: styleValue } ) => {
FONT_WEIGHTS.forEach(
( { name: weightName, value: weightValue } ) => {
const optionName =
styleValue === 'normal'
? weightName
: sprintf(
/* translators: 1: Font weight name. 2: Font style name. */
__( '%1$s %2$s' ),
weightName,
styleName
);

combinedOptions.push( {
key: `${ styleValue }-${ weightValue }`,
name: optionName,
style: {
fontStyle: styleValue,
fontWeight: weightValue,
},
} );
}
);
} );

if ( combinedStyleAndWeightOptions ) {
combinedOptions.push( ...combinedStyleAndWeightOptions );
}
return combinedOptions;
};

// Generates select options for font styles only.
const styleOptions = () => {
const combinedOptions = [ defaultOption ];
FONT_STYLES.forEach( ( { name, value } ) => {
fontStyles.forEach( ( { name, value } ) => {
combinedOptions.push( {
key: value,
name,
Expand All @@ -146,7 +81,7 @@ export default function FontAppearanceControl( props ) {
// Generates select options for font weights only.
const weightOptions = () => {
const combinedOptions = [ defaultOption ];
FONT_WEIGHTS.forEach( ( { name, value } ) => {
fontWeights.forEach( ( { name, value } ) => {
combinedOptions.push( {
key: value,
name,
Expand All @@ -158,12 +93,19 @@ export default function FontAppearanceControl( props ) {

// Map font styles and weights to select options.
const selectOptions = useMemo( () => {
// Display combined available font style and weight options.
if ( hasFontStyles && hasFontWeights ) {
return combineOptions();
}

// Display only font style options or font weight options.
return hasFontStyles ? styleOptions() : weightOptions();
}, [ props.options ] );
}, [
props.options,
fontStyles,
fontWeights,
combinedStyleAndWeightOptions,
] );

// Find current selection by comparing font style & weight against options,
// and fall back to the Default option if there is no matching option.
Expand Down
Loading
Loading