@wordpress/style-engine 0.7.0 → 0.10.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 +6 -0
- package/README.md +4 -4
- package/build/index.js +13 -2
- package/build/index.js.map +1 -1
- package/build/styles/color/background.js +21 -0
- package/build/styles/color/background.js.map +1 -0
- package/build/styles/color/gradient.js +21 -0
- package/build/styles/color/gradient.js.map +1 -0
- package/build/styles/color/index.js +21 -0
- package/build/styles/color/index.js.map +1 -0
- package/build/styles/color/text.js +21 -0
- package/build/styles/color/text.js.map +1 -0
- package/build/styles/constants.js +13 -0
- package/build/styles/constants.js.map +1 -0
- package/build/styles/index.js +5 -3
- package/build/styles/index.js.map +1 -1
- package/build/styles/spacing/index.js +19 -0
- package/build/styles/spacing/index.js.map +1 -0
- package/build/styles/{margin.js → spacing/margin.js} +1 -1
- package/build/styles/spacing/margin.js.map +1 -0
- package/build/styles/{padding.js → spacing/padding.js} +1 -1
- package/build/styles/spacing/padding.js.map +1 -0
- package/build/styles/typography/index.js +57 -0
- package/build/styles/typography/index.js.map +1 -0
- package/build/styles/utils.js +54 -2
- package/build/styles/utils.js.map +1 -1
- package/build-module/index.js +13 -2
- package/build-module/index.js.map +1 -1
- package/build-module/styles/color/background.js +12 -0
- package/build-module/styles/color/background.js.map +1 -0
- package/build-module/styles/color/gradient.js +12 -0
- package/build-module/styles/color/gradient.js.map +1 -0
- package/build-module/styles/color/index.js +8 -0
- package/build-module/styles/color/index.js.map +1 -0
- package/build-module/styles/color/text.js +12 -0
- package/build-module/styles/color/text.js.map +1 -0
- package/build-module/styles/constants.js +4 -0
- package/build-module/styles/constants.js.map +1 -0
- package/build-module/styles/index.js +4 -3
- package/build-module/styles/index.js.map +1 -1
- package/build-module/styles/spacing/index.js +7 -0
- package/build-module/styles/spacing/index.js.map +1 -0
- package/build-module/styles/{margin.js → spacing/margin.js} +1 -1
- package/build-module/styles/spacing/margin.js.map +1 -0
- package/build-module/styles/{padding.js → spacing/padding.js} +1 -1
- package/build-module/styles/spacing/padding.js.map +1 -0
- package/build-module/styles/typography/index.js +48 -0
- package/build-module/styles/typography/index.js.map +1 -0
- package/build-module/styles/utils.js +49 -2
- package/build-module/styles/utils.js.map +1 -1
- package/build-types/index.d.ts.map +1 -1
- package/build-types/styles/color/background.d.ts +14 -0
- package/build-types/styles/color/background.d.ts.map +1 -0
- package/build-types/styles/color/gradient.d.ts +14 -0
- package/build-types/styles/color/gradient.d.ts.map +1 -0
- package/build-types/styles/color/index.d.ts +10 -0
- package/build-types/styles/color/index.d.ts.map +1 -0
- package/build-types/styles/color/text.d.ts +14 -0
- package/build-types/styles/color/text.d.ts.map +1 -0
- package/build-types/styles/constants.d.ts +4 -0
- package/build-types/styles/constants.d.ts.map +1 -0
- package/build-types/styles/index.d.ts +5 -1
- package/build-types/styles/index.d.ts.map +1 -1
- package/build-types/styles/spacing/index.d.ts +6 -0
- package/build-types/styles/spacing/index.d.ts.map +1 -0
- package/build-types/styles/{margin.d.ts → spacing/margin.d.ts} +2 -2
- package/build-types/styles/spacing/margin.d.ts.map +1 -0
- package/build-types/styles/{padding.d.ts → spacing/padding.d.ts} +2 -2
- package/build-types/styles/spacing/padding.d.ts.map +1 -0
- package/build-types/styles/typography/index.d.ts +14 -0
- package/build-types/styles/typography/index.d.ts.map +1 -0
- package/build-types/styles/utils.d.ts +33 -0
- package/build-types/styles/utils.d.ts.map +1 -1
- package/build-types/types.d.ts +14 -1
- package/build-types/types.d.ts.map +1 -1
- package/class-wp-style-engine.php +288 -83
- package/package.json +2 -2
- package/phpunit/class-wp-style-engine-test.php +154 -6
- package/src/index.ts +13 -1
- package/src/styles/color/background.ts +19 -0
- package/src/styles/color/gradient.ts +19 -0
- package/src/styles/color/index.ts +8 -0
- package/src/styles/color/text.ts +14 -0
- package/src/styles/constants.ts +3 -0
- package/src/styles/index.ts +4 -3
- package/src/styles/spacing/index.ts +7 -0
- package/src/styles/{margin.ts → spacing/margin.ts} +2 -2
- package/src/styles/{padding.ts → spacing/padding.ts} +2 -2
- package/src/styles/typography/index.ts +99 -0
- package/src/styles/utils.ts +67 -2
- package/src/test/index.js +101 -2
- package/src/types.ts +14 -1
- package/tsconfig.tsbuildinfo +1 -1
- package/build/styles/margin.js.map +0 -1
- package/build/styles/padding.js.map +0 -1
- package/build-module/styles/margin.js.map +0 -1
- package/build-module/styles/padding.js.map +0 -1
- package/build-types/styles/margin.d.ts.map +0 -1
- package/build-types/styles/padding.d.ts.map +0 -1
|
@@ -17,8 +17,8 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
|
|
|
17
17
|
*
|
|
18
18
|
* @dataProvider data_generate_styles_fixtures
|
|
19
19
|
*/
|
|
20
|
-
function test_generate_styles( $block_styles, $expected_output ) {
|
|
21
|
-
$generated_styles = wp_style_engine_generate( $block_styles );
|
|
20
|
+
function test_generate_styles( $block_styles, $options, $expected_output ) {
|
|
21
|
+
$generated_styles = wp_style_engine_generate( $block_styles, $options );
|
|
22
22
|
$this->assertSame( $expected_output, $generated_styles );
|
|
23
23
|
}
|
|
24
24
|
|
|
@@ -31,11 +31,13 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
|
|
|
31
31
|
return array(
|
|
32
32
|
'default_return_value' => array(
|
|
33
33
|
'block_styles' => array(),
|
|
34
|
+
'options' => null,
|
|
34
35
|
'expected_output' => null,
|
|
35
36
|
),
|
|
36
37
|
|
|
37
38
|
'inline_invalid_block_styles_empty' => array(
|
|
38
39
|
'block_styles' => 'hello world!',
|
|
40
|
+
'options' => null,
|
|
39
41
|
'expected_output' => null,
|
|
40
42
|
),
|
|
41
43
|
|
|
@@ -43,6 +45,7 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
|
|
|
43
45
|
'block_styles' => array(
|
|
44
46
|
'pageBreakAfter' => 'verso',
|
|
45
47
|
),
|
|
48
|
+
'options' => null,
|
|
46
49
|
'expected_output' => array(),
|
|
47
50
|
),
|
|
48
51
|
|
|
@@ -50,6 +53,7 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
|
|
|
50
53
|
'block_styles' => array(
|
|
51
54
|
'pageBreakAfter' => 'verso',
|
|
52
55
|
),
|
|
56
|
+
'options' => null,
|
|
53
57
|
'expected_output' => array(),
|
|
54
58
|
),
|
|
55
59
|
|
|
@@ -59,6 +63,7 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
|
|
|
59
63
|
'gap' => '1000vw',
|
|
60
64
|
),
|
|
61
65
|
),
|
|
66
|
+
'options' => null,
|
|
62
67
|
'expected_output' => array(),
|
|
63
68
|
),
|
|
64
69
|
|
|
@@ -68,12 +73,19 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
|
|
|
68
73
|
'text' => 'var:preset|color|texas-flood',
|
|
69
74
|
),
|
|
70
75
|
'spacing' => array(
|
|
71
|
-
'margin'
|
|
76
|
+
'margin' => '111px',
|
|
77
|
+
'padding' => '0',
|
|
78
|
+
),
|
|
79
|
+
'border' => array(
|
|
80
|
+
'color' => 'var:preset|color|cool-caramel',
|
|
81
|
+
'width' => '2rem',
|
|
82
|
+
'style' => 'dotted',
|
|
72
83
|
),
|
|
73
84
|
),
|
|
85
|
+
'options' => array(),
|
|
74
86
|
'expected_output' => array(
|
|
75
|
-
'css' => 'margin: 111px;',
|
|
76
|
-
'classnames' => 'has-text-color has-texas-flood-color',
|
|
87
|
+
'css' => 'border-style: dotted; border-width: 2rem; padding: 0; margin: 111px;',
|
|
88
|
+
'classnames' => 'has-text-color has-texas-flood-color has-border-color has-cool-caramel-border-color',
|
|
77
89
|
),
|
|
78
90
|
),
|
|
79
91
|
|
|
@@ -93,9 +105,18 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
|
|
|
93
105
|
'right' => '10em',
|
|
94
106
|
),
|
|
95
107
|
),
|
|
108
|
+
'border' => array(
|
|
109
|
+
'radius' => array(
|
|
110
|
+
'topLeft' => '99px',
|
|
111
|
+
'topRight' => '98px',
|
|
112
|
+
'bottomLeft' => '97px',
|
|
113
|
+
'bottomRight' => '96px',
|
|
114
|
+
),
|
|
115
|
+
),
|
|
96
116
|
),
|
|
117
|
+
'options' => null,
|
|
97
118
|
'expected_output' => array(
|
|
98
|
-
'css' => 'padding-top: 42px; padding-left: 2%; padding-bottom: 44px; padding-right: 5rem; margin-top: 12rem; margin-left: 2vh; margin-bottom: 2px; margin-right: 10em;',
|
|
119
|
+
'css' => 'border-top-left-radius: 99px; border-top-right-radius: 98px; border-bottom-left-radius: 97px; border-bottom-right-radius: 96px; padding-top: 42px; padding-left: 2%; padding-bottom: 44px; padding-right: 5rem; margin-top: 12rem; margin-left: 2vh; margin-bottom: 2px; margin-right: 10em;',
|
|
99
120
|
),
|
|
100
121
|
),
|
|
101
122
|
|
|
@@ -112,10 +133,57 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
|
|
|
112
133
|
'letterSpacing' => '2',
|
|
113
134
|
),
|
|
114
135
|
),
|
|
136
|
+
'options' => null,
|
|
115
137
|
'expected_output' => array(
|
|
116
138
|
'css' => 'font-family: Roboto,Oxygen-Sans,Ubuntu,sans-serif; font-style: italic; font-weight: 800; line-height: 1.3; text-decoration: underline; text-transform: uppercase; letter-spacing: 2;',
|
|
117
139
|
),
|
|
118
140
|
),
|
|
141
|
+
|
|
142
|
+
'style_block_with_selector' => array(
|
|
143
|
+
'block_styles' => array(
|
|
144
|
+
'spacing' => array(
|
|
145
|
+
'padding' => array(
|
|
146
|
+
'top' => '42px',
|
|
147
|
+
'left' => '2%',
|
|
148
|
+
'bottom' => '44px',
|
|
149
|
+
'right' => '5rem',
|
|
150
|
+
),
|
|
151
|
+
),
|
|
152
|
+
),
|
|
153
|
+
'options' => array( 'selector' => '.wp-selector > p' ),
|
|
154
|
+
'expected_output' => array(
|
|
155
|
+
'css' => '.wp-selector > p { padding-top: 42px; padding-left: 2%; padding-bottom: 44px; padding-right: 5rem; }',
|
|
156
|
+
),
|
|
157
|
+
),
|
|
158
|
+
|
|
159
|
+
'elements_with_css_var_value' => array(
|
|
160
|
+
'block_styles' => array(
|
|
161
|
+
'color' => array(
|
|
162
|
+
'text' => 'var:preset|color|my-little-pony',
|
|
163
|
+
),
|
|
164
|
+
),
|
|
165
|
+
'options' => array(
|
|
166
|
+
'selector' => '.wp-selector',
|
|
167
|
+
'css_vars' => true,
|
|
168
|
+
),
|
|
169
|
+
'expected_output' => array(
|
|
170
|
+
'css' => '.wp-selector { color: var(--wp--preset--color--my-little-pony); }',
|
|
171
|
+
'classnames' => 'has-text-color has-my-little-pony-color',
|
|
172
|
+
),
|
|
173
|
+
),
|
|
174
|
+
|
|
175
|
+
'elements_with_invalid_preset_style_property' => array(
|
|
176
|
+
'block_styles' => array(
|
|
177
|
+
'color' => array(
|
|
178
|
+
'text' => 'var:preset|invalid_property|my-little-pony',
|
|
179
|
+
),
|
|
180
|
+
),
|
|
181
|
+
'options' => array( 'selector' => '.wp-selector' ),
|
|
182
|
+
'expected_output' => array(
|
|
183
|
+
'classnames' => 'has-text-color',
|
|
184
|
+
),
|
|
185
|
+
),
|
|
186
|
+
|
|
119
187
|
'valid_classnames_deduped' => array(
|
|
120
188
|
'block_styles' => array(
|
|
121
189
|
'color' => array(
|
|
@@ -128,10 +196,25 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
|
|
|
128
196
|
'fontFamily' => 'var:preset|font-family|totally-awesome',
|
|
129
197
|
),
|
|
130
198
|
),
|
|
199
|
+
'options' => array(),
|
|
131
200
|
'expected_output' => array(
|
|
132
201
|
'classnames' => 'has-text-color has-copper-socks-color has-background has-splendid-carrot-background-color has-like-wow-dude-gradient-background has-fantastic-font-size has-totally-awesome-font-family',
|
|
133
202
|
),
|
|
134
203
|
),
|
|
204
|
+
|
|
205
|
+
'valid_classnames_and_css_vars' => array(
|
|
206
|
+
'block_styles' => array(
|
|
207
|
+
'color' => array(
|
|
208
|
+
'text' => 'var:preset|color|teal-independents',
|
|
209
|
+
),
|
|
210
|
+
),
|
|
211
|
+
'options' => array( 'css_vars' => true ),
|
|
212
|
+
'expected_output' => array(
|
|
213
|
+
'css' => 'color: var(--wp--preset--color--teal-independents);',
|
|
214
|
+
'classnames' => 'has-text-color has-teal-independents-color',
|
|
215
|
+
),
|
|
216
|
+
),
|
|
217
|
+
|
|
135
218
|
'valid_classnames_with_null_style_values' => array(
|
|
136
219
|
'block_styles' => array(
|
|
137
220
|
'color' => array(
|
|
@@ -139,11 +222,13 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
|
|
|
139
222
|
'background' => null,
|
|
140
223
|
),
|
|
141
224
|
),
|
|
225
|
+
'options' => array(),
|
|
142
226
|
'expected_output' => array(
|
|
143
227
|
'css' => 'color: #fff;',
|
|
144
228
|
'classnames' => 'has-text-color',
|
|
145
229
|
),
|
|
146
230
|
),
|
|
231
|
+
|
|
147
232
|
'invalid_classnames_preset_value' => array(
|
|
148
233
|
'block_styles' => array(
|
|
149
234
|
'color' => array(
|
|
@@ -155,10 +240,12 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
|
|
|
155
240
|
'padding' => 'var:preset|spacing|padding',
|
|
156
241
|
),
|
|
157
242
|
),
|
|
243
|
+
'options' => array(),
|
|
158
244
|
'expected_output' => array(
|
|
159
245
|
'classnames' => 'has-text-color has-background',
|
|
160
246
|
),
|
|
161
247
|
),
|
|
248
|
+
|
|
162
249
|
'invalid_classnames_options' => array(
|
|
163
250
|
'block_styles' => array(
|
|
164
251
|
'typography' => array(
|
|
@@ -170,8 +257,69 @@ class WP_Style_Engine_Test extends WP_UnitTestCase {
|
|
|
170
257
|
),
|
|
171
258
|
),
|
|
172
259
|
),
|
|
260
|
+
'options' => array(),
|
|
173
261
|
'expected_output' => array(),
|
|
174
262
|
),
|
|
263
|
+
|
|
264
|
+
'inline_valid_box_model_style_with_sides' => array(
|
|
265
|
+
'block_styles' => array(
|
|
266
|
+
'border' => array(
|
|
267
|
+
'top' => array(
|
|
268
|
+
'color' => '#fe1',
|
|
269
|
+
'width' => '1.5rem',
|
|
270
|
+
'style' => 'dashed',
|
|
271
|
+
),
|
|
272
|
+
'right' => array(
|
|
273
|
+
'color' => '#fe2',
|
|
274
|
+
'width' => '1.4rem',
|
|
275
|
+
'style' => 'solid',
|
|
276
|
+
),
|
|
277
|
+
'bottom' => array(
|
|
278
|
+
'color' => '#fe3',
|
|
279
|
+
'width' => '1.3rem',
|
|
280
|
+
),
|
|
281
|
+
'left' => array(
|
|
282
|
+
'color' => 'var:preset|color|swampy-yellow',
|
|
283
|
+
'width' => '0.5rem',
|
|
284
|
+
'style' => 'dotted',
|
|
285
|
+
),
|
|
286
|
+
),
|
|
287
|
+
),
|
|
288
|
+
'options' => array(),
|
|
289
|
+
'expected_output' => array(
|
|
290
|
+
'css' => 'border-top-color: #fe1; border-top-width: 1.5rem; border-top-style: dashed; border-right-color: #fe2; border-right-width: 1.4rem; border-right-style: solid; border-bottom-color: #fe3; border-bottom-width: 1.3rem; border-left-color: var(--wp--preset--color--swampy-yellow); border-left-width: 0.5rem; border-left-style: dotted;',
|
|
291
|
+
),
|
|
292
|
+
),
|
|
293
|
+
|
|
294
|
+
'inline_invalid_box_model_style_with_sides' => array(
|
|
295
|
+
'block_styles' => array(
|
|
296
|
+
'border' => array(
|
|
297
|
+
'top' => array(
|
|
298
|
+
'top' => '#fe1',
|
|
299
|
+
'right' => '1.5rem',
|
|
300
|
+
'cheese' => 'dashed',
|
|
301
|
+
),
|
|
302
|
+
'right' => array(
|
|
303
|
+
'right' => '#fe2',
|
|
304
|
+
'top' => '1.4rem',
|
|
305
|
+
'bacon' => 'solid',
|
|
306
|
+
),
|
|
307
|
+
'bottom' => array(
|
|
308
|
+
'color' => 'var:preset|color|terrible-lizard',
|
|
309
|
+
'bottom' => '1.3rem',
|
|
310
|
+
),
|
|
311
|
+
'left' => array(
|
|
312
|
+
'left' => null,
|
|
313
|
+
'width' => null,
|
|
314
|
+
'top' => 'dotted',
|
|
315
|
+
),
|
|
316
|
+
),
|
|
317
|
+
),
|
|
318
|
+
'options' => array(),
|
|
319
|
+
'expected_output' => array(
|
|
320
|
+
'css' => 'border-bottom-color: var(--wp--preset--color--terrible-lizard);',
|
|
321
|
+
),
|
|
322
|
+
),
|
|
175
323
|
);
|
|
176
324
|
}
|
|
177
325
|
}
|
package/src/index.ts
CHANGED
|
@@ -24,6 +24,16 @@ import { styleDefinitions } from './styles';
|
|
|
24
24
|
*/
|
|
25
25
|
export function generate( style: Style, options: StyleOptions ): string {
|
|
26
26
|
const rules = getCSSRules( style, options );
|
|
27
|
+
|
|
28
|
+
// If no selector is provided, treat generated rules as inline styles to be returned as a single string.
|
|
29
|
+
if ( ! options?.selector ) {
|
|
30
|
+
const inlineRules: string[] = [];
|
|
31
|
+
rules.forEach( ( rule ) => {
|
|
32
|
+
inlineRules.push( `${ kebabCase( rule.key ) }: ${ rule.value };` );
|
|
33
|
+
} );
|
|
34
|
+
return inlineRules.join( ' ' );
|
|
35
|
+
}
|
|
36
|
+
|
|
27
37
|
const groupedRules = groupBy( rules, 'selector' );
|
|
28
38
|
const selectorRules = Object.keys( groupedRules ).reduce(
|
|
29
39
|
( acc: string[], subSelector: string ) => {
|
|
@@ -57,7 +67,9 @@ export function getCSSRules(
|
|
|
57
67
|
): GeneratedCSSRule[] {
|
|
58
68
|
const rules: GeneratedCSSRule[] = [];
|
|
59
69
|
styleDefinitions.forEach( ( definition: StyleDefinition ) => {
|
|
60
|
-
|
|
70
|
+
if ( typeof definition.generate === 'function' ) {
|
|
71
|
+
rules.push( ...definition.generate( style, options ) );
|
|
72
|
+
}
|
|
61
73
|
} );
|
|
62
74
|
|
|
63
75
|
return rules;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { Style, StyleOptions } from '../../types';
|
|
5
|
+
import { generateRule } from '../utils';
|
|
6
|
+
|
|
7
|
+
const background = {
|
|
8
|
+
name: 'background',
|
|
9
|
+
generate: ( style: Style, options: StyleOptions ) => {
|
|
10
|
+
return generateRule(
|
|
11
|
+
style,
|
|
12
|
+
options,
|
|
13
|
+
[ 'color', 'background' ],
|
|
14
|
+
'backgroundColor'
|
|
15
|
+
);
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export default background;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { Style, StyleOptions } from '../../types';
|
|
5
|
+
import { generateRule } from '../utils';
|
|
6
|
+
|
|
7
|
+
const gradient = {
|
|
8
|
+
name: 'gradient',
|
|
9
|
+
generate: ( style: Style, options: StyleOptions ) => {
|
|
10
|
+
return generateRule(
|
|
11
|
+
style,
|
|
12
|
+
options,
|
|
13
|
+
[ 'color', 'gradient' ],
|
|
14
|
+
'background'
|
|
15
|
+
);
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export default gradient;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { Style, StyleOptions } from '../../types';
|
|
5
|
+
import { generateRule } from '../utils';
|
|
6
|
+
|
|
7
|
+
const text = {
|
|
8
|
+
name: 'text',
|
|
9
|
+
generate: ( style: Style, options: StyleOptions ) => {
|
|
10
|
+
return generateRule( style, options, [ 'color', 'text' ], 'color' );
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export default text;
|
package/src/styles/index.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Internal dependencies
|
|
3
3
|
*/
|
|
4
|
-
import
|
|
5
|
-
import
|
|
4
|
+
import color from './color';
|
|
5
|
+
import spacing from './spacing';
|
|
6
|
+
import typography from './typography';
|
|
6
7
|
|
|
7
|
-
export const styleDefinitions = [
|
|
8
|
+
export const styleDefinitions = [ ...color, ...spacing, ...typography ];
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Internal dependencies
|
|
3
3
|
*/
|
|
4
|
-
import type { Style, StyleOptions } from '
|
|
5
|
-
import { generateBoxRules } from '
|
|
4
|
+
import type { Style, StyleOptions } from '../../types';
|
|
5
|
+
import { generateBoxRules } from '../utils';
|
|
6
6
|
|
|
7
7
|
const margin = {
|
|
8
8
|
name: 'margin',
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Internal dependencies
|
|
3
3
|
*/
|
|
4
|
-
import type { Style, StyleOptions } from '
|
|
5
|
-
import { generateBoxRules } from '
|
|
4
|
+
import type { Style, StyleOptions } from '../../types';
|
|
5
|
+
import { generateBoxRules } from '../utils';
|
|
6
6
|
|
|
7
7
|
const padding = {
|
|
8
8
|
name: 'padding',
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Internal dependencies
|
|
3
|
+
*/
|
|
4
|
+
import type { Style, StyleOptions } from '../../types';
|
|
5
|
+
import { generateRule } from '../utils';
|
|
6
|
+
|
|
7
|
+
const fontSize = {
|
|
8
|
+
name: 'fontSize',
|
|
9
|
+
generate: ( style: Style, options: StyleOptions ) => {
|
|
10
|
+
return generateRule(
|
|
11
|
+
style,
|
|
12
|
+
options,
|
|
13
|
+
[ 'typography', 'fontSize' ],
|
|
14
|
+
'fontSize'
|
|
15
|
+
);
|
|
16
|
+
},
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const fontStyle = {
|
|
20
|
+
name: 'fontStyle',
|
|
21
|
+
generate: ( style: Style, options: StyleOptions ) => {
|
|
22
|
+
return generateRule(
|
|
23
|
+
style,
|
|
24
|
+
options,
|
|
25
|
+
[ 'typography', 'fontStyle' ],
|
|
26
|
+
'fontStyle'
|
|
27
|
+
);
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const fontWeight = {
|
|
32
|
+
name: 'fontWeight',
|
|
33
|
+
generate: ( style: Style, options: StyleOptions ) => {
|
|
34
|
+
return generateRule(
|
|
35
|
+
style,
|
|
36
|
+
options,
|
|
37
|
+
[ 'typography', 'fontWeight' ],
|
|
38
|
+
'fontWeight'
|
|
39
|
+
);
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const letterSpacing = {
|
|
44
|
+
name: 'letterSpacing',
|
|
45
|
+
generate: ( style: Style, options: StyleOptions ) => {
|
|
46
|
+
return generateRule(
|
|
47
|
+
style,
|
|
48
|
+
options,
|
|
49
|
+
[ 'typography', 'letterSpacing' ],
|
|
50
|
+
'letterSpacing'
|
|
51
|
+
);
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
const lineHeight = {
|
|
56
|
+
name: 'letterSpacing',
|
|
57
|
+
generate: ( style: Style, options: StyleOptions ) => {
|
|
58
|
+
return generateRule(
|
|
59
|
+
style,
|
|
60
|
+
options,
|
|
61
|
+
[ 'typography', 'lineHeight' ],
|
|
62
|
+
'lineHeight'
|
|
63
|
+
);
|
|
64
|
+
},
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
const textDecoration = {
|
|
68
|
+
name: 'textDecoration',
|
|
69
|
+
generate: ( style: Style, options: StyleOptions ) => {
|
|
70
|
+
return generateRule(
|
|
71
|
+
style,
|
|
72
|
+
options,
|
|
73
|
+
[ 'typography', 'textDecoration' ],
|
|
74
|
+
'textDecoration'
|
|
75
|
+
);
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
const textTransform = {
|
|
80
|
+
name: 'textTransform',
|
|
81
|
+
generate: ( style: Style, options: StyleOptions ) => {
|
|
82
|
+
return generateRule(
|
|
83
|
+
style,
|
|
84
|
+
options,
|
|
85
|
+
[ 'typography', 'textTransform' ],
|
|
86
|
+
'textTransform'
|
|
87
|
+
);
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export default [
|
|
92
|
+
fontSize,
|
|
93
|
+
fontStyle,
|
|
94
|
+
fontWeight,
|
|
95
|
+
letterSpacing,
|
|
96
|
+
lineHeight,
|
|
97
|
+
textDecoration,
|
|
98
|
+
textTransform,
|
|
99
|
+
];
|
package/src/styles/utils.ts
CHANGED
|
@@ -7,7 +7,51 @@ import { get, upperFirst } from 'lodash';
|
|
|
7
7
|
* Internal dependencies
|
|
8
8
|
*/
|
|
9
9
|
import type { GeneratedCSSRule, Style, Box, StyleOptions } from '../types';
|
|
10
|
+
import {
|
|
11
|
+
VARIABLE_REFERENCE_PREFIX,
|
|
12
|
+
VARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE,
|
|
13
|
+
VARIABLE_PATH_SEPARATOR_TOKEN_STYLE,
|
|
14
|
+
} from './constants';
|
|
10
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Returns a JSON representation of the generated CSS rules.
|
|
18
|
+
*
|
|
19
|
+
* @param style Style object.
|
|
20
|
+
* @param options Options object with settings to adjust how the styles are generated.
|
|
21
|
+
* @param path An array of strings representing the path to the style value in the style object.
|
|
22
|
+
* @param ruleKey A CSS property key.
|
|
23
|
+
*
|
|
24
|
+
* @return GeneratedCSSRule[] CSS rules.
|
|
25
|
+
*/
|
|
26
|
+
export function generateRule(
|
|
27
|
+
style: Style,
|
|
28
|
+
options: StyleOptions,
|
|
29
|
+
path: string[],
|
|
30
|
+
ruleKey: string
|
|
31
|
+
) {
|
|
32
|
+
const styleValue: string | undefined = get( style, path );
|
|
33
|
+
|
|
34
|
+
return styleValue
|
|
35
|
+
? [
|
|
36
|
+
{
|
|
37
|
+
selector: options?.selector,
|
|
38
|
+
key: ruleKey,
|
|
39
|
+
value: getCSSVarFromStyleValue( styleValue ),
|
|
40
|
+
},
|
|
41
|
+
]
|
|
42
|
+
: [];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Returns a JSON representation of the generated CSS rules taking into account box model properties, top, right, bottom, left.
|
|
47
|
+
*
|
|
48
|
+
* @param style Style object.
|
|
49
|
+
* @param options Options object with settings to adjust how the styles are generated.
|
|
50
|
+
* @param path An array of strings representing the path to the style value in the style object.
|
|
51
|
+
* @param ruleKey A CSS property key.
|
|
52
|
+
*
|
|
53
|
+
* @return GeneratedCSSRule[] CSS rules.
|
|
54
|
+
*/
|
|
11
55
|
export function generateBoxRules(
|
|
12
56
|
style: Style,
|
|
13
57
|
options: StyleOptions,
|
|
@@ -22,7 +66,7 @@ export function generateBoxRules(
|
|
|
22
66
|
const rules: GeneratedCSSRule[] = [];
|
|
23
67
|
if ( typeof boxStyle === 'string' ) {
|
|
24
68
|
rules.push( {
|
|
25
|
-
selector: options
|
|
69
|
+
selector: options?.selector,
|
|
26
70
|
key: ruleKey,
|
|
27
71
|
value: boxStyle,
|
|
28
72
|
} );
|
|
@@ -32,7 +76,7 @@ export function generateBoxRules(
|
|
|
32
76
|
const value: string | undefined = get( boxStyle, [ side ] );
|
|
33
77
|
if ( value ) {
|
|
34
78
|
acc.push( {
|
|
35
|
-
selector: options
|
|
79
|
+
selector: options?.selector,
|
|
36
80
|
key: `${ ruleKey }${ upperFirst( side ) }`,
|
|
37
81
|
value,
|
|
38
82
|
} );
|
|
@@ -46,3 +90,24 @@ export function generateBoxRules(
|
|
|
46
90
|
|
|
47
91
|
return rules;
|
|
48
92
|
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Returns a CSS var value from incoming style value following the pattern `var:description|context|slug`.
|
|
96
|
+
*
|
|
97
|
+
* @param styleValue A raw style value.
|
|
98
|
+
*
|
|
99
|
+
* @return string A CSS var value.
|
|
100
|
+
*/
|
|
101
|
+
export function getCSSVarFromStyleValue( styleValue: string ): string {
|
|
102
|
+
if (
|
|
103
|
+
typeof styleValue === 'string' &&
|
|
104
|
+
styleValue.startsWith( VARIABLE_REFERENCE_PREFIX )
|
|
105
|
+
) {
|
|
106
|
+
const variable = styleValue
|
|
107
|
+
.slice( VARIABLE_REFERENCE_PREFIX.length )
|
|
108
|
+
.split( VARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE )
|
|
109
|
+
.join( VARIABLE_PATH_SEPARATOR_TOKEN_STYLE );
|
|
110
|
+
return `var(--wp--${ variable })`;
|
|
111
|
+
}
|
|
112
|
+
return styleValue;
|
|
113
|
+
}
|