@tenphi/tasty 0.0.0-snapshot.da430e1 → 0.0.0-snapshot.db2ef70
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 +408 -385
- package/dist/babel-types-Dc5zghyE.d.ts +18 -0
- package/dist/chunks/async-storage-f9j9JRtq.js +37 -0
- package/dist/chunks/async-storage-f9j9JRtq.js.map +1 -0
- package/dist/chunks/build-config-D9Iq8QQm.js +60 -0
- package/dist/chunks/build-config-D9Iq8QQm.js.map +1 -0
- package/dist/{collector-BevHh16N.js → chunks/collector-Cb8ah2JF.js} +10 -127
- package/dist/chunks/collector-Cb8ah2JF.js.map +1 -0
- package/dist/chunks/config-engine-CMYFZYQh.js +272 -0
- package/dist/chunks/config-engine-CMYFZYQh.js.map +1 -0
- package/dist/chunks/config-file-DlZXJMj8.js +25 -0
- package/dist/chunks/config-file-DlZXJMj8.js.map +1 -0
- package/dist/chunks/css-definitions-B4aWBkH3.js +910 -0
- package/dist/chunks/css-definitions-B4aWBkH3.js.map +1 -0
- package/dist/chunks/css-resources-P7IBUUZs.js +142 -0
- package/dist/chunks/css-resources-P7IBUUZs.js.map +1 -0
- package/dist/chunks/debug-D2Pu7Xhy.js +502 -0
- package/dist/chunks/debug-D2Pu7Xhy.js.map +1 -0
- package/dist/chunks/dsl-CsFl9LGn.js +1458 -0
- package/dist/chunks/dsl-CsFl9LGn.js.map +1 -0
- package/dist/chunks/hydration-TRcz5vnk.js +41 -0
- package/dist/chunks/hydration-TRcz5vnk.js.map +1 -0
- package/dist/chunks/react-runtime-DHiopXCu.js +691 -0
- package/dist/chunks/react-runtime-DHiopXCu.js.map +1 -0
- package/dist/chunks/runtime-engine-NGCofNXy.js +2435 -0
- package/dist/chunks/runtime-engine-NGCofNXy.js.map +1 -0
- package/dist/chunks/runtime-state-lZjMbvwV.js +152 -0
- package/dist/chunks/runtime-state-lZjMbvwV.js.map +1 -0
- package/dist/{merge-styles-BKfagXfV.js → chunks/shared-utils-y-HjvPN4.js} +60 -55
- package/dist/chunks/shared-utils-y-HjvPN4.js.map +1 -0
- package/dist/chunks/style-engine-DnkV_4Jg.js +5602 -0
- package/dist/chunks/style-engine-DnkV_4Jg.js.map +1 -0
- package/dist/{css-writer-BDPmetHV.js → chunks/zero-engine-DcwaCvFs.js} +65 -145
- package/dist/chunks/zero-engine-DcwaCvFs.js.map +1 -0
- package/dist/{collector-CLVr5_p3.d.ts → collector-D2OT6I2G.d.ts} +6 -5
- package/dist/{config-YsxGv4tq.d.ts → config-C4ftFxgI.d.ts} +181 -77
- package/dist/core/index.d.ts +5 -5
- package/dist/core/index.js +9 -6
- package/dist/hydrate-CRyjjAyl.d.ts +22 -0
- package/dist/{index-CBK778ne.d.ts → index-DxQNRj50.d.ts} +308 -255
- package/dist/index.d.ts +5 -5
- package/dist/index.js +11 -922
- package/dist/{merge-styles-CU7JbEwg.d.ts → merge-styles-D39qBtox.d.ts} +2 -3
- package/dist/ssr/astro-client.d.ts +1 -1
- package/dist/ssr/astro-client.js +1 -14
- package/dist/ssr/astro-client.js.map +1 -1
- package/dist/ssr/astro-middleware-extract-static.d.ts +1 -2
- package/dist/ssr/astro-middleware-extract-static.js +1 -1
- package/dist/ssr/astro-middleware-extract.d.ts +1 -2
- package/dist/ssr/astro-middleware-extract.js +1 -1
- package/dist/ssr/astro-middleware-static.d.ts +1 -2
- package/dist/ssr/astro-middleware-static.js +1 -12
- package/dist/ssr/astro-middleware-static.js.map +1 -1
- package/dist/ssr/astro-middleware.d.ts +1 -2
- package/dist/ssr/astro-middleware.js +1 -13
- package/dist/ssr/astro-middleware.js.map +1 -1
- package/dist/ssr/astro.d.ts +17 -17
- package/dist/ssr/astro.js +220 -1
- package/dist/ssr/astro.js.map +1 -0
- package/dist/ssr/client.d.ts +2 -0
- package/dist/ssr/client.js +2 -0
- package/dist/ssr/index.d.ts +5 -25
- package/dist/ssr/index.js +5 -5
- package/dist/ssr/index.js.map +1 -1
- package/dist/ssr/next-config.d.ts +64 -0
- package/dist/ssr/next-config.js +123 -0
- package/dist/ssr/next-config.js.map +1 -0
- package/dist/ssr/next.d.ts +9 -8
- package/dist/ssr/next.js +29 -43
- package/dist/ssr/next.js.map +1 -1
- package/dist/static/index.d.ts +8 -9
- package/dist/static/index.js +4 -22
- package/dist/static/index.js.map +1 -1
- package/dist/static/inject.d.ts +1 -2
- package/dist/{index-Cd45t5NM.d.ts → types-CYQIhUg1.d.ts} +868 -820
- package/dist/zero/babel.d.ts +73 -2
- package/dist/zero/babel.js +81 -144
- package/dist/zero/babel.js.map +1 -1
- package/dist/zero/index.d.ts +63 -8
- package/dist/zero/index.js +25 -2
- package/dist/zero/index.js.map +1 -0
- package/dist/zero/next.d.ts +4 -5
- package/dist/zero/next.js +3 -38
- package/dist/zero/next.js.map +1 -1
- package/docs/README.md +4 -1
- package/docs/adoption.md +25 -17
- package/docs/ai-agents.md +68 -33
- package/docs/comparison.md +9 -8
- package/docs/configuration.md +204 -70
- package/docs/debug.md +21 -8
- package/docs/design-system.md +117 -15
- package/docs/dsl.md +307 -117
- package/docs/getting-started.md +81 -22
- package/docs/injector.md +105 -67
- package/docs/methodology.md +48 -23
- package/docs/migration-v3.md +3 -3
- package/docs/pipeline.md +54 -27
- package/docs/plugins.md +22 -42
- package/docs/react-api.md +76 -11
- package/docs/runtime-benchmarks.md +282 -110
- package/docs/ssr.md +226 -80
- package/docs/styles.md +169 -33
- package/docs/tasty-static.md +176 -58
- package/package.json +108 -44
- package/tasty.config.ts +1 -0
- package/dist/astro-C_by9qPq.js +0 -307
- package/dist/astro-C_by9qPq.js.map +0 -1
- package/dist/async-storage-DKK-wTD4.js +0 -44
- package/dist/async-storage-DKK-wTD4.js.map +0 -1
- package/dist/babel-BUQGeOXA.d.ts +0 -83
- package/dist/collector-BevHh16N.js.map +0 -1
- package/dist/config-NcdqU9PC.js +0 -12269
- package/dist/config-NcdqU9PC.js.map +0 -1
- package/dist/context-CA8YKeMn.js +0 -24
- package/dist/context-CA8YKeMn.js.map +0 -1
- package/dist/core-D7s7u-k1.js +0 -1566
- package/dist/core-D7s7u-k1.js.map +0 -1
- package/dist/css-writer-BDPmetHV.js.map +0 -1
- package/dist/format-global-rules-DklyaXv-.js +0 -22
- package/dist/format-global-rules-DklyaXv-.js.map +0 -1
- package/dist/format-rules-BQCn7JZH.js +0 -130
- package/dist/format-rules-BQCn7JZH.js.map +0 -1
- package/dist/hydrate-C3cfqlaQ.js +0 -37
- package/dist/hydrate-C3cfqlaQ.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/keyframes-ByhOtsXB.js +0 -493
- package/dist/keyframes-ByhOtsXB.js.map +0 -1
- package/dist/merge-styles-BKfagXfV.js.map +0 -1
- package/dist/resolve-recipes-vvygrWLw.js +0 -144
- package/dist/resolve-recipes-vvygrWLw.js.map +0 -1
package/docs/getting-started.md
CHANGED
|
@@ -64,7 +64,7 @@ export default function App() {
|
|
|
64
64
|
|
|
65
65
|
`tasty()` returns a normal React component. The keys in each state map define priority: later branches win over earlier ones. Because `disabled` is last, a disabled button keeps its disabled styles even while the pointer is over it.
|
|
66
66
|
|
|
67
|
-
`disabled` and `
|
|
67
|
+
`disabled`, `checked`, and `hidden` are built-in automatic states. Tasty sets `data-disabled`, `data-checked`, and `data-hidden` from the matching native prop, so `disabled` in a state map is the concise form of `[disabled]` (it compiles to `[data-disabled]`).
|
|
68
68
|
|
|
69
69
|
Tasty compiles that priority into selectors that exclude one another. The hover, active, and disabled background rules cannot all match and ask the CSS cascade to decide the winner.
|
|
70
70
|
|
|
@@ -101,9 +101,9 @@ import App from './App';
|
|
|
101
101
|
createRoot(document.getElementById('root')!).render(<App />);
|
|
102
102
|
```
|
|
103
103
|
|
|
104
|
-
### Define shared tokens and
|
|
104
|
+
### Define shared tokens and units
|
|
105
105
|
|
|
106
|
-
Color tokens like `#primary` resolve to CSS custom properties
|
|
106
|
+
Color tokens like `#primary` and value tokens like `$sidebar-width` resolve to CSS custom properties (`var(--primary-color)`, `var(--sidebar-width)`). Define their values with `configure({ tokens })`:
|
|
107
107
|
|
|
108
108
|
```tsx
|
|
109
109
|
// src/tasty-config.ts
|
|
@@ -114,10 +114,7 @@ configure({
|
|
|
114
114
|
'#primary': 'oklch(55% 0.25 265)',
|
|
115
115
|
'#surface': '#fff',
|
|
116
116
|
'#text': '#111',
|
|
117
|
-
$
|
|
118
|
-
$radius: '4px',
|
|
119
|
-
'$border-width': '1px',
|
|
120
|
-
'$outline-width': '2px',
|
|
117
|
+
'$sidebar-width': '280px',
|
|
121
118
|
},
|
|
122
119
|
});
|
|
123
120
|
```
|
|
@@ -135,7 +132,20 @@ configure({
|
|
|
135
132
|
});
|
|
136
133
|
```
|
|
137
134
|
|
|
138
|
-
|
|
135
|
+
Tokens are injected as `:root` CSS custom properties when the first style is rendered, and every component using `#primary` follows them. You can also use standard CSS color values such as `rgb(...)`, `hsl(...)`, and named colors directly; the built-in `okhsl(...)` and `okhst(...)` functions are the recommended choice when you want authored colors that stay aligned with Tasty's design-system-oriented workflow.
|
|
136
|
+
|
|
137
|
+
Units work differently: `x`, `r`, `cr`, `bw`, and `ow` compile to plain values when styles are generated (`2x` → `16px` with the default `8px` step), so setting a `$gap` token does not change them. Change the scale with `configure({ units })`:
|
|
138
|
+
|
|
139
|
+
```tsx
|
|
140
|
+
configure({
|
|
141
|
+
units: {
|
|
142
|
+
x: '4px', // spacing step: 2x → 8px
|
|
143
|
+
r: '8px', // radius: 1r → 8px
|
|
144
|
+
},
|
|
145
|
+
});
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
When the scale has to change at runtime, for example per theme or density, point the unit at a custom property and define that property as a token. With `units: { x: 'var(--gap)' }` and `tokens: { $gap: '8px' }`, `2x` compiles to `calc(2 * var(--gap))`. See [Built-in Units](dsl.md#built-in-units) for the defaults.
|
|
139
149
|
|
|
140
150
|
> **Note:** `configure({ replaceTokens })` is a separate mechanism — it replaces tokens with literal values at parse time (baked into CSS). Use it for value aliases like `$card-padding: '4x'` that should be resolved during style generation, not for defining color or unit values. See [Configuration — Replace Tokens](configuration.md#replace-tokens-parse-time-substitution) for details.
|
|
141
151
|
|
|
@@ -145,7 +155,7 @@ See [Configuration](configuration.md) for the full `configure()` API — tokens,
|
|
|
145
155
|
|
|
146
156
|
## ESLint plugin
|
|
147
157
|
|
|
148
|
-
The ESLint plugin catches invalid style properties, bad token references, malformed state keys, and other mistakes at lint time — before they reach the browser.
|
|
158
|
+
The ESLint plugin catches invalid style properties, bad token references, malformed state keys, and other mistakes at lint time — before they reach the browser. It checks style objects and JSX style props alike, and runs under both ESLint 9+ and oxlint.
|
|
149
159
|
|
|
150
160
|
### Install
|
|
151
161
|
|
|
@@ -167,34 +177,80 @@ export default [
|
|
|
167
177
|
];
|
|
168
178
|
```
|
|
169
179
|
|
|
170
|
-
###
|
|
180
|
+
### Tell it about your design system
|
|
181
|
+
|
|
182
|
+
Without project configuration the plugin checks syntax and property names, but it cannot know which tokens, presets, or recipes exist. Add a `tasty.config.ts` at the project root to enable those checks for the names your `configure()` call defines:
|
|
183
|
+
|
|
184
|
+
```ts
|
|
185
|
+
// tasty.config.ts
|
|
186
|
+
export default {
|
|
187
|
+
tokens: ['#primary', '#surface', '#text', '$sidebar-width'],
|
|
188
|
+
states: ['@mobile', '@dark'],
|
|
189
|
+
presets: ['h1', 'h2', 't1', 't2'],
|
|
190
|
+
recipes: ['card'],
|
|
191
|
+
importSources: ['@my-org/ui'], // modules that re-export tasty()
|
|
192
|
+
};
|
|
193
|
+
```
|
|
171
194
|
|
|
172
|
-
|
|
195
|
+
Every list is optional; leaving one out skips that check. `importSources` matters when your design system re-exports `tasty()` from its own module: the plugin only lints calls whose import it can trace.
|
|
173
196
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
|
177
|
-
|
|
|
178
|
-
|
|
|
179
|
-
|
|
|
180
|
-
|
|
|
181
|
-
|
|
|
197
|
+
### What `recommended` catches
|
|
198
|
+
|
|
199
|
+
| Category | Rules | Examples |
|
|
200
|
+
| ------------------- | ------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------ |
|
|
201
|
+
| Property validation | `known-property`, `valid-boolean-property`, `valid-sub-element` | Flags typos like `pading` or invalid boolean usage |
|
|
202
|
+
| Value validation | `valid-value`, `valid-color-token`, `valid-custom-unit` | Catches `#nonexistent` tokens, bad unit syntax |
|
|
203
|
+
| State validation | `valid-state-key`, `no-nested-state-map`, `require-default-state` | Validates state key syntax, ensures `''` default exists |
|
|
204
|
+
| Structure | `valid-styles-structure`, `no-important`, `no-nested-selector` | Prevents `!important`, invalid nesting |
|
|
205
|
+
| Static mode | `static-no-dynamic-values`, `static-valid-selector` | Enforces build-time constraints in `tastyStatic()` |
|
|
206
|
+
| Style properties | `valid-preset`, `valid-recipe`, `valid-transition`, `valid-directional-modifier`, `valid-radius-shape` | Validates preset names, recipe references, transition syntax |
|
|
207
|
+
| Token usage | `no-raw-color-values`, `consistent-token-usage`, `no-raw-transition-duration` | Suggests `#color` tokens over raw colors, units over raw pixel lengths |
|
|
208
|
+
| Preferred syntax | `prefer-shorthand-property`, `prefer-auto-calc`, `prefer-hide`, `prefer-state-negation`, and others | Suggests `fill` over `backgroundColor`, `(...)` over `calc(...)` |
|
|
209
|
+
| Component props | `no-style-prop`, `no-classname-prop`, `no-styles-prop` | Points escape hatches toward `tokens`, `mods`, and the component's props |
|
|
210
|
+
| Dynamic styles | `no-runtime-styles-mutation`, `no-style-spread` | Warns on JavaScript values and spreads inside runtime style objects |
|
|
182
211
|
|
|
183
212
|
### Strict config
|
|
184
213
|
|
|
185
|
-
For stricter governance, use `tasty.configs.strict`. It adds
|
|
214
|
+
For stricter governance, use `tasty.configs.strict`. It adds existence checks for `$` custom properties and `@` state aliases, and validates state definitions in `configure()` and `tasty.config.ts`:
|
|
186
215
|
|
|
187
216
|
```js
|
|
188
217
|
export default [tasty.configs.strict];
|
|
189
218
|
```
|
|
190
219
|
|
|
220
|
+
See the [plugin README](https://github.com/tenphi/eslint-plugin-tasty) for every rule, the oxlint setup, and the experimental type-aware mode for JSX props.
|
|
221
|
+
|
|
222
|
+
---
|
|
223
|
+
|
|
224
|
+
## TypeScript autocomplete
|
|
225
|
+
|
|
226
|
+
Component props are typed from their definitions without extra setup: `modProps` value lists become unions, `variant` is typed from the variant keys, and sub-elements are typed components. To get autocomplete for your own token, preset, and theme names inside style values, register them with module augmentation:
|
|
227
|
+
|
|
228
|
+
```ts
|
|
229
|
+
// tasty.d.ts
|
|
230
|
+
import '@tenphi/tasty';
|
|
231
|
+
|
|
232
|
+
declare module '@tenphi/tasty' {
|
|
233
|
+
interface TastyNamedColors {
|
|
234
|
+
primary: true;
|
|
235
|
+
surface: true;
|
|
236
|
+
text: true;
|
|
237
|
+
}
|
|
238
|
+
interface TastyPresetNames {
|
|
239
|
+
h1: true;
|
|
240
|
+
t2: true;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
See [Configuration → TypeScript](configuration.md#typescript) for every augmentation interface and how component props are typed.
|
|
246
|
+
|
|
191
247
|
---
|
|
192
248
|
|
|
193
249
|
## Editor support
|
|
194
250
|
|
|
195
|
-
**[VS Code Extension](https://github.com/tenphi/tasty-vscode-extension)** — Syntax highlighting for Tasty styles in TypeScript/TSX/JavaScript/JSX
|
|
251
|
+
**[VS Code Extension](https://github.com/tenphi/tasty-vscode-extension)** — Syntax highlighting for Tasty styles in TypeScript/TSX/JavaScript/JSX: color tokens, custom units, state keys, presets, and style properties inside `tasty()`, `tastyStatic()`, `styles` objects, and style functions. It also adds snippets for components, state maps, and `configure()`. Install the `.vsix` from the [latest GitHub release](https://github.com/tenphi/tasty-vscode-extension/releases/latest); the extension is not published to the marketplace.
|
|
196
252
|
|
|
197
|
-
**[Glaze](https://github.com/tenphi/glaze)** —
|
|
253
|
+
**[Glaze](https://github.com/tenphi/glaze)** — Not an editor tool, but the fastest way to fill `configure({ tokens })`: it generates light, dark, and high-contrast palettes from one hue in the OKHST color space, solves WCAG and APCA contrast, and exports them as Tasty tokens with `configure({ tokens: theme.tasty() })`. See [Glaze in the README](../README.md#glaze).
|
|
198
254
|
|
|
199
255
|
---
|
|
200
256
|
|
|
@@ -226,6 +282,7 @@ Both `tasty()` and `tastyStatic()` share the same DSL, tokens, units, and state
|
|
|
226
282
|
- **[Adoption Guide](adoption.md)** — Roll out Tasty inside an existing design system or platform team
|
|
227
283
|
- **[Comparison](comparison.md)** — Evaluate Tasty against other styling systems
|
|
228
284
|
- **[Configuration](configuration.md)** — Full `configure()` API: tokens, recipes, custom units, style handlers, TypeScript extensions
|
|
285
|
+
- **[Plugins & Extension Points](plugins.md)** — Add your own style properties, units, functions, and component props
|
|
229
286
|
- **[Style Properties](styles.md)** — Complete reference for all enhanced style properties
|
|
230
287
|
- **[Debug Utilities](debug.md)** — Inspect generated CSS and debug runtime behavior when styles do not look right
|
|
231
288
|
|
|
@@ -235,4 +292,6 @@ Both `tasty()` and `tastyStatic()` share the same DSL, tokens, units, and state
|
|
|
235
292
|
|
|
236
293
|
- Styles are missing on first render: make sure the file that calls `configure()` is imported before any `tasty()` component renders.
|
|
237
294
|
- Token or unit values are not what you expect: check your `configure({ tokens, units })` setup, then inspect the generated CSS variables with [Debug Utilities](debug.md).
|
|
295
|
+
- Changing a `$gap` or `$radius` token does not change `2x` or `1r`: units compile to plain values from `configure({ units })`. Point a unit at the custom property (`units: { x: 'var(--gap)' }`) if it should follow the token.
|
|
296
|
+
- The linter accepts unknown tokens or presets: list them in `tasty.config.ts` so the plugin knows what your design system defines.
|
|
238
297
|
- You need zero-runtime delivery: use server-only [`tasty()` with server rendering](ssr.md) to keep the full React API, or use [`tastyStatic()` build-time extraction](tasty-static.md) when styles must be compiled before rendering.
|
package/docs/injector.md
CHANGED
|
@@ -9,11 +9,11 @@ A high-performance CSS-in-JS solution that powers the Tasty design system with e
|
|
|
9
9
|
The Style Injector is the core engine behind Tasty's styling system, providing:
|
|
10
10
|
|
|
11
11
|
- **Hash-based deduplication** - Identical CSS gets the same className
|
|
12
|
-
- **
|
|
13
|
-
- **
|
|
12
|
+
- **DOM-driven lifetime** - Styles are collected once nothing renders them
|
|
13
|
+
- **Selector assembly** - Prepends the doubled `.tX.tX` class and root prefix to the pipeline's selector fragments (sub-element and `&` nesting is flattened earlier, by the pipeline)
|
|
14
14
|
- **At-rule injection** - First-class `@keyframes`, `@property`, `@font-face`, `@counter-style`, and `@function` support
|
|
15
15
|
- **Smart cleanup** - CSS rules batched cleanup, keyframes disposed immediately
|
|
16
|
-
- **SSR support** - Deterministic class names and
|
|
16
|
+
- **SSR support** - Deterministic class names shared with `ServerStyleCollector`, and hydration of server-rendered classes
|
|
17
17
|
- **Multiple roots** - Works with Document and ShadowRoot
|
|
18
18
|
- **Non-stacking cleanups** - Prevents timeout accumulation for better performance
|
|
19
19
|
|
|
@@ -55,18 +55,26 @@ Injects CSS rules and returns a className with dispose function.
|
|
|
55
55
|
```typescript
|
|
56
56
|
import { inject } from '@tenphi/tasty';
|
|
57
57
|
|
|
58
|
-
//
|
|
58
|
+
// Rules flagged needsClassName get the generated class prepended (doubled)
|
|
59
59
|
const result = inject([{
|
|
60
|
-
selector: '
|
|
60
|
+
selector: '',
|
|
61
61
|
declarations: 'color: red; padding: 10px;',
|
|
62
|
+
needsClassName: true,
|
|
62
63
|
}]);
|
|
63
64
|
|
|
64
|
-
console.log(result.className); // 't
|
|
65
|
+
console.log(result.className); // 'th8n0uh' — `t` + base36 hash of the rules
|
|
66
|
+
// Injected: .th8n0uh.th8n0uh { color: red; padding: 10px; }
|
|
65
67
|
|
|
66
|
-
//
|
|
68
|
+
// Release the pin; the class becomes collectible once nothing renders it
|
|
67
69
|
result.dispose();
|
|
68
70
|
```
|
|
69
71
|
|
|
72
|
+
`inject()` **pins** the class it returns, and `gc()` never evicts a pinned class
|
|
73
|
+
— that is what `dispose()` releases. Pass `{ pin: false }` when the caller keeps
|
|
74
|
+
no handle and the DOM is the only record that the class is in use; `dispose()`
|
|
75
|
+
is then a no-op. The render path (`tasty()` / `computeStyles()`) injects this
|
|
76
|
+
way, because a hook-free render has no unmount signal to dispose on.
|
|
77
|
+
|
|
70
78
|
### `injectGlobal(rules, options?): { dispose: () => void }`
|
|
71
79
|
|
|
72
80
|
Injects global styles that don't reserve tasty class names.
|
|
@@ -169,7 +177,7 @@ const result = testInjector.inject(rules);
|
|
|
169
177
|
Injects CSS keyframes with automatic deduplication.
|
|
170
178
|
|
|
171
179
|
```typescript
|
|
172
|
-
// Generated name (
|
|
180
|
+
// Generated name (tk0, tk1, … — namePrefix + 'k' + counter)
|
|
173
181
|
const fadeIn = keyframes({
|
|
174
182
|
from: { opacity: 0 },
|
|
175
183
|
to: { opacity: 1 },
|
|
@@ -245,8 +253,30 @@ Dispose, ref-counted cleanup and GC therefore behave identically in every mode.
|
|
|
245
253
|
- Most options have sensible defaults and auto-detection
|
|
246
254
|
- `configure()` is optional - the injector works with defaults
|
|
247
255
|
- **Configuration is locked after styles are generated** - calling `configure()` after first render will emit a warning and be ignored
|
|
248
|
-
- `gc.touchInterval`: Number of
|
|
249
|
-
- `gc.
|
|
256
|
+
- `gc.touchInterval`: Number of renders between sweeps. When the counter reaches this value, a sweep is scheduled via `requestIdleCallback`; without idle callbacks nothing is collected automatically.
|
|
257
|
+
- `gc.grace`: How long a class is left alone after collection first notices nothing is carrying it, in milliseconds (default `10000`).
|
|
258
|
+
- Automatic sweeps are opt-in: they run only when `configure({ gc })` is set (`gc: {}` enables them with these defaults). Without it nothing is collected until you call `gc()` or `cleanup()`.
|
|
259
|
+
|
|
260
|
+
**What a sweep does.** Everything the injector holds falls into one of five bands, and only the last is ever deleted:
|
|
261
|
+
|
|
262
|
+
| Band | | Deleted |
|
|
263
|
+
|---|---|---|
|
|
264
|
+
| 1 | **Rendered** — some element carries the class right now | never |
|
|
265
|
+
| 2 | **Not ours** — queued for a batched write, pre-allocated, or server-rendered | never |
|
|
266
|
+
| 3 | **Hot** — nothing carries it, but that was noticed less than `grace` ago | never |
|
|
267
|
+
| 4 | **Cached** — cold, but within `capacity` when ordered by when it went cold | never |
|
|
268
|
+
| 5 | Everything else | on every sweep |
|
|
269
|
+
|
|
270
|
+
`gc({ force: true })` and `cleanup()` take bands 4 and 5 together, ignoring capacity. Band 3 is spared even then: an explicit cleanup is still no reason to take rules from a render that has not committed yet.
|
|
271
|
+
|
|
272
|
+
`tasty()` components cache the class names they computed, so a re-render with the same styles skips the pipeline. Whenever `gc()`, `cleanup()` or `destroy()` deletes rules, those caches are dropped too, so a component whose CSS was collected injects it again on its next render.
|
|
273
|
+
|
|
274
|
+
> **Why band 3 exists.** Rendering is not commit-aware: a render can resolve a class and commit it a little later, and in between nothing on the page carries it. From outside React that is indistinguishable from a class that is finished, so collection does not try to tell them apart — it leaves alone anything only just noticed to be cold. A render would have to stay pending for the whole window to lose its rules, and it gets them back on its next render.
|
|
275
|
+
>
|
|
276
|
+
> The clock starts when a sweep *notices*, not when the element actually left — nothing observes that moment, so starting it at the sighting is what gives every class the same full window however long ago it went.
|
|
277
|
+
>
|
|
278
|
+
> This is also why collection costs nothing to run: the timestamps are written by the sweep's own DOM scan, so rendering tracks nothing per class.
|
|
279
|
+
- `gc.capacity`: Maximum number of unused styles (not in the DOM, not pinned) to retain. When exceeded, the least recently used are evicted first. Rendered and pinned styles don't count against this limit.
|
|
250
280
|
|
|
251
281
|
---
|
|
252
282
|
|
|
@@ -264,7 +294,7 @@ Beyond ordinary rules, the injector has a dedicated entry point per at-rule. All
|
|
|
264
294
|
|
|
265
295
|
Every one of these takes an optional `root` (a `Document` or `ShadowRoot`).
|
|
266
296
|
|
|
267
|
-
`func()` also
|
|
297
|
+
`counterStyle()` and `func()` also accept `weak: true`, which registers the rule **without** overriding an existing one of the same name. `configure()` uses it for global `@counter-style` and `@function` definitions so a component-local definition always wins over the global default, regardless of which is injected first.
|
|
268
298
|
|
|
269
299
|
```ts
|
|
270
300
|
import { counterStyle, fontFace, func, property } from '@tenphi/tasty';
|
|
@@ -281,22 +311,24 @@ func('$$negative', { args: ['$value'], result: '(-1 * $value)' });
|
|
|
281
311
|
|
|
282
312
|
### Style Result Format
|
|
283
313
|
|
|
284
|
-
The injector works with `StyleResult` objects from the
|
|
314
|
+
The injector works with `StyleResult` objects from the pipeline (`renderStyles`):
|
|
285
315
|
|
|
286
316
|
```typescript
|
|
287
317
|
interface StyleResult {
|
|
288
|
-
selector: string;
|
|
289
|
-
declarations: string;
|
|
290
|
-
atRules?: string[];
|
|
291
|
-
|
|
318
|
+
selector: string; // full selector, or a fragment when needsClassName
|
|
319
|
+
declarations: string; // CSS declarations
|
|
320
|
+
atRules?: string[]; // @media, @container, @supports, …
|
|
321
|
+
needsClassName?: boolean; // prepend the generated `.tX.tX` (OR'd fragments joined with '|||')
|
|
322
|
+
rootPrefix?: string; // root-state prefix placed before the class
|
|
323
|
+
startingStyle?: boolean; // wrap declarations in @starting-style
|
|
292
324
|
}
|
|
293
325
|
|
|
294
326
|
// Example StyleResult
|
|
295
327
|
const styleRule: StyleResult = {
|
|
296
|
-
selector: '
|
|
328
|
+
selector: ':where([data-hovered])',
|
|
297
329
|
declarations: 'padding: 8px 16px; background: blue; color: white;',
|
|
298
330
|
atRules: ['@media (min-width: 768px)'],
|
|
299
|
-
|
|
331
|
+
needsClassName: true,
|
|
300
332
|
};
|
|
301
333
|
```
|
|
302
334
|
|
|
@@ -314,38 +346,39 @@ API here calls it for you.
|
|
|
314
346
|
### Deduplication & Performance
|
|
315
347
|
|
|
316
348
|
```typescript
|
|
317
|
-
|
|
318
|
-
const button1 = inject([{
|
|
319
|
-
selector: '.t-btn1',
|
|
320
|
-
declarations: 'padding: 8px; color: red;'
|
|
321
|
-
}]);
|
|
349
|
+
const rule = { selector: '', declarations: 'padding: 8px; color: red;', needsClassName: true };
|
|
322
350
|
|
|
323
|
-
const
|
|
324
|
-
|
|
325
|
-
declarations: 'padding: 8px; color: red;' // Same declarations
|
|
326
|
-
}]);
|
|
351
|
+
const button1 = inject([rule], { cacheKey: 'button' });
|
|
352
|
+
const button2 = inject([rule], { cacheKey: 'button' });
|
|
327
353
|
|
|
328
|
-
//
|
|
354
|
+
// One class, one copy of the CSS
|
|
329
355
|
console.log(button1.className === button2.className); // true
|
|
330
356
|
```
|
|
331
357
|
|
|
332
|
-
|
|
358
|
+
Without `cacheKey` the class name is a hash of the rules, so injecting the same rules again reuses the class the same way: the rules are written once, and each call adds a pin that its own `dispose()` releases.
|
|
359
|
+
|
|
360
|
+
### Pinning
|
|
333
361
|
|
|
334
362
|
```typescript
|
|
335
|
-
// Multiple
|
|
336
|
-
const comp1 = inject([commonStyle]);
|
|
337
|
-
const comp2 = inject([commonStyle]);
|
|
338
|
-
const comp3 = inject([commonStyle]);
|
|
339
|
-
|
|
340
|
-
// Style is
|
|
341
|
-
comp1.dispose(); //
|
|
342
|
-
comp2.dispose(); //
|
|
343
|
-
comp3.dispose(); //
|
|
344
|
-
|
|
345
|
-
//
|
|
346
|
-
//
|
|
363
|
+
// Multiple callers using the same styles
|
|
364
|
+
const comp1 = inject([commonStyle], { cacheKey: 'common' });
|
|
365
|
+
const comp2 = inject([commonStyle], { cacheKey: 'common' });
|
|
366
|
+
const comp3 = inject([commonStyle], { cacheKey: 'common' });
|
|
367
|
+
|
|
368
|
+
// Style is pinned while any caller holds a handle
|
|
369
|
+
comp1.dispose(); // pins: 3 → 2
|
|
370
|
+
comp2.dispose(); // pins: 2 → 1
|
|
371
|
+
comp3.dispose(); // pins: 1 → 0, now up to the DOM and gc()
|
|
372
|
+
|
|
373
|
+
// Unpinned does not mean deleted: the rule stays cached and is reused instantly
|
|
374
|
+
// by the next inject(). gc() decides when it actually goes.
|
|
347
375
|
```
|
|
348
376
|
|
|
377
|
+
Pinning covers callers that hold a handle. Styles that come from rendering are
|
|
378
|
+
never pinned — see `{ pin: false }` under
|
|
379
|
+
[`inject()`](#injectrules-options-injectresult) — so what keeps them alive is
|
|
380
|
+
being in the DOM, and nothing else.
|
|
381
|
+
|
|
349
382
|
### Garbage Collection
|
|
350
383
|
|
|
351
384
|
```typescript
|
|
@@ -353,6 +386,9 @@ import { configure, gc } from '@tenphi/tasty';
|
|
|
353
386
|
|
|
354
387
|
// Keyframes: Disposed immediately when refCount = 0 (safer for global scope)
|
|
355
388
|
// CSS rules: Tracked by touch count and cleaned up via gc()
|
|
389
|
+
//
|
|
390
|
+
// A CSS rule is collectible when no element carries its class AND nobody
|
|
391
|
+
// pinned it with inject().
|
|
356
392
|
|
|
357
393
|
configure({
|
|
358
394
|
gc: {
|
|
@@ -367,15 +403,19 @@ gc();
|
|
|
367
403
|
// Force-remove ALL unused styles (e.g. on route change or test teardown):
|
|
368
404
|
gc({ force: true });
|
|
369
405
|
|
|
370
|
-
//
|
|
371
|
-
|
|
406
|
+
// cleanup() is the same thing:
|
|
407
|
+
cleanup();
|
|
408
|
+
|
|
409
|
+
// Every `touchInterval` renders, a sweep is scheduled in idle time. It scans
|
|
410
|
+
// the DOM for the classes actually on the page and sorts everything the
|
|
411
|
+
// injector holds into five bands — only the last one is deleted. See below.
|
|
372
412
|
|
|
373
413
|
// Benefits:
|
|
374
414
|
// - Activity-proportional: busy apps trigger GC more often
|
|
375
415
|
// - DOM-safe: styles currently in the DOM are never evicted
|
|
376
416
|
// - Oldest-first: least recently used styles are evicted first
|
|
377
417
|
// - Keyframes: Immediate cleanup prevents global namespace pollution
|
|
378
|
-
// - Unused styles
|
|
418
|
+
// - Unused styles stay cached until evicted, so re-rendering them is a cache hit
|
|
379
419
|
```
|
|
380
420
|
|
|
381
421
|
### Shadow DOM Support
|
|
@@ -402,15 +442,16 @@ const shadowAnimation = keyframes({
|
|
|
402
442
|
|
|
403
443
|
### Server-Side Rendering
|
|
404
444
|
|
|
445
|
+
`getCSSText()` reads the injector's stylesheets, so it needs a DOM; on a server it throws. Collect server CSS with `ServerStyleCollector` from `@tenphi/tasty/ssr` (`collector.getCSS()`) — see [SSR](./ssr.md).
|
|
446
|
+
|
|
405
447
|
```typescript
|
|
406
448
|
import { getCSSText, getCSSTextForNode } from '@tenphi/tasty';
|
|
449
|
+
import { render } from '@testing-library/react';
|
|
407
450
|
|
|
408
|
-
//
|
|
409
|
-
const cssText = getCSSText();
|
|
451
|
+
const cssText = getCSSText(); // everything the injector holds (browser / test DOM)
|
|
410
452
|
|
|
411
|
-
|
|
412
|
-
const
|
|
413
|
-
const componentCSS = getCSSTextForNode(container);
|
|
453
|
+
const { container } = render(<MyComponent />);
|
|
454
|
+
const componentCSS = getCSSTextForNode(container); // only the CSS this subtree uses
|
|
414
455
|
```
|
|
415
456
|
|
|
416
457
|
### Test Environment Detection
|
|
@@ -470,11 +511,11 @@ const metrics = injector.instance.getMetrics();
|
|
|
470
511
|
console.log({
|
|
471
512
|
cacheHits: metrics.hits, // Successful cache hits
|
|
472
513
|
cacheMisses: metrics.misses, // New styles injected
|
|
473
|
-
unusedHits: metrics.unusedHits, //
|
|
514
|
+
unusedHits: metrics.unusedHits, // Styles currently eligible for eviction (scans the DOM)
|
|
474
515
|
bulkCleanups: metrics.bulkCleanups, // Number of bulk cleanup operations
|
|
475
516
|
stylesCleanedUp: metrics.stylesCleanedUp, // Total styles removed in bulk cleanups
|
|
476
517
|
totalInsertions: metrics.totalInsertions, // Lifetime insertions
|
|
477
|
-
totalUnused: metrics.totalUnused, //
|
|
518
|
+
totalUnused: metrics.totalUnused, // Times a pinned style lost its last pin
|
|
478
519
|
startTime: metrics.startTime, // Metrics collection start timestamp
|
|
479
520
|
cleanupHistory: metrics.cleanupHistory, // Detailed cleanup operation history
|
|
480
521
|
});
|
|
@@ -483,17 +524,14 @@ console.log({
|
|
|
483
524
|
### Debug Information
|
|
484
525
|
|
|
485
526
|
```typescript
|
|
486
|
-
|
|
487
|
-
const debugInfo = injector.instance.getDebugInfo();
|
|
527
|
+
import { tastyDebug } from '@tenphi/tasty';
|
|
488
528
|
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
unusedStyles: debugInfo.unusedStyles, // Styles marked for cleanup
|
|
492
|
-
totalSheets: debugInfo.totalSheets, // Number of stylesheets
|
|
493
|
-
totalRules: debugInfo.totalRules, // Total CSS rules
|
|
494
|
-
});
|
|
529
|
+
const { activeClasses, unusedClasses, hotClasses, totalRuleCount } =
|
|
530
|
+
tastyDebug.summary({ raw: true });
|
|
495
531
|
```
|
|
496
532
|
|
|
533
|
+
See [Debug Utilities](./debug.md).
|
|
534
|
+
|
|
497
535
|
### Cleanup History
|
|
498
536
|
|
|
499
537
|
```typescript
|
|
@@ -521,7 +559,7 @@ metrics.cleanupHistory.forEach(cleanup => {
|
|
|
521
559
|
const buttonBase = 'padding: 8px 16px; border-radius: 4px;';
|
|
522
560
|
|
|
523
561
|
// ✅ Avoid frequent disposal and re-injection
|
|
524
|
-
// Let the
|
|
562
|
+
// Let the injector handle cleanup
|
|
525
563
|
|
|
526
564
|
// ✅ Use bulk operations for global styles
|
|
527
565
|
injectGlobal([
|
|
@@ -547,13 +585,13 @@ configure({
|
|
|
547
585
|
// The injector automatically manages memory through:
|
|
548
586
|
|
|
549
587
|
// 1. Hash-based deduplication - same CSS = same className
|
|
550
|
-
// 2.
|
|
551
|
-
// 3.
|
|
552
|
-
// 4.
|
|
553
|
-
// 5.
|
|
588
|
+
// 2. DOM-driven lifetime - a rendered class is never evicted
|
|
589
|
+
// 3. Pinning - inject() callers hold their classes until they dispose
|
|
590
|
+
// 4. Immediate keyframes cleanup - disposed instantly when refCount = 0
|
|
591
|
+
// 5. Touch-count GC - unused CSS rules are evicted oldest-first when over capacity
|
|
554
592
|
|
|
555
593
|
// Manual cleanup is rarely needed but available:
|
|
556
|
-
cleanup(); //
|
|
594
|
+
cleanup(); // Remove every rule that is neither rendered nor pinned
|
|
557
595
|
destroy(); // Nuclear option: remove all stylesheets and reset
|
|
558
596
|
```
|
|
559
597
|
|
|
@@ -575,9 +613,9 @@ const StyledButton = tasty({
|
|
|
575
613
|
|
|
576
614
|
// Internally uses the injector:
|
|
577
615
|
// 1. Styles are parsed into StyleResult objects
|
|
578
|
-
// 2. inject() is called with the parsed results
|
|
616
|
+
// 2. inject() is called with the parsed results, unpinned
|
|
579
617
|
// 3. Component gets the returned className
|
|
580
|
-
// 4.
|
|
618
|
+
// 4. gc() reclaims the class once no element carries it
|
|
581
619
|
```
|
|
582
620
|
|
|
583
621
|
For most development, you'll use the [React API](./react-api.md) rather than the injector directly. The injector provides the high-performance foundation that makes Tasty's declarative styling possible.
|