@wordpress/style-engine 0.9.0 → 0.12.0

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.
Files changed (46) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +38 -6
  3. package/build/styles/border/index.js +94 -0
  4. package/build/styles/border/index.js.map +1 -0
  5. package/build/styles/index.js +3 -1
  6. package/build/styles/index.js.map +1 -1
  7. package/build/styles/spacing/margin.js +4 -1
  8. package/build/styles/spacing/margin.js.map +1 -1
  9. package/build/styles/spacing/padding.js +4 -1
  10. package/build/styles/spacing/padding.js.map +1 -1
  11. package/build/styles/utils.js +26 -10
  12. package/build/styles/utils.js.map +1 -1
  13. package/build-module/styles/border/index.js +85 -0
  14. package/build-module/styles/border/index.js.map +1 -0
  15. package/build-module/styles/index.js +2 -1
  16. package/build-module/styles/index.js.map +1 -1
  17. package/build-module/styles/spacing/margin.js +4 -1
  18. package/build-module/styles/spacing/margin.js.map +1 -1
  19. package/build-module/styles/spacing/padding.js +4 -1
  20. package/build-module/styles/spacing/padding.js.map +1 -1
  21. package/build-module/styles/utils.js +25 -11
  22. package/build-module/styles/utils.js.map +1 -1
  23. package/build-types/styles/border/index.d.ts +10 -0
  24. package/build-types/styles/border/index.d.ts.map +1 -0
  25. package/build-types/styles/index.d.ts +3 -7
  26. package/build-types/styles/index.d.ts.map +1 -1
  27. package/build-types/styles/spacing/margin.d.ts.map +1 -1
  28. package/build-types/styles/spacing/padding.d.ts.map +1 -1
  29. package/build-types/styles/utils.d.ts +16 -7
  30. package/build-types/styles/utils.d.ts.map +1 -1
  31. package/build-types/types.d.ts +26 -1
  32. package/build-types/types.d.ts.map +1 -1
  33. package/class-wp-style-engine-css-declarations.php +119 -0
  34. package/class-wp-style-engine.php +322 -108
  35. package/package.json +2 -2
  36. package/phpunit/class-wp-style-engine-css-declarations-test.php +108 -0
  37. package/phpunit/class-wp-style-engine-test.php +296 -12
  38. package/src/styles/border/index.ts +145 -0
  39. package/src/styles/index.ts +7 -1
  40. package/src/styles/spacing/margin.ts +4 -6
  41. package/src/styles/spacing/padding.ts +4 -6
  42. package/src/styles/utils.ts +36 -12
  43. package/src/test/index.js +51 -0
  44. package/src/test/utils.js +12 -0
  45. package/src/types.ts +33 -1
  46. package/tsconfig.tsbuildinfo +1 -1
@@ -34,87 +34,189 @@ class WP_Style_Engine {
34
34
  * Style definitions that contain the instructions to
35
35
  * parse/output valid Gutenberg styles from a block's attributes.
36
36
  * For every style definition, the follow properties are valid:
37
- * - classnames => an array of classnames to be returned for block styles. The key is a classname or pattern.
38
- * A value of `true` means the classname should be applied always. Otherwise a valid CSS property
39
- * to match the incoming value, e.g., "color" to match var:preset|color|somePresetName.
40
- * - property_key => the key that represents a valid CSS property, e.g., "margin" or "border".
41
- * - path => a path that accesses the corresponding style value in the block style object.
37
+ * - classnames => (array) an array of classnames to be returned for block styles. The key is a classname or pattern.
38
+ * A value of `true` means the classname should be applied always. Otherwise, a valid CSS property (string)
39
+ * to match the incoming value, e.g., "color" to match var:preset|color|somePresetSlug.
40
+ * - css_vars => (array) an array of key value pairs used to generate CSS var values. The key is a CSS var pattern, whose `$slug` fragment will be replaced with a preset slug.
41
+ * The value should be a valid CSS property (string) to match the incoming value, e.g., "color" to match var:preset|color|somePresetSlug.
42
+ * - property_keys => (array) array of keys whose values represent a valid CSS property, e.g., "margin" or "border".
43
+ * - path => (array) a path that accesses the corresponding style value in the block style object.
44
+ * - value_func => (string) the name of a function to generate a CSS definition array for a particular style object. The output of this function should be `array( "$property" => "$value", ... )`.
42
45
  */
43
46
  const BLOCK_STYLE_DEFINITIONS_METADATA = array(
44
47
  'color' => array(
45
48
  'text' => array(
46
- 'property_key' => 'color',
47
- 'path' => array( 'color', 'text' ),
48
- 'classnames' => array(
49
- 'has-text-color' => true,
50
- 'has-%s-color' => 'color',
49
+ 'property_keys' => array(
50
+ 'default' => 'color',
51
+ ),
52
+ 'path' => array( 'color', 'text' ),
53
+ 'css_vars' => array(
54
+ 'color' => '--wp--preset--color--$slug',
55
+ ),
56
+ 'classnames' => array(
57
+ 'has-text-color' => true,
58
+ 'has-$slug-color' => 'color',
51
59
  ),
52
60
  ),
53
61
  'background' => array(
54
- 'property_key' => 'background-color',
55
- 'path' => array( 'color', 'background' ),
56
- 'classnames' => array(
57
- 'has-background' => true,
58
- 'has-%s-background-color' => 'color',
62
+ 'property_keys' => array(
63
+ 'default' => 'background-color',
64
+ ),
65
+ 'path' => array( 'color', 'background' ),
66
+ 'classnames' => array(
67
+ 'has-background' => true,
68
+ 'has-$slug-background-color' => 'color',
59
69
  ),
60
70
  ),
61
71
  'gradient' => array(
62
- 'property_key' => 'background',
63
- 'path' => array( 'color', 'gradient' ),
64
- 'classnames' => array(
65
- 'has-background' => true,
66
- 'has-%s-gradient-background' => 'gradient',
72
+ 'property_keys' => array(
73
+ 'default' => 'background',
74
+ ),
75
+ 'path' => array( 'color', 'gradient' ),
76
+ 'classnames' => array(
77
+ 'has-background' => true,
78
+ 'has-$slug-gradient-background' => 'gradient',
79
+ ),
80
+ ),
81
+ ),
82
+ 'border' => array(
83
+ 'color' => array(
84
+ 'property_keys' => array(
85
+ 'default' => 'border-color',
86
+ 'individual' => 'border-%s-color',
87
+ ),
88
+ 'path' => array( 'border', 'color' ),
89
+ 'classnames' => array(
90
+ 'has-border-color' => true,
91
+ 'has-$slug-border-color' => 'color',
92
+ ),
93
+ ),
94
+ 'radius' => array(
95
+ 'property_keys' => array(
96
+ 'default' => 'border-radius',
97
+ 'individual' => 'border-%s-radius',
98
+ ),
99
+ 'path' => array( 'border', 'radius' ),
100
+ ),
101
+ 'style' => array(
102
+ 'property_keys' => array(
103
+ 'default' => 'border-style',
104
+ 'individual' => 'border-%s-style',
105
+ ),
106
+ 'path' => array( 'border', 'style' ),
107
+ ),
108
+ 'width' => array(
109
+ 'property_keys' => array(
110
+ 'default' => 'border-width',
111
+ 'individual' => 'border-%s-width',
112
+ ),
113
+ 'path' => array( 'border', 'width' ),
114
+ ),
115
+ 'top' => array(
116
+ 'value_func' => 'static::get_individual_property_css_declarations',
117
+ 'path' => array( 'border', 'top' ),
118
+ 'css_vars' => array(
119
+ 'color' => '--wp--preset--color--$slug',
120
+ ),
121
+ ),
122
+ 'right' => array(
123
+ 'value_func' => 'static::get_individual_property_css_declarations',
124
+ 'path' => array( 'border', 'right' ),
125
+ 'css_vars' => array(
126
+ 'color' => '--wp--preset--color--$slug',
127
+ ),
128
+ ),
129
+ 'bottom' => array(
130
+ 'value_func' => 'static::get_individual_property_css_declarations',
131
+ 'path' => array( 'border', 'bottom' ),
132
+ 'css_vars' => array(
133
+ 'color' => '--wp--preset--color--$slug',
134
+ ),
135
+ ),
136
+ 'left' => array(
137
+ 'value_func' => 'static::get_individual_property_css_declarations',
138
+ 'path' => array( 'border', 'left' ),
139
+ 'css_vars' => array(
140
+ 'color' => '--wp--preset--color--$slug',
67
141
  ),
68
142
  ),
69
143
  ),
70
144
  'spacing' => array(
71
145
  'padding' => array(
72
- 'property_key' => 'padding',
73
- 'path' => array( 'spacing', 'padding' ),
146
+ 'property_keys' => array(
147
+ 'default' => 'padding',
148
+ 'individual' => 'padding-%s',
149
+ ),
150
+ 'path' => array( 'spacing', 'padding' ),
151
+ 'css_vars' => array(
152
+ 'spacing' => '--wp--preset--spacing--$slug',
153
+ ),
74
154
  ),
75
155
  'margin' => array(
76
- 'property_key' => 'margin',
77
- 'path' => array( 'spacing', 'margin' ),
156
+ 'property_keys' => array(
157
+ 'default' => 'margin',
158
+ 'individual' => 'margin-%s',
159
+ ),
160
+ 'path' => array( 'spacing', 'margin' ),
161
+ 'css_vars' => array(
162
+ 'spacing' => '--wp--preset--spacing--$slug',
163
+ ),
78
164
  ),
79
165
  ),
80
166
  'typography' => array(
81
167
  'fontSize' => array(
82
- 'property_key' => 'font-size',
83
- 'path' => array( 'typography', 'fontSize' ),
84
- 'classnames' => array(
85
- 'has-%s-font-size' => 'font-size',
168
+ 'property_keys' => array(
169
+ 'default' => 'font-size',
170
+ ),
171
+ 'path' => array( 'typography', 'fontSize' ),
172
+ 'classnames' => array(
173
+ 'has-$slug-font-size' => 'font-size',
86
174
  ),
87
175
  ),
88
176
  'fontFamily' => array(
89
- 'property_key' => 'font-family',
90
- 'path' => array( 'typography', 'fontFamily' ),
91
- 'classnames' => array(
92
- 'has-%s-font-family' => 'font-family',
177
+ 'property_keys' => array(
178
+ 'default' => 'font-family',
179
+ ),
180
+ 'path' => array( 'typography', 'fontFamily' ),
181
+ 'classnames' => array(
182
+ 'has-$slug-font-family' => 'font-family',
93
183
  ),
94
184
  ),
95
185
  'fontStyle' => array(
96
- 'property_key' => 'font-style',
97
- 'path' => array( 'typography', 'fontStyle' ),
186
+ 'property_keys' => array(
187
+ 'default' => 'font-style',
188
+ ),
189
+ 'path' => array( 'typography', 'fontStyle' ),
98
190
  ),
99
191
  'fontWeight' => array(
100
- 'property_key' => 'font-weight',
101
- 'path' => array( 'typography', 'fontWeight' ),
192
+ 'property_keys' => array(
193
+ 'default' => 'font-weight',
194
+ ),
195
+ 'path' => array( 'typography', 'fontWeight' ),
102
196
  ),
103
197
  'lineHeight' => array(
104
- 'property_key' => 'line-height',
105
- 'path' => array( 'typography', 'lineHeight' ),
198
+ 'property_keys' => array(
199
+ 'default' => 'line-height',
200
+ ),
201
+ 'path' => array( 'typography', 'lineHeight' ),
106
202
  ),
107
203
  'textDecoration' => array(
108
- 'property_key' => 'text-decoration',
109
- 'path' => array( 'typography', 'textDecoration' ),
204
+ 'property_keys' => array(
205
+ 'default' => 'text-decoration',
206
+ ),
207
+ 'path' => array( 'typography', 'textDecoration' ),
110
208
  ),
111
209
  'textTransform' => array(
112
- 'property_key' => 'text-transform',
113
- 'path' => array( 'typography', 'textTransform' ),
210
+ 'property_keys' => array(
211
+ 'default' => 'text-transform',
212
+ ),
213
+ 'path' => array( 'typography', 'textTransform' ),
114
214
  ),
115
215
  'letterSpacing' => array(
116
- 'property_key' => 'letter-spacing',
117
- 'path' => array( 'typography', 'letterSpacing' ),
216
+ 'property_keys' => array(
217
+ 'default' => 'letter-spacing',
218
+ ),
219
+ 'path' => array( 'typography', 'letterSpacing' ),
118
220
  ),
119
221
  ),
120
222
  );
@@ -137,8 +239,8 @@ class WP_Style_Engine {
137
239
  /**
138
240
  * Extracts the slug in kebab case from a preset string, e.g., "heavenly-blue" from 'var:preset|color|heavenlyBlue'.
139
241
  *
140
- * @param string $style_value A single css preset value.
141
- * @param string $property_key The CSS property that is the second element of the preset string. Used for matching.
242
+ * @param string? $style_value A single css preset value.
243
+ * @param string $property_key The CSS property that is the second element of the preset string. Used for matching.
142
244
  *
143
245
  * @return string|null The slug, or null if not found.
144
246
  */
@@ -150,6 +252,47 @@ class WP_Style_Engine {
150
252
  return null;
151
253
  }
152
254
 
255
+ /**
256
+ * Generates a css var string, eg var(--wp--preset--color--background) from a preset string, eg. `var:preset|space|50`.
257
+ *
258
+ * @param string $style_value A single css preset value.
259
+ * @param array $css_vars The css var patterns used to generate the var string.
260
+ *
261
+ * @return string|null The css var, or null if no match for slug found.
262
+ */
263
+ protected static function get_css_var_value( $style_value, $css_vars ) {
264
+ foreach ( $css_vars as $property_key => $css_var_pattern ) {
265
+ $slug = static::get_slug_from_preset_value( $style_value, $property_key );
266
+ if ( $slug ) {
267
+ $var = strtr(
268
+ $css_var_pattern,
269
+ array( '$slug' => $slug )
270
+ );
271
+ return "var($var)";
272
+ }
273
+ }
274
+ return null;
275
+ }
276
+
277
+ /**
278
+ * Checks whether an incoming block style value is valid.
279
+ *
280
+ * @param string? $style_value A single css preset value.
281
+ *
282
+ * @return boolean
283
+ */
284
+ protected static function is_valid_style_value( $style_value ) {
285
+ if ( '0' === $style_value ) {
286
+ return true;
287
+ }
288
+
289
+ if ( empty( $style_value ) ) {
290
+ return false;
291
+ }
292
+
293
+ return true;
294
+ }
295
+
153
296
  /**
154
297
  * Returns classnames, and generates classname(s) from a CSS preset property pattern, e.g., 'var:preset|color|heavenly-blue'.
155
298
  *
@@ -160,6 +303,11 @@ class WP_Style_Engine {
160
303
  */
161
304
  protected static function get_classnames( $style_value, $style_definition ) {
162
305
  $classnames = array();
306
+
307
+ if ( empty( $style_value ) ) {
308
+ return $classnames;
309
+ }
310
+
163
311
  if ( ! empty( $style_definition['classnames'] ) ) {
164
312
  foreach ( $style_definition['classnames'] as $classname => $property_key ) {
165
313
  if ( true === $property_key ) {
@@ -173,7 +321,7 @@ class WP_Style_Engine {
173
321
  // One day, if there are no stored schemata, we could allow custom patterns or
174
322
  // generate classnames based on other properties
175
323
  // such as a path or a value or a prefix passed in options.
176
- $classnames[] = sprintf( $classname, $slug );
324
+ $classnames[] = strtr( $classname, array( '$slug' => $slug ) );
177
325
  }
178
326
  }
179
327
  }
@@ -182,75 +330,117 @@ class WP_Style_Engine {
182
330
  }
183
331
 
184
332
  /**
185
- * Returns CSS rules based on valid block style values.
333
+ * Returns an array of CSS declarations based on valid block style values.
186
334
  *
187
- * @param array $style_value A single raw style value from the generate() $block_styles array.
188
- * @param array<string> $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
335
+ * @param array $style_value A single raw style value from the generate() $block_styles array.
336
+ * @param array<string> $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
337
+ * @param boolean $should_skip_css_vars Whether to skip compiling CSS var values.
189
338
  *
190
- * @return array An array of CSS rules.
339
+ * @return array An array of CSS definitions, e.g., array( "$property" => "$value" ).
191
340
  */
192
- protected static function get_css( $style_value, $style_definition ) {
193
- // Low-specificity check to see if the value is a CSS preset.
341
+ protected static function get_css_declarations( $style_value, $style_definition, $should_skip_css_vars = false ) {
342
+ if (
343
+ isset( $style_definition['value_func'] ) &&
344
+ is_callable( $style_definition['value_func'] )
345
+ ) {
346
+ return call_user_func( $style_definition['value_func'], $style_value, $style_definition, $should_skip_css_vars );
347
+ }
348
+
349
+ $css_declarations = array();
350
+ $style_property_keys = $style_definition['property_keys'];
351
+
352
+ // Build CSS var values from var:? values, e.g, `var(--wp--css--rule-slug )`
353
+ // Check if the value is a CSS preset and there's a corresponding css_var pattern in the style definition.
194
354
  if ( is_string( $style_value ) && strpos( $style_value, 'var:' ) !== false ) {
195
- return array();
355
+ if ( ! $should_skip_css_vars && ! empty( $style_definition['css_vars'] ) ) {
356
+ $css_var = static::get_css_var_value( $style_value, $style_definition['css_vars'] );
357
+ if ( $css_var ) {
358
+ $css_declarations[ $style_property_keys['default'] ] = $css_var;
359
+ }
360
+ }
361
+ return $css_declarations;
362
+ }
363
+
364
+ // Default rule builder.
365
+ // If the input contains an array, assume box model-like properties
366
+ // for styles such as margins and padding.
367
+ if ( is_array( $style_value ) ) {
368
+ foreach ( $style_value as $key => $value ) {
369
+ if ( is_string( $value ) && strpos( $value, 'var:' ) !== false && ! $should_skip_css_vars && ! empty( $style_definition['css_vars'] ) ) {
370
+ $value = static::get_css_var_value( $value, $style_definition['css_vars'] );
371
+ }
372
+ $individual_property = sprintf( $style_property_keys['individual'], _wp_to_kebab_case( $key ) );
373
+ if ( static::is_valid_style_value( $style_value ) ) {
374
+ $css_declarations[ $individual_property ] = $value;
375
+ }
376
+ }
377
+ } else {
378
+ $css_declarations[ $style_property_keys['default'] ] = $style_value;
196
379
  }
197
380
 
198
- // If required in the future, style definitions could define a callable `value_func` to generate custom CSS rules.
199
- return static::get_css_rules( $style_value, $style_definition['property_key'] );
381
+ return $css_declarations;
200
382
  }
201
383
 
202
384
  /**
203
- * Returns an CSS ruleset.
204
- * Styles are bundled based on the instructions in BLOCK_STYLE_DEFINITIONS_METADATA.
385
+ * Returns classnames and CSS based on the values in a block attributes.styles object.
386
+ * Return values are parsed based on the instructions in BLOCK_STYLE_DEFINITIONS_METADATA.
205
387
  *
206
- * @param array $block_styles An array of styles from a block's attributes.
388
+ * @param array $block_styles Styles from a block's attributes object.
389
+ * @param array $options array(
390
+ * 'selector' => (string) When a selector is passed, `generate()` will return a full CSS rule `$selector { ...rules }`, otherwise a concatenated string of properties and values.
391
+ * 'convert_vars_to_classnames' => (boolean) Whether to skip converting CSS var:? values to var( --wp--preset--* ) values. Default is `false`.
392
+ * );.
207
393
  *
208
394
  * @return array|null array(
209
- * 'styles' => (string) A CSS ruleset formatted to be placed in an HTML `style` attribute or tag.
395
+ * 'css' => (string) A CSS ruleset formatted to be placed in an HTML `style` attribute or tag. Default is a string of inline styles.
210
396
  * 'classnames' => (string) Classnames separated by a space.
211
397
  * );
212
398
  */
213
- public function generate( $block_styles ) {
399
+ public function get_block_supports_styles( $block_styles, $options ) {
214
400
  if ( empty( $block_styles ) || ! is_array( $block_styles ) ) {
215
401
  return null;
216
402
  }
217
403
 
218
- $css_rules = array();
219
- $classnames = array();
220
- $styles_output = array();
404
+ $css_declarations = array();
405
+ $classnames = array();
406
+ $should_skip_css_vars = isset( $options['convert_vars_to_classnames'] ) && true === $options['convert_vars_to_classnames'];
221
407
 
222
408
  // Collect CSS and classnames.
223
- foreach ( self::BLOCK_STYLE_DEFINITIONS_METADATA as $definition_group ) {
224
- foreach ( $definition_group as $style_definition ) {
409
+ foreach ( static::BLOCK_STYLE_DEFINITIONS_METADATA as $definition_group_key => $definition_group_style ) {
410
+ if ( empty( $block_styles[ $definition_group_key ] ) ) {
411
+ continue;
412
+ }
413
+ foreach ( $definition_group_style as $style_definition ) {
225
414
  $style_value = _wp_array_get( $block_styles, $style_definition['path'], null );
226
415
 
227
- if ( empty( $style_value ) ) {
416
+ if ( ! static::is_valid_style_value( $style_value ) ) {
228
417
  continue;
229
418
  }
230
419
 
231
- $classnames = array_merge( $classnames, static::get_classnames( $style_value, $style_definition ) );
232
- $css_rules = array_merge( $css_rules, static::get_css( $style_value, $style_definition ) );
420
+ $classnames = array_merge( $classnames, static::get_classnames( $style_value, $style_definition ) );
421
+ $css_declarations = array_merge( $css_declarations, static::get_css_declarations( $style_value, $style_definition, $should_skip_css_vars ) );
233
422
  }
234
423
  }
235
424
 
236
425
  // Build CSS rules output.
237
- $css_output = '';
238
- if ( ! empty( $css_rules ) ) {
239
- // Generate inline style rules.
240
- // In the future there might be a flag in the option to output
241
- // inline CSS rules (for HTML style attributes) vs selectors + rules for style tags.
242
- foreach ( $css_rules as $rule => $value ) {
243
- $filtered_css = esc_html( safecss_filter_attr( "{$rule}: {$value}" ) );
244
- if ( ! empty( $filtered_css ) ) {
245
- $css_output .= $filtered_css . '; ';
246
- }
247
- }
248
- }
426
+ $css_selector = isset( $options['selector'] ) ? $options['selector'] : null;
427
+ $style_rules = new WP_Style_Engine_CSS_Declarations( $css_declarations );
249
428
 
250
- if ( ! empty( $css_output ) ) {
251
- $styles_output['css'] = trim( $css_output );
429
+ // The return object.
430
+ $styles_output = array();
431
+ $css = $style_rules->get_declarations_string();
432
+
433
+ // Return css, if any.
434
+ if ( ! empty( $css ) ) {
435
+ $styles_output['css'] = $css;
436
+ $styles_output['declarations'] = $style_rules->get_declarations();
437
+ // Return an entire rule if there is a selector.
438
+ if ( $css_selector ) {
439
+ $styles_output['css'] = $css_selector . ' { ' . $css . ' }';
440
+ }
252
441
  }
253
442
 
443
+ // Return classnames, if any.
254
444
  if ( ! empty( $classnames ) ) {
255
445
  $styles_output['classnames'] = implode( ' ', array_unique( $classnames ) );
256
446
  }
@@ -259,52 +449,76 @@ class WP_Style_Engine {
259
449
  }
260
450
 
261
451
  /**
262
- * Default style value parser that returns a CSS ruleset.
263
- * If the input contains an array, it will be treated like a box model
264
- * for styles such as margins and padding
452
+ * Style value parser that returns a CSS definition array comprising style properties
453
+ * that have keys representing individual style properties, otherwise known as longhand CSS properties.
454
+ * e.g., "$style_property-$individual_feature: $value;", which could represent the following:
455
+ * "border-{top|right|bottom|left}-{color|width|style}: {value};" or,
456
+ * "border-image-{outset|source|width|repeat|slice}: {value};"
265
457
  *
266
- * @param string|array $style_value A single raw Gutenberg style attributes value for a CSS property.
267
- * @param string $style_property The CSS property for which we're creating a rule.
458
+ * @param array $style_value A single raw Gutenberg style attributes value for a CSS property.
459
+ * @param array $individual_property_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
460
+ * @param boolean $should_skip_css_vars Whether to skip compiling CSS var values.
268
461
  *
269
- * @return array The class name for the added style.
462
+ * @return array An array of CSS definitions, e.g., array( "$property" => "$value" ).
270
463
  */
271
- protected static function get_css_rules( $style_value, $style_property ) {
272
- $rules = array();
464
+ protected static function get_individual_property_css_declarations( $style_value, $individual_property_definition, $should_skip_css_vars ) {
465
+ $css_declarations = array();
273
466
 
274
- if ( ! $style_value ) {
275
- return $rules;
467
+ if ( ! is_array( $style_value ) || empty( $style_value ) || empty( $individual_property_definition['path'] ) ) {
468
+ return $css_declarations;
276
469
  }
277
470
 
278
- // We assume box model-like properties.
279
- if ( is_array( $style_value ) ) {
280
- foreach ( $style_value as $key => $value ) {
281
- $rules[ "$style_property-$key" ] = $value;
471
+ // The first item in $individual_property_definition['path'] array tells us the style property, e.g., "border".
472
+ // We use this to get a corresponding CSS style definition such as "color" or "width" from the same group.
473
+ // The second item in $individual_property_definition['path'] array refers to the individual property marker, e.g., "top".
474
+ $definition_group_key = $individual_property_definition['path'][0];
475
+ $individual_property_key = $individual_property_definition['path'][1];
476
+
477
+ foreach ( $style_value as $css_property => $value ) {
478
+ if ( empty( $value ) ) {
479
+ continue;
282
480
  }
283
- } else {
284
- $rules[ $style_property ] = $style_value;
285
- }
286
481
 
287
- return $rules;
482
+ // Build a path to the individual rules in definitions.
483
+ $style_definition_path = array( $definition_group_key, $css_property );
484
+ $style_definition = _wp_array_get( static::BLOCK_STYLE_DEFINITIONS_METADATA, $style_definition_path, null );
485
+
486
+ if ( $style_definition && isset( $style_definition['property_keys']['individual'] ) ) {
487
+ // Set a CSS var if there is a valid preset value.
488
+ if ( is_string( $value ) && strpos( $value, 'var:' ) !== false && ! $should_skip_css_vars && ! empty( $individual_property_definition['css_vars'] ) ) {
489
+ $value = static::get_css_var_value( $value, $individual_property_definition['css_vars'] );
490
+ }
491
+ $individual_css_property = sprintf( $style_definition['property_keys']['individual'], $individual_property_key );
492
+ $css_declarations[ $individual_css_property ] = $value;
493
+ }
494
+ }
495
+ return $css_declarations;
288
496
  }
289
497
  }
290
498
 
291
499
  /**
292
- * Global public interface method to WP_Style_Engine->generate.
500
+ * Global public interface method to WP_Style_Engine->get_block_supports_styles to generate block styles from a single block style object.
501
+ * See: https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/
502
+ *
503
+ * Example usage:
504
+ *
505
+ * $styles = wp_style_engine_get_block_supports_styles( array( 'color' => array( 'text' => '#cccccc' ) ) );
506
+ * // Returns `array( 'css' => 'color: #cccccc', 'classnames' => 'has-color' )`.
293
507
  *
294
- * Returns an CSS ruleset.
295
- * Styles are bundled based on the instructions in BLOCK_STYLE_DEFINITIONS_METADATA.
508
+ * @access public
296
509
  *
297
- * @param array $block_styles An array of styles from a block's attributes.
510
+ * @param array $block_styles The value of a block's attributes.style.
511
+ * @param array<string> $options An array of options to determine the output.
298
512
  *
299
- * @return array|null array(
513
+ * @return array<string>|null array(
300
514
  * 'styles' => (string) A CSS ruleset formatted to be placed in an HTML `style` attribute or tag.
301
515
  * 'classnames' => (string) Classnames separated by a space.
302
516
  * );
303
517
  */
304
- function wp_style_engine_generate( $block_styles ) {
518
+ function wp_style_engine_get_block_supports_styles( $block_styles, $options = array() ) {
305
519
  if ( class_exists( 'WP_Style_Engine' ) ) {
306
520
  $style_engine = WP_Style_Engine::get_instance();
307
- return $style_engine->generate( $block_styles );
521
+ return $style_engine->get_block_supports_styles( $block_styles, $options );
308
522
  }
309
523
  return null;
310
524
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/style-engine",
3
- "version": "0.9.0",
3
+ "version": "0.12.0",
4
4
  "description": "WordPress Style engine.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -34,5 +34,5 @@
34
34
  "publishConfig": {
35
35
  "access": "public"
36
36
  },
37
- "gitHead": "a3e0b62091e8a8bdf5e2518e42d60d7098af48cc"
37
+ "gitHead": "9d9d33bbdf317a4381b8ca1713e43bb50df653b3"
38
38
  }