-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
utils.js
283 lines (258 loc) · 7.26 KB
/
utils.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/**
* External dependencies
*/
import { isEmpty } from 'lodash';
/**
* WordPress dependencies
*/
import { __, _x } from '@wordpress/i18n';
import { Platform } from '@wordpress/element';
const isWeb = Platform.OS === 'web';
const allUnits = {
px: {
value: 'px',
label: isWeb ? 'px' : __( 'Pixels (px)' ),
default: '',
a11yLabel: __( 'Pixels (px)' ),
step: 1,
},
percent: {
value: '%',
label: isWeb ? '%' : __( 'Percentage (%)' ),
default: '',
a11yLabel: __( 'Percent (%)' ),
step: 0.1,
},
em: {
value: 'em',
label: isWeb ? 'em' : __( 'Relative to parent font size (em)' ),
default: '',
a11yLabel: _x( 'ems', 'Relative to parent font size (em)' ),
step: 0.01,
},
rem: {
value: 'rem',
label: isWeb ? 'rem' : __( 'Relative to root font size (rem)' ),
default: '',
a11yLabel: _x( 'rems', 'Relative to root font size (rem)' ),
step: 0.01,
},
vw: {
value: 'vw',
label: isWeb ? 'vw' : __( 'Viewport width (vw)' ),
default: '',
a11yLabel: __( 'Viewport width (vw)' ),
step: 0.1,
},
vh: {
value: 'vh',
label: isWeb ? 'vh' : __( 'Viewport height (vh)' ),
default: '',
a11yLabel: __( 'Viewport height (vh)' ),
step: 0.1,
},
vmin: {
value: 'vmin',
label: isWeb ? 'vmin' : __( 'Viewport smallest dimension (vmin)' ),
default: '',
a11yLabel: __( 'Viewport smallest dimension (vmin)' ),
step: 0.1,
},
vmax: {
value: 'vmax',
label: isWeb ? 'vmax' : __( 'Viewport largest dimension (vmax)' ),
default: '',
a11yLabel: __( 'Viewport largest dimension (vmax)' ),
step: 0.1,
},
ch: {
value: 'ch',
label: isWeb ? 'ch' : __( 'Width of the zero (0) character (ch)' ),
default: '',
a11yLabel: __( 'Width of the zero (0) character (ch)' ),
step: 0.01,
},
ex: {
value: 'ex',
label: isWeb ? 'ex' : __( 'x-height of the font (ex)' ),
default: '',
a11yLabel: __( 'x-height of the font (ex)' ),
step: 0.01,
},
cm: {
value: 'cm',
label: isWeb ? 'cm' : __( 'Centimeters (cm)' ),
default: '',
a11yLabel: __( 'Centimeters (cm)' ),
step: 0.001,
},
mm: {
value: 'mm',
label: isWeb ? 'mm' : __( 'Millimeters (mm)' ),
default: '',
a11yLabel: __( 'Millimeters (mm)' ),
step: 0.1,
},
in: {
value: 'in',
label: isWeb ? 'in' : __( 'Inches (in)' ),
default: '',
a11yLabel: __( 'Inches (in)' ),
step: 0.001,
},
pc: {
value: 'pc',
label: isWeb ? 'pc' : __( 'Picas (pc)' ),
default: '',
a11yLabel: __( 'Picas (pc)' ),
step: 1,
},
pt: {
value: 'pt',
label: isWeb ? 'pt' : __( 'Points (pt)' ),
default: '',
a11yLabel: __( 'Points (pt)' ),
step: 1,
},
};
/**
* An array of all available CSS length units.
*/
export const ALL_CSS_UNITS = Object.values( allUnits );
/**
* Units of measurements. `a11yLabel` is used by screenreaders.
*/
export const CSS_UNITS = [
allUnits.px,
allUnits.percent,
allUnits.em,
allUnits.rem,
allUnits.vw,
allUnits.vh,
];
export const DEFAULT_UNIT = allUnits.px;
/**
* Handles legacy value + unit handling.
* This component use to manage both incoming value and units separately.
*
* Moving forward, ideally the value should be a string that contains both
* the value and unit, example: '10px'
*
* @param {number|string} value Value
* @param {string} unit Unit value
* @param {Array<Object>} units Units to derive from.
* @return {Array<number, string>} The extracted number and unit.
*/
export function getParsedValue( value, unit, units ) {
const initialValue = unit ? `${ value }${ unit }` : value;
return parseUnit( initialValue, units );
}
/**
* Checks if units are defined.
*
* @param {any} units Units to check.
* @return {boolean} Whether units are defined.
*/
export function hasUnits( units ) {
return ! isEmpty( units ) && units !== false;
}
/**
* Parses a number and unit from a value.
*
* @param {string} initialValue Value to parse
* @param {Array<Object>} units Units to derive from.
* @return {Array<number, string>} The extracted number and unit.
*/
export function parseUnit( initialValue, units = ALL_CSS_UNITS ) {
const value = String( initialValue ).trim();
let num = parseFloat( value, 10 );
num = isNaN( num ) ? '' : num;
const unitMatch = value.match( /[\d.\-\+]*\s*(.*)/ )[ 1 ];
let unit = unitMatch !== undefined ? unitMatch : '';
unit = unit.toLowerCase();
if ( hasUnits( units ) ) {
const match = units.find( ( item ) => item.value === unit );
unit = match?.value;
} else {
unit = DEFAULT_UNIT.value;
}
return [ num, unit ];
}
/**
* Parses a number and unit from a value. Validates parsed value, using fallback
* value if invalid.
*
* @param {number|string} next The next value.
* @param {Array<Object>} units Units to derive from.
* @param {number|string} fallbackValue The fallback value.
* @param {string} fallbackUnit The fallback value.
* @return {Array<number, string>} The extracted number and unit.
*/
export function getValidParsedUnit( next, units, fallbackValue, fallbackUnit ) {
const [ parsedValue, parsedUnit ] = parseUnit( next, units );
let baseValue = parsedValue;
let baseUnit;
if ( isNaN( parsedValue ) || parsedValue === '' ) {
baseValue = fallbackValue;
}
baseUnit = parsedUnit || fallbackUnit;
/**
* If no unit is found, attempt to use the first value from the collection
* of units as a default fallback.
*/
if ( hasUnits( units ) && ! baseUnit ) {
baseUnit = units[ 0 ]?.value;
}
return [ baseValue, baseUnit ];
}
/**
* Takes a unit value and finds the matching accessibility label for the
* unit abbreviation.
*
* @param {string} unit Unit value (example: px)
* @return {string} a11y label for the unit abbreviation
*/
export function parseA11yLabelForUnit( unit ) {
const match = ALL_CSS_UNITS.find( ( item ) => item.value === unit );
return match?.a11yLabel ? match?.a11yLabel : match?.value;
}
/**
* Filters available units based on values defined by settings.
*
* @param {Array} settings Collection of preferred units.
* @param {Array} units Collection of available units.
*
* @return {Array} Filtered units based on settings.
*/
export function filterUnitsWithSettings( settings = [], units = [] ) {
return units.filter( ( unit ) => {
return settings.includes( unit.value );
} );
}
/**
* Custom hook to retrieve and consolidate units setting from add_theme_support().
* TODO: ideally this hook shouldn't be needed
* https://github.com/WordPress/gutenberg/pull/31822#discussion_r633280823
*
* @param {Object} args An object containing units, settingPath & defaultUnits.
* @param {Array<Object>|undefined} args.units Collection of available units.
* @param {Array<string>|undefined} args.availableUnits The setting path. Defaults to 'spacing.units'.
* @param {Object|undefined} args.defaultValues Collection of default values for defined units. Example: { px: '350', em: '15' }.
*
* @return {Array|boolean} Filtered units based on settings.
*/
export const useCustomUnits = ( { units, availableUnits, defaultValues } ) => {
units = units || ALL_CSS_UNITS;
const usedUnits = filterUnitsWithSettings(
! availableUnits ? [] : availableUnits,
units
);
if ( defaultValues ) {
usedUnits.forEach( ( unit, i ) => {
if ( defaultValues[ unit.value ] ) {
usedUnits[ i ].default = defaultValues[ unit.value ];
}
} );
}
return usedUnits.length === 0 ? false : usedUnits;
};