@wordpress/edit-site 4.14.7 → 4.14.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,73 +4,59 @@
4
4
  * ---------------------------------------------------------------
5
5
  */
6
6
 
7
+ /**
8
+ * WordPress dependencies
9
+ */
10
+ import { getComputedFluidTypographyValue } from '@wordpress/block-editor';
11
+
12
+ /**
13
+ * @typedef {Object} FluidPreset
14
+ * @property {string|undefined} max A maximum font size value.
15
+ * @property {?string|undefined} min A minimum font size value.
16
+ */
17
+
18
+ /**
19
+ * @typedef {Object} Preset
20
+ * @property {?string|?number} size A default font size.
21
+ * @property {string} name A font size name, displayed in the UI.
22
+ * @property {string} slug A font size slug
23
+ * @property {boolean|FluidPreset|undefined} fluid A font size slug
24
+ */
25
+
7
26
  /**
8
27
  * Returns a font-size value based on a given font-size preset.
9
28
  * Takes into account fluid typography parameters and attempts to return a css formula depending on available, valid values.
10
29
  *
11
- * @param {Object} preset
12
- * @param {string} preset.size A default font size.
13
- * @param {string} preset.name A font size name, displayed in the UI.
14
- * @param {string} preset.slug A font size slug.
15
- * @param {Object} preset.fluid
16
- * @param {string|undefined} preset.fluid.max A maximum font size value.
17
- * @param {string|undefined} preset.fluid.min A minimum font size value.
18
- * @param {Object} typographySettings
19
- * @param {boolean} typographySettings.fluid Whether fluid typography is enabled.
30
+ * @param {Preset} preset
31
+ * @param {Object} typographySettings
32
+ * @param {boolean} typographySettings.fluid Whether fluid typography is enabled.
20
33
  *
21
- * @return {string} An font-size value
34
+ * @return {string|*} A font-size value or the value of preset.size.
22
35
  */
23
36
  export function getTypographyFontSizeValue( preset, typographySettings ) {
24
37
  const { size: defaultSize } = preset;
25
38
 
26
- if ( true !== typographySettings?.fluid ) {
39
+ /*
40
+ * Catches falsy values and 0/'0'.
41
+ * Fluid calculations cannot be performed on 0.
42
+ */
43
+ if ( ! defaultSize || '0' === defaultSize ) {
27
44
  return defaultSize;
28
45
  }
29
46
 
30
- // Defaults.
31
- const DEFAULT_MAXIMUM_VIEWPORT_WIDTH = '1600px';
32
- const DEFAULT_MINIMUM_VIEWPORT_WIDTH = '768px';
33
- const DEFAULT_MINIMUM_FONT_SIZE_FACTOR = 0.75;
34
- const DEFAULT_MAXIMUM_FONT_SIZE_FACTOR = 1.5;
35
- const DEFAULT_SCALE_FACTOR = 1;
36
-
37
- // Font sizes.
38
- // A font size has explicitly bypassed fluid calculations.
39
- if ( false === preset?.fluid ) {
47
+ if ( true !== typographySettings?.fluid ) {
40
48
  return defaultSize;
41
49
  }
42
50
 
43
- const fluidFontSizeSettings = preset?.fluid || {};
44
-
45
- // Try to grab explicit min and max fluid font sizes.
46
- let minimumFontSizeRaw = fluidFontSizeSettings?.min;
47
- let maximumFontSizeRaw = fluidFontSizeSettings?.max;
48
- const preferredSize = getTypographyValueAndUnit( defaultSize );
49
-
50
- // Protect against unsupported units.
51
- if ( ! preferredSize?.unit ) {
51
+ // A font size has explicitly bypassed fluid calculations.
52
+ if ( false === preset?.fluid ) {
52
53
  return defaultSize;
53
54
  }
54
55
 
55
- // If no fluid min or max font sizes are available, create some using min/max font size factors.
56
- if ( ! minimumFontSizeRaw ) {
57
- minimumFontSizeRaw =
58
- preferredSize.value * DEFAULT_MINIMUM_FONT_SIZE_FACTOR +
59
- preferredSize.unit;
60
- }
61
-
62
- if ( ! maximumFontSizeRaw ) {
63
- maximumFontSizeRaw =
64
- preferredSize.value * DEFAULT_MAXIMUM_FONT_SIZE_FACTOR +
65
- preferredSize.unit;
66
- }
67
-
68
56
  const fluidFontSizeValue = getComputedFluidTypographyValue( {
69
- maximumViewPortWidth: DEFAULT_MAXIMUM_VIEWPORT_WIDTH,
70
- minimumViewPortWidth: DEFAULT_MINIMUM_VIEWPORT_WIDTH,
71
- maximumFontSize: maximumFontSizeRaw,
72
- minimumFontSize: minimumFontSizeRaw,
73
- scaleFactor: DEFAULT_SCALE_FACTOR,
57
+ minimumFontSize: preset?.fluid?.min,
58
+ maximumFontSize: preset?.fluid?.max,
59
+ fontSize: defaultSize,
74
60
  } );
75
61
 
76
62
  if ( !! fluidFontSizeValue ) {
@@ -79,150 +65,3 @@ export function getTypographyFontSizeValue( preset, typographySettings ) {
79
65
 
80
66
  return defaultSize;
81
67
  }
82
-
83
- /**
84
- * Internal implementation of clamp() based on available min/max viewport width, and min/max font sizes.
85
- *
86
- * @param {Object} args
87
- * @param {string} args.maximumViewPortWidth Maximum size up to which type will have fluidity.
88
- * @param {string} args.minimumViewPortWidth Minimum viewport size from which type will have fluidity.
89
- * @param {string} args.maximumFontSize Maximum font size for any clamp() calculation.
90
- * @param {string} args.minimumFontSize Minimum font size for any clamp() calculation.
91
- * @param {number} args.scaleFactor A scale factor to determine how fast a font scales within boundaries.
92
- *
93
- * @return {string|null} A font-size value using clamp().
94
- */
95
- export function getComputedFluidTypographyValue( {
96
- maximumViewPortWidth,
97
- minimumViewPortWidth,
98
- maximumFontSize,
99
- minimumFontSize,
100
- scaleFactor,
101
- } ) {
102
- // Grab the minimum font size and normalize it in order to use the value for calculations.
103
- const minimumFontSizeParsed = getTypographyValueAndUnit( minimumFontSize );
104
-
105
- // We get a 'preferred' unit to keep units consistent when calculating,
106
- // otherwise the result will not be accurate.
107
- const fontSizeUnit = minimumFontSizeParsed?.unit || 'rem';
108
-
109
- // Grab the maximum font size and normalize it in order to use the value for calculations.
110
- const maximumFontSizeParsed = getTypographyValueAndUnit( maximumFontSize, {
111
- coerceTo: fontSizeUnit,
112
- } );
113
-
114
- // Protect against unsupported units.
115
- if ( ! minimumFontSizeParsed || ! maximumFontSizeParsed ) {
116
- return null;
117
- }
118
-
119
- // Use rem for accessible fluid target font scaling.
120
- const minimumFontSizeRem = getTypographyValueAndUnit( minimumFontSize, {
121
- coerceTo: 'rem',
122
- } );
123
-
124
- // Viewport widths defined for fluid typography. Normalize units
125
- const maximumViewPortWidthParsed = getTypographyValueAndUnit(
126
- maximumViewPortWidth,
127
- { coerceTo: fontSizeUnit }
128
- );
129
- const minumumViewPortWidthParsed = getTypographyValueAndUnit(
130
- minimumViewPortWidth,
131
- { coerceTo: fontSizeUnit }
132
- );
133
-
134
- // Protect against unsupported units.
135
- if (
136
- ! maximumViewPortWidthParsed ||
137
- ! minumumViewPortWidthParsed ||
138
- ! minimumFontSizeRem
139
- ) {
140
- return null;
141
- }
142
-
143
- // Build CSS rule.
144
- // Borrowed from https://websemantics.uk/tools/responsive-font-calculator/.
145
- const minViewPortWidthOffsetValue = roundToPrecision(
146
- minumumViewPortWidthParsed.value / 100,
147
- 3
148
- );
149
-
150
- const viewPortWidthOffset = minViewPortWidthOffsetValue + fontSizeUnit;
151
- let linearFactor =
152
- 100 *
153
- ( ( maximumFontSizeParsed.value - minimumFontSizeParsed.value ) /
154
- ( maximumViewPortWidthParsed.value -
155
- minumumViewPortWidthParsed.value ) );
156
- linearFactor = roundToPrecision( linearFactor, 3 ) || 1;
157
- const linearFactorScaled = linearFactor * scaleFactor;
158
- const fluidTargetFontSize = `${ minimumFontSizeRem.value }${ minimumFontSizeRem.unit } + ((1vw - ${ viewPortWidthOffset }) * ${ linearFactorScaled })`;
159
-
160
- return `clamp(${ minimumFontSize }, ${ fluidTargetFontSize }, ${ maximumFontSize })`;
161
- }
162
-
163
- /**
164
- *
165
- * @param {string} rawValue Raw size value from theme.json.
166
- * @param {Object|undefined} options Calculation options.
167
- *
168
- * @return {{ unit: string, value: number }|null} An object consisting of `'value'` and `'unit'` properties.
169
- */
170
- export function getTypographyValueAndUnit( rawValue, options = {} ) {
171
- if ( ! rawValue ) {
172
- return null;
173
- }
174
-
175
- const { coerceTo, rootSizeValue, acceptableUnits } = {
176
- coerceTo: '',
177
- // Default browser font size. Later we could inject some JS to compute this `getComputedStyle( document.querySelector( "html" ) ).fontSize`.
178
- rootSizeValue: 16,
179
- acceptableUnits: [ 'rem', 'px', 'em' ],
180
- ...options,
181
- };
182
-
183
- const acceptableUnitsGroup = acceptableUnits?.join( '|' );
184
- const regexUnits = new RegExp(
185
- `^(\\d*\\.?\\d+)(${ acceptableUnitsGroup }){1,1}$`
186
- );
187
-
188
- const matches = rawValue.match( regexUnits );
189
-
190
- // We need a number value and a unit.
191
- if ( ! matches || matches.length < 3 ) {
192
- return null;
193
- }
194
-
195
- let [ , value, unit ] = matches;
196
-
197
- let returnValue = parseFloat( value );
198
-
199
- if ( 'px' === coerceTo && ( 'em' === unit || 'rem' === unit ) ) {
200
- returnValue = returnValue * rootSizeValue;
201
- unit = coerceTo;
202
- }
203
-
204
- if ( 'px' === unit && ( 'em' === coerceTo || 'rem' === coerceTo ) ) {
205
- returnValue = returnValue / rootSizeValue;
206
- unit = coerceTo;
207
- }
208
-
209
- return {
210
- value: returnValue,
211
- unit,
212
- };
213
- }
214
-
215
- /**
216
- * Returns a value rounded to defined precision.
217
- * Returns `undefined` if the value is not a valid finite number.
218
- *
219
- * @param {number} value Raw value.
220
- * @param {number} digits The number of digits to appear after the decimal point
221
- *
222
- * @return {number|undefined} Value rounded to standard precision.
223
- */
224
- export function roundToPrecision( value, digits = 3 ) {
225
- return Number.isFinite( value )
226
- ? parseFloat( value.toFixed( digits ) )
227
- : undefined;
228
- }
@@ -24,6 +24,7 @@ import {
24
24
  * Internal dependencies
25
25
  */
26
26
  import { PRESET_METADATA, ROOT_BLOCK_SELECTOR, scopeSelector } from './utils';
27
+ import { getTypographyFontSizeValue } from './typography-utils';
27
28
  import { GlobalStylesContext } from './context';
28
29
  import { useSetting } from './hooks';
29
30
 
@@ -276,6 +277,21 @@ export function getStylesDeclarations(
276
277
  }
277
278
  }
278
279
 
280
+ // Calculate fluid typography rules where available.
281
+ if ( cssProperty === 'font-size' ) {
282
+ /*
283
+ * getTypographyFontSizeValue() will check
284
+ * if fluid typography has been activated and also
285
+ * whether the incoming value can be converted to a fluid value.
286
+ * Values that already have a "clamp()" function will not pass the test,
287
+ * and therefore the original $value will be returned.
288
+ */
289
+ ruleValue = getTypographyFontSizeValue(
290
+ { size: ruleValue },
291
+ tree?.settings?.typography
292
+ );
293
+ }
294
+
279
295
  output.push( `${ cssProperty }: ${ ruleValue }` );
280
296
  } );
281
297