@wordpress/style-engine 1.0.1 → 1.1.1-next.4d3b314fd5.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 (43) hide show
  1. package/CHANGELOG.md +2 -0
  2. package/README.md +51 -38
  3. package/build/styles/border/index.js +37 -50
  4. package/build/styles/border/index.js.map +1 -1
  5. package/build/styles/utils.js +19 -5
  6. package/build/styles/utils.js.map +1 -1
  7. package/build-module/styles/border/index.js +38 -51
  8. package/build-module/styles/border/index.js.map +1 -1
  9. package/build-module/styles/utils.js +18 -6
  10. package/build-module/styles/utils.js.map +1 -1
  11. package/build-types/styles/border/index.d.ts +2 -5
  12. package/build-types/styles/border/index.d.ts.map +1 -1
  13. package/build-types/styles/color/background.d.ts +1 -5
  14. package/build-types/styles/color/background.d.ts.map +1 -1
  15. package/build-types/styles/color/gradient.d.ts +1 -5
  16. package/build-types/styles/color/gradient.d.ts.map +1 -1
  17. package/build-types/styles/color/index.d.ts +1 -5
  18. package/build-types/styles/color/index.d.ts.map +1 -1
  19. package/build-types/styles/color/text.d.ts +1 -5
  20. package/build-types/styles/color/text.d.ts.map +1 -1
  21. package/build-types/styles/shadow/index.d.ts +1 -5
  22. package/build-types/styles/shadow/index.d.ts.map +1 -1
  23. package/build-types/styles/typography/index.d.ts +1 -5
  24. package/build-types/styles/typography/index.d.ts.map +1 -1
  25. package/build-types/styles/utils.d.ts +12 -8
  26. package/build-types/styles/utils.d.ts.map +1 -1
  27. package/build-types/types.d.ts +19 -16
  28. package/build-types/types.d.ts.map +1 -1
  29. package/class-wp-style-engine-css-declarations.php +1 -1
  30. package/class-wp-style-engine-css-rule.php +1 -1
  31. package/class-wp-style-engine-css-rules-store.php +1 -1
  32. package/class-wp-style-engine-processor.php +1 -1
  33. package/docs/toc.json +8 -0
  34. package/docs/using-the-style-engine-with-block-supports.md +92 -32
  35. package/package.json +2 -2
  36. package/phpunit/style-engine-test.php +6 -6
  37. package/src/styles/border/index.ts +49 -99
  38. package/src/styles/utils.ts +19 -5
  39. package/src/test/index.js +13 -0
  40. package/src/test/utils.js +31 -1
  41. package/src/types.ts +23 -20
  42. package/style-engine.php +4 -4
  43. package/tsconfig.tsbuildinfo +1 -1
package/CHANGELOG.md CHANGED
@@ -2,6 +2,8 @@
2
2
 
3
3
  ## Unreleased
4
4
 
5
+ ## 1.1.0 (2022-09-21)
6
+
5
7
  ### Enhancement
6
8
  - Allow for prettified output ([#42909](https://github.com/WordPress/gutenberg/pull/42909)).
7
9
  - Enqueue block supports styles in Gutenberg ([#42880](https://github.com/WordPress/gutenberg/pull/42880)).
package/README.md CHANGED
@@ -4,7 +4,7 @@ The Style Engine aims to provide a consistent API for rendering styling for bloc
4
4
 
5
5
  Initially, it will offer a single, centralized agent responsible for generating block styles, and, in later phases, it will also assume the responsibility of processing and rendering optimized frontend CSS.
6
6
 
7
- ## Important
7
+ ## Please note
8
8
 
9
9
  This package is new as of WordPress 6.1 and therefore in its infancy.
10
10
 
@@ -23,23 +23,17 @@ For more information about the roadmap, please refer to [Block editor styles: in
23
23
 
24
24
  ### wp_style_engine_get_styles()
25
25
 
26
- Global public function to generate styles from a single style object, e.g., the value of
27
- a [block's attributes.style object](https://developer.wordpress.org/block-editor/reference-guides/theme-json-reference/theme-json-living/#styles)
28
- or
29
- the [top level styles in theme.json](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/).
26
+ Global public function to generate styles from a single style object, e.g., the value of a [block's attributes.style object](https://developer.wordpress.org/block-editor/reference-guides/theme-json-reference/theme-json-living/#styles) or the [top level styles in theme.json](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/).
30
27
 
31
- See also [Using the Style Engine to generate block supports styles](https://developer.wordpress.org/block-editor/reference-guides/packages/packages-style-engine/using-the-style-engine-with-block-supports).
28
+ See also [Using the Style Engine to generate block supports styles](https://github.com/WordPress/gutenberg/tree/HEAD/packages/style-engine/docs/using-the-style-engine-with-block-supports.md).
32
29
 
33
30
  _Parameters_
34
31
 
35
32
  - _$block_styles_ `array` A block's `attributes.style` object or the top level styles in theme.json
36
33
  - _$options_ `array<string|boolean>` An array of options to determine the output.
37
- - _context_ `string` An identifier describing the origin of the style object, e.g., 'block-supports' or '
38
- global-styles'. Default is 'block-supports'. When both `context` and `selector` are set, the style engine will store the CSS rules using the `context` as a key.
39
- - _convert_vars_to_classnames_ `boolean` Whether to skip converting CSS var:? values to var( --wp--preset--\* )
40
- values. Default is `false`.
41
- - _selector_ `string` When a selector is passed, `generate()` will return a full CSS rule `$selector { ...rules }`,
42
- otherwise a concatenated string of properties and values.
34
+ - _context_ `string` An identifier describing the origin of the style object, e.g., 'block-supports' or 'global-styles'. Default is 'block-supports'. When both `context` and `selector` are set, the Style Engine will store the CSS rules using the `context` as a key.
35
+ - _convert_vars_to_classnames_ `boolean` Whether to skip converting CSS var:? values to var( --wp--preset--\* ) values. Default is `false`.
36
+ - _selector_ `string` When a selector is passed, `generate()` will return a full CSS rule `$selector { ...rules }`, otherwise a concatenated string of properties and values.
43
37
 
44
38
  _Returns_
45
39
  `array<string|array>|null`
@@ -57,19 +51,25 @@ It will return compiled CSS declarations for inline styles, or, where a selector
57
51
  To enqueue a style for rendering in the site's frontend, the `$options` array requires the following:
58
52
 
59
53
  1. **selector (string)** - this is the CSS selector for your block style CSS declarations.
60
- 2. **context (string)** - this tells the style engine where to store the styles. Styles in the same context will be
61
- stored together.
54
+ 2. **context (string)** - this tells the Style Engine where to store the styles. Styles in the same context will be stored together.
62
55
 
63
56
  `wp_style_engine_get_styles` will return the compiled CSS and CSS declarations array.
64
57
 
65
58
  #### Usage
66
59
 
60
+ As mentioned, `wp_style_engine_get_styles()` is useful whenever you wish to generate CSS and/or classnames from a **block's style object**. A good example is [using the Style Engine to generate block supports styles](https://github.com/WordPress/gutenberg/tree/HEAD/packages/style-engine/docs/using-the-style-engine-with-block-supports.md).
61
+
62
+ In the following snippet, we're taking the style object from a block's attributes and passing it to the Style Engine to get the styles. By passing a `context` in the options, the Style Engine will store the styles for later retrieval, for example, should you wish to batch enqueue a set of CSS rules.
63
+
67
64
  ```php
68
- $block_styles = array(
69
- 'spacing' => array( 'padding' => '100px' )
65
+ $block_attributes = array(
66
+ 'style' => array(
67
+ 'spacing' => array( 'padding' => '100px' ),
68
+ ),
70
69
  );
70
+
71
71
  $styles = wp_style_engine_get_styles(
72
- $block_styles,
72
+ $block_attributes['style'],
73
73
  array(
74
74
  'selector' => '.a-selector',
75
75
  'context' => 'block-supports',
@@ -79,46 +79,53 @@ print_r( $styles );
79
79
 
80
80
  /*
81
81
  array(
82
- 'css' => '.a-selector{padding:100px}'
83
- 'declarations' => array( 'padding' => '100px' )
82
+ 'css' => '.a-selector{padding:100px}'
83
+ 'declarations' => array( 'padding' => '100px' )
84
84
  )
85
85
  */
86
86
  ```
87
87
 
88
88
  ### wp_style_engine_get_stylesheet_from_css_rules()
89
89
 
90
- Use this function to compile and return a stylesheet for any CSS rules. The style engine will automatically merge declarations and combine selectors.
90
+ Use this function to compile and return a stylesheet for any CSS rules. The Style Engine will automatically merge declarations and combine selectors.
91
91
 
92
- This function acts as a CSS compiler, but will also enqueue styles for rendering where `enqueue` and `context` strings are passed in the options.
92
+ This function acts as a CSS compiler, but will also register the styles in a store where a `context` string is passed in the options.
93
93
 
94
94
  _Parameters_
95
95
 
96
96
  - _$css_rules_ `array<array>`
97
- - _$options_ `array<string|boolean>` An array of options to determine the output.
98
- - _context_ `string` An identifier describing the origin of the style object, e.g., 'block-supports' or '
99
- global-styles'. When set, the style engine will store the CSS rules using the `context` value as a key.
97
+ - _$options_ `array<string|bool>` An array of options to determine the output.
98
+ - _context_ `string` An identifier describing the origin of the style object, e.g., 'block-supports' or 'global-styles'. Default is 'block-supports'. When set, the Style Engine will attempt to store the CSS rules.
99
+ - _prettify_ `bool` Whether to add new lines and indents to output. Default is to inherit the value of the global constant `SCRIPT_DEBUG`, if it is defined.
100
+ - _optimize_ `bool` Whether to optimize the CSS output, e.g., combine rules. Default is `false`.
100
101
 
101
102
  _Returns_
102
103
  `string` A compiled CSS string based on `$css_rules`.
103
104
 
104
105
  #### Usage
105
106
 
107
+ Useful for when you wish to compile a bespoke set of CSS rules from a series of selector + declaration items.
108
+
109
+ The Style Engine will return a sanitized and optimized stylesheet. By passing a `context` identifier in the options, the Style Engine will store the styles for later retrieval, for example, should you wish to batch enqueue a set of CSS rules.
110
+
111
+ You can call `wp_style_engine_get_stylesheet_from_css_rules()` multiple times, and, so long as your styles use the same `context` identifier, they will be stored together.
112
+
106
113
  ```php
107
114
  $styles = array(
108
115
  array(
109
- 'selector'. => '.wp-pumpkin',
116
+ 'selector' => '.wp-pumpkin',
110
117
  'declarations' => array( 'color' => 'orange' )
111
118
  ),
112
119
  array(
113
- 'selector'. => '.wp-tomato',
120
+ 'selector' => '.wp-tomato',
114
121
  'declarations' => array( 'color' => 'red' )
115
122
  ),
116
123
  array(
117
- 'selector'. => '.wp-tomato',
124
+ 'selector' => '.wp-tomato',
118
125
  'declarations' => array( 'padding' => '100px' )
119
126
  ),
120
127
  array(
121
- 'selector'. => '.wp-kumquat',
128
+ 'selector' => '.wp-kumquat',
122
129
  'declarations' => array( 'color' => 'orange' )
123
130
  ),
124
131
  );
@@ -126,7 +133,7 @@ $styles = array(
126
133
  $stylesheet = wp_style_engine_get_stylesheet_from_css_rules(
127
134
  $styles,
128
135
  array(
129
- 'context' => 'block-supports', // Indicates that these styles should be stored with block supports CSS.
136
+ 'context' => 'block-supports', // Indicates that these styles should be stored with block supports CSS.
130
137
  )
131
138
  );
132
139
  print_r( $stylesheet ); // .wp-pumpkin,.wp-kumquat{color:orange}.wp-tomato{color:red;padding:100px}
@@ -139,34 +146,40 @@ Returns compiled CSS from a stored context, if found.
139
146
  _Parameters_
140
147
 
141
148
  - _$store_name_ `string` An identifier describing the origin of the style object, e.g., 'block-supports' or ' global-styles'. Default is 'block-supports'.
149
+ - _$options_ `array<bool>` An array of options to determine the output.
150
+ - _prettify_ `bool` Whether to add new lines and indents to output. Default is to inherit the value of the global constant `SCRIPT_DEBUG`, if it is defined.
151
+ - _optimize_ `bool` Whether to optimize the CSS output, e.g., combine rules. Default is `false`.
142
152
 
143
153
  _Returns_
144
154
  `string` A compiled CSS string from the stored CSS rules.
145
155
 
146
156
  #### Usage
147
157
 
148
- A use case would be to fetch the stylesheet, which contains all the compiled CSS rules from the store, and enqueue it for rendering on the frontend.
158
+ Use this function to generate a stylesheet using all the styles stored under a specific context identifier.
159
+
160
+ A use case would be when you wish to enqueue all stored styles for rendering to the frontend. The Style Engine will merge and deduplicate styles upon retrieval.
149
161
 
150
162
  ```php
151
- // First register some styles.
163
+ // First, let's gather and register our styles.
152
164
  $styles = array(
153
165
  array(
154
- 'selector'. => '.wp-apple',
166
+ 'selector' => '.wp-apple',
155
167
  'declarations' => array( 'color' => 'green' )
156
168
  ),
157
169
  );
158
170
 
159
- $stylesheet = wp_style_engine_get_stylesheet_from_css_rules(
171
+ wp_style_engine_get_stylesheet_from_css_rules(
160
172
  $styles,
161
173
  array(
162
- 'context' => 'fruit-styles',
163
- 'enqueue' => true,
174
+ 'context' => 'fruit-styles',
164
175
  )
165
176
  );
166
177
 
167
- // Later, fetch compiled rules from context store.
168
- $stylesheet = gutenberg_style_engine_get_stylesheet_from_context( 'fruit-styles' );
178
+ // Later, we fetch compiled rules from context store.
179
+ $stylesheet = wp_style_engine_get_stylesheet_from_context( 'fruit-styles' );
180
+
169
181
  print_r( $stylesheet ); // .wp-apple{color:green;}
182
+
170
183
  if ( ! empty( $stylesheet ) ) {
171
184
  wp_register_style( 'my-stylesheet', false, array(), true, true );
172
185
  wp_add_inline_style( 'my-stylesheet', $stylesheet );
@@ -242,7 +255,7 @@ A guide to the terms and variable names referenced by the Style Engine package.
242
255
  <dt>CSS rule</dt>
243
256
  <dd>A CSS selector followed by a CSS declarations block inside a set of curly braces. Usually found in a CSS stylesheet.</dd>
244
257
  <dt>CSS selector (or CSS class selector)</dt>
245
- <dd>The first component of a CSS rule, a CSS selector is a pattern of elements, classnames or other terms that define the element to which the rule&rsquo;s CSS definitions apply. E.g., <code>p.my-cool-classname > span</code>. A CSS selector matches HTML elements based on the contents of the "class" attribute. See <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors" target="_blank">MDN CSS selectors article</a>.</dd>
258
+ <dd>The first component of a CSS rule, a CSS selector is a pattern of elements, classnames or other terms that define the element to which the rule&rsquo;s CSS definitions apply. E.g., <code>p.my-cool-classname > span</code>. A CSS selector matches HTML elements based on the contents of the "class" attribute. See <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors" target="_blank">MDN CSS selectors article</a>.</dd>
246
259
  <dt>CSS stylesheet</dt>
247
260
  <dd>A collection of CSS rules contained within a file or within an <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/style" target="_blank">HTML style tag</a>.</dd>
248
261
  <dt>CSS value</dt>
@@ -10,13 +10,38 @@ var _utils = require("../utils");
10
10
  /**
11
11
  * Internal dependencies
12
12
  */
13
+
14
+ /**
15
+ * Creates a function for generating CSS rules when the style path is the same as the camelCase CSS property used in React.
16
+ *
17
+ * @param path An array of strings representing the path to the style value in the style object.
18
+ *
19
+ * @return A function that generates CSS rules.
20
+ */
21
+ function createBorderGenerateFunction(path) {
22
+ return (style, options) => (0, _utils.generateRule)(style, options, path, (0, _utils.camelCaseJoin)(path));
23
+ }
24
+ /**
25
+ * Creates a function for generating border-{top,bottom,left,right}-{color,style,width} CSS rules.
26
+ *
27
+ * @param edge The edge to create CSS rules for.
28
+ *
29
+ * @return A function that generates CSS rules.
30
+ */
31
+
32
+
33
+ function createBorderEdgeGenerateFunction(edge) {
34
+ return (style, options) => {
35
+ return ['color', 'style', 'width'].flatMap(key => {
36
+ const path = ['border', edge, key];
37
+ return createBorderGenerateFunction(path)(style, options);
38
+ });
39
+ };
40
+ }
41
+
13
42
  const color = {
14
43
  name: 'color',
15
- generate: function (style, options) {
16
- let path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ['border', 'color'];
17
- let ruleKey = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'borderColor';
18
- return (0, _utils.generateRule)(style, options, path, ruleKey);
19
- }
44
+ generate: createBorderGenerateFunction(['border', 'color'])
20
45
  };
21
46
  const radius = {
22
47
  name: 'radius',
@@ -29,66 +54,28 @@ const radius = {
29
54
  };
30
55
  const borderStyle = {
31
56
  name: 'style',
32
- generate: function (style, options) {
33
- let path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ['border', 'style'];
34
- let ruleKey = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'borderStyle';
35
- return (0, _utils.generateRule)(style, options, path, ruleKey);
36
- }
57
+ generate: createBorderGenerateFunction(['border', 'style'])
37
58
  };
38
59
  const width = {
39
60
  name: 'width',
40
- generate: function (style, options) {
41
- let path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ['border', 'width'];
42
- let ruleKey = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'borderWidth';
43
- return (0, _utils.generateRule)(style, options, path, ruleKey);
44
- }
45
- };
46
- const borderDefinitionsWithIndividualStyles = [color, borderStyle, width];
47
- /**
48
- * Returns a curried generator function with the individual border property ('top' | 'right' | 'bottom' | 'left') baked in.
49
- *
50
- * @param individualProperty Individual border property ('top' | 'right' | 'bottom' | 'left').
51
- *
52
- * @return StyleDefinition[ 'generate' ]
53
- */
54
-
55
- const createBorderGenerateFunction = individualProperty => (style, options) => {
56
- var _style$border;
57
-
58
- const styleValue = style === null || style === void 0 ? void 0 : (_style$border = style.border) === null || _style$border === void 0 ? void 0 : _style$border[individualProperty];
59
-
60
- if (!styleValue) {
61
- return [];
62
- }
63
-
64
- return borderDefinitionsWithIndividualStyles.reduce((acc, borderDefinition) => {
65
- const key = borderDefinition.name;
66
-
67
- if (styleValue.hasOwnProperty(key) && typeof borderDefinition.generate === 'function') {
68
- const ruleKey = `border${(0, _utils.upperFirst)(individualProperty)}${(0, _utils.upperFirst)(key)}`;
69
- acc.push(...borderDefinition.generate(style, options, ['border', individualProperty, key], ruleKey));
70
- }
71
-
72
- return acc;
73
- }, []);
61
+ generate: createBorderGenerateFunction(['border', 'width'])
74
62
  };
75
-
76
63
  const borderTop = {
77
64
  name: 'borderTop',
78
- generate: createBorderGenerateFunction('top')
65
+ generate: createBorderEdgeGenerateFunction('top')
79
66
  };
80
67
  const borderRight = {
81
68
  name: 'borderRight',
82
- generate: createBorderGenerateFunction('right')
69
+ generate: createBorderEdgeGenerateFunction('right')
83
70
  };
84
71
  const borderBottom = {
85
72
  name: 'borderBottom',
86
- generate: createBorderGenerateFunction('bottom')
73
+ generate: createBorderEdgeGenerateFunction('bottom')
87
74
  };
88
75
  const borderLeft = {
89
76
  name: 'borderLeft',
90
- generate: createBorderGenerateFunction('left')
77
+ generate: createBorderEdgeGenerateFunction('left')
91
78
  };
92
- var _default = [...borderDefinitionsWithIndividualStyles, radius, borderTop, borderRight, borderBottom, borderLeft];
79
+ var _default = [color, borderStyle, width, radius, borderTop, borderRight, borderBottom, borderLeft];
93
80
  exports.default = _default;
94
81
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/style-engine/src/styles/border/index.ts"],"names":["color","name","generate","style","options","path","ruleKey","radius","default","individual","borderStyle","width","borderDefinitionsWithIndividualStyles","createBorderGenerateFunction","individualProperty","styleValue","border","reduce","acc","borderDefinition","key","hasOwnProperty","push","borderTop","borderRight","borderBottom","borderLeft"],"mappings":";;;;;;;AAWA;;AAXA;AACA;AACA;AAWA,MAAMA,KAAK,GAAG;AACbC,EAAAA,IAAI,EAAE,OADO;AAEbC,EAAAA,QAAQ,EAAE,UACTC,KADS,EAETC,OAFS,EAKe;AAAA,QAFxBC,IAEwB,uEAFP,CAAE,QAAF,EAAY,OAAZ,CAEO;AAAA,QADxBC,OACwB,uEADN,aACM;AACxB,WAAO,yBAAcH,KAAd,EAAqBC,OAArB,EAA8BC,IAA9B,EAAoCC,OAApC,CAAP;AACA;AATY,CAAd;AA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*/\nimport type {\n\tBorderIndividualStyles,\n\tBorderIndividualProperty,\n\tGeneratedCSSRule,\n\tStyle,\n\tStyleDefinition,\n\tStyleOptions,\n} from '../../types';\nimport { generateRule, generateBoxRules, upperFirst } from '../utils';\n\nconst color = {\n\tname: 'color',\n\tgenerate: (\n\t\tstyle: Style,\n\t\toptions: StyleOptions,\n\t\tpath: string[] = [ 'border', 'color' ],\n\t\truleKey: string = 'borderColor'\n\t): GeneratedCSSRule[] => {\n\t\treturn generateRule( style, options, path, ruleKey );\n\t},\n};\n\nconst radius = {\n\tname: 'radius',\n\tgenerate: ( style: Style, options: StyleOptions ): GeneratedCSSRule[] => {\n\t\treturn generateBoxRules(\n\t\t\tstyle,\n\t\t\toptions,\n\t\t\t[ 'border', 'radius' ],\n\t\t\t{\n\t\t\t\tdefault: 'borderRadius',\n\t\t\t\tindividual: 'border%sRadius',\n\t\t\t},\n\t\t\t[ 'topLeft', 'topRight', 'bottomLeft', 'bottomRight' ]\n\t\t);\n\t},\n};\n\nconst borderStyle = {\n\tname: 'style',\n\tgenerate: (\n\t\tstyle: Style,\n\t\toptions: StyleOptions,\n\t\tpath: string[] = [ 'border', 'style' ],\n\t\truleKey: string = 'borderStyle'\n\t): GeneratedCSSRule[] => {\n\t\treturn generateRule( style, options, path, ruleKey );\n\t},\n};\n\nconst width = {\n\tname: 'width',\n\tgenerate: (\n\t\tstyle: Style,\n\t\toptions: StyleOptions,\n\t\tpath: string[] = [ 'border', 'width' ],\n\t\truleKey: string = 'borderWidth'\n\t): GeneratedCSSRule[] => {\n\t\treturn generateRule( style, options, path, ruleKey );\n\t},\n};\n\nconst borderDefinitionsWithIndividualStyles: StyleDefinition[] = [\n\tcolor,\n\tborderStyle,\n\twidth,\n];\n\n/**\n * Returns a curried generator function with the individual border property ('top' | 'right' | 'bottom' | 'left') baked in.\n *\n * @param individualProperty Individual border property ('top' | 'right' | 'bottom' | 'left').\n *\n * @return StyleDefinition[ 'generate' ]\n */\nconst createBorderGenerateFunction =\n\t( individualProperty: BorderIndividualProperty ) =>\n\t( style: Style, options: StyleOptions ) => {\n\t\tconst styleValue:\n\t\t\t| BorderIndividualStyles< typeof individualProperty >\n\t\t\t| undefined = style?.border?.[ individualProperty ];\n\n\t\tif ( ! styleValue ) {\n\t\t\treturn [];\n\t\t}\n\n\t\treturn borderDefinitionsWithIndividualStyles.reduce(\n\t\t\t(\n\t\t\t\tacc: GeneratedCSSRule[],\n\t\t\t\tborderDefinition: StyleDefinition\n\t\t\t): GeneratedCSSRule[] => {\n\t\t\t\tconst key = borderDefinition.name;\n\t\t\t\tif (\n\t\t\t\t\tstyleValue.hasOwnProperty( key ) &&\n\t\t\t\t\ttypeof borderDefinition.generate === 'function'\n\t\t\t\t) {\n\t\t\t\t\tconst ruleKey = `border${ upperFirst(\n\t\t\t\t\t\tindividualProperty\n\t\t\t\t\t) }${ upperFirst( key ) }`;\n\t\t\t\t\tacc.push(\n\t\t\t\t\t\t...borderDefinition.generate(\n\t\t\t\t\t\t\tstyle,\n\t\t\t\t\t\t\toptions,\n\t\t\t\t\t\t\t[ 'border', individualProperty, key ],\n\t\t\t\t\t\t\truleKey\n\t\t\t\t\t\t)\n\t\t\t\t\t);\n\t\t\t\t}\n\t\t\t\treturn acc;\n\t\t\t},\n\t\t\t[]\n\t\t);\n\t};\n\nconst borderTop = {\n\tname: 'borderTop',\n\tgenerate: createBorderGenerateFunction( 'top' ),\n};\n\nconst borderRight = {\n\tname: 'borderRight',\n\tgenerate: createBorderGenerateFunction( 'right' ),\n};\n\nconst borderBottom = {\n\tname: 'borderBottom',\n\tgenerate: createBorderGenerateFunction( 'bottom' ),\n};\n\nconst borderLeft = {\n\tname: 'borderLeft',\n\tgenerate: createBorderGenerateFunction( 'left' ),\n};\n\nexport default [\n\t...borderDefinitionsWithIndividualStyles,\n\tradius,\n\tborderTop,\n\tborderRight,\n\tborderBottom,\n\tborderLeft,\n];\n"]}
1
+ {"version":3,"sources":["@wordpress/style-engine/src/styles/border/index.ts"],"names":["createBorderGenerateFunction","path","style","options","createBorderEdgeGenerateFunction","edge","flatMap","key","color","name","generate","radius","default","individual","borderStyle","width","borderTop","borderRight","borderBottom","borderLeft"],"mappings":";;;;;;;AAIA;;AAJA;AACA;AACA;;AAIA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,SAASA,4BAAT,CAAuCC,IAAvC,EAA0E;AACzE,SAAO,CAAEC,KAAF,EAASC,OAAT,KACN,yBAAcD,KAAd,EAAqBC,OAArB,EAA8BF,IAA9B,EAAoC,0BAAeA,IAAf,CAApC,CADD;AAEA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;;AACA,SAASG,gCAAT,CAA2CC,IAA3C,EAA6E;AAC5E,SAAO,CAAEH,KAAF,EAASC,OAAT,KAAsB;AAC5B,WAAO,CAAE,OAAF,EAAW,OAAX,EAAoB,OAApB,EAA8BG,OAA9B,CAAyCC,GAAF,IAAW;AACxD,YAAMN,IAAI,GAAG,CAAE,QAAF,EAAYI,IAAZ,EAAkBE,GAAlB,CAAb;AACA,aAAOP,4BAA4B,CAAEC,IAAF,CAA5B,CAAsCC,KAAtC,EAA6CC,OAA7C,CAAP;AACA,KAHM,CAAP;AAIA,GALD;AAMA;;AAED,MAAMK,KAAsB,GAAG;AAC9BC,EAAAA,IAAI,EAAE,OADwB;AAE9BC,EAAAA,QAAQ,EAAEV,4BAA4B,CAAE,CAAE,QAAF,EAAY,OAAZ,CAAF;AAFR,CAA/B;AAKA,MAAMW,MAAuB,GAAG;AAC/BF,EAAAA,IAAI,EAAE,QADyB;AAE/BC,EAAAA,QAAQ,EAAE,CAAER,KAAF,EAASC,OAAT,KAAsB;AAC/B,WAAO,6BACND,KADM,EAENC,OAFM,EAGN,CAAE,QAAF,EAAY,QAAZ,CAHM,EAIN;AACCS,MAAAA,OAAO,EAAE,cADV;AAECC,MAAAA,UAAU,EAAE;AAFb,KAJM,EAQN,CAAE,SAAF,EAAa,UAAb,EAAyB,YAAzB,EAAuC,aAAvC,CARM,CAAP;AAUA;AAb8B,CAAhC;AAgBA,MAAMC,WAA4B,GAAG;AACpCL,EAAAA,IAAI,EAAE,OAD8B;AAEpCC,EAAAA,QAAQ,EAAEV,4BAA4B,CAAE,CAAE,QAAF,EAAY,OAAZ,CAAF;AAFF,CAArC;AAKA,MAAMe,KAAsB,GAAG;AAC9BN,EAAAA,IAAI,EAAE,OADwB;AAE9BC,EAAAA,QAAQ,EAAEV,4BAA4B,CAAE,CAAE,QAAF,EAAY,OAAZ,CAAF;AAFR,CAA/B;AAKA,MAAMgB,SAA0B,GAAG;AAClCP,EAAAA,IAAI,EAAE,WAD4B;AAElCC,EAAAA,QAAQ,EAAEN,gCAAgC,CAAE,KAAF;AAFR,CAAnC;AAKA,MAAMa,WAA4B,GAAG;AACpCR,EAAAA,IAAI,EAAE,aAD8B;AAEpCC,EAAAA,QAAQ,EAAEN,gCAAgC,CAAE,OAAF;AAFN,CAArC;AAKA,MAAMc,YAA6B,GAAG;AACrCT,EAAAA,IAAI,EAAE,cAD+B;AAErCC,EAAAA,QAAQ,EAAEN,gCAAgC,CAAE,QAAF;AAFL,CAAtC;AAKA,MAAMe,UAA2B,GAAG;AACnCV,EAAAA,IAAI,EAAE,YAD6B;AAEnCC,EAAAA,QAAQ,EAAEN,gCAAgC,CAAE,MAAF;AAFP,CAApC;eAKe,CACdI,KADc,EAEdM,WAFc,EAGdC,KAHc,EAIdJ,MAJc,EAKdK,SALc,EAMdC,WANc,EAOdC,YAPc,EAQdC,UARc,C","sourcesContent":["/**\n * Internal dependencies\n */\nimport type { BoxEdge, GenerateFunction, StyleDefinition } from '../../types';\nimport { generateRule, generateBoxRules, camelCaseJoin } from '../utils';\n\n/**\n * Creates a function for generating CSS rules when the style path is the same as the camelCase CSS property used in React.\n *\n * @param path An array of strings representing the path to the style value in the style object.\n *\n * @return A function that generates CSS rules.\n */\nfunction createBorderGenerateFunction( path: string[] ): GenerateFunction {\n\treturn ( style, options ) =>\n\t\tgenerateRule( style, options, path, camelCaseJoin( path ) );\n}\n\n/**\n * Creates a function for generating border-{top,bottom,left,right}-{color,style,width} CSS rules.\n *\n * @param edge The edge to create CSS rules for.\n *\n * @return A function that generates CSS rules.\n */\nfunction createBorderEdgeGenerateFunction( edge: BoxEdge ): GenerateFunction {\n\treturn ( style, options ) => {\n\t\treturn [ 'color', 'style', 'width' ].flatMap( ( key ) => {\n\t\t\tconst path = [ 'border', edge, key ];\n\t\t\treturn createBorderGenerateFunction( path )( style, options );\n\t\t} );\n\t};\n}\n\nconst color: StyleDefinition = {\n\tname: 'color',\n\tgenerate: createBorderGenerateFunction( [ 'border', 'color' ] ),\n};\n\nconst radius: StyleDefinition = {\n\tname: 'radius',\n\tgenerate: ( style, options ) => {\n\t\treturn generateBoxRules(\n\t\t\tstyle,\n\t\t\toptions,\n\t\t\t[ 'border', 'radius' ],\n\t\t\t{\n\t\t\t\tdefault: 'borderRadius',\n\t\t\t\tindividual: 'border%sRadius',\n\t\t\t},\n\t\t\t[ 'topLeft', 'topRight', 'bottomLeft', 'bottomRight' ]\n\t\t);\n\t},\n};\n\nconst borderStyle: StyleDefinition = {\n\tname: 'style',\n\tgenerate: createBorderGenerateFunction( [ 'border', 'style' ] ),\n};\n\nconst width: StyleDefinition = {\n\tname: 'width',\n\tgenerate: createBorderGenerateFunction( [ 'border', 'width' ] ),\n};\n\nconst borderTop: StyleDefinition = {\n\tname: 'borderTop',\n\tgenerate: createBorderEdgeGenerateFunction( 'top' ),\n};\n\nconst borderRight: StyleDefinition = {\n\tname: 'borderRight',\n\tgenerate: createBorderEdgeGenerateFunction( 'right' ),\n};\n\nconst borderBottom: StyleDefinition = {\n\tname: 'borderBottom',\n\tgenerate: createBorderEdgeGenerateFunction( 'bottom' ),\n};\n\nconst borderLeft: StyleDefinition = {\n\tname: 'borderLeft',\n\tgenerate: createBorderEdgeGenerateFunction( 'left' ),\n};\n\nexport default [\n\tcolor,\n\tborderStyle,\n\twidth,\n\tradius,\n\tborderTop,\n\tborderRight,\n\tborderBottom,\n\tborderLeft,\n];\n"]}
@@ -3,6 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
+ exports.camelCaseJoin = camelCaseJoin;
6
7
  exports.generateBoxRules = generateBoxRules;
7
8
  exports.generateRule = generateRule;
8
9
  exports.getCSSVarFromStyleValue = getCSSVarFromStyleValue;
@@ -93,7 +94,7 @@ function generateBoxRules(style, options, path, ruleKeys) {
93
94
 
94
95
  function getCSSVarFromStyleValue(styleValue) {
95
96
  if (typeof styleValue === 'string' && styleValue.startsWith(_constants.VARIABLE_REFERENCE_PREFIX)) {
96
- const variable = styleValue.slice(_constants.VARIABLE_REFERENCE_PREFIX.length).split(_constants.VARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE).join(_constants.VARIABLE_PATH_SEPARATOR_TOKEN_STYLE);
97
+ const variable = styleValue.slice(_constants.VARIABLE_REFERENCE_PREFIX.length).split(_constants.VARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE).map(presetVariable => (0, _lodash.kebabCase)(presetVariable)).join(_constants.VARIABLE_PATH_SEPARATOR_TOKEN_STYLE);
97
98
  return `var(--wp--${variable})`;
98
99
  }
99
100
 
@@ -102,14 +103,27 @@ function getCSSVarFromStyleValue(styleValue) {
102
103
  /**
103
104
  * Capitalizes the first letter in a string.
104
105
  *
105
- * @param {string} str The string whose first letter the function will capitalize.
106
+ * @param string The string whose first letter the function will capitalize.
106
107
  *
107
- * @return string A CSS var value.
108
+ * @return String with the first letter capitalized.
108
109
  */
109
110
 
110
111
 
111
- function upperFirst(_ref) {
112
- let [firstLetter, ...rest] = _ref;
112
+ function upperFirst(string) {
113
+ const [firstLetter, ...rest] = string;
113
114
  return firstLetter.toUpperCase() + rest.join('');
114
115
  }
116
+ /**
117
+ * Converts an array of strings into a camelCase string.
118
+ *
119
+ * @param strings The strings to join into a camelCase string.
120
+ *
121
+ * @return camelCase string.
122
+ */
123
+
124
+
125
+ function camelCaseJoin(strings) {
126
+ const [firstItem, ...rest] = strings;
127
+ return firstItem.toLowerCase() + rest.map(upperFirst).join('');
128
+ }
115
129
  //# sourceMappingURL=utils.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["@wordpress/style-engine/src/styles/utils.ts"],"names":["generateRule","style","options","path","ruleKey","styleValue","selector","key","value","getCSSVarFromStyleValue","generateBoxRules","ruleKeys","individualProperties","boxStyle","rules","push","default","sideRules","reduce","acc","side","individual","replace","upperFirst","startsWith","VARIABLE_REFERENCE_PREFIX","variable","slice","length","split","VARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE","join","VARIABLE_PATH_SEPARATOR_TOKEN_STYLE","firstLetter","rest","toUpperCase"],"mappings":";;;;;;;;;;AAGA;;AAYA;;AAfA;AACA;AACA;;AAmBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,YAAT,CACNC,KADM,EAENC,OAFM,EAGNC,IAHM,EAINC,OAJM,EAKL;AACD,QAAMC,UAA8B,GAAG,iBAAKJ,KAAL,EAAYE,IAAZ,CAAvC;AAEA,SAAOE,UAAU,GACd,CACA;AACCC,IAAAA,QAAQ,EAAEJ,OAAF,aAAEA,OAAF,uBAAEA,OAAO,CAAEI,QADpB;AAECC,IAAAA,GAAG,EAAEH,OAFN;AAGCI,IAAAA,KAAK,EAAEC,uBAAuB,CAAEJ,UAAF;AAH/B,GADA,CADc,GAQd,EARH;AASA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASK,gBAAT,CACNT,KADM,EAENC,OAFM,EAGNC,IAHM,EAINQ,QAJM,EAMe;AAAA,MADrBC,oBACqB,uEADY,CAAE,KAAF,EAAS,OAAT,EAAkB,QAAlB,EAA4B,MAA5B,CACZ;AACrB,QAAMC,QAAkC,GAAG,iBAAKZ,KAAL,EAAYE,IAAZ,CAA3C;;AACA,MAAK,CAAEU,QAAP,EAAkB;AACjB,WAAO,EAAP;AACA;;AAED,QAAMC,KAAyB,GAA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,QAAU,GAA/B;AACA;;AACD,SAAOrB,UAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASkB,UAAT,OAAwD;AAAA,MAAnC,CAAEU,WAAF,EAAe,GAAGC,IAAlB,CAAmC;AAC9D,SAAOD,WAAW,CAACE,WAAZ,KAA4BD,IAAI,CAACH,IAAL,CAAW,EAAX,CAAnC;AACA","sourcesContent":["/**\n * External dependencies\n */\nimport { get } from 'lodash';\n\n/**\n * Internal dependencies\n */\nimport type {\n\tCssRulesKeys,\n\tGeneratedCSSRule,\n\tStyle,\n\tBox,\n\tStyleOptions,\n} from '../types';\nimport {\n\tVARIABLE_REFERENCE_PREFIX,\n\tVARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE,\n\tVARIABLE_PATH_SEPARATOR_TOKEN_STYLE,\n} from './constants';\n\n/**\n * Returns a JSON representation of the generated CSS rules.\n *\n * @param style Style object.\n * @param options Options object with settings to adjust how the styles are generated.\n * @param path An array of strings representing the path to the style value in the style object.\n * @param ruleKey A CSS property key.\n *\n * @return GeneratedCSSRule[] CSS rules.\n */\nexport function generateRule(\n\tstyle: Style,\n\toptions: StyleOptions,\n\tpath: string[],\n\truleKey: string\n) {\n\tconst styleValue: string | undefined = get( style, path );\n\n\treturn styleValue\n\t\t? [\n\t\t\t\t{\n\t\t\t\t\tselector: options?.selector,\n\t\t\t\t\tkey: ruleKey,\n\t\t\t\t\tvalue: getCSSVarFromStyleValue( styleValue ),\n\t\t\t\t},\n\t\t ]\n\t\t: [];\n}\n\n/**\n * Returns a JSON representation of the generated CSS rules taking into account box model properties, top, right, bottom, left.\n *\n * @param style Style object.\n * @param options Options object with settings to adjust how the styles are generated.\n * @param path An array of strings representing the path to the style value in the style object.\n * @param ruleKeys An array of CSS property keys and patterns.\n * @param individualProperties The \"sides\" or individual properties for which to generate rules.\n *\n * @return GeneratedCSSRule[] CSS rules.\n */\nexport function generateBoxRules(\n\tstyle: Style,\n\toptions: StyleOptions,\n\tpath: string[],\n\truleKeys: CssRulesKeys,\n\tindividualProperties: string[] = [ 'top', 'right', 'bottom', 'left' ]\n): GeneratedCSSRule[] {\n\tconst boxStyle: Box | string | undefined = get( style, path );\n\tif ( ! boxStyle ) {\n\t\treturn [];\n\t}\n\n\tconst rules: GeneratedCSSRule[] = [];\n\tif ( typeof boxStyle === 'string' ) {\n\t\trules.push( {\n\t\t\tselector: options?.selector,\n\t\t\tkey: ruleKeys.default,\n\t\t\tvalue: boxStyle,\n\t\t} );\n\t} else {\n\t\tconst sideRules = individualProperties.reduce(\n\t\t\t( acc: GeneratedCSSRule[], side: string ) => {\n\t\t\t\tconst value: string | undefined = getCSSVarFromStyleValue(\n\t\t\t\t\tget( boxStyle, [ side ] )\n\t\t\t\t);\n\t\t\t\tif ( value ) {\n\t\t\t\t\tacc.push( {\n\t\t\t\t\t\tselector: options?.selector,\n\t\t\t\t\t\tkey: ruleKeys?.individual.replace(\n\t\t\t\t\t\t\t'%s',\n\t\t\t\t\t\t\tupperFirst( side )\n\t\t\t\t\t\t),\n\t\t\t\t\t\tvalue,\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t\treturn acc;\n\t\t\t},\n\t\t\t[]\n\t\t);\n\t\trules.push( ...sideRules );\n\t}\n\n\treturn rules;\n}\n\n/**\n * Returns a CSS var value from incoming style value following the pattern `var:description|context|slug`.\n *\n * @param styleValue A raw style value.\n *\n * @return string A CSS var value.\n */\nexport function getCSSVarFromStyleValue( styleValue: string ): string {\n\tif (\n\t\ttypeof styleValue === 'string' &&\n\t\tstyleValue.startsWith( VARIABLE_REFERENCE_PREFIX )\n\t) {\n\t\tconst variable = styleValue\n\t\t\t.slice( VARIABLE_REFERENCE_PREFIX.length )\n\t\t\t.split( VARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE )\n\t\t\t.join( VARIABLE_PATH_SEPARATOR_TOKEN_STYLE );\n\t\treturn `var(--wp--${ variable })`;\n\t}\n\treturn styleValue;\n}\n\n/**\n * Capitalizes the first letter in a string.\n *\n * @param {string} str The string whose first letter the function will capitalize.\n *\n * @return string A CSS var value.\n */\nexport function upperFirst( [ firstLetter, ...rest ]: string ) {\n\treturn firstLetter.toUpperCase() + rest.join( '' );\n}\n"]}
1
+ {"version":3,"sources":["@wordpress/style-engine/src/styles/utils.ts"],"names":["generateRule","style","options","path","ruleKey","styleValue","selector","key","value","getCSSVarFromStyleValue","generateBoxRules","ruleKeys","individualProperties","boxStyle","rules","push","default","sideRules","reduce","acc","side","individual","replace","upperFirst","startsWith","VARIABLE_REFERENCE_PREFIX","variable","slice","length","split","VARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE","map","presetVariable","join","VARIABLE_PATH_SEPARATOR_TOKEN_STYLE","string","firstLetter","rest","toUpperCase","camelCaseJoin","strings","firstItem","toLowerCase"],"mappings":";;;;;;;;;;;AAGA;;AAYA;;AAfA;AACA;AACA;;AAmBA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASA,YAAT,CACNC,KADM,EAENC,OAFM,EAGNC,IAHM,EAINC,OAJM,EAKe;AACrB,QAAMC,UAA8B,GAAG,iBAAKJ,KAAL,EAAYE,IAAZ,CAAvC;AAEA,SAAOE,UAAU,GACd,CACA;AACCC,IAAAA,QAAQ,EAAEJ,OAAF,aAAEA,OAAF,uBAAEA,OAAO,CAAEI,QADpB;AAECC,IAAAA,GAAG,EAAEH,OAFN;AAGCI,IAAAA,KAAK,EAAEC,uBAAuB,CAAEJ,UAAF;AAH/B,GADA,CADc,GAQd,EARH;AASA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASK,gBAAT,CACNT,KADM,EAENC,OAFM,EAGNC,IAHM,EAINQ,QAJM,EAMe;AAAA,MADrBC,oBACqB,uEADY,CAAE,KAAF,EAAS,OAAT,EAAkB,QAAlB,EAA4B,MAA5B,CACZ;AACrB,QAAMC,QAAkC,GAAG,iBAAKZ,KAAL,EAAYE,IAAZ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,MADlB,EAEfC,KAFe,CAERC,kDAFQ,EAGfC,GAHe,CAGRC,cAAF,IAAsB,uBAAWA,cAAX,CAHZ,EAIfC,IAJe,CAITC,8CAJS,CAAjB;AAKA,WAAQ,aAAaR,QAAU,GAA/B;AACA;;AACD,SAAOrB,UAAP;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASkB,UAAT,CAAqBY,MAArB,EAA8C;AACpD,QAAM,CAAEC,WAAF,EAAe,GAAGC,IAAlB,IAA2BF,MAAjC;AACA,SAAOC,WAAW,CAACE,WAAZ,KAA4BD,IAAI,CAACJ,IAAL,CAAW,EAAX,CAAnC;AACA;AAED;AACA;AACA;AACA;AACA;AACA;AACA;;;AACO,SAASM,aAAT,CAAwBC,OAAxB,EAAoD;AAC1D,QAAM,CAAEC,SAAF,EAAa,GAAGJ,IAAhB,IAAyBG,OAA/B;AACA,SAAOC,SAAS,CAACC,WAAV,KAA0BL,IAAI,CAACN,GAAL,CAAUR,UAAV,EAAuBU,IAAvB,CAA6B,EAA7B,CAAjC;AACA","sourcesContent":["/**\n * External dependencies\n */\nimport { get, kebabCase } from 'lodash';\n\n/**\n * Internal dependencies\n */\nimport type {\n\tCssRulesKeys,\n\tGeneratedCSSRule,\n\tStyle,\n\tBox,\n\tStyleOptions,\n} from '../types';\nimport {\n\tVARIABLE_REFERENCE_PREFIX,\n\tVARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE,\n\tVARIABLE_PATH_SEPARATOR_TOKEN_STYLE,\n} from './constants';\n\n/**\n * Returns a JSON representation of the generated CSS rules.\n *\n * @param style Style object.\n * @param options Options object with settings to adjust how the styles are generated.\n * @param path An array of strings representing the path to the style value in the style object.\n * @param ruleKey A CSS property key.\n *\n * @return GeneratedCSSRule[] CSS rules.\n */\nexport function generateRule(\n\tstyle: Style,\n\toptions: StyleOptions,\n\tpath: string[],\n\truleKey: string\n): GeneratedCSSRule[] {\n\tconst styleValue: string | undefined = get( style, path );\n\n\treturn styleValue\n\t\t? [\n\t\t\t\t{\n\t\t\t\t\tselector: options?.selector,\n\t\t\t\t\tkey: ruleKey,\n\t\t\t\t\tvalue: getCSSVarFromStyleValue( styleValue ),\n\t\t\t\t},\n\t\t ]\n\t\t: [];\n}\n\n/**\n * Returns a JSON representation of the generated CSS rules taking into account box model properties, top, right, bottom, left.\n *\n * @param style Style object.\n * @param options Options object with settings to adjust how the styles are generated.\n * @param path An array of strings representing the path to the style value in the style object.\n * @param ruleKeys An array of CSS property keys and patterns.\n * @param individualProperties The \"sides\" or individual properties for which to generate rules.\n *\n * @return GeneratedCSSRule[] CSS rules.\n */\nexport function generateBoxRules(\n\tstyle: Style,\n\toptions: StyleOptions,\n\tpath: string[],\n\truleKeys: CssRulesKeys,\n\tindividualProperties: string[] = [ 'top', 'right', 'bottom', 'left' ]\n): GeneratedCSSRule[] {\n\tconst boxStyle: Box | string | undefined = get( style, path );\n\tif ( ! boxStyle ) {\n\t\treturn [];\n\t}\n\n\tconst rules: GeneratedCSSRule[] = [];\n\tif ( typeof boxStyle === 'string' ) {\n\t\trules.push( {\n\t\t\tselector: options?.selector,\n\t\t\tkey: ruleKeys.default,\n\t\t\tvalue: boxStyle,\n\t\t} );\n\t} else {\n\t\tconst sideRules = individualProperties.reduce(\n\t\t\t( acc: GeneratedCSSRule[], side: string ) => {\n\t\t\t\tconst value: string | undefined = getCSSVarFromStyleValue(\n\t\t\t\t\tget( boxStyle, [ side ] )\n\t\t\t\t);\n\t\t\t\tif ( value ) {\n\t\t\t\t\tacc.push( {\n\t\t\t\t\t\tselector: options?.selector,\n\t\t\t\t\t\tkey: ruleKeys?.individual.replace(\n\t\t\t\t\t\t\t'%s',\n\t\t\t\t\t\t\tupperFirst( side )\n\t\t\t\t\t\t),\n\t\t\t\t\t\tvalue,\n\t\t\t\t\t} );\n\t\t\t\t}\n\t\t\t\treturn acc;\n\t\t\t},\n\t\t\t[]\n\t\t);\n\t\trules.push( ...sideRules );\n\t}\n\n\treturn rules;\n}\n\n/**\n * Returns a CSS var value from incoming style value following the pattern `var:description|context|slug`.\n *\n * @param styleValue A raw style value.\n *\n * @return string A CSS var value.\n */\nexport function getCSSVarFromStyleValue( styleValue: string ): string {\n\tif (\n\t\ttypeof styleValue === 'string' &&\n\t\tstyleValue.startsWith( VARIABLE_REFERENCE_PREFIX )\n\t) {\n\t\tconst variable = styleValue\n\t\t\t.slice( VARIABLE_REFERENCE_PREFIX.length )\n\t\t\t.split( VARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE )\n\t\t\t.map( ( presetVariable ) => kebabCase( presetVariable ) )\n\t\t\t.join( VARIABLE_PATH_SEPARATOR_TOKEN_STYLE );\n\t\treturn `var(--wp--${ variable })`;\n\t}\n\treturn styleValue;\n}\n\n/**\n * Capitalizes the first letter in a string.\n *\n * @param string The string whose first letter the function will capitalize.\n *\n * @return String with the first letter capitalized.\n */\nexport function upperFirst( string: string ): string {\n\tconst [ firstLetter, ...rest ] = string;\n\treturn firstLetter.toUpperCase() + rest.join( '' );\n}\n\n/**\n * Converts an array of strings into a camelCase string.\n *\n * @param strings The strings to join into a camelCase string.\n *\n * @return camelCase string.\n */\nexport function camelCaseJoin( strings: string[] ): string {\n\tconst [ firstItem, ...rest ] = strings;\n\treturn firstItem.toLowerCase() + rest.map( upperFirst ).join( '' );\n}\n"]}
@@ -1,14 +1,39 @@
1
1
  /**
2
2
  * Internal dependencies
3
3
  */
4
- import { generateRule, generateBoxRules, upperFirst } from '../utils';
4
+ import { generateRule, generateBoxRules, camelCaseJoin } from '../utils';
5
+ /**
6
+ * Creates a function for generating CSS rules when the style path is the same as the camelCase CSS property used in React.
7
+ *
8
+ * @param path An array of strings representing the path to the style value in the style object.
9
+ *
10
+ * @return A function that generates CSS rules.
11
+ */
12
+
13
+ function createBorderGenerateFunction(path) {
14
+ return (style, options) => generateRule(style, options, path, camelCaseJoin(path));
15
+ }
16
+ /**
17
+ * Creates a function for generating border-{top,bottom,left,right}-{color,style,width} CSS rules.
18
+ *
19
+ * @param edge The edge to create CSS rules for.
20
+ *
21
+ * @return A function that generates CSS rules.
22
+ */
23
+
24
+
25
+ function createBorderEdgeGenerateFunction(edge) {
26
+ return (style, options) => {
27
+ return ['color', 'style', 'width'].flatMap(key => {
28
+ const path = ['border', edge, key];
29
+ return createBorderGenerateFunction(path)(style, options);
30
+ });
31
+ };
32
+ }
33
+
5
34
  const color = {
6
35
  name: 'color',
7
- generate: function (style, options) {
8
- let path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ['border', 'color'];
9
- let ruleKey = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'borderColor';
10
- return generateRule(style, options, path, ruleKey);
11
- }
36
+ generate: createBorderGenerateFunction(['border', 'color'])
12
37
  };
13
38
  const radius = {
14
39
  name: 'radius',
@@ -21,65 +46,27 @@ const radius = {
21
46
  };
22
47
  const borderStyle = {
23
48
  name: 'style',
24
- generate: function (style, options) {
25
- let path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ['border', 'style'];
26
- let ruleKey = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'borderStyle';
27
- return generateRule(style, options, path, ruleKey);
28
- }
49
+ generate: createBorderGenerateFunction(['border', 'style'])
29
50
  };
30
51
  const width = {
31
52
  name: 'width',
32
- generate: function (style, options) {
33
- let path = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : ['border', 'width'];
34
- let ruleKey = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 'borderWidth';
35
- return generateRule(style, options, path, ruleKey);
36
- }
37
- };
38
- const borderDefinitionsWithIndividualStyles = [color, borderStyle, width];
39
- /**
40
- * Returns a curried generator function with the individual border property ('top' | 'right' | 'bottom' | 'left') baked in.
41
- *
42
- * @param individualProperty Individual border property ('top' | 'right' | 'bottom' | 'left').
43
- *
44
- * @return StyleDefinition[ 'generate' ]
45
- */
46
-
47
- const createBorderGenerateFunction = individualProperty => (style, options) => {
48
- var _style$border;
49
-
50
- const styleValue = style === null || style === void 0 ? void 0 : (_style$border = style.border) === null || _style$border === void 0 ? void 0 : _style$border[individualProperty];
51
-
52
- if (!styleValue) {
53
- return [];
54
- }
55
-
56
- return borderDefinitionsWithIndividualStyles.reduce((acc, borderDefinition) => {
57
- const key = borderDefinition.name;
58
-
59
- if (styleValue.hasOwnProperty(key) && typeof borderDefinition.generate === 'function') {
60
- const ruleKey = `border${upperFirst(individualProperty)}${upperFirst(key)}`;
61
- acc.push(...borderDefinition.generate(style, options, ['border', individualProperty, key], ruleKey));
62
- }
63
-
64
- return acc;
65
- }, []);
53
+ generate: createBorderGenerateFunction(['border', 'width'])
66
54
  };
67
-
68
55
  const borderTop = {
69
56
  name: 'borderTop',
70
- generate: createBorderGenerateFunction('top')
57
+ generate: createBorderEdgeGenerateFunction('top')
71
58
  };
72
59
  const borderRight = {
73
60
  name: 'borderRight',
74
- generate: createBorderGenerateFunction('right')
61
+ generate: createBorderEdgeGenerateFunction('right')
75
62
  };
76
63
  const borderBottom = {
77
64
  name: 'borderBottom',
78
- generate: createBorderGenerateFunction('bottom')
65
+ generate: createBorderEdgeGenerateFunction('bottom')
79
66
  };
80
67
  const borderLeft = {
81
68
  name: 'borderLeft',
82
- generate: createBorderGenerateFunction('left')
69
+ generate: createBorderEdgeGenerateFunction('left')
83
70
  };
84
- export default [...borderDefinitionsWithIndividualStyles, radius, borderTop, borderRight, borderBottom, borderLeft];
71
+ export default [color, borderStyle, width, radius, borderTop, borderRight, borderBottom, borderLeft];
85
72
  //# sourceMappingURL=index.js.map