@stylix/core 6.2.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 CHANGED
@@ -1,367 +1,227 @@
1
1
  # Stylix
2
2
 
3
- > React, with style. Add styles to your React apps with props: the easy, natural, and low-learning-curve approach.
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
- function App() {
43
- return (
44
- <StylixProvider>
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
- In this example:
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
- When styling custom React components or third-party elements, use the `useStyles` hook to generate a `className`:
15
+ ---
73
16
 
74
- ```tsx
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
- - **`useGlobalStyles`**: Injects global CSS rules into the document.
99
- - **`useKeyframes`**: Generates unique keyframe animation names.
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
- ```tsx
102
- import React from 'react';
103
- import { StylixProvider, useGlobalStyles, useKeyframes } from '@stylix/core';
104
- import $ from '@stylix/core';
25
+ ---
105
26
 
106
- function GlobalStyle() {
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
- function App() {
125
- return (
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
- ## Features
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
- ### StylixProvider
37
+ ### Basic Styling
147
38
 
148
- Provides styling context, media query definitions, and plugin support to your React tree.
39
+ Use any HTML element via the `$` namespace with CSS properties as props:
149
40
 
150
41
  ```tsx
151
- import React from 'react';
152
- import { StylixProvider } from '@stylix/core';
42
+ import $ from '@stylix/core';
153
43
 
154
- <StylixProvider
155
- media={{
156
- mobile: (styles) => ({ '@media (max-width: 640px)': styles }),
157
- desktop: (styles) => ({ '@media (min-width: 1024px)': styles }),
158
- }}
159
- plugins={[/* array of StylixPlugin */]}
44
+ <$.div
45
+ display="flex"
46
+ gap={16}
47
+ padding={24}
48
+ background="#f5f5f5"
49
+ border-radius={8}
160
50
  >
161
- <App />
162
- </StylixProvider>
51
+ <$.span color="blue" font-weight="bold">Styled text</$.span>
52
+ </$.div>
163
53
  ```
164
54
 
165
- - `media?: Record<string, (styles: StylixStyles) => StylixStyles>` Named breakpoints mapping to functions that wrap styles in media queries.
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
- ### `$` (Styled HTML Tags)
57
+ ### Complex Styles with `$css`
169
58
 
170
- Stylix exports a default `$` object with all standard HTML tags, pre-wired for style props:
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="8px 16px"
177
- background-color="teal"
63
+ padding="12px 24px"
64
+ background="blue"
178
65
  color="white"
179
66
  border="none"
180
- border-radius={4}
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
- Supports:
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
- Generate a unique class name from a style object:
79
+ Define breakpoints in a provider, then use them in any prop:
194
80
 
195
81
  ```tsx
196
- import { useStyles } from '@stylix/core';
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
- ### useGlobalStyles
211
-
212
- Inject global CSS rules into your document:
213
-
214
- ```tsx
215
- import { useGlobalStyles } from '@stylix/core';
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
- useGlobalStyles({
218
- html: { boxSizing: 'border-box' },
219
- '*, *:before, *:after': { boxSizing: 'inherit' },
220
- body: { margin: 0, fontFamily: 'sans-serif' },
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
- Accepts a single style object or an array of objects for grouping rules.
225
-
226
- ### useKeyframes
101
+ ### Creating Components
227
102
 
228
- Create reusable CSS keyframe animations:
103
+ Spread style props to create reusable, customizable components:
229
104
 
230
105
  ```tsx
231
- import { useKeyframes } from '@stylix/core';
106
+ import $, { StylixProps } from '@stylix/core';
232
107
 
233
- const fadeIn = useKeyframes({
234
- '0%': { opacity: 0 },
235
- '100%': { opacity: 1 },
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
- return <$.div animation={`${fadeIn} 2s ease-in`}>Fade In</$.div>;
122
+ // Parent can override any style
123
+ <Button background="green" color="white">Save</Button>
239
124
  ```
240
125
 
241
- Returns a unique animation name for use in `animation` props.
126
+ ---
242
127
 
243
- ### Responsive Styles (Media Queries)
128
+ ## Documentation
244
129
 
245
- Stylix lets you define custom breakpoints in `StylixProvider` and write responsive style props directly in your components.
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
- #### Define breakpoints
137
+ ---
248
138
 
249
- ```tsx
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
- #### Use media objects in style props
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
- The `default` key provides fallback styles; other keys correspond to your breakpoints.
147
+ Renders as:
274
148
 
275
- #### Top-level media prop syntax
149
+ ```html
150
+ <div class="stylix-a1b2c3">...</div>
151
+ ```
276
152
 
277
- You can also use breakpoint names as props to apply multiple styles at once:
153
+ With CSS:
278
154
 
279
- ```tsx
280
- <$.div
281
- mobile={{ padding: 8, backgroundColor: 'lightgray' }}
282
- desktop={{ padding: 24 }}
283
- >
284
- Alternate syntax
285
- </$.div>
155
+ ```css
156
+ .stylix-a1b2c3 {
157
+ color: red;
158
+ padding: 16px;
159
+ }
286
160
  ```
287
161
 
288
- Under the hood, Stylix's `mediaObjects` plugin merges these into proper CSS media queries in the generated stylesheet.
162
+ Identical styles share class names automatically—100 elements with the same styles create only one CSS rule.
289
163
 
164
+ ---
290
165
 
291
- ### $css (Advanced Selectors & Pseudo-classes)
166
+ ## When to Use Stylix
292
167
 
293
- Use the `$css` prop to pass additional style objects or arrays, enabling nested selectors, pseudo-classes, and complex rules. Always use `&` in selector keys to refer to the current component’s generated class name. Without the `&`, the selector will apply to child elements instead.
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
- ```tsx
296
- // $css accepts a StylixStyles object or array of objects
297
- <$.button
298
- background-color="blue"
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
- Note: using a selector like `':hover'` without `&` will only apply to child elements. Prefix with `&` to ensure it targets the component itself.
179
+ See [Philosophy](./docs/philosophy.md) for more context on these tradeoffs.
311
180
 
312
- ### Plugins
181
+ ---
313
182
 
314
- Customize and extend Stylix with your own styling transforms:
183
+ ## Browser Support
315
184
 
316
- ```tsx
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
- // Define a plugin for horizontal padding shorthand
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
- // Use the plugin in your provider
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
- Available:
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
- ### Utilities
193
+ ### Exports
342
194
 
343
- #### cx
344
- Compose class names safely:
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
- ```ts
347
- import { cx } from '@stylix/core';
205
+ ### StylixProvider Props
348
206
 
349
- const isActive = true;
350
- const cls = cx('btn', isActive && 'active', undefined, null);
351
- // -> 'btn active'
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
- ### Types
215
+ ---
355
216
 
356
- - `StylixProps` — Style prop interface.
357
- - `StylixHTMLProps` — Props for stylable HTML elements.
358
- - `StylixStyles` — Style object for hooks.
359
- - `StylixPlugin` — Plugin function type.
217
+ ## License
360
218
 
361
- ## Contributing
219
+ MIT
362
220
 
363
- We welcome contributions! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for our contribution guidelines, code of conduct, and development setup instructions. Feel free to open issues or submit pull requests on our GitHub repository.
221
+ ---
364
222
 
365
- ## License
223
+ ## Links
366
224
 
367
- MIT © [Alex Brombal](https://github.com/brombal)
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)