Skip to content

Commit b241636

Browse files
committed
Update docs for unified CSS runtime
1 parent 9f74fc0 commit b241636

12 files changed

Lines changed: 352 additions & 254 deletions

File tree

docs/api/babel.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports = {
2121
|--------|------|---------|-------------|
2222
| `platform` | `'web'` \| `'ios'` \| `'android'` | `'web'` | Target platform |
2323
| `reactType` | `'react-native'` \| `'web'` | auto | React target type |
24-
| `cache` | `'teamplay'` | auto | Caching library |
24+
| `cache` | `'teamplay'` | auto | Legacy compatibility alias |
2525
| `transformPug` | `boolean` | `true` | Enable Pug transformation |
2626
| `transformCss` | `boolean` | `true` | Enable CSS transformation |
2727

@@ -33,7 +33,7 @@ module.exports = {
3333
presets: [
3434
['cssxjs/babel', {
3535
transformPug: false, // Disable pug if not using it
36-
cache: 'teamplay' // Force teamplay caching
36+
cache: 'teamplay' // Legacy compatibility alias
3737
}]
3838
]
3939
}
@@ -61,7 +61,9 @@ You can also set platform-specific variables in your Stylus code:
6161

6262
## Caching
6363

64-
When `cache: 'teamplay'` is set (or auto-detected), the Babel transform generates code that integrates with [teamplay](https://github.com/startupjs/teamplay) for optimized style memoization.
64+
CSSX uses the built-in resolver cache by default. The old `cache: 'teamplay'`
65+
option is still accepted so existing configs do not break, but CSSX no longer
66+
imports Teamplay and components do not need `observer()`.
6567

6668
See the [Caching guide](/guide/caching) for more details.
6769

@@ -103,6 +105,7 @@ The Babel preset converts this into optimized runtime code that:
103105
- Compiles Stylus to style objects at build time
104106
- Connects `styleName` to the compiled styles
105107
- Injects part style props automatically
108+
- Re-renders only when used CSS variables or matching media queries change
106109

107110
## TypeScript
108111

docs/api/css.md

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,39 @@ The custom `u` unit works in `css` too:
122122
}
123123
```
124124

125+
Variables can appear anywhere CSS allows `var()`: whole values, parts of
126+
shorthands, comma-separated value chunks, and nested fallbacks.
127+
128+
```css
129+
.card {
130+
box-shadow: var(--shadow, 0 4px 12px rgba(0, 0, 0, 0.16));
131+
border: var(--border-width, 1px) solid var(--border-color, #ddd);
132+
}
133+
```
134+
135+
### JavaScript Interpolation
136+
137+
Function-scoped `css` templates support JavaScript interpolation in CSS value
138+
positions:
139+
140+
```jsx
141+
function Badge({ color, size }) {
142+
return <View styleName="badge" />
143+
144+
css`
145+
.badge {
146+
background-color: ${color};
147+
padding: ${size}px 12px;
148+
}
149+
`
150+
}
151+
```
152+
153+
Interpolation is an alternative to `var()`. It is only supported in the same
154+
places a CSS value can use `var()`, and only inside function-scoped JS tagged
155+
templates. Module-level templates, imported CSS files, and runtime CSS strings
156+
must use plain CSS text.
157+
125158
### Part Selectors
126159

127160
```css
@@ -134,13 +167,73 @@ The custom `u` unit works in `css` too:
134167
}
135168
```
136169

170+
### Hover and Active Styles
171+
172+
CSSX maps `:hover` and `:active` to the same output as `:part(hover)` and
173+
`:part(active)`. Components can receive those props as `hoverStyle` and
174+
`activeStyle`.
175+
176+
```css
177+
.button:hover {
178+
background-color: #0056b3;
179+
}
180+
181+
.button:active {
182+
transform: scale(0.97);
183+
}
184+
```
185+
186+
### Filters and Background Images
187+
188+
React Native supports `filter` and experimental background gradients in current
189+
versions. CSSX passes `filter` through and maps `background-image` to
190+
`experimental_backgroundImage` on React Native.
191+
192+
```css
193+
.hero {
194+
filter: blur(8px) brightness(0.8);
195+
background-image:
196+
linear-gradient(0deg, white, rgba(238, 64, 53, 0.8), rgba(238, 64, 53, 0) 70%),
197+
radial-gradient(circle, rgba(0, 0, 0, 0.2), transparent 70%);
198+
}
199+
```
200+
201+
Only `linear-gradient()` and `radial-gradient()` background images are emitted
202+
for React Native. Other image values are ignored with a diagnostic.
203+
204+
### Runtime CSS Strings
205+
206+
Use `useCompiledCss()` and `cssx()` for CSS generated at runtime, such as CSS
207+
returned by an AI system.
208+
209+
```jsx
210+
import { cssx, useCompiledCss } from 'cssxjs'
211+
212+
function Button({ generatedCss, disabled, label }) {
213+
const sheet = useCompiledCss(generatedCss)
214+
215+
return (
216+
<Div {...cssx(['root', { disabled }], sheet, {
217+
style: { backgroundColor: 'red' }
218+
})}>
219+
<Span {...cssx('label', sheet)}>{label}</Span>
220+
</Div>
221+
)
222+
}
223+
```
224+
225+
Runtime compilation uses graceful diagnostics by default. Invalid CSS does not
226+
throw during render; the returned sheet contains diagnostics and any rules that
227+
could still be compiled.
228+
137229
## Limitations
138230

139231
The `css` template does **not** support:
140232

141233
- Stylus variables (`$var`)
142234
- Stylus mixins
143235
- Global `styles/index.styl` imports
236+
- JavaScript interpolation in module-level templates or runtime CSS strings
144237

145238
For these features, use the [styl template](/api/styl) instead.
146239

@@ -154,7 +247,9 @@ For these features, use the [styl template](/api/styl) instead.
154247
| Global imports | `styles/index.styl` | Not supported |
155248
| `u` unit | Yes | Yes |
156249
| CSS variables | Yes | Yes |
250+
| Function-scoped JS interpolation | Yes | Yes |
157251
| Part selectors | Yes | Yes |
252+
| Runtime CSS strings | No | `useCompiledCss()` |
158253

159254
## See Also
160255

docs/api/index.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@ import {
1212
variables,
1313
setDefaultVariables,
1414
defaultVariables,
15-
dimensions,
16-
matcher
15+
cssx,
16+
useCompiledCss,
17+
useCssxSheet,
18+
useCssxTemplate,
19+
CssxProvider,
20+
configureCssx
1721
} from 'cssxjs'
1822
```
1923

@@ -28,6 +32,7 @@ import {
2832
- [styl() Function](/api/styl-function) — Apply styles via spread
2933
- [JSX Props](/api/jsx-props)`styleName`, `part`
3034
- [CSS Variables](/api/variables) — Runtime theming
35+
- [Caching](/guide/caching) — Built-in cache and runtime CSS helpers
3136

3237
**Configuration:**
3338
- [Babel Config](/api/babel) — Preset options
@@ -43,5 +48,9 @@ import {
4348
| `variables` | Observable object | Set CSS variable values at runtime |
4449
| `setDefaultVariables` | Function | Set default CSS variable values |
4550
| `defaultVariables` | Object | Read-only default variable values |
46-
| `dimensions` | Observable object | Current screen width for media queries |
47-
| `matcher` | Function | Internal style matching (advanced) |
51+
| `cssx` | Function | Resolve a runtime sheet and `styleName` to props |
52+
| `useCompiledCss` | Hook | Compile runtime CSS text into a tracked sheet |
53+
| `useCssxSheet` | Hook | Track an already compiled sheet |
54+
| `useCssxTemplate` | Hook | Track a compiled sheet with interpolation values |
55+
| `CssxProvider` | Component | Provide runtime options to a subtree |
56+
| `configureCssx` | Function | Configure global runtime defaults |

docs/api/jsx-props.md

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,30 @@ The pattern:
6666

6767
### Dynamic Styles
6868

69-
For truly dynamic values, combine `styleName` with the `style` prop:
69+
For CSS values that come from props, prefer function-scoped template
70+
interpolation:
7071

7172
```jsx
7273
import { View, Text } from 'react-native'
7374

74-
function ProgressBar({ progress }) {
75+
function ProgressBar({ progress, color }) {
7576
return (
76-
<View styleName="bar" style={{ width: `${progress}%` }}>
77+
<View styleName="bar">
7778
<Text>{progress}%</Text>
7879
</View>
7980
)
8081

8182
styl`
8283
.bar
84+
width ${progress}%
8385
height 20px
84-
background-color #4caf50
86+
background-color ${color}
8587
`
8688
}
8789
```
8890

91+
For ad hoc overrides, combine `styleName` with the regular `style` prop.
92+
8993
---
9094

9195
## part
@@ -128,26 +132,35 @@ See the [Component Parts guide](/guide/component-parts) for detailed examples.
128132

129133
---
130134

131-
## matcher
135+
## cssx()
132136

133-
The internal function that matches `styleName` values against compiled styles. Advanced use only.
137+
The low-level runtime helper that resolves a compiled or runtime sheet and
138+
returns props to spread onto a component. Most components should use
139+
`styleName`; use `cssx()` when CSS arrives as a runtime string or when a custom
140+
component cannot use the Babel transform.
134141

135142
**Signature:**
136143
```ts
137-
function matcher(
138-
styleName: string,
139-
fileStyles: object,
140-
globalStyles: object,
141-
localStyles: object,
142-
inlineStyleProps: object
144+
function cssx(
145+
styleName: string | array | object,
146+
sheet: string | CompiledCssSheet | TrackedCssxSheet,
147+
inlineStyleProps?: object
143148
): object
144149
```
145150

146-
**Parameters:**
147-
- `styleName` - Space-separated class names (supports classnames-like syntax)
148-
- `fileStyles` - Styles from the imported CSS file
149-
- `globalStyles` - Module-level `styl` styles
150-
- `localStyles` - Function-level `styl` styles
151-
- `inlineStyleProps` - Inline style overrides
151+
```jsx
152+
import { cssx, useCompiledCss } from 'cssxjs'
153+
154+
function GeneratedCard({ cssText, selected }) {
155+
const sheet = useCompiledCss(cssText)
156+
157+
return (
158+
<Card {...cssx(['card', { selected }], sheet, {
159+
style: { marginTop: 16 }
160+
})} />
161+
)
162+
}
163+
```
152164

153-
**Returns:** An object with style props, including `style` and any `{part}Style` props.
165+
`cssx()` returns an object with `style` and any part style props such as
166+
`titleStyle`, `hoverStyle`, or `activeStyle`.

docs/api/styl.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,28 @@ CSSX adds a custom `u` unit where `1u = 8px` (Material Design grid):
208208

209209
See [CSS Variables](/api/variables) for runtime variable updates.
210210

211+
### JavaScript Interpolation
212+
213+
Function-scoped `styl` templates support JavaScript interpolation in CSS value
214+
positions:
215+
216+
```jsx
217+
function Button({ color, spacing }) {
218+
return <View styleName="button" />
219+
220+
styl`
221+
.button
222+
background ${color}
223+
padding ${spacing}px 12px
224+
`
225+
}
226+
```
227+
228+
Interpolation is lowered through the same runtime value path as `var()`, so it
229+
can be used for whole values, parts of shorthands, and values nested inside
230+
functions. It is not supported in module-level templates because there is no
231+
render-time value array there.
232+
211233
## Selectors
212234

213235
| Selector | Description |
@@ -216,10 +238,12 @@ See [CSS Variables](/api/variables) for runtime variable updates.
216238
| `.class1.class2` | Multiple classes (same element) |
217239
| `&.modifier` | Modifier class (used within parent) |
218240
| `:part(name)` | Part selector |
241+
| `:hover` | Emits `hoverStyle`, same as `:part(hover)` |
242+
| `:active` | Emits `activeStyle`, same as `:part(active)` |
219243

220244
> **Note:** Descendant selectors (`.parent .child`) are not supported. Apply modifiers directly to each element that needs styling.
221245
222-
> **Note:** Pseudo-classes (`:hover`, `:focus`, `:active`, etc.) and pseudo-elements (`::before`, `::after`) are not supported. Use state-based modifiers instead (e.g., `&.focused`, `&.active`).
246+
> **Note:** `:focus`, other pseudo-classes, and pseudo-elements (`::before`, `::after`) are not supported. Use state-based modifiers for those cases.
223247
224248
### Part Selector
225249

@@ -248,9 +272,9 @@ When the same property is defined in multiple places (highest to lowest):
248272

249273
## Limitations
250274

251-
- No expression interpolations: `` styl`color ${color}` `` is not allowed
252-
- Must be a plain template literal
253-
- For dynamic values, use CSS variables or the `style` prop
275+
- JavaScript interpolation is local-only: module-level `styl` templates must be plain template literals
276+
- Interpolation is value-only, not selector or property-name interpolation
277+
- For runtime-generated plain CSS strings, use `useCompiledCss()` with the `css` runtime API
254278

255279
## See Also
256280

0 commit comments

Comments
 (0)