@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.
- package/README.md +81 -42
- package/lib/StyleXCSSTypes.d.ts +1414 -0
- package/lib/StyleXCSSTypes.js +0 -9
- package/lib/StyleXCSSTypes.js.flow +1503 -0
- package/lib/StyleXSheet.d.ts +49 -0
- package/lib/StyleXSheet.js +7 -120
- package/lib/StyleXSheet.js.flow +49 -0
- package/lib/StyleXTypes.d.ts +212 -0
- package/lib/StyleXTypes.js +0 -9
- package/lib/StyleXTypes.js.flow +184 -0
- package/lib/native/CSSCustomPropertyValue.d.ts +26 -0
- package/lib/native/CSSCustomPropertyValue.js +27 -0
- package/lib/native/CSSCustomPropertyValue.js.flow +27 -0
- package/lib/native/CSSLengthUnitValue.d.ts +18 -0
- package/lib/native/CSSLengthUnitValue.js +73 -0
- package/lib/native/CSSLengthUnitValue.js.flow +21 -0
- package/lib/native/CSSMediaQuery.d.ts +25 -0
- package/lib/native/CSSMediaQuery.js +55 -0
- package/lib/native/CSSMediaQuery.js.flow +26 -0
- package/lib/native/SpreadOptions.d.ts +19 -0
- package/lib/native/SpreadOptions.js +1 -0
- package/lib/native/SpreadOptions.js.flow +19 -0
- package/lib/native/__tests__/__snapshots__/stylex-css-var-test.js.snap +48 -0
- package/lib/native/__tests__/__snapshots__/stylex-test.js.snap +1046 -0
- package/lib/native/__tests__/parseTimeValue-test.js +11 -0
- package/lib/native/__tests__/stylex-css-var-test.js +148 -0
- package/lib/native/__tests__/stylex-test.js +924 -0
- package/lib/native/errorMsg.d.ts +11 -0
- package/lib/native/errorMsg.js +13 -0
- package/lib/native/errorMsg.js.flow +12 -0
- package/lib/native/fixContentBox.d.ts +11 -0
- package/lib/native/fixContentBox.js +59 -0
- package/lib/native/fixContentBox.js.flow +11 -0
- package/lib/native/flattenStyle.d.ts +15 -0
- package/lib/native/flattenStyle.js +20 -0
- package/lib/native/flattenStyle.js.flow +20 -0
- package/lib/native/parseShadow.d.ts +18 -0
- package/lib/native/parseShadow.js +36 -0
- package/lib/native/parseShadow.js.flow +19 -0
- package/lib/native/parseTimeValue.d.ts +11 -0
- package/lib/native/parseTimeValue.js +18 -0
- package/lib/native/parseTimeValue.js.flow +12 -0
- package/lib/native/stylex.d.ts +50 -0
- package/lib/native/stylex.js +393 -0
- package/lib/native/stylex.js.flow +60 -0
- package/lib/stylex-inject.d.ts +15 -0
- package/lib/stylex-inject.js +0 -9
- package/lib/stylex-inject.js.flow +14 -0
- package/lib/stylex.d.ts +134 -59
- package/lib/stylex.js +118 -47
- package/lib/stylex.js.flow +151 -0
- 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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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.
|
|
103
|
+
### stylex.props()
|
|
91
104
|
|
|
92
|
-
Applying style rules to specific elements is done using `stylex.
|
|
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.
|
|
110
|
+
<div {...stylex.props(styles.root, styles.highlighted)} />
|
|
96
111
|
```
|
|
97
112
|
|
|
98
|
-
The `stylex.
|
|
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
|
|
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={[
|
|
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.
|
|
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
|
|
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.
|
|
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.
|
|
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
|
|
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
|
|
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.
|
|
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
|
|
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<>`
|
|
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
|
|
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%'`
|
|
244
|
-
|
|
245
|
-
|
|
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.
|