@stylexjs/stylex 0.2.0-beta.8 → 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 +284 -0
- 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 +123 -91
- package/lib/stylex.js.flow +151 -0
- package/package.json +8 -6
package/README.md
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
# @stylexjs/stylex
|
|
2
|
+
|
|
3
|
+
StyleX is a JavaScript library for defining styles for optimized user
|
|
4
|
+
interfaces.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
To start playing with StyleX without having to set up any build settings you can
|
|
9
|
+
install just two packages:
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install --save @stylexjs/stylex
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
### Compiler
|
|
16
|
+
|
|
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.
|
|
20
|
+
|
|
21
|
+
```sh
|
|
22
|
+
npm install --save-dev @stylexjs/babel-plugin
|
|
23
|
+
```
|
|
24
|
+
|
|
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).
|
|
28
|
+
|
|
29
|
+
### Runtime compiler
|
|
30
|
+
|
|
31
|
+
The runtime compiler should only be used for development and testing purposes.
|
|
32
|
+
|
|
33
|
+
```sh
|
|
34
|
+
npm install --save-dev @stylexjs/dev-runtime
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Import `@stylexjs/dev-runtime` in your JS entry-point to set everything up.
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import inject from '@stylexjs/dev-runtime';
|
|
41
|
+
|
|
42
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
43
|
+
inject({
|
|
44
|
+
// configuration options
|
|
45
|
+
classNamePrefix: 'x-',
|
|
46
|
+
dev: true,
|
|
47
|
+
test: false,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
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).
|
|
55
|
+
|
|
56
|
+
## API
|
|
57
|
+
|
|
58
|
+
### stylex.create()
|
|
59
|
+
|
|
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.
|
|
63
|
+
|
|
64
|
+
```tsx
|
|
65
|
+
import stylex from '@stylexjs/stylex';
|
|
66
|
+
|
|
67
|
+
const styles = stylex.create({
|
|
68
|
+
root: {
|
|
69
|
+
width: '100%',
|
|
70
|
+
color: 'rgb(60,60,60)',
|
|
71
|
+
},
|
|
72
|
+
highlighted: {
|
|
73
|
+
color: 'yellow',
|
|
74
|
+
},
|
|
75
|
+
});
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Pseudo-classes and Media Queries can be nested within style definitions:
|
|
79
|
+
|
|
80
|
+
```tsx
|
|
81
|
+
import stylex from '@stylexjs/stylex';
|
|
82
|
+
|
|
83
|
+
const styles = stylex.create({
|
|
84
|
+
root: {
|
|
85
|
+
width: '100%',
|
|
86
|
+
color: 'rgb(60,60,60)',
|
|
87
|
+
'@media (min-width: 800px)': {
|
|
88
|
+
maxWidth: '800px',
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
highlighted: {
|
|
92
|
+
color: 'yellow',
|
|
93
|
+
':hover': {
|
|
94
|
+
opacity: '0.9',
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
The compiler will extract the rules to CSS and replace the rules in the source
|
|
101
|
+
code with a "compiled style" object.
|
|
102
|
+
|
|
103
|
+
### stylex.props()
|
|
104
|
+
|
|
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.
|
|
108
|
+
|
|
109
|
+
```tsx
|
|
110
|
+
<div {...stylex.props(styles.root, styles.highlighted)} />
|
|
111
|
+
```
|
|
112
|
+
|
|
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:
|
|
117
|
+
|
|
118
|
+
```tsx
|
|
119
|
+
const styles = stylex.create({
|
|
120
|
+
internalRoot: {
|
|
121
|
+
padding: 10,
|
|
122
|
+
},
|
|
123
|
+
exportedRoot: {
|
|
124
|
+
position: 'relative',
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
function InternalComponent(props) {
|
|
129
|
+
return (
|
|
130
|
+
<div {...props} {...stylex.props([styles.internalRoot, props.style])} />
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export function ExportedComponent(props) {
|
|
135
|
+
return <InternalComponent style={[styles.exportedRoot, props.style]} />;
|
|
136
|
+
}
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Styles can be conditionally included using standard JavaScript.
|
|
140
|
+
|
|
141
|
+
```tsx
|
|
142
|
+
<div {...stylex.props(styles.root, isHighlighted && styles.highlighted)} />
|
|
143
|
+
```
|
|
144
|
+
|
|
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.
|
|
148
|
+
|
|
149
|
+
```tsx
|
|
150
|
+
<div {...stylex.props(props.style, styles.root)} />
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
You can even mix compiled styles with inline styles
|
|
154
|
+
|
|
155
|
+
```tsx
|
|
156
|
+
<div {...stylex.props(styles.root, { opacity })} />
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
### stylex.firstThatWorks()
|
|
160
|
+
|
|
161
|
+
Defining fallback styles is done with `stylex.firstThatWorks()`. This is useful
|
|
162
|
+
for engines that may not support a specific style property.
|
|
163
|
+
|
|
164
|
+
```tsx
|
|
165
|
+
import stylex from '@stylexjs/stylex';
|
|
166
|
+
|
|
167
|
+
const styles = stylex.create({
|
|
168
|
+
header: {
|
|
169
|
+
position: stylex.firstThatWorks('sticky', '-webkit-sticky', 'fixed'),
|
|
170
|
+
},
|
|
171
|
+
});
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
This is equivalent to defining CSS as follows:
|
|
175
|
+
|
|
176
|
+
```css
|
|
177
|
+
.header {
|
|
178
|
+
position: fixed;
|
|
179
|
+
position: -webkit-sticky;
|
|
180
|
+
position: sticky;
|
|
181
|
+
}
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## Types
|
|
185
|
+
|
|
186
|
+
StyleX comes with full support for Static Types.
|
|
187
|
+
|
|
188
|
+
### `XStyle<>`
|
|
189
|
+
|
|
190
|
+
The most common type you might need to use is `XStyle<>`. This lets you accept
|
|
191
|
+
an object of arbitrary StyleX styles.
|
|
192
|
+
|
|
193
|
+
```tsx
|
|
194
|
+
type Props = {
|
|
195
|
+
...
|
|
196
|
+
style?: XStyle<>,
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
function MyComponent({style, ...}: Props) {
|
|
200
|
+
return (
|
|
201
|
+
<div {...stylex.props(localStyles.foo, localStyles.bar, style)} />
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### `XStyleWithout<>`
|
|
207
|
+
|
|
208
|
+
To disallow specific style properties, use the `XStyleWithout<>` type.
|
|
209
|
+
|
|
210
|
+
```tsx
|
|
211
|
+
type Props = {
|
|
212
|
+
// ...
|
|
213
|
+
style?: XStyleWithout<{
|
|
214
|
+
postion: unknown;
|
|
215
|
+
display: unknown;
|
|
216
|
+
}>;
|
|
217
|
+
};
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### `XStyleValue<>`
|
|
221
|
+
|
|
222
|
+
To accept specific style properties only, use the `XStyle<{...}>` and
|
|
223
|
+
`XStyleValue` types. For example, to allow only color-related style props:
|
|
224
|
+
|
|
225
|
+
```tsx
|
|
226
|
+
type Props = {
|
|
227
|
+
// ...
|
|
228
|
+
style?: XStyle<{
|
|
229
|
+
color?: StyleXValue;
|
|
230
|
+
backgroundColor?: StyleXValue;
|
|
231
|
+
borderColor?: StyleXValue;
|
|
232
|
+
borderTopColor?: StyleXValue;
|
|
233
|
+
borderEndColor?: StyleXValue;
|
|
234
|
+
borderBottomColor?: StyleXValue;
|
|
235
|
+
borderStartColor?: StyleXValue;
|
|
236
|
+
}>;
|
|
237
|
+
};
|
|
238
|
+
```
|
|
239
|
+
|
|
240
|
+
### `XStyleValueFor<>`
|
|
241
|
+
|
|
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.
|
|
247
|
+
|
|
248
|
+
```tsx
|
|
249
|
+
type Props = {
|
|
250
|
+
...
|
|
251
|
+
style?: XStyle<{
|
|
252
|
+
marginTop: XStyleValueFor<0 | 4 | 8>
|
|
253
|
+
}>,
|
|
254
|
+
};
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
## How StyleX works
|
|
258
|
+
|
|
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:
|
|
261
|
+
|
|
262
|
+
```tsx
|
|
263
|
+
import stylex from '@stylexjs/stylex';
|
|
264
|
+
|
|
265
|
+
const styles = stylex.create({
|
|
266
|
+
root: {
|
|
267
|
+
width: '100%',
|
|
268
|
+
color: 'red',
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
```
|
|
272
|
+
|
|
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.
|