-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
index.js
222 lines (202 loc) · 5.13 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* WordPress dependencies
*/
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',
},
];
/**
* 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.
*/
export const getFontAppearanceLabel = ( hasFontStyles, hasFontWeights ) => {
if ( ! hasFontStyles ) {
return __( 'Font weight' );
}
if ( ! hasFontWeights ) {
return __( 'Font style' );
}
return __( 'Appearance' );
};
/**
* Control to display unified font style and weight options.
*
* @param {Object} props Component props.
*
* @return {WPElement} Font appearance control.
*/
export default function FontAppearanceControl( props ) {
const {
onChange,
hasFontStyles = true,
hasFontWeights = true,
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 },
};
// Combines both font style and weight options into a single dropdown.
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,
},
} );
}
);
} );
return combinedOptions;
};
// Generates select options for font styles only.
const styleOptions = () => {
const combinedOptions = [ defaultOption ];
FONT_STYLES.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 ];
FONT_WEIGHTS.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( () => {
if ( hasFontStyles && hasFontWeights ) {
return combineOptions();
}
return hasFontStyles ? styleOptions() : weightOptions();
}, [ props.options ] );
// 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"
label={ label }
describedBy={ getDescribedBy() }
options={ selectOptions }
value={ currentSelection }
onChange={ ( { selectedItem } ) =>
onChange( selectedItem.style )
}
__nextUnconstrainedWidth
/>
)
);
}