Skip to content

Commit 7a69390

Browse files
committed
Document runtime CSS compilation
1 parent fa775ee commit 7a69390

8 files changed

Lines changed: 224 additions & 79 deletions

File tree

docs/api/babel.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
CSSX uses a Babel preset to transform styles at build time.
44

5+
For CSS strings that are generated in the client at runtime, use the
6+
[Runtime Compilation API](/api/runtime) instead.
7+
58
## cssxjs/babel
69

710
The Babel preset that transforms CSSX syntax.

docs/api/css.md

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -203,28 +203,9 @@ for React Native. Other image values are ignored with a diagnostic.
203203

204204
### Runtime CSS Strings
205205

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.
206+
For CSS text that is generated at runtime, use the
207+
[Runtime Compilation API](/api/runtime). Runtime strings must be plain CSS text
208+
and use `var()` for dynamic values.
228209

229210
## Limitations
230211

@@ -249,9 +230,10 @@ For these features, use the [styl template](/api/styl) instead.
249230
| CSS variables | Yes | Yes |
250231
| Function-scoped JS interpolation | Yes | Yes |
251232
| Part selectors | Yes | Yes |
252-
| Runtime CSS strings | No | `useCompiledCss()` |
233+
| Runtime CSS strings | No | [Runtime API](/api/runtime) |
253234

254235
## See Also
255236

256237
- [styl Template](/api/styl) — Stylus syntax with variables and mixins
257238
- [styleName Prop](/api/jsx-props) — Connect elements to styles
239+
- [Runtime Compilation](/api/runtime) — Compile generated CSS strings

docs/api/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ import {
3232
- [styl() Function](/api/styl-function) — Apply styles via spread
3333
- [JSX Props](/api/jsx-props)`styleName`, `part`
3434
- [CSS Variables](/api/variables) — Runtime theming
35-
- [Caching](/guide/caching) — Built-in cache and runtime CSS helpers
35+
- [Runtime Compilation](/api/runtime) — Compile generated CSS strings at runtime
36+
- [Caching](/guide/caching) — Built-in resolver cache behavior
3637

3738
**Configuration:**
3839
- [Babel Config](/api/babel) — Preset options

docs/api/jsx-props.md

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,8 @@ function cssx(
148148
): object
149149
```
150150

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-
```
164-
165151
`cssx()` returns an object with `style` and any part style props such as
166152
`titleStyle`, `hoverStyle`, or `activeStyle`.
153+
154+
See [Runtime Compilation](/api/runtime) for generated CSS strings, diagnostics,
155+
tracking, and caching behavior.

docs/api/runtime.md

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
# Runtime Compilation
2+
3+
Runtime compilation is for CSS text that is not known during Babel compilation,
4+
for example CSS generated by an AI system, loaded from a CMS, or edited inside a
5+
client-side builder.
6+
7+
Most app code should still use `styleName` with `css` or `styl` templates. Use
8+
the runtime API when the CSS source is a string at render time.
9+
10+
## Basic Usage
11+
12+
```jsx
13+
import { cssx, useCompiledCss } from 'cssxjs'
14+
15+
function Button({ generatedCss, disabled, label }) {
16+
const sheet = useCompiledCss(generatedCss)
17+
18+
return (
19+
<Div {...cssx(['root', { disabled }], sheet, {
20+
style: { backgroundColor: 'red' }
21+
})}>
22+
<Span {...cssx('label', sheet)}>{label}</Span>
23+
</Div>
24+
)
25+
}
26+
```
27+
28+
`useCompiledCss()` compiles the string into a tracked sheet. `cssx()` resolves a
29+
`styleName` against that sheet and returns props such as `style`, `labelStyle`,
30+
`hoverStyle`, and `activeStyle`.
31+
32+
## CSS Input
33+
34+
Runtime input must be plain CSS text:
35+
36+
```css
37+
.root {
38+
padding: 12px 16px;
39+
background: var(--button-bg, #1677ff);
40+
}
41+
42+
.root.disabled {
43+
opacity: 0.5;
44+
}
45+
46+
.label {
47+
color: var(--label-color, white);
48+
}
49+
```
50+
51+
Runtime strings do not support Stylus syntax or JavaScript template
52+
interpolation. Use `var()` for dynamic values in generated CSS.
53+
54+
## API
55+
56+
```ts
57+
useCompiledCss(cssText, options?)
58+
cssx(styleName, sheet, inlineStyleProps?, options?)
59+
```
60+
61+
`styleName` accepts the same shapes as the JSX prop:
62+
63+
```jsx
64+
cssx('card', sheet)
65+
cssx(['card', variant, { selected, disabled }], sheet)
66+
```
67+
68+
`sheet` can be:
69+
70+
- the `TrackedCssxSheet` returned by `useCompiledCss()`
71+
- an already compiled sheet passed through `useCssxSheet()`
72+
- an array of sheets, ordered from lowest to highest priority
73+
74+
`inlineStyleProps` uses the same prop names that components receive:
75+
76+
```jsx
77+
<Card {...cssx('card', sheet, {
78+
style: { marginTop: 16 },
79+
titleStyle: { fontWeight: '700' }
80+
})} />
81+
```
82+
83+
Inline styles have the highest priority.
84+
85+
## Diagnostics
86+
87+
Runtime compilation is graceful by default. Invalid CSS does not throw during
88+
render. The returned sheet contains diagnostics and any rules that could still
89+
be compiled.
90+
91+
```jsx
92+
const sheet = useCompiledCss(generatedCss)
93+
94+
if (sheet.getSheet().diagnostics.length > 0) {
95+
reportCssErrors(sheet.getSheet().diagnostics)
96+
}
97+
```
98+
99+
Diagnostics include a severity, code, message, and line/column when available.
100+
This makes runtime compilation suitable for AI-generated CSS because the app can
101+
show or feed back errors without crashing.
102+
103+
Build-time template compilation is stricter where Babel needs the module to be
104+
compiled correctly.
105+
106+
## Variables And Updates
107+
108+
Runtime CSS supports `var()` in the same places as build-time CSSX styles:
109+
whole values, parts of shorthands, comma-separated chunks, nested fallbacks, and
110+
complex values such as shadows and gradients.
111+
112+
```css
113+
.card {
114+
border: var(--border-width, 1px) solid var(--border-color, #ddd);
115+
box-shadow: var(--shadow, 0 4px 12px rgba(0, 0, 0, 0.16));
116+
}
117+
```
118+
119+
Only variables used by the resolved element are tracked. If `--border-color`
120+
changes, elements that used it update. If an unrelated variable changes, they do
121+
not.
122+
123+
## Media Queries
124+
125+
Runtime CSS can use media queries:
126+
127+
```css
128+
.layout {
129+
padding: 24px;
130+
}
131+
132+
@media (max-width: 640px) {
133+
.layout {
134+
padding: 12px;
135+
}
136+
}
137+
```
138+
139+
CSSX subscribes only to media queries used by committed renders. Dimension and
140+
media updates invalidate only affected elements.
141+
142+
## Caching
143+
144+
`useCompiledCss()` recompiles only when the CSS string or target changes.
145+
`cssx()` caches the resolved props for the current inputs:
146+
147+
- sheet identity and content hash
148+
- normalized `styleName`
149+
- runtime variable and media dependencies actually used
150+
- interpolation values for compiled templates
151+
- `JSON.stringify()` hash of inline style props
152+
153+
When those inputs are unchanged, CSSX returns the same object references. When
154+
inputs change, it recalculates and replaces the previous cached entry instead of
155+
keeping unbounded variants.
156+
157+
## Other Runtime Hooks
158+
159+
Use these helpers for lower-level integrations:
160+
161+
```ts
162+
useCssxSheet(compiledSheet, options?)
163+
useCssxTemplate(compiledSheet, values, options?)
164+
useCssxLayer(input, options?)
165+
CssxProvider
166+
configureCssx(options)
167+
```
168+
169+
`useCssxSheet()` tracks an already compiled sheet. `useCssxTemplate()` is used by
170+
compiled local templates with JavaScript interpolation values. `useCssxLayer()`
171+
accepts strings, compiled sheets, tracked sheets, or layer objects and returns
172+
the tracked equivalent.
173+
174+
`CssxProvider` and `configureCssx()` configure runtime defaults such as target
175+
and dimension debounce behavior.
176+
177+
## Platform Resolution
178+
179+
Import from `cssxjs` in application code:
180+
181+
```js
182+
import { cssx, useCompiledCss } from 'cssxjs'
183+
```
184+
185+
CSSX resolves the correct web or React Native runtime through package export
186+
conditions. Expo and React Native use the React Native target; other bundlers
187+
use the web target by default.
188+
189+
## When Not To Use It
190+
191+
Use build-time `css` or `styl` templates when the CSS is authored in source
192+
files. Babel can then precompile the sheet, lower JavaScript interpolation, and
193+
connect `styleName` automatically.
194+
195+
Runtime compilation is best reserved for CSS that truly arrives as data.
196+
197+
## See Also
198+
199+
- [Babel Config](/api/babel) - Build-time compilation
200+
- [css Template](/api/css) - Plain CSS templates
201+
- [JSX Props](/api/jsx-props) - `styleName`, `part`, and `cssx()`
202+
- [Caching](/guide/caching) - Resolver cache behavior
203+
- [CSS Variables](/api/variables) - Runtime theming

docs/api/styl.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,11 @@ When the same property is defined in multiple places (highest to lowest):
274274

275275
- JavaScript interpolation is local-only: module-level `styl` templates must be plain template literals
276276
- Interpolation is value-only, not selector or property-name interpolation
277-
- For runtime-generated plain CSS strings, use `useCompiledCss()` with the `css` runtime API
277+
- For runtime-generated plain CSS strings, use the [Runtime Compilation API](/api/runtime)
278278

279279
## See Also
280280

281281
- [css Template](/api/css) — Plain CSS alternative
282282
- [styl() Function](/api/styl-function) — Apply styles via spread
283283
- [styleName Prop](/api/jsx-props) — Connect elements to styles
284+
- [Runtime Compilation](/api/runtime) — Compile generated CSS strings

docs/guide/caching.md

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -90,28 +90,9 @@ configureCssx({
9090

9191
## Runtime CSS Strings
9292

93-
For client-generated CSS, compile the string with `useCompiledCss()` and pass the
94-
tracked sheet to `cssx()` inline:
95-
96-
```jsx
97-
import { cssx, useCompiledCss } from 'cssxjs'
98-
99-
function Button({ generatedCss, disabled, label }) {
100-
const sheet = useCompiledCss(generatedCss)
101-
102-
return (
103-
<Div {...cssx(['root', { disabled }], sheet, {
104-
style: { backgroundColor: 'red' }
105-
})}>
106-
<Span {...cssx('label', sheet)}>{label}</Span>
107-
</Div>
108-
)
109-
}
110-
```
111-
112-
Runtime compilation is graceful by default. Invalid generated CSS produces an
113-
empty or partially compiled sheet with diagnostics attached to the sheet instead
114-
of throwing during render.
93+
For client-generated CSS, use `useCompiledCss()` and `cssx()`. Runtime
94+
compilation has its own API reference covering diagnostics, subscriptions, and
95+
platform behavior: [Runtime Compilation](/api/runtime).
11596

11697
## Inline Style Hashing
11798

@@ -148,25 +129,9 @@ Each compiled template has one cache slot for its latest interpolation values.
148129
If `color` changes, CSSX recalculates the sheet result and replaces the previous
149130
cached variant instead of keeping every historical value combination.
150131

151-
## Manual Runtime API
152-
153-
The public helpers exported from `cssxjs` are:
154-
155-
```ts
156-
useCompiledCss(cssText, options?)
157-
useCssxSheet(compiledSheet, options?)
158-
useCssxTemplate(compiledSheet, values, options?)
159-
cssx(styleName, sheet, inlineStyleProps?, options?)
160-
CssxProvider
161-
configureCssx(options)
162-
```
163-
164-
Most applications only need `styleName`. Use these helpers when CSS arrives as a
165-
runtime string or when building lower-level components that do not use Babel's
166-
`styleName` transform.
167-
168132
## Next Steps
169133

170134
- [CSS Variables](/guide/variables) - Runtime theming
171-
- [css Template](/api/css) - Runtime CSS and interpolation
135+
- [Runtime Compilation](/api/runtime) - Generated CSS strings
136+
- [css Template](/api/css) - Plain CSS templates and interpolation
172137
- [Animations](/guide/animations) - Reanimated v4 output

rspress.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export default defineConfig({
8787
{ text: 'styl() Function', link: '/api/styl-function' },
8888
{ text: 'CSS Variables', link: '/api/variables' },
8989
{ text: 'JSX Props', link: '/api/jsx-props' },
90+
{ text: 'Runtime Compilation', link: '/api/runtime' },
9091
{ text: 'Babel Config', link: '/api/babel' }
9192
]
9293
},

0 commit comments

Comments
 (0)