|
| 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 |
0 commit comments