@stylexjs/stylex 0.2.0-beta.9 → 0.3.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 (52) hide show
  1. package/README.md +81 -42
  2. package/lib/StyleXCSSTypes.d.ts +1414 -0
  3. package/lib/StyleXCSSTypes.js +0 -9
  4. package/lib/StyleXCSSTypes.js.flow +1503 -0
  5. package/lib/StyleXSheet.d.ts +49 -0
  6. package/lib/StyleXSheet.js +7 -120
  7. package/lib/StyleXSheet.js.flow +49 -0
  8. package/lib/StyleXTypes.d.ts +212 -0
  9. package/lib/StyleXTypes.js +0 -9
  10. package/lib/StyleXTypes.js.flow +184 -0
  11. package/lib/native/CSSCustomPropertyValue.d.ts +26 -0
  12. package/lib/native/CSSCustomPropertyValue.js +27 -0
  13. package/lib/native/CSSCustomPropertyValue.js.flow +27 -0
  14. package/lib/native/CSSLengthUnitValue.d.ts +18 -0
  15. package/lib/native/CSSLengthUnitValue.js +73 -0
  16. package/lib/native/CSSLengthUnitValue.js.flow +21 -0
  17. package/lib/native/CSSMediaQuery.d.ts +25 -0
  18. package/lib/native/CSSMediaQuery.js +55 -0
  19. package/lib/native/CSSMediaQuery.js.flow +26 -0
  20. package/lib/native/SpreadOptions.d.ts +19 -0
  21. package/lib/native/SpreadOptions.js +1 -0
  22. package/lib/native/SpreadOptions.js.flow +19 -0
  23. package/lib/native/__tests__/__snapshots__/stylex-css-var-test.js.snap +48 -0
  24. package/lib/native/__tests__/__snapshots__/stylex-test.js.snap +1046 -0
  25. package/lib/native/__tests__/parseTimeValue-test.js +11 -0
  26. package/lib/native/__tests__/stylex-css-var-test.js +148 -0
  27. package/lib/native/__tests__/stylex-test.js +924 -0
  28. package/lib/native/errorMsg.d.ts +11 -0
  29. package/lib/native/errorMsg.js +13 -0
  30. package/lib/native/errorMsg.js.flow +12 -0
  31. package/lib/native/fixContentBox.d.ts +11 -0
  32. package/lib/native/fixContentBox.js +59 -0
  33. package/lib/native/fixContentBox.js.flow +11 -0
  34. package/lib/native/flattenStyle.d.ts +15 -0
  35. package/lib/native/flattenStyle.js +20 -0
  36. package/lib/native/flattenStyle.js.flow +20 -0
  37. package/lib/native/parseShadow.d.ts +18 -0
  38. package/lib/native/parseShadow.js +36 -0
  39. package/lib/native/parseShadow.js.flow +19 -0
  40. package/lib/native/parseTimeValue.d.ts +11 -0
  41. package/lib/native/parseTimeValue.js +18 -0
  42. package/lib/native/parseTimeValue.js.flow +12 -0
  43. package/lib/native/stylex.d.ts +50 -0
  44. package/lib/native/stylex.js +393 -0
  45. package/lib/native/stylex.js.flow +60 -0
  46. package/lib/stylex-inject.d.ts +15 -0
  47. package/lib/stylex-inject.js +0 -9
  48. package/lib/stylex-inject.js.flow +14 -0
  49. package/lib/stylex.d.ts +134 -59
  50. package/lib/stylex.js +118 -47
  51. package/lib/stylex.js.flow +151 -0
  52. package/package.json +11 -7
package/README.md CHANGED
@@ -1,8 +1,12 @@
1
1
  # @stylexjs/stylex
2
2
 
3
+ StyleX is a JavaScript library for defining styles for optimized user
4
+ interfaces.
5
+
3
6
  ## Installation
4
7
 
5
- To start playing with StyleX without having to set up any build settings you can install just two packages:
8
+ To start playing with StyleX without having to set up any build settings you can
9
+ install just two packages:
6
10
 
7
11
  ```sh
8
12
  npm install --save @stylexjs/stylex
@@ -10,13 +14,17 @@ npm install --save @stylexjs/stylex
10
14
 
11
15
  ### Compiler
12
16
 
13
- StyleX is designed to extract styles to a static CSS style sheet during an app's build process. StyleX provides a Babel plugin along with plugin integrations for Webpack, Rollup and NextJS.
17
+ StyleX is designed to extract styles to a static CSS style sheet during an app's
18
+ build process. StyleX provides a Babel plugin along with plugin integrations for
19
+ Webpack, Rollup and NextJS.
14
20
 
15
21
  ```sh
16
22
  npm install --save-dev @stylexjs/babel-plugin
17
23
  ```
18
24
 
19
- For more information on working with the compiler, please see the documentation for [`@stylexjs/babel-plugin`](https://www.npmjs.com/package/@stylexjs/babel-plugin).
25
+ For more information on working with the compiler, please see the documentation
26
+ for
27
+ [`@stylexjs/babel-plugin`](https://www.npmjs.com/package/@stylexjs/babel-plugin).
20
28
 
21
29
  ### Runtime compiler
22
30
 
@@ -41,13 +49,17 @@ if (process.env.NODE_ENV !== 'production') {
41
49
  }
42
50
  ```
43
51
 
44
- For more information on working with the compiler, please see the documentation for [`@stylexjs/dev-runtime`](https://www.npmjs.com/package/@stylexjs/dev-runtime).
52
+ For more information on working with the compiler, please see the documentation
53
+ for
54
+ [`@stylexjs/dev-runtime`](https://www.npmjs.com/package/@stylexjs/dev-runtime).
45
55
 
46
56
  ## API
47
57
 
48
58
  ### stylex.create()
49
59
 
50
- Styles are defined as a map of CSS rules using `stylex.create()`. In the example below, there are 2 different CSS rules. The names "root" and "highlighted" are arbitrary names given to the rules.
60
+ Styles are defined as a map of CSS rules using `stylex.create()`. In the example
61
+ below, there are 2 different CSS rules. The names "root" and "highlighted" are
62
+ arbitrary names given to the rules.
51
63
 
52
64
  ```tsx
53
65
  import stylex from '@stylexjs/stylex';
@@ -59,7 +71,7 @@ const styles = stylex.create({
59
71
  },
60
72
  highlighted: {
61
73
  color: 'yellow',
62
- }
74
+ },
63
75
  });
64
76
  ```
65
77
 
@@ -80,63 +92,74 @@ const styles = stylex.create({
80
92
  color: 'yellow',
81
93
  ':hover': {
82
94
  opacity: '0.9',
83
- }
84
- }
95
+ },
96
+ },
85
97
  });
86
98
  ```
87
99
 
88
- The compiler will extract the rules to CSS and replace the rules in the source code with a "compiled style" object.
100
+ The compiler will extract the rules to CSS and replace the rules in the source
101
+ code with a "compiled style" object.
89
102
 
90
- ### stylex.apply()
103
+ ### stylex.props()
91
104
 
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.
105
+ Applying style rules to specific elements is done using `stylex.props`. Each
106
+ argument to this function must be a reference to a compiled style object, or an
107
+ array of compiled style objects. The function merges styles from left to right.
93
108
 
94
109
  ```tsx
95
- <div {...stylex.apply(styles.root, styles.highlighted)} />
110
+ <div {...stylex.props(styles.root, styles.highlighted)} />
96
111
  ```
97
112
 
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:
113
+ The `stylex.props` function returns React props as required to render an
114
+ element. StyleX styles can still be passed to other components via props, but
115
+ only the components rendering host platform elements will use `stylex.props()`.
116
+ For example:
99
117
 
100
118
  ```tsx
101
119
  const styles = stylex.create({
102
120
  internalRoot: {
103
- padding: 10
121
+ padding: 10,
104
122
  },
105
123
  exportedRoot: {
106
- position: 'relative'
107
- }
124
+ position: 'relative',
125
+ },
108
126
  });
109
127
 
110
128
  function InternalComponent(props) {
111
- return <div {...props} {...stylex.apply([ styles.internalRoot, props.style ])} />
129
+ return (
130
+ <div {...props} {...stylex.props([styles.internalRoot, props.style])} />
131
+ );
112
132
  }
113
133
 
114
134
  export function ExportedComponent(props) {
115
- return <InternalComponent style={[ styles.exportedRoot, props.style ]} />
135
+ return <InternalComponent style={[styles.exportedRoot, props.style]} />;
116
136
  }
117
137
  ```
118
138
 
119
139
  Styles can be conditionally included using standard JavaScript.
120
140
 
121
141
  ```tsx
122
- <div {...stylex.apply(styles.root, isHighlighted && styles.highlighted)} />
142
+ <div {...stylex.props(styles.root, isHighlighted && styles.highlighted)} />
123
143
  ```
124
144
 
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.
145
+ And the local merging of styles can be used to control the relative priority of
146
+ rules. For example, to allow a component's local styles to take priority over
147
+ style property values passed in via props.
126
148
 
127
149
  ```tsx
128
- <div {...stylex.apply(props.style, styles.root)} />
150
+ <div {...stylex.props(props.style, styles.root)} />
129
151
  ```
130
152
 
131
153
  You can even mix compiled styles with inline styles
132
154
 
133
155
  ```tsx
134
- <div {...stylex.apply(styles.root, { opacity })} />
156
+ <div {...stylex.props(styles.root, { opacity })} />
135
157
  ```
136
158
 
137
159
  ### stylex.firstThatWorks()
138
160
 
139
- Defining fallback styles is done with `stylex.firstThatWorks()`. This is useful for engines that may not support a specific style property.
161
+ Defining fallback styles is done with `stylex.firstThatWorks()`. This is useful
162
+ for engines that may not support a specific style property.
140
163
 
141
164
  ```tsx
142
165
  import stylex from '@stylexjs/stylex';
@@ -164,7 +187,8 @@ StyleX comes with full support for Static Types.
164
187
 
165
188
  ### `XStyle<>`
166
189
 
167
- The most common type you might need to use is `XStyle<>`. This lets you accept an object of arbitrary StyleX styles.
190
+ The most common type you might need to use is `XStyle<>`. This lets you accept
191
+ an object of arbitrary StyleX styles.
168
192
 
169
193
  ```tsx
170
194
  type Props = {
@@ -174,7 +198,7 @@ type Props = {
174
198
 
175
199
  function MyComponent({style, ...}: Props) {
176
200
  return (
177
- <div {...stylex.apply(localStyles.foo, localStyles.bar, style)} />
201
+ <div {...stylex.props(localStyles.foo, localStyles.bar, style)} />
178
202
  );
179
203
  }
180
204
  ```
@@ -187,34 +211,39 @@ To disallow specific style properties, use the `XStyleWithout<>` type.
187
211
  type Props = {
188
212
  // ...
189
213
  style?: XStyleWithout<{
190
- postion: unknown,
191
- display: unknown
192
- }>
214
+ postion: unknown;
215
+ display: unknown;
216
+ }>;
193
217
  };
194
218
  ```
195
219
 
196
220
  ### `XStyleValue<>`
197
221
 
198
- To accept specific style properties only, use the `XStyle<{...}>` and `XStyleValue` types. For example, to allow only color-related style props:
222
+ To accept specific style properties only, use the `XStyle<{...}>` and
223
+ `XStyleValue` types. For example, to allow only color-related style props:
199
224
 
200
225
  ```tsx
201
226
  type Props = {
202
227
  // ...
203
228
  style?: XStyle<{
204
- color?: StyleXValue,
205
- backgroundColor?: StyleXValue,
206
- borderColor?: StyleXValue,
207
- borderTopColor?: StyleXValue,
208
- borderEndColor?: StyleXValue,
209
- borderBottomColor?: StyleXValue,
210
- borderStartColor?: StyleXValue,
211
- }>,
229
+ color?: StyleXValue;
230
+ backgroundColor?: StyleXValue;
231
+ borderColor?: StyleXValue;
232
+ borderTopColor?: StyleXValue;
233
+ borderEndColor?: StyleXValue;
234
+ borderBottomColor?: StyleXValue;
235
+ borderStartColor?: StyleXValue;
236
+ }>;
212
237
  };
213
238
  ```
214
239
 
215
240
  ### `XStyleValueFor<>`
216
241
 
217
- To limit the possible values for style properties, use the `XStyleValueFor<>` type. Pass in a type argument with a union of literal types that provide the set of possible values that the style property can have. For example, if a component should accept `marginTop` but only accept one of `0`, `4`, or `8` pixels as values.
242
+ To limit the possible values for style properties, use the `XStyleValueFor<>`
243
+ type. Pass in a type argument with a union of literal types that provide the set
244
+ of possible values that the style property can have. For example, if a component
245
+ should accept `marginTop` but only accept one of `0`, `4`, or `8` pixels as
246
+ values.
218
247
 
219
248
  ```tsx
220
249
  type Props = {
@@ -227,7 +256,8 @@ type Props = {
227
256
 
228
257
  ## How StyleX works
229
258
 
230
- StyleX produces atomic styles, which means that each CSS rule contains only a single declaration and uses a unique class name. For example:
259
+ StyleX produces atomic styles, which means that each CSS rule contains only a
260
+ single declaration and uses a unique class name. For example:
231
261
 
232
262
  ```tsx
233
263
  import stylex from '@stylexjs/stylex';
@@ -240,6 +270,15 @@ const styles = stylex.create({
240
270
  }
241
271
  ```
242
272
 
243
- From this code, StyleX will generate 2 classes. One for the `width: '100%'` declaration, and one for the `color: 'red'` declaration. If you use the declaration `width: '100%'` anywhere else in your application, it will *reuse the same CSS class* rather than creating a new one.
244
-
245
- One of the benefits of this approach is that the generated CSS file grows *logarithmically* as you add new styled components to your app. As more style declarations are added to components, they are more likely to already be in use elsehwere in the app. As a result of this CSS optimization, the generated CSS style sheet for an app is usually small enough to be contained in a single file and used across routes, avoiding style recalculation and layout thrashing as users navigate through your app.
273
+ From this code, StyleX will generate 2 classes. One for the `width: '100%'`
274
+ declaration, and one for the `color: 'red'` declaration. If you use the
275
+ declaration `width: '100%'` anywhere else in your application, it will _reuse
276
+ the same CSS class_ rather than creating a new one.
277
+
278
+ One of the benefits of this approach is that the generated CSS file grows
279
+ _logarithmically_ as you add new styled components to your app. As more style
280
+ declarations are added to components, they are more likely to already be in use
281
+ elsehwere in the app. As a result of this CSS optimization, the generated CSS
282
+ style sheet for an app is usually small enough to be contained in a single file
283
+ and used across routes, avoiding style recalculation and layout thrashing as
284
+ users navigate through your app.