@stylix/core 6.1.1 → 6.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 +140 -280
- package/dist/index.d.ts +85 -60
- package/dist/index.js +351 -301
- package/dist/index.js.map +1 -1
- package/docs/CLAUDE_CONTEXT.md +156 -0
- package/docs/cheatsheet.md +354 -0
- package/docs/patterns.md +754 -0
- package/docs/performance.md +291 -0
- package/docs/philosophy.md +168 -0
- package/package.json +23 -20
- package/tsconfig.json +0 -0
package/README.md
CHANGED
|
@@ -1,367 +1,227 @@
|
|
|
1
1
|
# Stylix
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
Stylix is a CSS-in-JS library built for React developers who want a seamless, intuitive styling experience. With Stylix, you write styles as props on components you already know, staying within the React paradigm you love.
|
|
6
|
-
|
|
7
|
-
Stylix offers:
|
|
8
|
-
- Props-based styling on HTML elements
|
|
9
|
-
- Hooks for dynamic and global styles
|
|
10
|
-
- Keyframe animations
|
|
11
|
-
- Server-side rendering
|
|
12
|
-
- An extensible plugin system
|
|
13
|
-
|
|
14
|
-
## Why Stylix?
|
|
15
|
-
|
|
16
|
-
We created Stylix to solve common styling pain points in React applications:
|
|
17
|
-
- **Co-located styles**: Keep your styles next to your components—no separate CSS files.
|
|
18
|
-
- **Familiar API**: Style props feel like any other React prop.
|
|
19
|
-
- **Type safety**: Enjoy autocompletion and validation with TypeScript.
|
|
20
|
-
- **Zero config**: No extra build steps or CSS preprocessors required.
|
|
21
|
-
- **Extensible**: Customize behavior with a flexible plugin system.
|
|
22
|
-
|
|
23
|
-
## Installation
|
|
24
|
-
|
|
25
|
-
```bash
|
|
26
|
-
npm install @stylix/core
|
|
27
|
-
# or
|
|
28
|
-
yarn add @stylix/core
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
Peer dependencies: `react`, `react-dom` >= 18.0.0.
|
|
32
|
-
|
|
33
|
-
## Quick Start
|
|
34
|
-
|
|
35
|
-
Let's build a simple styled card component to see Stylix in action:
|
|
3
|
+
**Props-based CSS-in-JS for React**
|
|
36
4
|
|
|
37
5
|
```tsx
|
|
38
|
-
import React from 'react';
|
|
39
|
-
import { StylixProvider } from '@stylix/core';
|
|
40
6
|
import $ from '@stylix/core';
|
|
41
7
|
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
<$.div
|
|
46
|
-
padding={20}
|
|
47
|
-
background-color="white"
|
|
48
|
-
border-radius={8}
|
|
49
|
-
box-shadow="0 2px 4px rgba(0,0,0,0.1)"
|
|
50
|
-
max-width={300}
|
|
51
|
-
margin="auto"
|
|
52
|
-
>
|
|
53
|
-
<$.h2 margin-bottom={12} font-size={18} font-weight="600">
|
|
54
|
-
Welcome to Stylix
|
|
55
|
-
</$.h2>
|
|
56
|
-
<$.p color="gray" line-height={1.5}>
|
|
57
|
-
Styling your React app has never been easier.
|
|
58
|
-
</$.p>
|
|
59
|
-
</$.div>
|
|
60
|
-
</StylixProvider>
|
|
61
|
-
);
|
|
62
|
-
}
|
|
8
|
+
<$.div color="teal" font-size={24} padding={16}>
|
|
9
|
+
Hello, Stylix!
|
|
10
|
+
</$.div>
|
|
63
11
|
```
|
|
64
12
|
|
|
65
|
-
|
|
66
|
-
- We wrap our app in `<StylixProvider>` to enable styling context and automatic style injection.
|
|
67
|
-
- We use the `$` export to render styled HTML tags like `<$.div>`, `<$.h2>`, and `<$.p>`.
|
|
68
|
-
- We pass CSS properties as props, supporting camelCase and kebab-case syntax.
|
|
69
|
-
|
|
70
|
-
### Styling Custom Components
|
|
13
|
+
Stylix lets you write CSS as JSX props. If you know CSS and React, you already know Stylix.
|
|
71
14
|
|
|
72
|
-
|
|
15
|
+
---
|
|
73
16
|
|
|
74
|
-
|
|
75
|
-
import React from 'react';
|
|
76
|
-
import { useStyles } from '@stylix/core';
|
|
77
|
-
|
|
78
|
-
function Button({ children }) {
|
|
79
|
-
const cn = useStyles({
|
|
80
|
-
padding: 12,
|
|
81
|
-
backgroundColor: 'blue',
|
|
82
|
-
color: 'white',
|
|
83
|
-
border: 'none',
|
|
84
|
-
borderRadius: 4,
|
|
85
|
-
cursor: 'pointer',
|
|
86
|
-
':hover': {
|
|
87
|
-
backgroundColor: 'darkblue',
|
|
88
|
-
},
|
|
89
|
-
});
|
|
90
|
-
return <button className={cn}>{children}</button>;
|
|
91
|
-
}
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
### Global Styles and Keyframes
|
|
95
|
-
|
|
96
|
-
Often you need to set up global CSS rules or define animations. Stylix exposes two hooks:
|
|
17
|
+
## Features
|
|
97
18
|
|
|
98
|
-
-
|
|
99
|
-
-
|
|
19
|
+
- **Zero learning curve** — CSS properties become component props
|
|
20
|
+
- **Full CSS support** — pseudo-classes, media queries, keyframes, container queries, cascade layers
|
|
21
|
+
- **Type-safe** — TypeScript autocomplete and validation for all CSS properties
|
|
22
|
+
- **Responsive design** — define breakpoints once, use them everywhere
|
|
23
|
+
- **Lightweight** — no build step required, works with any React setup
|
|
100
24
|
|
|
101
|
-
|
|
102
|
-
import React from 'react';
|
|
103
|
-
import { StylixProvider, useGlobalStyles, useKeyframes } from '@stylix/core';
|
|
104
|
-
import $ from '@stylix/core';
|
|
25
|
+
---
|
|
105
26
|
|
|
106
|
-
|
|
107
|
-
// Define a spin animation
|
|
108
|
-
const spin = useKeyframes({
|
|
109
|
-
from: { transform: 'rotate(0deg)' },
|
|
110
|
-
to: { transform: 'rotate(360deg)' },
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
// Apply global CSS rules
|
|
114
|
-
useGlobalStyles([
|
|
115
|
-
{ html: { boxSizing: 'border-box' } },
|
|
116
|
-
{ '*, *:before, *:after': { boxSizing: 'inherit' } },
|
|
117
|
-
{ body: { margin: 0, fontFamily: 'sans-serif' } },
|
|
118
|
-
{ '.spin': { animation: `${spin} 1s linear infinite` } },
|
|
119
|
-
]);
|
|
120
|
-
|
|
121
|
-
return null;
|
|
122
|
-
}
|
|
27
|
+
## Installation
|
|
123
28
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
<StylixProvider>
|
|
127
|
-
<GlobalStyle />
|
|
128
|
-
<$.div className="spin">🌀 Spinning</$.div>
|
|
129
|
-
</StylixProvider>
|
|
130
|
-
);
|
|
131
|
-
}
|
|
29
|
+
```sh
|
|
30
|
+
npm install @stylix/core
|
|
132
31
|
```
|
|
133
32
|
|
|
33
|
+
---
|
|
134
34
|
|
|
135
|
-
##
|
|
136
|
-
|
|
137
|
-
- **Props-based Styling**: Style any element or component by passing style props.
|
|
138
|
-
- **Hooks**: `useStyles`, `useGlobalStyles`, and `useKeyframes` for dynamic, global, and animated styles.
|
|
139
|
-
- **Server-side Rendering**: Extract CSS on the server with `RenderServerStyles`.
|
|
140
|
-
- **HTML Tags**: `$` export offers all standard HTML tags with built-in style prop support.
|
|
141
|
-
- **Plugin System**: Extend and customize styling behavior with a flexible plugin API.
|
|
142
|
-
- **Utilities**: `cx` for className composition.
|
|
143
|
-
|
|
144
|
-
## API Reference
|
|
35
|
+
## Quick Start
|
|
145
36
|
|
|
146
|
-
###
|
|
37
|
+
### Basic Styling
|
|
147
38
|
|
|
148
|
-
|
|
39
|
+
Use any HTML element via the `$` namespace with CSS properties as props:
|
|
149
40
|
|
|
150
41
|
```tsx
|
|
151
|
-
import
|
|
152
|
-
import { StylixProvider } from '@stylix/core';
|
|
42
|
+
import $ from '@stylix/core';
|
|
153
43
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
44
|
+
<$.div
|
|
45
|
+
display="flex"
|
|
46
|
+
gap={16}
|
|
47
|
+
padding={24}
|
|
48
|
+
background="#f5f5f5"
|
|
49
|
+
border-radius={8}
|
|
160
50
|
>
|
|
161
|
-
|
|
162
|
-
|
|
51
|
+
<$.span color="blue" font-weight="bold">Styled text</$.span>
|
|
52
|
+
</$.div>
|
|
163
53
|
```
|
|
164
54
|
|
|
165
|
-
|
|
166
|
-
- `plugins?: StylixPlugin[]` — Additional style transformation plugins.
|
|
55
|
+
Numeric values automatically get `px` units (except for unitless properties like `flex`, `opacity`, `z-index`).
|
|
167
56
|
|
|
168
|
-
###
|
|
57
|
+
### Complex Styles with `$css`
|
|
169
58
|
|
|
170
|
-
|
|
59
|
+
For pseudo-classes, selectors, and media queries, use the `$css` prop:
|
|
171
60
|
|
|
172
61
|
```tsx
|
|
173
|
-
import $ from '@stylix/core';
|
|
174
|
-
|
|
175
62
|
<$.button
|
|
176
|
-
padding="
|
|
177
|
-
background
|
|
63
|
+
padding="12px 24px"
|
|
64
|
+
background="blue"
|
|
178
65
|
color="white"
|
|
179
66
|
border="none"
|
|
180
|
-
|
|
67
|
+
$css={{
|
|
68
|
+
'&:hover': { background: 'darkblue' },
|
|
69
|
+
'&:active': { transform: 'scale(0.98)' },
|
|
70
|
+
'@media (max-width: 600px)': { padding: '8px 16px' }
|
|
71
|
+
}}
|
|
181
72
|
>
|
|
182
73
|
Click me
|
|
183
74
|
</$.button>
|
|
184
75
|
```
|
|
185
76
|
|
|
186
|
-
|
|
187
|
-
- Any CSS property (camelCase or kebab-case).
|
|
188
|
-
- Pseudo-selectors (`:hover`, `:focus`, etc.).
|
|
189
|
-
- Responsive props using breakpoint names (e.g., `mobile-padding`).
|
|
190
|
-
|
|
191
|
-
### useStyles
|
|
77
|
+
### Responsive Props
|
|
192
78
|
|
|
193
|
-
|
|
79
|
+
Define breakpoints in a provider, then use them in any prop:
|
|
194
80
|
|
|
195
81
|
```tsx
|
|
196
|
-
import {
|
|
197
|
-
|
|
198
|
-
const className = useStyles({
|
|
199
|
-
margin: 8,
|
|
200
|
-
color: 'rebeccapurple',
|
|
201
|
-
':hover': { opacity: 0.8 },
|
|
202
|
-
mobile: { display: 'none' }, // uses media queries defined in StylixProvider
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
return <div className={className}>Hover me (hidden on mobile)</div>;
|
|
206
|
-
```
|
|
207
|
-
|
|
208
|
-
Returns a deterministic class name string based on your style object.
|
|
82
|
+
import $, { StylixProvider } from '@stylix/core';
|
|
209
83
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
84
|
+
// Define once at app root
|
|
85
|
+
<StylixProvider
|
|
86
|
+
media={{
|
|
87
|
+
mobile: styles => ({ '@media (max-width: 768px)': styles }),
|
|
88
|
+
tablet: styles => ({ '@media (max-width: 1024px)': styles }),
|
|
89
|
+
}}
|
|
90
|
+
>
|
|
91
|
+
<App />
|
|
92
|
+
</StylixProvider>
|
|
216
93
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
94
|
+
// Use anywhere
|
|
95
|
+
<$.div
|
|
96
|
+
font-size={{ default: 18, tablet: 16, mobile: 14 }}
|
|
97
|
+
padding={{ default: 32, mobile: 16 }}
|
|
98
|
+
/>
|
|
222
99
|
```
|
|
223
100
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
### useKeyframes
|
|
101
|
+
### Creating Components
|
|
227
102
|
|
|
228
|
-
|
|
103
|
+
Spread style props to create reusable, customizable components:
|
|
229
104
|
|
|
230
105
|
```tsx
|
|
231
|
-
import {
|
|
106
|
+
import $, { StylixProps } from '@stylix/core';
|
|
232
107
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
108
|
+
function Button({ children, ...styles }: StylixProps & { children: React.ReactNode }) {
|
|
109
|
+
return (
|
|
110
|
+
<$.button
|
|
111
|
+
padding="10px 20px"
|
|
112
|
+
border="none"
|
|
113
|
+
border-radius={4}
|
|
114
|
+
cursor="pointer"
|
|
115
|
+
{...styles}
|
|
116
|
+
>
|
|
117
|
+
{children}
|
|
118
|
+
</$.button>
|
|
119
|
+
);
|
|
120
|
+
}
|
|
237
121
|
|
|
238
|
-
|
|
122
|
+
// Parent can override any style
|
|
123
|
+
<Button background="green" color="white">Save</Button>
|
|
239
124
|
```
|
|
240
125
|
|
|
241
|
-
|
|
126
|
+
---
|
|
242
127
|
|
|
243
|
-
|
|
128
|
+
## Documentation
|
|
244
129
|
|
|
245
|
-
|
|
130
|
+
| Document | Description |
|
|
131
|
+
|----------|-------------|
|
|
132
|
+
| [Cheatsheet](./docs/cheatsheet.md) | Quick reference for common patterns |
|
|
133
|
+
| [Philosophy](./docs/philosophy.md) | Why Stylix exists and when to use it |
|
|
134
|
+
| [Patterns](./docs/patterns.md) | Detailed examples and best practices |
|
|
135
|
+
| [Performance](./docs/performance.md) | Optimization guidelines |
|
|
246
136
|
|
|
247
|
-
|
|
137
|
+
---
|
|
248
138
|
|
|
249
|
-
|
|
250
|
-
<StylixProvider
|
|
251
|
-
media={{
|
|
252
|
-
mobile: (styles) => ({ '@media (max-width: 640px)': styles }),
|
|
253
|
-
desktop: (styles) => ({ '@media (min-width: 1024px)': styles }),
|
|
254
|
-
}}
|
|
255
|
-
>
|
|
256
|
-
{/* your app */}
|
|
257
|
-
</StylixProvider>
|
|
258
|
-
```
|
|
139
|
+
## How It Works
|
|
259
140
|
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
Specify style props as objects keyed by breakpoint names:
|
|
141
|
+
Stylix generates unique class names based on your styles and injects CSS at runtime:
|
|
263
142
|
|
|
264
143
|
```tsx
|
|
265
|
-
<$.div
|
|
266
|
-
padding={{ default: 16, mobile: 8 }}
|
|
267
|
-
color={{ default: 'black', mobile: 'gray' }}
|
|
268
|
-
>
|
|
269
|
-
Responsive padding and color!
|
|
270
|
-
</$.div>
|
|
144
|
+
<$.div color="red" padding={16} />
|
|
271
145
|
```
|
|
272
146
|
|
|
273
|
-
|
|
147
|
+
Renders as:
|
|
274
148
|
|
|
275
|
-
|
|
149
|
+
```html
|
|
150
|
+
<div class="stylix-a1b2c3">...</div>
|
|
151
|
+
```
|
|
276
152
|
|
|
277
|
-
|
|
153
|
+
With CSS:
|
|
278
154
|
|
|
279
|
-
```
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
Alternate syntax
|
|
285
|
-
</$.div>
|
|
155
|
+
```css
|
|
156
|
+
.stylix-a1b2c3 {
|
|
157
|
+
color: red;
|
|
158
|
+
padding: 16px;
|
|
159
|
+
}
|
|
286
160
|
```
|
|
287
161
|
|
|
288
|
-
|
|
162
|
+
Identical styles share class names automatically—100 elements with the same styles create only one CSS rule.
|
|
289
163
|
|
|
164
|
+
---
|
|
290
165
|
|
|
291
|
-
|
|
166
|
+
## When to Use Stylix
|
|
292
167
|
|
|
293
|
-
|
|
168
|
+
**Good for:**
|
|
169
|
+
- Small-to-medium applications
|
|
170
|
+
- Rapid prototyping
|
|
171
|
+
- Teams that prefer colocated styles
|
|
172
|
+
- Augmenting existing styling approaches
|
|
294
173
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
color="white"
|
|
300
|
-
$css={{
|
|
301
|
-
'&:hover': { background-color: 'darkblue' },
|
|
302
|
-
'&:active': { transform: 'scale(0.98)' },
|
|
303
|
-
'.icon': { margin-right: 8 },
|
|
304
|
-
}}
|
|
305
|
-
>
|
|
306
|
-
Click me
|
|
307
|
-
</$.button>
|
|
308
|
-
```
|
|
174
|
+
**Consider alternatives for:**
|
|
175
|
+
- Large component libraries intended for external use
|
|
176
|
+
- Applications where bundle size is critical
|
|
177
|
+
- High-frequency animations (use CSS transitions or the native `style` prop)
|
|
309
178
|
|
|
310
|
-
|
|
179
|
+
See [Philosophy](./docs/philosophy.md) for more context on these tradeoffs.
|
|
311
180
|
|
|
312
|
-
|
|
181
|
+
---
|
|
313
182
|
|
|
314
|
-
|
|
183
|
+
## Browser Support
|
|
315
184
|
|
|
316
|
-
|
|
317
|
-
import React from 'react';
|
|
318
|
-
import { StylixProvider, customProps } from '@stylix/core';
|
|
319
|
-
import $ from '@stylix/core';
|
|
185
|
+
Stylix uses native CSS nesting for complex styles. Check [Can I use CSS Nesting](https://caniuse.com/css-nesting) for current browser support (90%+ as of 2024).
|
|
320
186
|
|
|
321
|
-
|
|
322
|
-
const spacingPlugin = customProps({
|
|
323
|
-
px: (value) => ({ paddingLeft: value, paddingRight: value }),
|
|
324
|
-
});
|
|
187
|
+
For basic prop-based styles without nesting, all modern browsers are supported.
|
|
325
188
|
|
|
326
|
-
|
|
327
|
-
function App() {
|
|
328
|
-
return (
|
|
329
|
-
<StylixProvider plugins={[spacingPlugin]}>
|
|
330
|
-
<$.div px={20}>Padded horizontally</$.div>
|
|
331
|
-
</StylixProvider>
|
|
332
|
-
);
|
|
333
|
-
}
|
|
334
|
-
```
|
|
189
|
+
---
|
|
335
190
|
|
|
336
|
-
|
|
337
|
-
- `defaultPlugins`: Built-in plugins for media queries, custom props, and more.
|
|
338
|
-
- `customProps`: Factory to create custom style prop mappings.
|
|
339
|
-
- Types: `StylixPlugin`, `StylixPluginFunctionContext`.
|
|
191
|
+
## API Overview
|
|
340
192
|
|
|
341
|
-
###
|
|
193
|
+
### Exports
|
|
342
194
|
|
|
343
|
-
|
|
344
|
-
|
|
195
|
+
```tsx
|
|
196
|
+
import $, {
|
|
197
|
+
StylixProvider, // Context provider for configuration
|
|
198
|
+
StylixProps, // TypeScript type for style props
|
|
199
|
+
useKeyframes, // Create keyframe animations
|
|
200
|
+
useGlobalStyles, // Define global styles
|
|
201
|
+
cx, // Class name utility
|
|
202
|
+
} from '@stylix/core';
|
|
203
|
+
```
|
|
345
204
|
|
|
346
|
-
|
|
347
|
-
import { cx } from '@stylix/core';
|
|
205
|
+
### StylixProvider Props
|
|
348
206
|
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
207
|
+
```tsx
|
|
208
|
+
<StylixProvider
|
|
209
|
+
media={{ /* breakpoint definitions */ }}
|
|
210
|
+
plugins={[ /* custom plugins */ ]}
|
|
211
|
+
ssr={true} // Enable server-side rendering mode
|
|
212
|
+
>
|
|
352
213
|
```
|
|
353
214
|
|
|
354
|
-
|
|
215
|
+
---
|
|
355
216
|
|
|
356
|
-
|
|
357
|
-
- `StylixHTMLProps` — Props for stylable HTML elements.
|
|
358
|
-
- `StylixStyles` — Style object for hooks.
|
|
359
|
-
- `StylixPlugin` — Plugin function type.
|
|
217
|
+
## License
|
|
360
218
|
|
|
361
|
-
|
|
219
|
+
MIT
|
|
362
220
|
|
|
363
|
-
|
|
221
|
+
---
|
|
364
222
|
|
|
365
|
-
##
|
|
223
|
+
## Links
|
|
366
224
|
|
|
367
|
-
|
|
225
|
+
- [GitHub](https://github.com/brombal/stylix)
|
|
226
|
+
- [npm](https://www.npmjs.com/package/@stylix/core)
|
|
227
|
+
- [Report an Issue](https://github.com/brombal/stylix/issues)
|