@stylexjs/stylex 0.2.0-beta.8 → 0.2.0-beta.9
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 +245 -0
- package/lib/stylex.js +36 -75
- package/package.json +3 -5
package/README.md
ADDED
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
# @stylexjs/stylex
|
|
2
|
+
|
|
3
|
+
## Installation
|
|
4
|
+
|
|
5
|
+
To start playing with StyleX without having to set up any build settings you can install just two packages:
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install --save @stylexjs/stylex
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
### Compiler
|
|
12
|
+
|
|
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.
|
|
14
|
+
|
|
15
|
+
```sh
|
|
16
|
+
npm install --save-dev @stylexjs/babel-plugin
|
|
17
|
+
```
|
|
18
|
+
|
|
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).
|
|
20
|
+
|
|
21
|
+
### Runtime compiler
|
|
22
|
+
|
|
23
|
+
The runtime compiler should only be used for development and testing purposes.
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
npm install --save-dev @stylexjs/dev-runtime
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Import `@stylexjs/dev-runtime` in your JS entry-point to set everything up.
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
import inject from '@stylexjs/dev-runtime';
|
|
33
|
+
|
|
34
|
+
if (process.env.NODE_ENV !== 'production') {
|
|
35
|
+
inject({
|
|
36
|
+
// configuration options
|
|
37
|
+
classNamePrefix: 'x-',
|
|
38
|
+
dev: true,
|
|
39
|
+
test: false,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
```
|
|
43
|
+
|
|
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).
|
|
45
|
+
|
|
46
|
+
## API
|
|
47
|
+
|
|
48
|
+
### stylex.create()
|
|
49
|
+
|
|
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.
|
|
51
|
+
|
|
52
|
+
```tsx
|
|
53
|
+
import stylex from '@stylexjs/stylex';
|
|
54
|
+
|
|
55
|
+
const styles = stylex.create({
|
|
56
|
+
root: {
|
|
57
|
+
width: '100%',
|
|
58
|
+
color: 'rgb(60,60,60)',
|
|
59
|
+
},
|
|
60
|
+
highlighted: {
|
|
61
|
+
color: 'yellow',
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Pseudo-classes and Media Queries can be nested within style definitions:
|
|
67
|
+
|
|
68
|
+
```tsx
|
|
69
|
+
import stylex from '@stylexjs/stylex';
|
|
70
|
+
|
|
71
|
+
const styles = stylex.create({
|
|
72
|
+
root: {
|
|
73
|
+
width: '100%',
|
|
74
|
+
color: 'rgb(60,60,60)',
|
|
75
|
+
'@media (min-width: 800px)': {
|
|
76
|
+
maxWidth: '800px',
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
highlighted: {
|
|
80
|
+
color: 'yellow',
|
|
81
|
+
':hover': {
|
|
82
|
+
opacity: '0.9',
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
The compiler will extract the rules to CSS and replace the rules in the source code with a "compiled style" object.
|
|
89
|
+
|
|
90
|
+
### stylex.apply()
|
|
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.
|
|
93
|
+
|
|
94
|
+
```tsx
|
|
95
|
+
<div {...stylex.apply(styles.root, styles.highlighted)} />
|
|
96
|
+
```
|
|
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:
|
|
99
|
+
|
|
100
|
+
```tsx
|
|
101
|
+
const styles = stylex.create({
|
|
102
|
+
internalRoot: {
|
|
103
|
+
padding: 10
|
|
104
|
+
},
|
|
105
|
+
exportedRoot: {
|
|
106
|
+
position: 'relative'
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
function InternalComponent(props) {
|
|
111
|
+
return <div {...props} {...stylex.apply([ styles.internalRoot, props.style ])} />
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export function ExportedComponent(props) {
|
|
115
|
+
return <InternalComponent style={[ styles.exportedRoot, props.style ]} />
|
|
116
|
+
}
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Styles can be conditionally included using standard JavaScript.
|
|
120
|
+
|
|
121
|
+
```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)} />
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
You can even mix compiled styles with inline styles
|
|
132
|
+
|
|
133
|
+
```tsx
|
|
134
|
+
<div {...stylex.apply(styles.root, { opacity })} />
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### stylex.firstThatWorks()
|
|
138
|
+
|
|
139
|
+
Defining fallback styles is done with `stylex.firstThatWorks()`. This is useful for engines that may not support a specific style property.
|
|
140
|
+
|
|
141
|
+
```tsx
|
|
142
|
+
import stylex from '@stylexjs/stylex';
|
|
143
|
+
|
|
144
|
+
const styles = stylex.create({
|
|
145
|
+
header: {
|
|
146
|
+
position: stylex.firstThatWorks('sticky', '-webkit-sticky', 'fixed'),
|
|
147
|
+
},
|
|
148
|
+
});
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
This is equivalent to defining CSS as follows:
|
|
152
|
+
|
|
153
|
+
```css
|
|
154
|
+
.header {
|
|
155
|
+
position: fixed;
|
|
156
|
+
position: -webkit-sticky;
|
|
157
|
+
position: sticky;
|
|
158
|
+
}
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Types
|
|
162
|
+
|
|
163
|
+
StyleX comes with full support for Static Types.
|
|
164
|
+
|
|
165
|
+
### `XStyle<>`
|
|
166
|
+
|
|
167
|
+
The most common type you might need to use is `XStyle<>`. This lets you accept an object of arbitrary StyleX styles.
|
|
168
|
+
|
|
169
|
+
```tsx
|
|
170
|
+
type Props = {
|
|
171
|
+
...
|
|
172
|
+
style?: XStyle<>,
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
function MyComponent({style, ...}: Props) {
|
|
176
|
+
return (
|
|
177
|
+
<div {...stylex.apply(localStyles.foo, localStyles.bar, style)} />
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
### `XStyleWithout<>`
|
|
183
|
+
|
|
184
|
+
To disallow specific style properties, use the `XStyleWithout<>` type.
|
|
185
|
+
|
|
186
|
+
```tsx
|
|
187
|
+
type Props = {
|
|
188
|
+
// ...
|
|
189
|
+
style?: XStyleWithout<{
|
|
190
|
+
postion: unknown,
|
|
191
|
+
display: unknown
|
|
192
|
+
}>
|
|
193
|
+
};
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
### `XStyleValue<>`
|
|
197
|
+
|
|
198
|
+
To accept specific style properties only, use the `XStyle<{...}>` and `XStyleValue` types. For example, to allow only color-related style props:
|
|
199
|
+
|
|
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
|
+
```
|
|
214
|
+
|
|
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
|
+
|
|
219
|
+
```tsx
|
|
220
|
+
type Props = {
|
|
221
|
+
...
|
|
222
|
+
style?: XStyle<{
|
|
223
|
+
marginTop: XStyleValueFor<0 | 4 | 8>
|
|
224
|
+
}>,
|
|
225
|
+
};
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
## How StyleX works
|
|
229
|
+
|
|
230
|
+
StyleX produces atomic styles, which means that each CSS rule contains only a single declaration and uses a unique class name. For example:
|
|
231
|
+
|
|
232
|
+
```tsx
|
|
233
|
+
import stylex from '@stylexjs/stylex';
|
|
234
|
+
|
|
235
|
+
const styles = stylex.create({
|
|
236
|
+
root: {
|
|
237
|
+
width: '100%',
|
|
238
|
+
color: 'red',
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
```
|
|
242
|
+
|
|
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.
|
package/lib/stylex.js
CHANGED
|
@@ -12,99 +12,60 @@
|
|
|
12
12
|
Object.defineProperty(exports, "__esModule", {
|
|
13
13
|
value: true
|
|
14
14
|
});
|
|
15
|
-
exports.
|
|
15
|
+
exports.UNSUPPORTED_PROPERTY = void 0;
|
|
16
|
+
exports.apply = apply;
|
|
17
|
+
exports.stylex = exports.keyframes = exports.inject = exports.include = exports.firstThatWorks = exports.default = exports.create = void 0;
|
|
16
18
|
var _stylexInject = _interopRequireDefault(require("./stylex-inject"));
|
|
19
|
+
var _styleq = require("styleq");
|
|
17
20
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
18
|
-
const
|
|
19
|
-
const cache = enableCache ? new WeakMap() : null;
|
|
20
|
-
function stylex() {
|
|
21
|
-
// Keep a set of property commits to the className
|
|
22
|
-
const definedProperties = [];
|
|
23
|
-
let className = '';
|
|
24
|
-
let nextCache = cache;
|
|
21
|
+
const stylex = function stylex() {
|
|
25
22
|
for (var _len = arguments.length, styles = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
26
23
|
styles[_key] = arguments[_key];
|
|
27
24
|
}
|
|
28
|
-
|
|
29
|
-
// Push nested styles back onto the stack to be processed
|
|
30
|
-
const possibleStyle = styles.pop();
|
|
31
|
-
if (Array.isArray(possibleStyle)) {
|
|
32
|
-
for (let i = 0; i < possibleStyle.length; i++) {
|
|
33
|
-
styles.push(possibleStyle[i]);
|
|
34
|
-
}
|
|
35
|
-
continue;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// Process an individual style object
|
|
39
|
-
const styleObj = possibleStyle;
|
|
40
|
-
if (styleObj != null && typeof styleObj === 'object') {
|
|
41
|
-
// Build up the class names defined by this object
|
|
42
|
-
let classNameChunk = '';
|
|
43
|
-
if (nextCache != null && nextCache.has(styleObj)) {
|
|
44
|
-
// Cache: read
|
|
45
|
-
const cacheEntry = nextCache.get(styleObj);
|
|
46
|
-
if (cacheEntry != null) {
|
|
47
|
-
classNameChunk = cacheEntry.classNameChunk;
|
|
48
|
-
definedProperties.push(...cacheEntry.definedPropertiesChunk);
|
|
49
|
-
nextCache = cacheEntry.next;
|
|
50
|
-
}
|
|
51
|
-
} else {
|
|
52
|
-
// Record the properties this object defines (and that haven't already
|
|
53
|
-
// been defined by later objects.)
|
|
54
|
-
const definedPropertiesChunk = [];
|
|
55
|
-
for (const prop in styleObj) {
|
|
56
|
-
const value = styleObj[prop];
|
|
57
|
-
// Style declarations, e.g., opacity: 's3fkgpd'
|
|
58
|
-
if (typeof value === 'string' || value === null) {
|
|
59
|
-
// Skip adding to the chunks if property has already been seen
|
|
60
|
-
if (!definedProperties.includes(prop)) {
|
|
61
|
-
definedProperties.push(prop);
|
|
62
|
-
definedPropertiesChunk.push(prop);
|
|
63
|
-
if (typeof value === 'string') {
|
|
64
|
-
classNameChunk += classNameChunk ? ' ' + value : value;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
// Cache: write
|
|
70
|
-
if (nextCache != null) {
|
|
71
|
-
const emptyCache = new WeakMap();
|
|
72
|
-
nextCache.set(styleObj, {
|
|
73
|
-
classNameChunk,
|
|
74
|
-
definedPropertiesChunk,
|
|
75
|
-
next: emptyCache
|
|
76
|
-
});
|
|
77
|
-
nextCache = emptyCache;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// Order of classes in chunks matches property-iteration order of style
|
|
82
|
-
// object. Order of chunks matches passed order of styles from first to
|
|
83
|
-
// last (which we iterate over in reverse).
|
|
84
|
-
if (classNameChunk) {
|
|
85
|
-
className = className ? classNameChunk + ' ' + className : classNameChunk;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
25
|
+
const [className] = (0, _styleq.styleq)(styles);
|
|
89
26
|
return className;
|
|
27
|
+
};
|
|
28
|
+
exports.stylex = stylex;
|
|
29
|
+
function apply() {
|
|
30
|
+
for (var _len2 = arguments.length, styles = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
|
|
31
|
+
styles[_key2] = arguments[_key2];
|
|
32
|
+
}
|
|
33
|
+
const [className, style] = (0, _styleq.styleq)(styles);
|
|
34
|
+
return {
|
|
35
|
+
className,
|
|
36
|
+
style
|
|
37
|
+
};
|
|
90
38
|
}
|
|
39
|
+
stylex.apply = apply;
|
|
91
40
|
function stylexCreate(_styles) {
|
|
92
41
|
throw new Error('stylex.create should never be called. It should be compiled away.');
|
|
93
42
|
}
|
|
94
43
|
function stylexIncludes(_styles) {
|
|
95
44
|
throw new Error('stylex.extends should never be called. It should be compiled away.');
|
|
96
45
|
}
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
stylex.
|
|
46
|
+
const create = stylexCreate;
|
|
47
|
+
exports.create = create;
|
|
48
|
+
stylex.create = create;
|
|
49
|
+
const include = stylexIncludes;
|
|
50
|
+
exports.include = include;
|
|
51
|
+
stylex.include = include;
|
|
52
|
+
const keyframes = _keyframes => {
|
|
100
53
|
throw new Error('stylex.keyframes should never be called');
|
|
101
54
|
};
|
|
102
|
-
|
|
55
|
+
exports.keyframes = keyframes;
|
|
56
|
+
stylex.keyframes = keyframes;
|
|
57
|
+
const firstThatWorks = function () {
|
|
103
58
|
throw new Error('stylex.firstThatWorks should never be called.');
|
|
104
59
|
};
|
|
105
|
-
|
|
106
|
-
stylex.
|
|
60
|
+
exports.firstThatWorks = firstThatWorks;
|
|
61
|
+
stylex.firstThatWorks = firstThatWorks;
|
|
62
|
+
const inject = _stylexInject.default;
|
|
63
|
+
exports.inject = inject;
|
|
64
|
+
stylex.inject = inject;
|
|
65
|
+
const UNSUPPORTED_PROPERTY = props => {
|
|
107
66
|
throw new Error('stylex.UNSUPPORTED_PROPERTY should never be called. It should be compiled away.');
|
|
108
67
|
};
|
|
68
|
+
exports.UNSUPPORTED_PROPERTY = UNSUPPORTED_PROPERTY;
|
|
69
|
+
stylex.UNSUPPORTED_PROPERTY = UNSUPPORTED_PROPERTY;
|
|
109
70
|
var _default = stylex;
|
|
110
71
|
exports.default = _default;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@stylexjs/stylex",
|
|
3
|
-
"version": "0.2.0-beta.
|
|
3
|
+
"version": "0.2.0-beta.9",
|
|
4
4
|
"description": "A minimal runtime styling library for web.",
|
|
5
5
|
"main": "lib/stylex.js",
|
|
6
6
|
"types": "lib/stylex.d.ts",
|
|
@@ -14,10 +14,8 @@
|
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
16
|
"invariant": "^2.2.4",
|
|
17
|
-
"utility-types": "^3.10.0"
|
|
18
|
-
|
|
19
|
-
"devDependencies": {
|
|
20
|
-
"benchmark": "^2.1.4"
|
|
17
|
+
"utility-types": "^3.10.0",
|
|
18
|
+
"styleq": "0.1.3"
|
|
21
19
|
},
|
|
22
20
|
"jest": {},
|
|
23
21
|
"files": [
|