@wordpress/style-engine 0.16.0 → 1.0.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.
- package/CHANGELOG.md +18 -32
- package/README.md +30 -28
- package/build/index.js +6 -2
- package/build/index.js.map +1 -1
- package/build-module/index.js +6 -2
- package/build-module/index.js.map +1 -1
- package/build-types/index.d.ts +6 -2
- package/build-types/index.d.ts.map +1 -1
- package/class-wp-style-engine-css-declarations.php +14 -15
- package/class-wp-style-engine-css-rule.php +14 -14
- package/class-wp-style-engine-css-rules-store.php +7 -7
- package/class-wp-style-engine-processor.php +18 -6
- package/class-wp-style-engine.php +80 -192
- package/docs/using-the-style-engine-with-block-supports.md +151 -0
- package/package.json +4 -3
- package/phpunit/class-wp-style-engine-css-declarations-test.php +142 -95
- package/phpunit/class-wp-style-engine-css-rule-test.php +50 -21
- package/phpunit/class-wp-style-engine-css-rules-store-test.php +75 -58
- package/phpunit/class-wp-style-engine-processor-test.php +76 -33
- package/phpunit/{class-wp-style-engine-test.php → style-engine-test.php} +74 -48
- package/src/index.ts +6 -2
- package/style-engine.php +154 -0
- package/tsconfig.tsbuildinfo +1 -1
|
@@ -12,12 +12,11 @@ if ( class_exists( 'WP_Style_Engine' ) ) {
|
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
|
-
*
|
|
15
|
+
* Class WP_Style_Engine.
|
|
16
16
|
*
|
|
17
|
-
*
|
|
18
|
-
* CSS styles generation.
|
|
17
|
+
* The Style Engine aims to provide a consistent API for rendering styling for blocks across both client-side and server-side applications.
|
|
19
18
|
*
|
|
20
|
-
* This class is for internal
|
|
19
|
+
* This class is for internal Core usage and is not supposed to be used by extenders (plugins and/or themes).
|
|
21
20
|
* This is a low-level API that may need to do breaking changes. Please, use wp_style_engine_get_styles instead.
|
|
22
21
|
*
|
|
23
22
|
* @access private
|
|
@@ -35,6 +34,8 @@ class WP_Style_Engine {
|
|
|
35
34
|
* - property_keys => (array) array of keys whose values represent a valid CSS property, e.g., "margin" or "border".
|
|
36
35
|
* - path => (array) a path that accesses the corresponding style value in the block style object.
|
|
37
36
|
* - 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", ... )`.
|
|
37
|
+
*
|
|
38
|
+
* @var array
|
|
38
39
|
*/
|
|
39
40
|
const BLOCK_STYLE_DEFINITIONS_METADATA = array(
|
|
40
41
|
'color' => array(
|
|
@@ -217,13 +218,13 @@ class WP_Style_Engine {
|
|
|
217
218
|
/**
|
|
218
219
|
* Util: Extracts the slug in kebab case from a preset string, e.g., "heavenly-blue" from 'var:preset|color|heavenlyBlue'.
|
|
219
220
|
*
|
|
220
|
-
* @param string
|
|
221
|
-
* @param string
|
|
221
|
+
* @param string $style_value A single CSS preset value.
|
|
222
|
+
* @param string $property_key The CSS property that is the second element of the preset string. Used for matching.
|
|
222
223
|
*
|
|
223
224
|
* @return string The slug, or empty string if not found.
|
|
224
225
|
*/
|
|
225
226
|
protected static function get_slug_from_preset_value( $style_value, $property_key ) {
|
|
226
|
-
if ( is_string( $style_value ) && str_contains( $style_value, "var:preset|{$property_key}|" ) ) {
|
|
227
|
+
if ( is_string( $style_value ) && is_string( $property_key ) && str_contains( $style_value, "var:preset|{$property_key}|" ) ) {
|
|
227
228
|
$index_to_splice = strrpos( $style_value, '|' ) + 1;
|
|
228
229
|
return _wp_to_kebab_case( substr( $style_value, $index_to_splice ) );
|
|
229
230
|
}
|
|
@@ -231,10 +232,10 @@ class WP_Style_Engine {
|
|
|
231
232
|
}
|
|
232
233
|
|
|
233
234
|
/**
|
|
234
|
-
* Util: Generates a
|
|
235
|
+
* Util: Generates a CSS var string, e.g., var(--wp--preset--color--background) from a preset string such as `var:preset|space|50`.
|
|
235
236
|
*
|
|
236
|
-
* @param string
|
|
237
|
-
* @param
|
|
237
|
+
* @param string $style_value A single CSS preset value.
|
|
238
|
+
* @param string[] $css_vars An associate array of CSS var patterns used to generate the var string.
|
|
238
239
|
*
|
|
239
240
|
* @return string The css var, or an empty string if no match for slug found.
|
|
240
241
|
*/
|
|
@@ -257,7 +258,7 @@ class WP_Style_Engine {
|
|
|
257
258
|
*
|
|
258
259
|
* @param string? $style_value A single css preset value.
|
|
259
260
|
*
|
|
260
|
-
* @return
|
|
261
|
+
* @return bool
|
|
261
262
|
*/
|
|
262
263
|
protected static function is_valid_style_value( $style_value ) {
|
|
263
264
|
return '0' === $style_value || ! empty( $style_value );
|
|
@@ -266,9 +267,9 @@ class WP_Style_Engine {
|
|
|
266
267
|
/**
|
|
267
268
|
* Stores a CSS rule using the provided CSS selector and CSS declarations.
|
|
268
269
|
*
|
|
269
|
-
* @param string
|
|
270
|
-
* @param string
|
|
271
|
-
* @param
|
|
270
|
+
* @param string $store_name A valid store key.
|
|
271
|
+
* @param string $css_selector When a selector is passed, the function will return a full CSS rule `$selector { ...rules }`, otherwise a concatenated string of properties and values.
|
|
272
|
+
* @param string[] $css_declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
|
|
272
273
|
*
|
|
273
274
|
* @return void.
|
|
274
275
|
*/
|
|
@@ -294,16 +295,21 @@ class WP_Style_Engine {
|
|
|
294
295
|
* Returns classnames and CSS based on the values in a styles object.
|
|
295
296
|
* Return values are parsed based on the instructions in BLOCK_STYLE_DEFINITIONS_METADATA.
|
|
296
297
|
*
|
|
298
|
+
* @since 6.1.0
|
|
299
|
+
*
|
|
297
300
|
* @param array $block_styles The style object.
|
|
298
|
-
* @param array $options
|
|
299
|
-
*
|
|
300
|
-
*
|
|
301
|
-
* )
|
|
301
|
+
* @param array $options {
|
|
302
|
+
* Optional. An array of options. Default empty array.
|
|
303
|
+
*
|
|
304
|
+
* @type bool $convert_vars_to_classnames Whether to skip converting incoming CSS var patterns, e.g., `var:preset|<PRESET_TYPE>|<PRESET_SLUG>`, to var( --wp--preset--* ) values. Default `false`.
|
|
305
|
+
* @type string $selector Optional. When a selector is passed, the value of `$css` in the return value will comprise a full CSS rule `$selector { ...$css_declarations }`,
|
|
306
|
+
* otherwise, the value will be a concatenated string of CSS declarations.
|
|
307
|
+
* }
|
|
302
308
|
*
|
|
303
|
-
* @return array
|
|
304
|
-
*
|
|
305
|
-
*
|
|
306
|
-
*
|
|
309
|
+
* @return array {
|
|
310
|
+
* @type string $classnames Classnames separated by a space.
|
|
311
|
+
* @type string[] $declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
|
|
312
|
+
* }
|
|
307
313
|
*/
|
|
308
314
|
public static function parse_block_styles( $block_styles, $options ) {
|
|
309
315
|
$parsed_styles = array(
|
|
@@ -335,12 +341,12 @@ class WP_Style_Engine {
|
|
|
335
341
|
}
|
|
336
342
|
|
|
337
343
|
/**
|
|
338
|
-
* Returns classnames, and generates classname(s) from a CSS preset property pattern, e.g., 'var:preset
|
|
344
|
+
* Returns classnames, and generates classname(s) from a CSS preset property pattern, e.g., '`var:preset|<PRESET_TYPE>|<PRESET_SLUG>`'.
|
|
339
345
|
*
|
|
340
|
-
* @param array
|
|
341
|
-
* @param array
|
|
346
|
+
* @param array $style_value A single raw style value or css preset property from the $block_styles array.
|
|
347
|
+
* @param array $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
|
|
342
348
|
*
|
|
343
|
-
* @return array
|
|
349
|
+
* @return array|string[] An array of CSS classnames, or empty array.
|
|
344
350
|
*/
|
|
345
351
|
protected static function get_classnames( $style_value, $style_definition ) {
|
|
346
352
|
if ( empty( $style_value ) ) {
|
|
@@ -357,10 +363,12 @@ class WP_Style_Engine {
|
|
|
357
363
|
$slug = static::get_slug_from_preset_value( $style_value, $property_key );
|
|
358
364
|
|
|
359
365
|
if ( $slug ) {
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
366
|
+
/*
|
|
367
|
+
* Right now we expect a classname pattern to be stored in BLOCK_STYLE_DEFINITIONS_METADATA.
|
|
368
|
+
* One day, if there are no stored schemata, we could allow custom patterns or
|
|
369
|
+
* generate classnames based on other properties
|
|
370
|
+
* such as a path or a value or a prefix passed in options.
|
|
371
|
+
*/
|
|
364
372
|
$classnames[] = strtr( $classname, array( '$slug' => $slug ) );
|
|
365
373
|
}
|
|
366
374
|
}
|
|
@@ -372,15 +380,19 @@ class WP_Style_Engine {
|
|
|
372
380
|
/**
|
|
373
381
|
* Returns an array of CSS declarations based on valid block style values.
|
|
374
382
|
*
|
|
375
|
-
* @
|
|
376
|
-
* @param array<string> $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
|
|
377
|
-
* @param array $options array(
|
|
378
|
-
* 'convert_vars_to_classnames' => (boolean) Whether to skip converting CSS var:? values to var( --wp--preset--* ) values. Default is `false`.
|
|
379
|
-
* );.
|
|
383
|
+
* @since 6.1.0
|
|
380
384
|
*
|
|
381
|
-
* @
|
|
385
|
+
* @param array $style_value A single raw style value from $block_styles array.
|
|
386
|
+
* @param array $style_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
|
|
387
|
+
* @param array $options {
|
|
388
|
+
* Optional. An array of options. Default empty array.
|
|
389
|
+
*
|
|
390
|
+
* @type bool $convert_vars_to_classnames Whether to skip converting incoming CSS var patterns, e.g., `var:preset|<PRESET_TYPE>|<PRESET_SLUG>`, to var( --wp--preset--* ) values. Default `false`.
|
|
391
|
+
* }
|
|
392
|
+
*
|
|
393
|
+
* @return string[] An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
|
|
382
394
|
*/
|
|
383
|
-
protected static function get_css_declarations( $style_value, $style_definition, $options ) {
|
|
395
|
+
protected static function get_css_declarations( $style_value, $style_definition, $options = array() ) {
|
|
384
396
|
if ( isset( $style_definition['value_func'] ) && is_callable( $style_definition['value_func'] ) ) {
|
|
385
397
|
return call_user_func( $style_definition['value_func'], $style_value, $style_definition, $options );
|
|
386
398
|
}
|
|
@@ -389,8 +401,10 @@ class WP_Style_Engine {
|
|
|
389
401
|
$style_property_keys = $style_definition['property_keys'];
|
|
390
402
|
$should_skip_css_vars = isset( $options['convert_vars_to_classnames'] ) && true === $options['convert_vars_to_classnames'];
|
|
391
403
|
|
|
392
|
-
|
|
393
|
-
|
|
404
|
+
/*
|
|
405
|
+
* Build CSS var values from `var:preset|<PRESET_TYPE>|<PRESET_SLUG>` values, e.g, `var(--wp--css--rule-slug )`.
|
|
406
|
+
* Check if the value is a CSS preset and there's a corresponding css_var pattern in the style definition.
|
|
407
|
+
*/
|
|
394
408
|
if ( is_string( $style_value ) && str_contains( $style_value, 'var:' ) ) {
|
|
395
409
|
if ( ! $should_skip_css_vars && ! empty( $style_definition['css_vars'] ) ) {
|
|
396
410
|
$css_var = static::get_css_var_value( $style_value, $style_definition['css_vars'] );
|
|
@@ -401,9 +415,11 @@ class WP_Style_Engine {
|
|
|
401
415
|
return $css_declarations;
|
|
402
416
|
}
|
|
403
417
|
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
418
|
+
/*
|
|
419
|
+
* Default rule builder.
|
|
420
|
+
* If the input contains an array, assume box model-like properties
|
|
421
|
+
* for styles such as margins and padding.
|
|
422
|
+
*/
|
|
407
423
|
if ( is_array( $style_value ) ) {
|
|
408
424
|
// Bail out early if the `'individual'` property is not defined.
|
|
409
425
|
if ( ! isset( $style_property_keys['individual'] ) ) {
|
|
@@ -436,22 +452,26 @@ class WP_Style_Engine {
|
|
|
436
452
|
* "border-{top|right|bottom|left}-{color|width|style}: {value};" or,
|
|
437
453
|
* "border-image-{outset|source|width|repeat|slice}: {value};"
|
|
438
454
|
*
|
|
439
|
-
* @param array $style_value A single raw
|
|
440
|
-
* @param array $individual_property_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
|
|
441
|
-
* @param array $options
|
|
442
|
-
*
|
|
443
|
-
* );.
|
|
455
|
+
* @param array $style_value A single raw style value from $block_styles array.
|
|
456
|
+
* @param array $individual_property_definition A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA representing an individual property of a CSS property, e.g., 'top' in 'border-top'.
|
|
457
|
+
* @param array $options {
|
|
458
|
+
* Optional. An array of options. Default empty array.
|
|
444
459
|
*
|
|
445
|
-
*
|
|
460
|
+
* @type bool $convert_vars_to_classnames Whether to skip converting incoming CSS var patterns, e.g., `var:preset|<PRESET_TYPE>|<PRESET_SLUG>`, to var( --wp--preset--* ) values. Default `false`.
|
|
461
|
+
* }
|
|
462
|
+
*
|
|
463
|
+
* @return string[] An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
|
|
446
464
|
*/
|
|
447
|
-
protected static function get_individual_property_css_declarations( $style_value, $individual_property_definition, $options ) {
|
|
465
|
+
protected static function get_individual_property_css_declarations( $style_value, $individual_property_definition, $options = array() ) {
|
|
448
466
|
if ( ! is_array( $style_value ) || empty( $style_value ) || empty( $individual_property_definition['path'] ) ) {
|
|
449
467
|
return array();
|
|
450
468
|
}
|
|
451
469
|
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
470
|
+
/*
|
|
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
|
+
*/
|
|
455
475
|
$definition_group_key = $individual_property_definition['path'][0];
|
|
456
476
|
$individual_property_key = $individual_property_definition['path'][1];
|
|
457
477
|
$should_skip_css_vars = isset( $options['convert_vars_to_classnames'] ) && true === $options['convert_vars_to_classnames'];
|
|
@@ -481,8 +501,8 @@ class WP_Style_Engine {
|
|
|
481
501
|
/**
|
|
482
502
|
* Returns compiled CSS from css_declarations.
|
|
483
503
|
*
|
|
484
|
-
* @param
|
|
485
|
-
* @param string
|
|
504
|
+
* @param string[] $css_declarations An associative array of CSS definitions, e.g., array( "$property" => "$value", "$property" => "$value" ).
|
|
505
|
+
* @param string $css_selector When a selector is passed, the function will return a full CSS rule `$selector { ...rules }`, otherwise a concatenated string of properties and values.
|
|
486
506
|
*
|
|
487
507
|
* @return string A compiled CSS string.
|
|
488
508
|
*/
|
|
@@ -505,10 +525,12 @@ class WP_Style_Engine {
|
|
|
505
525
|
* Returns a compiled stylesheet from stored CSS rules.
|
|
506
526
|
*
|
|
507
527
|
* @param WP_Style_Engine_CSS_Rule[] $css_rules An array of WP_Style_Engine_CSS_Rule objects from a store or otherwise.
|
|
508
|
-
* @param array $options
|
|
509
|
-
*
|
|
510
|
-
*
|
|
511
|
-
*
|
|
528
|
+
* @param array $options {
|
|
529
|
+
* Optional. An array of options. Default empty array.
|
|
530
|
+
*
|
|
531
|
+
* @type bool $optimize Whether to optimize the CSS output, e.g., combine rules. Default is `false`.
|
|
532
|
+
* @type bool $prettify Whether to add new lines and indents to output. Default is the test of whether the global constant `SCRIPT_DEBUG` is defined.
|
|
533
|
+
* }
|
|
512
534
|
*
|
|
513
535
|
* @return string A compiled stylesheet from stored CSS rules.
|
|
514
536
|
*/
|
|
@@ -518,137 +540,3 @@ class WP_Style_Engine {
|
|
|
518
540
|
return $processor->get_css( $options );
|
|
519
541
|
}
|
|
520
542
|
}
|
|
521
|
-
|
|
522
|
-
/**
|
|
523
|
-
* Global public interface method to generate styles from a single style object, e.g.,
|
|
524
|
-
* the value of a block's attributes.style object or the top level styles in theme.json.
|
|
525
|
-
* See: https://developer.wordpress.org/block-editor/reference-guides/theme-json-reference/theme-json-living/#styles and
|
|
526
|
-
* https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/
|
|
527
|
-
*
|
|
528
|
-
* Example usage:
|
|
529
|
-
*
|
|
530
|
-
* $styles = wp_style_engine_get_styles( array( 'color' => array( 'text' => '#cccccc' ) ) );
|
|
531
|
-
* // Returns `array( 'css' => 'color: #cccccc', 'declarations' => array( 'color' => '#cccccc' ), 'classnames' => 'has-color' )`.
|
|
532
|
-
*
|
|
533
|
-
* @access public
|
|
534
|
-
*
|
|
535
|
-
* @param array $block_styles The style object.
|
|
536
|
-
* @param array<string|boolean> $options array(
|
|
537
|
-
* 'context' => (string|null) An identifier describing the origin of the style object, e.g., 'block-supports' or 'global-styles'. Default is 'block-supports'.
|
|
538
|
-
* When set, the style engine will attempt to store the CSS rules, where a selector is also passed.
|
|
539
|
-
* 'convert_vars_to_classnames' => (boolean) Whether to skip converting CSS var:? values to var( --wp--preset--* ) values. Default is `false`.
|
|
540
|
-
* 'selector' => (string) When a selector is passed, `generate()` will return a full CSS rule `$selector { ...rules }`, otherwise a concatenated string of properties and values.
|
|
541
|
-
* );.
|
|
542
|
-
*
|
|
543
|
-
* @return array<string|array> array(
|
|
544
|
-
* 'css' => (string) A CSS ruleset or declarations block formatted to be placed in an HTML `style` attribute or tag.
|
|
545
|
-
* 'declarations' => (array) An array of property/value pairs representing parsed CSS declarations.
|
|
546
|
-
* 'classnames' => (string) Classnames separated by a space.
|
|
547
|
-
* );
|
|
548
|
-
*/
|
|
549
|
-
function wp_style_engine_get_styles( $block_styles, $options = array() ) {
|
|
550
|
-
if ( ! class_exists( 'WP_Style_Engine' ) ) {
|
|
551
|
-
return array();
|
|
552
|
-
}
|
|
553
|
-
|
|
554
|
-
$options = wp_parse_args(
|
|
555
|
-
$options,
|
|
556
|
-
array(
|
|
557
|
-
'selector' => null,
|
|
558
|
-
'context' => null,
|
|
559
|
-
'convert_vars_to_classnames' => false,
|
|
560
|
-
)
|
|
561
|
-
);
|
|
562
|
-
|
|
563
|
-
$parsed_styles = WP_Style_Engine::parse_block_styles( $block_styles, $options );
|
|
564
|
-
|
|
565
|
-
// Output.
|
|
566
|
-
$styles_output = array();
|
|
567
|
-
|
|
568
|
-
if ( ! empty( $parsed_styles['declarations'] ) ) {
|
|
569
|
-
$styles_output['css'] = WP_Style_Engine::compile_css( $parsed_styles['declarations'], $options['selector'] );
|
|
570
|
-
$styles_output['declarations'] = $parsed_styles['declarations'];
|
|
571
|
-
if ( ! empty( $options['context'] ) ) {
|
|
572
|
-
WP_Style_Engine::store_css_rule( $options['context'], $options['selector'], $parsed_styles['declarations'] );
|
|
573
|
-
}
|
|
574
|
-
}
|
|
575
|
-
|
|
576
|
-
if ( ! empty( $parsed_styles['classnames'] ) ) {
|
|
577
|
-
$styles_output['classnames'] = implode( ' ', array_unique( $parsed_styles['classnames'] ) );
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
return array_filter( $styles_output );
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
/**
|
|
584
|
-
* Returns compiled CSS from a collection of selectors and declarations.
|
|
585
|
-
* This won't add to any store, but is useful for returning a compiled style sheet from any CSS selector + declarations combos.
|
|
586
|
-
*
|
|
587
|
-
* @access public
|
|
588
|
-
*
|
|
589
|
-
* @param array<array> $css_rules array(
|
|
590
|
-
* array(
|
|
591
|
-
* 'selector' => (string) A CSS selector.
|
|
592
|
-
* declarations' => (boolean) An array of CSS definitions, e.g., array( "$property" => "$value" ).
|
|
593
|
-
* )
|
|
594
|
-
* );.
|
|
595
|
-
* @param array<string> $options array(
|
|
596
|
-
* 'context' => (string|null) An identifier describing the origin of the style object, e.g., 'block-supports' or 'global-styles'. Default is 'block-supports'.
|
|
597
|
-
* When set, the style engine will attempt to store the CSS rules.
|
|
598
|
-
* 'optimize' => (boolean) Whether to optimize the CSS output, e.g., combine rules.
|
|
599
|
-
* 'prettify' => (boolean) Whether to add new lines to output.
|
|
600
|
-
* );.
|
|
601
|
-
*
|
|
602
|
-
* @return string A compiled CSS string.
|
|
603
|
-
*/
|
|
604
|
-
function wp_style_engine_get_stylesheet_from_css_rules( $css_rules, $options = array() ) {
|
|
605
|
-
if ( ! class_exists( 'WP_Style_Engine' ) || empty( $css_rules ) ) {
|
|
606
|
-
return '';
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
$options = wp_parse_args(
|
|
610
|
-
$options,
|
|
611
|
-
array(
|
|
612
|
-
'context' => null,
|
|
613
|
-
)
|
|
614
|
-
);
|
|
615
|
-
|
|
616
|
-
$css_rule_objects = array();
|
|
617
|
-
foreach ( $css_rules as $css_rule ) {
|
|
618
|
-
if ( empty( $css_rule['selector'] ) || empty( $css_rule['declarations'] ) || ! is_array( $css_rule['declarations'] ) ) {
|
|
619
|
-
continue;
|
|
620
|
-
}
|
|
621
|
-
|
|
622
|
-
if ( ! empty( $options['context'] ) ) {
|
|
623
|
-
WP_Style_Engine::store_css_rule( $options['context'], $css_rule['selector'], $css_rule['declarations'] );
|
|
624
|
-
}
|
|
625
|
-
|
|
626
|
-
$css_rule_objects[] = new WP_Style_Engine_CSS_Rule( $css_rule['selector'], $css_rule['declarations'] );
|
|
627
|
-
}
|
|
628
|
-
|
|
629
|
-
if ( empty( $css_rule_objects ) ) {
|
|
630
|
-
return '';
|
|
631
|
-
}
|
|
632
|
-
|
|
633
|
-
return WP_Style_Engine::compile_stylesheet_from_css_rules( $css_rule_objects, $options );
|
|
634
|
-
}
|
|
635
|
-
|
|
636
|
-
/**
|
|
637
|
-
* Returns compiled CSS from a store, if found.
|
|
638
|
-
*
|
|
639
|
-
* @access public
|
|
640
|
-
*
|
|
641
|
-
* @param string $store_name A valid store name.
|
|
642
|
-
* @param array $options array(
|
|
643
|
-
* 'optimize' => (boolean) Whether to optimize the CSS output, e.g., combine rules.
|
|
644
|
-
* 'prettify' => (boolean) Whether to add new lines to output.
|
|
645
|
-
* );.
|
|
646
|
-
* @return string A compiled CSS string.
|
|
647
|
-
*/
|
|
648
|
-
function wp_style_engine_get_stylesheet_from_context( $store_name, $options = array() ) {
|
|
649
|
-
if ( ! class_exists( 'WP_Style_Engine' ) || empty( $store_name ) ) {
|
|
650
|
-
return '';
|
|
651
|
-
}
|
|
652
|
-
|
|
653
|
-
return WP_Style_Engine::compile_stylesheet_from_css_rules( WP_Style_Engine::get_store( $store_name )->get_all_rules(), $options );
|
|
654
|
-
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# Using the Style Engine to generate block supports styles
|
|
2
|
+
|
|
3
|
+
[Block supports](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/) is the API that allows a block to declare support for certain features.
|
|
4
|
+
|
|
5
|
+
Where a block declares support for a specific style group or property, e.g., "spacing" or "spacing.padding", the block's attributes are extended to include a **style object**.
|
|
6
|
+
|
|
7
|
+
For example:
|
|
8
|
+
|
|
9
|
+
```json
|
|
10
|
+
{
|
|
11
|
+
"attributes": {
|
|
12
|
+
"style": {
|
|
13
|
+
"spacing": {
|
|
14
|
+
"margin": {
|
|
15
|
+
"top": "10px"
|
|
16
|
+
},
|
|
17
|
+
"padding": "1em"
|
|
18
|
+
},
|
|
19
|
+
"typography": {
|
|
20
|
+
"fontSize": "2.2rem"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
Using this object, the Style Engine can generate the classes and CSS required to style the block element.
|
|
28
|
+
|
|
29
|
+
The global function `wp_style_engine_get_styles` accepts a style object as its first argument, and will output compiled CSS and an array of CSS declaration property/value pairs.
|
|
30
|
+
|
|
31
|
+
```php
|
|
32
|
+
$block_styles = array(
|
|
33
|
+
'spacing' => array( 'padding' => '10px', 'margin' => array( 'top' => '1em') ),
|
|
34
|
+
'typography' => array( 'fontSize' => '2.2rem' ),
|
|
35
|
+
);
|
|
36
|
+
$styles = wp_style_engine_get_styles(
|
|
37
|
+
$block_styles
|
|
38
|
+
);
|
|
39
|
+
print_r( $styles );
|
|
40
|
+
|
|
41
|
+
/*
|
|
42
|
+
array(
|
|
43
|
+
'css' => 'padding:10px;margin-top:1em;font-size:2.2rem',
|
|
44
|
+
'declarations' => array( 'padding' => '10px', 'margin-top' => '1em', 'font-size' => '2.2rem' )
|
|
45
|
+
)
|
|
46
|
+
*/
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Checking for block support and skip serialization
|
|
50
|
+
|
|
51
|
+
Before passing the block style object to the Style Engine, it's important to take into account:
|
|
52
|
+
|
|
53
|
+
1. whether the theme has elected to support a particular block style, and
|
|
54
|
+
2. whether a block has elected to "skip serialization" of that particular block style, that is, opt-out of automatic application of styles to the block's element (usually in order to do it via the block's internals). See the [block API documentation](https://developer.wordpress.org/block-editor/explanations/architecture/styles/#block-supports-api) for further information.
|
|
55
|
+
|
|
56
|
+
If a block either:
|
|
57
|
+
|
|
58
|
+
- has no support for a specific style, or
|
|
59
|
+
- skips serialization of that style
|
|
60
|
+
|
|
61
|
+
it's likely that you'll want to remove those style values from the style object.
|
|
62
|
+
|
|
63
|
+
For example:
|
|
64
|
+
|
|
65
|
+
```php
|
|
66
|
+
// Check if a block has support using block_has_support (https://developer.wordpress.org/reference/functions/block_has_support/)
|
|
67
|
+
$has_padding_support = block_has_support( $block_type, array( 'spacing', 'padding' ), false ); // Returns true.
|
|
68
|
+
$has_margin_support = block_has_support( $block_type, array( 'spacing', 'margin' ), false ); // Returns false.
|
|
69
|
+
|
|
70
|
+
// Check skipping of serialization.
|
|
71
|
+
$should_skip_padding = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'padding' ); // Returns true.
|
|
72
|
+
$should_skip_margin = wp_should_skip_block_supports_serialization( $block_type, 'spacing', 'margin' ); // Returns false.
|
|
73
|
+
|
|
74
|
+
// Now build the styles object.
|
|
75
|
+
$spacing_block_styles = array();
|
|
76
|
+
$spacing_block_styles['padding'] = $has_padding_support && ! $skip_padding ? _wp_array_get( $block_attributes['style'], array( 'spacing', 'padding' ), null ) : null;
|
|
77
|
+
$spacing_block_styles['margin'] = $has_margin_support && ! $skip_margin ? _wp_array_get( $block_attributes['style'], array( 'spacing', 'margin' ), null ) : null;
|
|
78
|
+
|
|
79
|
+
// Now get the styles.
|
|
80
|
+
$styles = wp_style_engine_get_styles( array( 'spacing' => $spacing_block_styles ) );
|
|
81
|
+
|
|
82
|
+
print_r( $styles );
|
|
83
|
+
|
|
84
|
+
/*
|
|
85
|
+
// Nothing, because there's no support for margin and the block skip's serialization for padding.
|
|
86
|
+
array()
|
|
87
|
+
*/
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Generating classnames and CSS custom selectors from presets
|
|
91
|
+
|
|
92
|
+
Many of theme.json's presets will generate both CSS custom properties and CSS rules (consisting of a selector and the CSS declarations) on the frontend.
|
|
93
|
+
|
|
94
|
+
Styling a block using these presets normally involves adding the selector to the "className" attribute of the block.
|
|
95
|
+
|
|
96
|
+
For styles that can have preset values, such as text color and font family, the Style Engine knows how to construct the classnames using the preset slug.
|
|
97
|
+
|
|
98
|
+
To discern CSS values from preset values however, the Style Engine expects a special format.
|
|
99
|
+
|
|
100
|
+
Preset values must follow the pattern `var:preset|<PRESET_TYPE>|<PRESET_SLUG>`.
|
|
101
|
+
|
|
102
|
+
When the Style Engine encounters these values, it will parse them and create a CSS value of `var(--wp--preset--font-size--small)` and/or generate a classname if required.
|
|
103
|
+
|
|
104
|
+
Example:
|
|
105
|
+
|
|
106
|
+
```php
|
|
107
|
+
// Let's say the block attributes styles contain a fontSize preset slug of "small".
|
|
108
|
+
$preset_font_size = "var:preset|font-size|{$block_attributes['fontSize']}";
|
|
109
|
+
// Now let's say the block attributes styles contain a backgroundColor preset slug of "blue".
|
|
110
|
+
$preset_background_color = "var:preset|color|{$block_attributes['backgroundColor']}";
|
|
111
|
+
|
|
112
|
+
$block_styles = array(
|
|
113
|
+
'typography' => array( 'fontSize' => $preset_font_size ),
|
|
114
|
+
'color' => array( 'background' => $preset_background_color )
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
$styles = wp_style_engine_get_styles(
|
|
118
|
+
$block_styles
|
|
119
|
+
);
|
|
120
|
+
print_r( $styles );
|
|
121
|
+
|
|
122
|
+
/*
|
|
123
|
+
array(
|
|
124
|
+
'css' => 'background-color:var(--wp--preset--color--blue);font-size:var(--wp--preset--font-size--small);',
|
|
125
|
+
'classnames' => 'has-background-color has-blue-background-color has-small-font-size',
|
|
126
|
+
)
|
|
127
|
+
*/
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
If you don't want the Style Engine to output the CSS custom vars as well, which you might not if you're applying both the CSS and classnames to the block element, you can pass `'convert_vars_to_classnames' => true` in the options array.
|
|
131
|
+
|
|
132
|
+
```php
|
|
133
|
+
$options = array(
|
|
134
|
+
// Whether to skip converting CSS var:? values to var( --wp--preset--* ) values. Default is `false`.
|
|
135
|
+
'convert_vars_to_classnames' => 'true',
|
|
136
|
+
);
|
|
137
|
+
$styles = wp_style_engine_get_styles(
|
|
138
|
+
$block_styles,
|
|
139
|
+
$options
|
|
140
|
+
);
|
|
141
|
+
print_r( $styles );
|
|
142
|
+
|
|
143
|
+
/*
|
|
144
|
+
array(
|
|
145
|
+
'css' => 'letter-spacing:12px;', // non-preset-based CSS will still be compiled.
|
|
146
|
+
'classnames' => 'has-background-color has-blue-background-color has-small-font-size',
|
|
147
|
+
)
|
|
148
|
+
*/
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Read more about [global styles](https://developer.wordpress.org/block-editor/explanations/architecture/styles/#global-styles) and [preset CSS custom properties](https://developer.wordpress.org/block-editor/how-to-guides/themes/theme-json/#css-custom-properties-presets-custom) and [theme supports](https://developer.wordpress.org/block-editor/how-to-guides/themes/theme-support/).
|
package/package.json
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wordpress/style-engine",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "WordPress
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A suite of parsers and compilers for WordPress styles.",
|
|
5
5
|
"author": "The WordPress Contributors",
|
|
6
6
|
"license": "GPL-2.0-or-later",
|
|
7
7
|
"keywords": [
|
|
8
8
|
"wordpress",
|
|
9
9
|
"gutenberg",
|
|
10
10
|
"styles",
|
|
11
|
+
"css",
|
|
11
12
|
"global styles"
|
|
12
13
|
],
|
|
13
14
|
"homepage": "https://github.com/WordPress/gutenberg/tree/HEAD/packages/style-engine/README.md",
|
|
@@ -34,5 +35,5 @@
|
|
|
34
35
|
"publishConfig": {
|
|
35
36
|
"access": "public"
|
|
36
37
|
},
|
|
37
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "56ef3f5a754e44155ee79e827c7a1d0efc1ee5aa"
|
|
38
39
|
}
|