@wordpress/style-engine 1.29.0 → 1.30.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 CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 1.30.0 (2023-11-29)
6
+
5
7
  ## 1.29.0 (2023-11-16)
6
8
 
7
9
  ## 1.28.0 (2023-11-02)
@@ -210,6 +210,9 @@ final class WP_Style_Engine {
210
210
  'property_keys' => array(
211
211
  'default' => 'font-size',
212
212
  ),
213
+ 'css_vars' => array(
214
+ 'font-size' => '--wp--preset--font-size--$slug',
215
+ ),
213
216
  'path' => array( 'typography', 'fontSize' ),
214
217
  'classnames' => array(
215
218
  'has-$slug-font-size' => 'font-size',
@@ -219,6 +222,9 @@ final class WP_Style_Engine {
219
222
  'property_keys' => array(
220
223
  'default' => 'font-family',
221
224
  ),
225
+ 'css_vars' => array(
226
+ 'font-family' => '--wp--preset--font-family--$slug',
227
+ ),
222
228
  'path' => array( 'typography', 'fontFamily' ),
223
229
  'classnames' => array(
224
230
  'has-$slug-font-family' => 'font-family',
@@ -30,11 +30,11 @@ The global function `wp_style_engine_get_styles` accepts a style object as its f
30
30
 
31
31
  ```php
32
32
  $block_styles = array(
33
- 'spacing' => array( 'padding' => '10px', 'margin' => array( 'top' => '1em') ),
34
- 'typography' => array( 'fontSize' => '2.2rem' ),
33
+ 'spacing' => array( 'padding' => '10px', 'margin' => array( 'top' => '1em') ),
34
+ 'typography' => array( 'fontSize' => '2.2rem' ),
35
35
  );
36
36
  $styles = wp_style_engine_get_styles(
37
- $block_styles
37
+ $block_styles
38
38
  );
39
39
  print_r( $styles );
40
40
 
@@ -51,7 +51,7 @@ When [registering a block support](https://developer.wordpress.org/reference/cla
51
51
 
52
52
  If a block has opted into the block support, the values of "class" and "style" will be applied to the block element's "class" and "style" attributes accordingly when rendered in the frontend HTML. Note, this applies only to server-side rendered blocks, for example, the [Site Title block](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/#site-title).
53
53
 
54
- The callback receives `$block_type` and `$block_attributes` as arguments. The `style` attribute within `$block_attributes` only contains the raw style object, if any styles have been set for the block, and not any CSS or classnames to be applied to the block's HTML elements.
54
+ The callback receives `$block_type` and `$block_attributes` as arguments. The `style` attribute within `$block_attributes` only contains the raw style object, if any styles have been set for the block, and not any CSS or classnames to be applied to the block's HTML elements.
55
55
 
56
56
  Here is where `wp_style_engine_get_styles` comes in handy: it will generate CSS and, if appropriate, classnames to be added to the "style" and "class" HTML attributes in the final rendered block markup.
57
57
 
@@ -100,7 +100,7 @@ Before passing the block style object to the Style Engine, you'll need to take i
100
100
 
101
101
  If a block either:
102
102
 
103
- - has no support for a style, or
103
+ - has no support for a style, or
104
104
  - skips serialization of that style
105
105
 
106
106
  it's likely that you'll want to remove those style values from the style object before passing it to the Style Engine with help of two functions:
@@ -108,24 +108,24 @@ it's likely that you'll want to remove those style values from the style object
108
108
  - wp_should_skip_block_supports_serialization()
109
109
  - block_has_support()
110
110
 
111
- We can now update the apply callback code above so that we'll only return "style" and "class", where a block has support and it doesn't skip serialization:
111
+ We can now update the "apply" callback code above so that we'll only return "style" and "class", where a block has support, and it doesn't skip serialization:
112
112
 
113
113
  ```php
114
114
  function gutenberg_apply_colors_support( $block_type, $block_attributes ) {
115
115
  // The return value.
116
116
  $attributes = array();
117
-
117
+
118
118
  // Return early if the block skips all serialization for block supports.
119
119
  if ( gutenberg_should_skip_block_supports_serialization( $block_type, 'color' ) ) {
120
120
  return $attributes;
121
121
  }
122
122
 
123
123
  // Checks for support and skip serialization.
124
- $has_text_support = block_has_support( $block_type, array( 'color', 'text' ), false );
124
+ $has_text_support = block_has_support( $block_type, array( 'color', 'text' ), false );
125
125
  $has_background_support = block_has_support( $block_type, array( 'color', 'background' ), false );
126
- $skips_serialization_of_color_text = wp_should_skip_block_supports_serialization( $block_type, 'color', 'text' );
127
- $skips_serialization_of_color_background = wp_should_skip_block_supports_serialization( $block_type, 'color', 'background' );
128
-
126
+ $skips_serialization_of_color_text = wp_should_skip_block_supports_serialization( $block_type, 'color', 'text' );
127
+ $skips_serialization_of_color_background = wp_should_skip_block_supports_serialization( $block_type, 'color', 'background' );
128
+
129
129
  // Get the color styles from the style object.
130
130
  $block_color_styles = isset( $block_attributes['style']['color'] ) ? $block_attributes['style']['color'] : null;
131
131
 
@@ -161,7 +161,7 @@ Styling a block using these presets normally involves adding the selector to the
161
161
 
162
162
  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.
163
163
 
164
- To discern CSS values from preset values however, the Style Engine expects a special format.
164
+ To discern CSS values from preset values, the Style Engine expects a special format.
165
165
 
166
166
  Preset values must follow the pattern `var:preset|<PRESET_TYPE>|<PRESET_SLUG>`.
167
167
 
@@ -171,13 +171,16 @@ Example:
171
171
 
172
172
  ```php
173
173
  // Let's say the block attributes styles contain a fontSize preset slug of "small".
174
+ // $block_attributes['fontSize'] = 'var:preset|font-size|small';
174
175
  $preset_font_size = "var:preset|font-size|{$block_attributes['fontSize']}";
175
176
  // Now let's say the block attributes styles contain a backgroundColor preset slug of "blue".
177
+ // $block_attributes['backgroundColor'] = 'var:preset|color|blue';
176
178
  $preset_background_color = "var:preset|color|{$block_attributes['backgroundColor']}";
177
179
 
178
180
  $block_styles = array(
179
181
  'typography' => array( 'fontSize' => $preset_font_size ),
180
- 'color' => array( 'background' => $preset_background_color )
182
+ 'color' => array( 'background' => $preset_background_color ),
183
+ 'spacing' => array( 'padding' => '10px', 'margin' => array( 'top' => '1em') ),
181
184
  );
182
185
 
183
186
  $styles = wp_style_engine_get_styles(
@@ -187,19 +190,29 @@ print_r( $styles );
187
190
 
188
191
  /*
189
192
  array(
190
- 'css' => 'background-color:var(--wp--preset--color--blue);font-size:var(--wp--preset--font-size--small);',
191
- 'classnames' => 'has-background-color has-blue-background-color has-small-font-size',
193
+ 'css' => 'background-color:var(--wp--preset--color--blue);padding:10px;margin-top:1em;font-size:var(--wp--preset--font-size--small);',
194
+ 'declarations' => array(
195
+ 'background-color' => 'var(--wp--preset--color--blue)',
196
+ 'padding' => '10px',
197
+ 'margin-top' => '1em',
198
+ 'font-size' => 'var(--wp--preset--font-size--small)',
199
+ ),
200
+ 'classnames' => 'has-background has-blue-background-color has-small-font-size',
192
201
  )
193
202
  */
194
203
  ```
195
204
 
196
- 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.
205
+ If you don't want the Style Engine to output the CSS custom vars in the generated CSS string as well, which you might not if you're applying both the CSS rules and classnames to the block element, you can pass `'convert_vars_to_classnames' => true` in the options array.
206
+
207
+ This flag means "convert the vars to classnames and don't output the vars to the CSS". The Style Engine will therefore **only** generate the required classnames and omit the CSS custom vars in the CSS.
208
+
209
+ Using the above example code, observe the different output when we pass the option:
197
210
 
198
211
  ```php
199
212
  $options = array(
200
- // Whether to skip converting CSS var:? values to var( --wp--preset--* ) values. Default is `false`.
201
- 'convert_vars_to_classnames' => 'true',
213
+ 'convert_vars_to_classnames' => true,
202
214
  );
215
+
203
216
  $styles = wp_style_engine_get_styles(
204
217
  $block_styles,
205
218
  $options
@@ -208,8 +221,12 @@ print_r( $styles );
208
221
 
209
222
  /*
210
223
  array(
211
- 'css' => 'letter-spacing:12px;', // non-preset-based CSS will still be compiled.
212
- 'classnames' => 'has-background-color has-blue-background-color has-small-font-size',
224
+ 'css' => 'padding:10px;margin-top:1em;',
225
+ 'declarations' => array(
226
+ 'padding' => '10px',
227
+ 'margin-top' => '1em',
228
+ ),
229
+ 'classnames' => 'has-background has-blue-background-color has-small-font-size',
213
230
  )
214
231
  */
215
232
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/style-engine",
3
- "version": "1.29.0",
3
+ "version": "1.30.0",
4
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",
@@ -35,5 +35,5 @@
35
35
  "publishConfig": {
36
36
  "access": "public"
37
37
  },
38
- "gitHead": "839018ff6029ba749780e288e08ff9cd898e50e8"
38
+ "gitHead": "d98dff8ea96f29cfea045bf964269f46f040d539"
39
39
  }