@stylexjs/stylex 0.2.0-beta.9 → 0.4.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/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
 
@@ -72,71 +84,76 @@ const styles = stylex.create({
72
84
  root: {
73
85
  width: '100%',
74
86
  color: 'rgb(60,60,60)',
75
- '@media (min-width: 800px)': {
76
- maxWidth: '800px',
87
+ maxWidth: {
88
+ '@media (min-width: 800px)': '800px',
77
89
  },
78
90
  },
79
91
  highlighted: {
80
92
  color: 'yellow',
81
- ':hover': {
82
- opacity: '0.9',
83
- }
84
- }
93
+ opacity: {
94
+ ':hover': '0.9',
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)} />
123
- ```
124
-
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
-
127
- ```tsx
128
- <div {...stylex.apply(props.style, styles.root)} />
142
+ <div {...stylex.props(styles.root, isHighlighted && styles.highlighted)} />
129
143
  ```
130
144
 
131
- You can even mix compiled styles with inline styles
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.
132
148
 
133
149
  ```tsx
134
- <div {...stylex.apply(styles.root, { opacity })} />
150
+ <div {...stylex.props(props.style, styles.root)} />
135
151
  ```
136
152
 
137
153
  ### stylex.firstThatWorks()
138
154
 
139
- Defining fallback styles is done with `stylex.firstThatWorks()`. This is useful for engines that may not support a specific style property.
155
+ Defining fallback styles is done with `stylex.firstThatWorks()`. This is useful
156
+ for engines that may not support a specific style property.
140
157
 
141
158
  ```tsx
142
159
  import stylex from '@stylexjs/stylex';
@@ -162,72 +179,60 @@ This is equivalent to defining CSS as follows:
162
179
 
163
180
  StyleX comes with full support for Static Types.
164
181
 
165
- ### `XStyle<>`
182
+ ### `StyleXStyles<>`
166
183
 
167
- The most common type you might need to use is `XStyle<>`. This lets you accept an object of arbitrary StyleX styles.
184
+ The most common type you might need to use is `StyleXStyles<>`. This lets you accept
185
+ an object of arbitrary StyleX styles.
168
186
 
169
187
  ```tsx
170
188
  type Props = {
171
189
  ...
172
- style?: XStyle<>,
190
+ style?: StyleXStyles<>,
173
191
  };
174
192
 
175
193
  function MyComponent({style, ...}: Props) {
176
194
  return (
177
- <div {...stylex.apply(localStyles.foo, localStyles.bar, style)} />
195
+ <div {...stylex.props(localStyles.foo, localStyles.bar, style)} />
178
196
  );
179
197
  }
180
198
  ```
181
199
 
182
- ### `XStyleWithout<>`
200
+ ### `StyleXStylesWithout<>`
183
201
 
184
- To disallow specific style properties, use the `XStyleWithout<>` type.
202
+ To disallow specific style properties, use the `StyleXStylesWithout<>` type.
185
203
 
186
204
  ```tsx
187
205
  type Props = {
188
206
  // ...
189
- style?: XStyleWithout<{
190
- postion: unknown,
191
- display: unknown
192
- }>
207
+ style?: StyleXStylesWithout<{
208
+ position: unknown;
209
+ display: unknown;
210
+ }>;
193
211
  };
194
212
  ```
195
213
 
196
- ### `XStyleValue<>`
197
214
 
198
- To accept specific style properties only, use the `XStyle<{...}>` and `XStyleValue` types. For example, to allow only color-related style props:
199
215
 
200
- ```tsx
201
- type Props = {
202
- // ...
203
- style?: XStyle<{
204
- color?: StyleXValue,
205
- backgroundColor?: StyleXValue,
206
- borderColor?: StyleXValue,
207
- borderTopColor?: StyleXValue,
208
- borderEndColor?: StyleXValue,
209
- borderBottomColor?: StyleXValue,
210
- borderStartColor?: StyleXValue,
211
- }>,
212
- };
213
- ```
216
+ ### `StaticStyles<>`
214
217
 
215
- ### `XStyleValueFor<>`
216
-
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.
218
+ To constrain the styles to specific properties and values, use the `StaticStyles<>`
219
+ type. For example, if a component
220
+ should accept `marginTop` but only accept one of `0`, `4`, or `8` pixels as
221
+ values.
218
222
 
219
223
  ```tsx
220
224
  type Props = {
221
225
  ...
222
- style?: XStyle<{
223
- marginTop: XStyleValueFor<0 | 4 | 8>
226
+ style?: StaticStyles<{
227
+ marginTop?: 0 | 4 | 8;
224
228
  }>,
225
229
  };
226
230
  ```
227
231
 
228
232
  ## How StyleX works
229
233
 
230
- StyleX produces atomic styles, which means that each CSS rule contains only a single declaration and uses a unique class name. For example:
234
+ StyleX produces atomic styles, which means that each CSS rule contains only a
235
+ single declaration and uses a unique class name. For example:
231
236
 
232
237
  ```tsx
233
238
  import stylex from '@stylexjs/stylex';
@@ -240,6 +245,15 @@ const styles = stylex.create({
240
245
  }
241
246
  ```
242
247
 
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.
248
+ From this code, StyleX will generate 2 classes. One for the `width: '100%'`
249
+ declaration, and one for the `color: 'red'` declaration. If you use the
250
+ declaration `width: '100%'` anywhere else in your application, it will _reuse
251
+ the same CSS class_ rather than creating a new one.
252
+
253
+ One of the benefits of this approach is that the generated CSS file grows
254
+ _logarithmically_ as you add new styled components to your app. As more style
255
+ declarations are added to components, they are more likely to already be in use
256
+ elsewhere in the app. As a result of this CSS optimization, the generated CSS
257
+ style sheet for an app is usually small enough to be contained in a single file
258
+ and used across routes, avoiding style recalculation and layout thrashing as
259
+ users navigate through your app.