-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.js
166 lines (150 loc) · 4.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/**
* WordPress dependencies
*/
import { CustomSelectControl } from '@wordpress/components';
import { useMemo } from '@wordpress/element';
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
* are disabled.
*
* @param {boolean} hasFontStyles Whether font styles are enabled and present.
* @param {boolean} hasFontWeights Whether font weights are enabled and present.
* @return {string} A label representing what font appearance is being edited.
*/
const getFontAppearanceLabel = ( hasFontStyles, hasFontWeights ) => {
if ( ! hasFontStyles ) {
return __( 'Font weight' );
}
if ( ! hasFontWeights ) {
return __( 'Font style' );
}
return __( 'Appearance' );
};
/**
* Control to display font style and weight options of the active font.
*
* @param {Object} props Component props.
*
* @return {Element} Font appearance control.
*/
export default function FontAppearanceControl( props ) {
const {
/** Start opting into the larger default height that will become the default size in a future version. */
__next40pxDefaultSize = false,
onChange,
hasFontStyles = true,
hasFontWeights = true,
fontFamilyFaces,
value: { fontStyle, fontWeight },
...otherProps
} = props;
const hasStylesOrWeights = hasFontStyles || hasFontWeights;
const label = getFontAppearanceLabel( hasFontStyles, hasFontWeights );
const defaultOption = {
key: 'default',
name: __( 'Default' ),
style: { fontStyle: undefined, fontWeight: undefined },
};
const { fontStyles, fontWeights, combinedStyleAndWeightOptions } =
getFontStylesAndWeights( fontFamilyFaces );
// Generates select options for combined font styles and weights.
const combineOptions = () => {
const combinedOptions = [ defaultOption ];
if ( combinedStyleAndWeightOptions ) {
combinedOptions.push( ...combinedStyleAndWeightOptions );
}
return combinedOptions;
};
// Generates select options for font styles only.
const styleOptions = () => {
const combinedOptions = [ defaultOption ];
fontStyles.forEach( ( { name, value } ) => {
combinedOptions.push( {
key: value,
name,
style: { fontStyle: value, fontWeight: undefined },
} );
} );
return combinedOptions;
};
// Generates select options for font weights only.
const weightOptions = () => {
const combinedOptions = [ defaultOption ];
fontWeights.forEach( ( { name, value } ) => {
combinedOptions.push( {
key: value,
name,
style: { fontStyle: undefined, fontWeight: value },
} );
} );
return combinedOptions;
};
// 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,
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.
const currentSelection =
selectOptions.find(
( option ) =>
option.style.fontStyle === fontStyle &&
option.style.fontWeight === fontWeight
) || selectOptions[ 0 ];
// Adjusts screen reader description based on styles or weights.
const getDescribedBy = () => {
if ( ! currentSelection ) {
return __( 'No selected font appearance' );
}
if ( ! hasFontStyles ) {
return sprintf(
// translators: %s: Currently selected font weight.
__( 'Currently selected font weight: %s' ),
currentSelection.name
);
}
if ( ! hasFontWeights ) {
return sprintf(
// translators: %s: Currently selected font style.
__( 'Currently selected font style: %s' ),
currentSelection.name
);
}
return sprintf(
// translators: %s: Currently selected font appearance.
__( 'Currently selected font appearance: %s' ),
currentSelection.name
);
};
return (
hasStylesOrWeights && (
<CustomSelectControl
{ ...otherProps }
className="components-font-appearance-control"
__next40pxDefaultSize={ __next40pxDefaultSize }
label={ label }
describedBy={ getDescribedBy() }
options={ selectOptions }
value={ currentSelection }
onChange={ ( { selectedItem } ) =>
onChange( selectedItem.style )
}
/>
)
);
}