@stylexjs/stylex 0.2.0-beta.10 → 0.2.0-beta.11

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/README.md CHANGED
@@ -87,15 +87,15 @@ const styles = stylex.create({
87
87
 
88
88
  The compiler will extract the rules to CSS and replace the rules in the source code with a "compiled style" object.
89
89
 
90
- ### stylex.apply()
90
+ ### stylex.spread()
91
91
 
92
- Applying style rules to specific elements is done using `stylex.apply()`. Each argument to this function must be a reference to a compiled style object, or an array of compiled style objects. The function merges styles from left to right.
92
+ Applying style rules to specific elements is done using `stylex.spread`. Each argument to this function must be a reference to a compiled style object, or an array of compiled style objects. The function merges styles from left to right.
93
93
 
94
94
  ```tsx
95
- <div {...stylex.apply(styles.root, styles.highlighted)} />
95
+ <div {...stylex.spread(styles.root, styles.highlighted)} />
96
96
  ```
97
97
 
98
- The `stylex.apply` function returns React DOM style props as required to render an HTML element. StyleX styles can still be passed to other components via props, but only the components rendering HTML elements will use `stylex.apply()`. For example:
98
+ The `stylex.spread` function returns React props as required to render an element. StyleX styles can still be passed to other components via props, but only the components rendering host platform elements will use `stylex.spread()`. For example:
99
99
 
100
100
  ```tsx
101
101
  const styles = stylex.create({
@@ -108,7 +108,7 @@ const styles = stylex.create({
108
108
  });
109
109
 
110
110
  function InternalComponent(props) {
111
- return <div {...props} {...stylex.apply([ styles.internalRoot, props.style ])} />
111
+ return <div {...props} {...stylex.spread([ styles.internalRoot, props.style ])} />
112
112
  }
113
113
 
114
114
  export function ExportedComponent(props) {
@@ -119,19 +119,19 @@ export function ExportedComponent(props) {
119
119
  Styles can be conditionally included using standard JavaScript.
120
120
 
121
121
  ```tsx
122
- <div {...stylex.apply(styles.root, isHighlighted && styles.highlighted)} />
122
+ <div {...stylex.spread(styles.root, isHighlighted && styles.highlighted)} />
123
123
  ```
124
124
 
125
125
  And the local merging of styles can be used to control the relative priority of rules. For example, to allow a component's local styles to take priority over style property values passed in via props.
126
126
 
127
127
  ```tsx
128
- <div {...stylex.apply(props.style, styles.root)} />
128
+ <div {...stylex.spread(props.style, styles.root)} />
129
129
  ```
130
130
 
131
131
  You can even mix compiled styles with inline styles
132
132
 
133
133
  ```tsx
134
- <div {...stylex.apply(styles.root, { opacity })} />
134
+ <div {...stylex.spread(styles.root, { opacity })} />
135
135
  ```
136
136
 
137
137
  ### stylex.firstThatWorks()
@@ -174,7 +174,7 @@ type Props = {
174
174
 
175
175
  function MyComponent({style, ...}: Props) {
176
176
  return (
177
- <div {...stylex.apply(localStyles.foo, localStyles.bar, style)} />
177
+ <div {...stylex.spread(localStyles.foo, localStyles.bar, style)} />
178
178
  );
179
179
  }
180
180
  ```
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.CSSCustomPropertyValue = void 0;
7
+ exports.isCustomPropertyValue = isCustomPropertyValue;
8
+ /**
9
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
10
+ *
11
+ * This source code is licensed under the MIT license found in the
12
+ * LICENSE file in the root directory of this source tree.
13
+ *
14
+ *
15
+ */
16
+
17
+ const CUSTOM_PROPERTY_REGEX = /^var\(--(.+)\)$/;
18
+ function camelize(s) {
19
+ return s.replace(/-./g, x => x.toUpperCase()[1]);
20
+ }
21
+ function isCustomPropertyValue(value) {
22
+ return typeof value === 'string' && CUSTOM_PROPERTY_REGEX.test(value);
23
+ }
24
+ class CSSCustomPropertyValue {
25
+ constructor(value) {
26
+ const found = value.match(CUSTOM_PROPERTY_REGEX);
27
+ if (found == null) {
28
+ throw new Error('[stylex]: Unable to find custom property reference in input string');
29
+ }
30
+ const [, kebabCasePropName] = found;
31
+ this.name = camelize(kebabCasePropName);
32
+ }
33
+ }
34
+ exports.CSSCustomPropertyValue = CSSCustomPropertyValue;
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.CSSLengthUnitValue = void 0;
7
+ /**
8
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
9
+ *
10
+ * This source code is licensed under the MIT license found in the
11
+ * LICENSE file in the root directory of this source tree.
12
+ *
13
+ *
14
+ */
15
+
16
+ const LENGTH_REGEX = /^([0-9]*[.]?[0-9]+)(em|px|rem|vh|vmax|vmin|vw)$/;
17
+ // TODO: this only works on simple values
18
+ class CSSLengthUnitValue {
19
+ static parse(inp) {
20
+ const match = inp.match(LENGTH_REGEX);
21
+ if (match == null) {
22
+ return null;
23
+ }
24
+ const [, value, unit] = match;
25
+ const parsedValue = parseFloat(value);
26
+ // $FlowFixMe
27
+ return new CSSLengthUnitValue(parsedValue, unit);
28
+ }
29
+ constructor(value, unit) {
30
+ this.value = value;
31
+ this.unit = unit;
32
+ }
33
+ resolvePixelValue(viewportWidth, viewportHeight, fontScale, inheritedFontSize) {
34
+ const unit = this.unit;
35
+ const value = this.value;
36
+ const valuePercent = value / 100;
37
+ switch (unit) {
38
+ case 'em':
39
+ {
40
+ if (inheritedFontSize == null) {
41
+ return fontScale * 16 * value;
42
+ } else {
43
+ return inheritedFontSize * value;
44
+ }
45
+ }
46
+ case 'px':
47
+ {
48
+ return value;
49
+ }
50
+ case 'rem':
51
+ {
52
+ return fontScale * 16 * value;
53
+ }
54
+ case 'vh':
55
+ {
56
+ return viewportHeight * valuePercent;
57
+ }
58
+ case 'vmin':
59
+ {
60
+ return Math.min(viewportWidth, viewportHeight) * valuePercent;
61
+ }
62
+ case 'vmax':
63
+ {
64
+ return Math.max(viewportWidth, viewportHeight) * valuePercent;
65
+ }
66
+ case 'vw':
67
+ {
68
+ return viewportWidth * valuePercent;
69
+ }
70
+ default:
71
+ {
72
+ console.error(`[stylex]: Unsupported unit of "${unit}"`);
73
+ return 0;
74
+ }
75
+ }
76
+ }
77
+ }
78
+ exports.CSSLengthUnitValue = CSSLengthUnitValue;
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.CSSMediaQuery = void 0;
7
+ var _cssMediaquery = _interopRequireDefault(require("css-mediaquery"));
8
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
9
+ /**
10
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
11
+ *
12
+ * This source code is licensed under the MIT license found in the
13
+ * LICENSE file in the root directory of this source tree.
14
+ *
15
+ *
16
+ */
17
+
18
+ const MQ_PREFIX = '@media';
19
+ class CSSMediaQuery {
20
+ static isMediaQueryString(inp) {
21
+ return inp.startsWith(MQ_PREFIX);
22
+ }
23
+ static resolveMediaQueries(styleObj, matchObj) {
24
+ const mediaQueries = [];
25
+ const result = {};
26
+
27
+ // collect all the media queries
28
+ for (const [key, value] of Object.entries(styleObj)) {
29
+ if (value instanceof CSSMediaQuery) {
30
+ mediaQueries.push(value);
31
+ } else {
32
+ result[key] = value;
33
+ }
34
+ }
35
+
36
+ // check the media queries to see if any apply and if they do,
37
+ // merge their styles into the result
38
+ if (mediaQueries.length > 0) {
39
+ for (const mqInst of mediaQueries) {
40
+ const unresolvedMatchedStyle = mqInst.resolve(matchObj);
41
+ const resolvedMatchedStyle = this.resolveMediaQueries(unresolvedMatchedStyle, matchObj);
42
+ for (const propName in resolvedMatchedStyle) {
43
+ result[propName] = resolvedMatchedStyle[propName];
44
+ }
45
+ }
46
+ }
47
+ return result;
48
+ }
49
+ constructor(query, matchedStyle) {
50
+ this.query = query.replace(MQ_PREFIX, '');
51
+ this.matchedStyle = matchedStyle;
52
+ }
53
+ resolve(matchObject) {
54
+ const {
55
+ width,
56
+ height,
57
+ direction
58
+ } = matchObject;
59
+ const matches = _cssMediaquery.default.match(this.query, {
60
+ width,
61
+ height,
62
+ orientation: width > height ? 'landscape' : 'portrait',
63
+ 'aspect-ratio': width / height,
64
+ direction: direction
65
+ });
66
+ return matches ? this.matchedStyle : {};
67
+ }
68
+ }
69
+ exports.CSSMediaQuery = CSSMediaQuery;
@@ -0,0 +1,372 @@
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports[`custom properties filters out the property and emits a warning when encountering a variable name which has not been provided 1`] = `{}`;
4
+
5
+ exports[`custom properties resolves custom properties' values 1`] = `
6
+ {
7
+ "style": {
8
+ "color": "#333",
9
+ "width": 42,
10
+ },
11
+ }
12
+ `;
13
+
14
+ exports[`length units 10 "em" units are resolved to pixels 1`] = `
15
+ {
16
+ "style": {
17
+ "width": 160,
18
+ },
19
+ }
20
+ `;
21
+
22
+ exports[`length units 10 "em" units based on inherited font-size 1`] = `
23
+ {
24
+ "style": {
25
+ "width": 120,
26
+ },
27
+ }
28
+ `;
29
+
30
+ exports[`length units 10 "px" units are resolved to pixels 1`] = `
31
+ {
32
+ "style": {
33
+ "width": 10,
34
+ },
35
+ }
36
+ `;
37
+
38
+ exports[`length units 10 "rem" units are resolved to pixels 1`] = `
39
+ {
40
+ "style": {
41
+ "width": 160,
42
+ },
43
+ }
44
+ `;
45
+
46
+ exports[`length units 10 "vh" units are resolved to pixels 1`] = `
47
+ {
48
+ "style": {
49
+ "width": 60,
50
+ },
51
+ }
52
+ `;
53
+
54
+ exports[`length units 10 "vmax" units are resolved to pixels 1`] = `
55
+ {
56
+ "style": {
57
+ "width": 60,
58
+ },
59
+ }
60
+ `;
61
+
62
+ exports[`length units 10 "vmin" units are resolved to pixels 1`] = `
63
+ {
64
+ "style": {
65
+ "width": 32,
66
+ },
67
+ }
68
+ `;
69
+
70
+ exports[`length units 10 "vw" units are resolved to pixels 1`] = `
71
+ {
72
+ "style": {
73
+ "width": 32,
74
+ },
75
+ }
76
+ `;
77
+
78
+ exports[`styles box-shadow 1`] = `
79
+ {
80
+ "style": {
81
+ "shadowColor": "red",
82
+ "shadowOffset": {
83
+ "height": 2,
84
+ "width": 1,
85
+ },
86
+ "shadowOpacity": 1,
87
+ "shadowRadius": 3,
88
+ },
89
+ }
90
+ `;
91
+
92
+ exports[`styles direction 1`] = `
93
+ {
94
+ "style": {
95
+ "direction": "ltr",
96
+ },
97
+ }
98
+ `;
99
+
100
+ exports[`styles direction 2`] = `
101
+ {
102
+ "style": {
103
+ "direction": "rtl",
104
+ },
105
+ }
106
+ `;
107
+
108
+ exports[`styles font-size: default 1`] = `
109
+ {
110
+ "style": {
111
+ "fontSize": 40,
112
+ },
113
+ }
114
+ `;
115
+
116
+ exports[`styles font-size: fontScale:2 1`] = `
117
+ {
118
+ "style": {
119
+ "fontSize": 80,
120
+ },
121
+ }
122
+ `;
123
+
124
+ exports[`styles font-variant 1`] = `
125
+ {
126
+ "style": {
127
+ "fontVariant": "common-ligatures small-caps",
128
+ },
129
+ }
130
+ `;
131
+
132
+ exports[`styles font-weight 1`] = `
133
+ {
134
+ "style": {
135
+ "fontWeight": "900",
136
+ },
137
+ }
138
+ `;
139
+
140
+ exports[`styles font-weight 2`] = `
141
+ {
142
+ "style": {
143
+ "fontWeight": "bold",
144
+ },
145
+ }
146
+ `;
147
+
148
+ exports[`styles line-clamp 1`] = `
149
+ {
150
+ "numberOfLines": 3,
151
+ }
152
+ `;
153
+
154
+ exports[`styles object-fit: contain 1`] = `
155
+ {
156
+ "style": {
157
+ "objectFit": "contain",
158
+ },
159
+ }
160
+ `;
161
+
162
+ exports[`styles object-fit: contain 2`] = `
163
+ {
164
+ "style": {
165
+ "objectFit": "cover",
166
+ },
167
+ }
168
+ `;
169
+
170
+ exports[`styles object-fit: fill 1`] = `
171
+ {
172
+ "style": {
173
+ "objectFit": "fill",
174
+ },
175
+ }
176
+ `;
177
+
178
+ exports[`styles object-fit: none 1`] = `
179
+ {
180
+ "style": {
181
+ "objectFit": "none",
182
+ },
183
+ }
184
+ `;
185
+
186
+ exports[`styles object-fit: scaleDown 1`] = `
187
+ {
188
+ "style": {
189
+ "objectFit": "scale-down",
190
+ },
191
+ }
192
+ `;
193
+
194
+ exports[`styles pointer-events 1`] = `
195
+ {
196
+ "style": {
197
+ "pointerEvents": "none",
198
+ },
199
+ }
200
+ `;
201
+
202
+ exports[`styles position: absolute 1`] = `
203
+ {
204
+ "style": {
205
+ "position": "absolute",
206
+ },
207
+ }
208
+ `;
209
+
210
+ exports[`styles position: fixed 1`] = `
211
+ {
212
+ "style": {
213
+ "position": "absolute",
214
+ },
215
+ }
216
+ `;
217
+
218
+ exports[`styles position: relative 1`] = `
219
+ {
220
+ "style": {
221
+ "position": "relative",
222
+ },
223
+ }
224
+ `;
225
+
226
+ exports[`styles position: static 1`] = `
227
+ {
228
+ "style": {
229
+ "position": "static",
230
+ },
231
+ }
232
+ `;
233
+
234
+ exports[`styles position: sticky 1`] = `
235
+ {
236
+ "style": {
237
+ "position": "relative",
238
+ },
239
+ }
240
+ `;
241
+
242
+ exports[`styles text-shadow 1`] = `
243
+ {
244
+ "style": {
245
+ "textShadowColor": "red",
246
+ "textShadowOffset": {
247
+ "height": 2,
248
+ "width": 1,
249
+ },
250
+ "textShadowRadius": 3,
251
+ },
252
+ }
253
+ `;
254
+
255
+ exports[`styles text-shadow 2`] = `
256
+ {
257
+ "style": {
258
+ "textShadowColor": "red",
259
+ "textShadowOffset": {
260
+ "height": 2,
261
+ "width": 1,
262
+ },
263
+ "textShadowRadius": 3,
264
+ },
265
+ }
266
+ `;
267
+
268
+ exports[`styles transform: matrix 1`] = `
269
+ {
270
+ "style": {
271
+ "transform": "matrix(0.1, 1, -0.3, 1, 0, 0)",
272
+ },
273
+ }
274
+ `;
275
+
276
+ exports[`styles transform: mixed 1`] = `
277
+ {
278
+ "style": {
279
+ "transform": "
280
+ rotateX(1deg) rotateY(2deg) rotateZ(3deg) rotate3d(1deg, 2deg, 3deg)
281
+ scale(1) scaleX(2) scaleY(3) scaleZ(4) scale3d(1,2,3)
282
+ translateX(1px) translateY(1em) translateZ(1rem) translate3d(1px, 1em, 1rem)
283
+ ",
284
+ },
285
+ }
286
+ `;
287
+
288
+ exports[`styles transform: none 1`] = `
289
+ {
290
+ "style": {
291
+ "transform": "none",
292
+ },
293
+ }
294
+ `;
295
+
296
+ exports[`styles transform: perspective 1`] = `
297
+ {
298
+ "style": {
299
+ "transform": "perspective(10px)",
300
+ },
301
+ }
302
+ `;
303
+
304
+ exports[`styles transform: rotate 1`] = `
305
+ {
306
+ "style": {
307
+ "transform": "rotate(10deg) rotateX(20deg) rotateY(30deg) rotateZ(40deg) rotate3d(0, 0.5, 1, 90deg)",
308
+ },
309
+ }
310
+ `;
311
+
312
+ exports[`styles transform: rotate 2`] = `
313
+ {
314
+ "style": {
315
+ "transform": "rotate(10deg) rotateX(20deg) rotateY(30deg) rotateZ(40deg) rotate3d(0, 0.5, 1, 90deg)",
316
+ },
317
+ }
318
+ `;
319
+
320
+ exports[`styles transform: scale 1`] = `
321
+ {
322
+ "style": {
323
+ "transform": "scale(1, 2) scaleX(1) scaleY(2) scaleZ(3) scale3d(1, 2, 3)",
324
+ },
325
+ }
326
+ `;
327
+
328
+ exports[`styles transform: skew 1`] = `
329
+ {
330
+ "style": {
331
+ "transform": "skew(10deg, 15deg) skewX(20deg) skewY(30deg)",
332
+ },
333
+ }
334
+ `;
335
+
336
+ exports[`styles transform: translate 1`] = `
337
+ {
338
+ "style": {
339
+ "transform": "translate(10px, 20px) translateX(11px) translateY(12px) translateZ(13px) translate3d(20px, 30px, 40px)",
340
+ },
341
+ }
342
+ `;
343
+
344
+ exports[`styles user-select 1`] = `
345
+ {
346
+ "style": {
347
+ "userSelect": "none",
348
+ },
349
+ }
350
+ `;
351
+
352
+ exports[`styles vertical-align: middle 1`] = `
353
+ {
354
+ "style": {
355
+ "verticalAlign": "middle",
356
+ },
357
+ }
358
+ `;
359
+
360
+ exports[`styles vertical-align: top 1`] = `
361
+ {
362
+ "style": {
363
+ "verticalAlign": "top",
364
+ },
365
+ }
366
+ `;
367
+
368
+ exports[`unsupported style values calc 1`] = `{}`;
369
+
370
+ exports[`unsupported style values inherit 1`] = `{}`;
371
+
372
+ exports[`unsupported style values initial 1`] = `{}`;