@tenphi/tasty 0.0.0-snapshot.002b1b3
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/LICENSE +21 -0
- package/README.md +637 -0
- package/dist/async-storage-B7_o6FKt.js +44 -0
- package/dist/async-storage-B7_o6FKt.js.map +1 -0
- package/dist/collector-LuU1vZ68.d.ts +98 -0
- package/dist/collector-MOYY2SOr.js +230 -0
- package/dist/collector-MOYY2SOr.js.map +1 -0
- package/dist/config-A237aY9H.js +10235 -0
- package/dist/config-A237aY9H.js.map +1 -0
- package/dist/config-vuCRkBWX.d.ts +884 -0
- package/dist/context-CkSg-kDT.js +24 -0
- package/dist/context-CkSg-kDT.js.map +1 -0
- package/dist/core/index.d.ts +5 -0
- package/dist/core/index.js +6 -0
- package/dist/core-BkKav78f.js +1592 -0
- package/dist/core-BkKav78f.js.map +1 -0
- package/dist/css-writer-Cos9tQRM.js +329 -0
- package/dist/css-writer-Cos9tQRM.js.map +1 -0
- package/dist/format-global-rules-Dbc_1tc3.js +22 -0
- package/dist/format-global-rules-Dbc_1tc3.js.map +1 -0
- package/dist/format-rules-C2oiTsEO.js +143 -0
- package/dist/format-rules-C2oiTsEO.js.map +1 -0
- package/dist/hydrate-miFzWIKR.js +45 -0
- package/dist/hydrate-miFzWIKR.js.map +1 -0
- package/dist/index-CJMXAAO5.d.ts +1602 -0
- package/dist/index-dUtwpOux.d.ts +1266 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +732 -0
- package/dist/index.js.map +1 -0
- package/dist/keyframes-DDtNo_hl.js +587 -0
- package/dist/keyframes-DDtNo_hl.js.map +1 -0
- package/dist/merge-styles-CtDJMhpJ.d.ts +7 -0
- package/dist/merge-styles-D_HbBOlq.js +144 -0
- package/dist/merge-styles-D_HbBOlq.js.map +1 -0
- package/dist/resolve-recipes-B7-823LL.js +144 -0
- package/dist/resolve-recipes-B7-823LL.js.map +1 -0
- package/dist/ssr/astro-client.d.ts +1 -0
- package/dist/ssr/astro-client.js +19 -0
- package/dist/ssr/astro-client.js.map +1 -0
- package/dist/ssr/astro-middleware.d.ts +15 -0
- package/dist/ssr/astro-middleware.js +19 -0
- package/dist/ssr/astro-middleware.js.map +1 -0
- package/dist/ssr/astro.d.ts +97 -0
- package/dist/ssr/astro.js +149 -0
- package/dist/ssr/astro.js.map +1 -0
- package/dist/ssr/index.d.ts +44 -0
- package/dist/ssr/index.js +10 -0
- package/dist/ssr/index.js.map +1 -0
- package/dist/ssr/next.d.ts +46 -0
- package/dist/ssr/next.js +75 -0
- package/dist/ssr/next.js.map +1 -0
- package/dist/static/index.d.ts +91 -0
- package/dist/static/index.js +50 -0
- package/dist/static/index.js.map +1 -0
- package/dist/static/inject.d.ts +5 -0
- package/dist/static/inject.js +17 -0
- package/dist/static/inject.js.map +1 -0
- package/dist/zero/babel.d.ts +81 -0
- package/dist/zero/babel.js +466 -0
- package/dist/zero/babel.js.map +1 -0
- package/dist/zero/index.d.ts +67 -0
- package/dist/zero/index.js +2 -0
- package/dist/zero/next.d.ts +86 -0
- package/dist/zero/next.js +143 -0
- package/dist/zero/next.js.map +1 -0
- package/docs/README.md +31 -0
- package/docs/adoption.md +298 -0
- package/docs/comparison.md +419 -0
- package/docs/configuration.md +394 -0
- package/docs/debug.md +320 -0
- package/docs/design-system.md +436 -0
- package/docs/dsl.md +688 -0
- package/docs/getting-started.md +217 -0
- package/docs/injector.md +544 -0
- package/docs/methodology.md +616 -0
- package/docs/pipeline.md +673 -0
- package/docs/react-api.md +557 -0
- package/docs/ssr.md +442 -0
- package/docs/styles.md +596 -0
- package/docs/tasty-static.md +532 -0
- package/package.json +222 -0
- package/tasty.config.ts +15 -0
package/docs/pipeline.md
ADDED
|
@@ -0,0 +1,673 @@
|
|
|
1
|
+
# Tasty Style Rendering Pipeline
|
|
2
|
+
|
|
3
|
+
This document describes the style rendering pipeline that transforms style objects into CSS rules. The pipeline ensures that each style value is applied to exactly one condition through exclusive condition building, boolean simplification, and intelligent CSS generation.
|
|
4
|
+
|
|
5
|
+
**Implementation:** [`src/pipeline/`](../src/pipeline/) — TypeScript file names below are relative to that directory.
|
|
6
|
+
|
|
7
|
+
## Overview
|
|
8
|
+
|
|
9
|
+
The pipeline takes a `Styles` object and produces an array of `CSSRule` objects ready for injection into the DOM. Entry points include `renderStylesPipeline` (full pipeline + optional class-name prefixing) and `renderStyles` (direct selector/class mode). The per-handler flow is:
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
Input: Styles Object
|
|
13
|
+
↓
|
|
14
|
+
┌──────────────────────────────────────────┐
|
|
15
|
+
│ 0. PRE-PARSE NORMALIZATION │
|
|
16
|
+
│ extractCompoundStates │
|
|
17
|
+
│ (drop don't-care AND atoms) │
|
|
18
|
+
└──────────────────────────────────────────┘
|
|
19
|
+
↓
|
|
20
|
+
┌──────────────────────────────────────────┐
|
|
21
|
+
│ 1. PARSE CONDITIONS │
|
|
22
|
+
│ parseStyleEntries + parseStateKey │
|
|
23
|
+
└──────────────────────────────────────────┘
|
|
24
|
+
↓
|
|
25
|
+
┌──────────────────────────────────────────┐
|
|
26
|
+
│ 1b. MERGE ENTRIES BY VALUE │
|
|
27
|
+
│ mergeEntriesByValue │
|
|
28
|
+
│ (collapse same-value non-defaults) │
|
|
29
|
+
└──────────────────────────────────────────┘
|
|
30
|
+
↓
|
|
31
|
+
┌──────────────────────────────────────────┐
|
|
32
|
+
│ 2a. EXPAND USER OR BRANCHES │
|
|
33
|
+
│ expandOrConditions │
|
|
34
|
+
│ (A | B | C → A, B&!A, C&!A&!B) │
|
|
35
|
+
└──────────────────────────────────────────┘
|
|
36
|
+
↓
|
|
37
|
+
┌──────────────────────────────────────────┐
|
|
38
|
+
│ 2b. BUILD EXCLUSIVE CONDITIONS │
|
|
39
|
+
│ Negate higher-priority entries │
|
|
40
|
+
└──────────────────────────────────────────┘
|
|
41
|
+
↓
|
|
42
|
+
┌──────────────────────────────────────────┐
|
|
43
|
+
│ 3. EXPAND DE MORGAN OR BRANCHES │
|
|
44
|
+
│ expandExclusiveOrs │
|
|
45
|
+
│ (only for at-rule ORs from negation) │
|
|
46
|
+
└──────────────────────────────────────────┘
|
|
47
|
+
↓
|
|
48
|
+
┌──────────────────────────────────────────┐
|
|
49
|
+
│ 4. COMPUTE STATE COMBINATIONS │
|
|
50
|
+
│ Cartesian product across styles │
|
|
51
|
+
└──────────────────────────────────────────┘
|
|
52
|
+
↓
|
|
53
|
+
┌──────────────────────────────────────────┐
|
|
54
|
+
│ 5. CALL HANDLERS │
|
|
55
|
+
│ Compute CSS declarations │
|
|
56
|
+
└──────────────────────────────────────────┘
|
|
57
|
+
↓
|
|
58
|
+
┌──────────────────────────────────────────┐
|
|
59
|
+
│ 6. MERGE BY VALUE │
|
|
60
|
+
│ Combine rules with same output │
|
|
61
|
+
└──────────────────────────────────────────┘
|
|
62
|
+
↓
|
|
63
|
+
┌──────────────────────────────────────────┐
|
|
64
|
+
│ 7. MATERIALIZE CSS │
|
|
65
|
+
│ Condition → selectors + at-rules │
|
|
66
|
+
└──────────────────────────────────────────┘
|
|
67
|
+
↓
|
|
68
|
+
┌──────────────────────────────────────────┐
|
|
69
|
+
│ runPipeline post-pass: │
|
|
70
|
+
│ - dedupe identical rules │
|
|
71
|
+
│ - emit @starting-style rules last │
|
|
72
|
+
└──────────────────────────────────────────┘
|
|
73
|
+
↓
|
|
74
|
+
Output: CSSRule[]
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
**Simplification** (`simplifyCondition` in `simplify.ts`) is not a separate numbered stage. It runs inside OR expansion, exclusive building, `expandExclusiveOrs` branch cleanup, combination ANDs, merge-by-value ORs, and materialization. Every call is cached by condition unique-id, so the repetition is cheap.
|
|
78
|
+
|
|
79
|
+
**Post-pass:** After `processStyles` collects rules from every handler, `runPipeline` (`index.ts:188`) filters duplicates using a key of `selector|declarations|atRules|rootPrefix|startingStyle` and then reorders rules so every `@starting-style` rule is emitted **after** all normal rules. This ordering is cascade-critical: `@starting-style` rules share specificity with their normal counterparts, and source order decides which value wins.
|
|
80
|
+
|
|
81
|
+
---
|
|
82
|
+
|
|
83
|
+
## Stage 0: Pre-parse Normalization
|
|
84
|
+
|
|
85
|
+
**File:** `exclusive.ts` (`extractCompoundStates`)
|
|
86
|
+
|
|
87
|
+
### What It Does
|
|
88
|
+
|
|
89
|
+
Runs on each style's value map **before** any parsing. If a compound AND state key shares a value with the "atom absent" variant, the atom is a don't-care and every key is simplified by dropping it. Duplicate keys collapse.
|
|
90
|
+
|
|
91
|
+
### How It Works
|
|
92
|
+
|
|
93
|
+
1. Gather the unique set of top-level AND atoms across all keys.
|
|
94
|
+
2. An atom is **redundant** when every entry that contains it has a same-value partner with the atom absent and the rest of the atoms identical.
|
|
95
|
+
3. Keys containing `|`, `^`, or `,` at top level are treated as opaque single atoms (they don't participate in atom-level extraction).
|
|
96
|
+
4. Drop redundant atoms from every key; collapse duplicates.
|
|
97
|
+
|
|
98
|
+
### Why
|
|
99
|
+
|
|
100
|
+
Removing don't-care dimensions before parsing prevents combinatorial blowup in later stages. `mergeEntriesByValue`, `buildExclusiveConditions`, and materialization all see fewer entries and fewer spurious conditions. Implemented as part of the Apr 2026 fix for overlapping CSS rules (commit 7cd9dbe).
|
|
101
|
+
|
|
102
|
+
### Example
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
// Input (value map)
|
|
106
|
+
{ '': 'A', '@dark': 'B', '@hc': 'A', '@dark & @hc': 'B' }
|
|
107
|
+
// @hc is a don't-care: its presence never changes the value.
|
|
108
|
+
|
|
109
|
+
// Output
|
|
110
|
+
{ '': 'A', '@dark': 'B' }
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
---
|
|
114
|
+
|
|
115
|
+
## Stage 1: Parse Conditions
|
|
116
|
+
|
|
117
|
+
**Files:** `exclusive.ts` (`parseStyleEntries`), `parseStateKey.ts` (`parseStateKey`)
|
|
118
|
+
|
|
119
|
+
### What It Does
|
|
120
|
+
|
|
121
|
+
Converts each state key in a style value map (like `'hovered & !disabled'`, `'@media(w < 768px)'`) into `ConditionNode` trees. `parseStyleEntries` walks the object keys in source order and assigns priorities; `parseStateKey` parses a single key string.
|
|
122
|
+
|
|
123
|
+
### How It Works
|
|
124
|
+
|
|
125
|
+
1. **Tokenization**: The state key is split into tokens using a regex pattern that recognizes:
|
|
126
|
+
- Operators: `&` (AND), `|` (OR), `!` (NOT), `^` (XOR)
|
|
127
|
+
- Parentheses for grouping
|
|
128
|
+
- State tokens: `@media(...)`, `@root(...)`, `@parent(...)`, `@own(...)`, `@supports(...)`, `@(...)`, `@starting`, predefined states, modifiers, pseudo-classes
|
|
129
|
+
|
|
130
|
+
2. **Recursive Descent Parsing**: Tokens are parsed with operator precedence:
|
|
131
|
+
```
|
|
132
|
+
! (NOT) > ^ (XOR) > | (OR) > & (AND)
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
3. **State Token Interpretation**: Each state token is converted to a specific condition type:
|
|
136
|
+
- `hovered` → `ModifierCondition` with `attribute: 'data-hovered'`
|
|
137
|
+
- `theme=dark` → `ModifierCondition` with `attribute: 'data-theme', value: 'dark'`
|
|
138
|
+
- `:hover` → `PseudoCondition`
|
|
139
|
+
- `@media(w < 768px)` → `MediaCondition` (`subtype: 'dimension'`) with bounds
|
|
140
|
+
- `@media(prefers-color-scheme: dark)` → `MediaCondition` (`subtype: 'feature'`, `feature` + `featureValue`)
|
|
141
|
+
- `@root(schema=dark)` → `RootCondition` wrapping the inner condition
|
|
142
|
+
- `@parent(hovered)` → `ParentCondition` (optional `direct` for immediate parent)
|
|
143
|
+
- `@own(hovered)` → `OwnCondition` wrapping the parsed inner condition
|
|
144
|
+
- `@supports(display: grid)` → `SupportsCondition`
|
|
145
|
+
- `@(w < 600px)` → `ContainerCondition` (dimension, style, or raw subtypes)
|
|
146
|
+
- `@mobile` → Resolved via predefined states, then parsed recursively
|
|
147
|
+
|
|
148
|
+
Pipeline warnings for invalid inputs (e.g. bad `$` selector affix) are emitted from `warnings.ts`.
|
|
149
|
+
|
|
150
|
+
### Why
|
|
151
|
+
|
|
152
|
+
The condition tree representation enables:
|
|
153
|
+
- Boolean algebra operations (simplification, negation)
|
|
154
|
+
- Semantic analysis (detect contradictions)
|
|
155
|
+
- Flexible CSS generation (different output for media vs. selectors)
|
|
156
|
+
|
|
157
|
+
### Example
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
// Input
|
|
161
|
+
'hovered & @media(w < 768px)'
|
|
162
|
+
|
|
163
|
+
// Output ConditionNode
|
|
164
|
+
{
|
|
165
|
+
kind: 'compound',
|
|
166
|
+
operator: 'AND',
|
|
167
|
+
children: [
|
|
168
|
+
{ kind: 'state', type: 'modifier', attribute: 'data-hovered', ... },
|
|
169
|
+
{ kind: 'state', type: 'media', subtype: 'dimension', upperBound: { value: '768px', ... }, ... }
|
|
170
|
+
]
|
|
171
|
+
}
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
---
|
|
175
|
+
|
|
176
|
+
## Stage 1b: Merge Entries By Value
|
|
177
|
+
|
|
178
|
+
**File:** `exclusive.ts` (`mergeEntriesByValue`)
|
|
179
|
+
|
|
180
|
+
### What It Does
|
|
181
|
+
|
|
182
|
+
Collapses parsed entries that share the same value. Only **non-default** entries are merged — an entry with the default state (`''` → `TrueCondition`) is never merged with a non-default entry.
|
|
183
|
+
|
|
184
|
+
### How It Works
|
|
185
|
+
|
|
186
|
+
1. Group entries by serialized value.
|
|
187
|
+
2. Within each group, split out default (TRUE) entries.
|
|
188
|
+
3. Keep default entries as-is; they must retain TRUE so they participate correctly in exclusive building.
|
|
189
|
+
4. Combine non-default entries into a single entry with condition `OR(e1.condition, e2.condition, …)`, simplified via `simplifyCondition`. The merged entry keeps the **highest** priority in the group.
|
|
190
|
+
5. Re-sort by priority (highest first).
|
|
191
|
+
|
|
192
|
+
### Why
|
|
193
|
+
|
|
194
|
+
Without this, a value map like `{ '@dark': 'red', '@dark & @hc': 'red' }` would create two separate entries that later produce two CSS rules with identical output. Merging before exclusive building keeps the exclusive condition algebra small and avoids duplicate CSS.
|
|
195
|
+
|
|
196
|
+
**Why defaults are kept separate:** merging `TRUE | X` collapses to `TRUE`, destroying X's participation in the exclusive cascade. Intermediate-priority states would then lose their `:not(X)` negation, producing overlapping CSS rules. See `exclusive.ts:140-160` for the rationale.
|
|
197
|
+
|
|
198
|
+
### Example
|
|
199
|
+
|
|
200
|
+
```typescript
|
|
201
|
+
// Input entries (highest priority first)
|
|
202
|
+
[
|
|
203
|
+
{ stateKey: '@dark & @hc', value: 'red', condition: dark & hc },
|
|
204
|
+
{ stateKey: '@dark', value: 'red', condition: dark },
|
|
205
|
+
]
|
|
206
|
+
|
|
207
|
+
// Output: one merged entry
|
|
208
|
+
[
|
|
209
|
+
{ stateKey: '@dark & @hc | @dark', value: 'red',
|
|
210
|
+
condition: simplify((dark & hc) | dark) = dark }
|
|
211
|
+
]
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
---
|
|
215
|
+
|
|
216
|
+
## Stage 2a: Expand User OR Branches
|
|
217
|
+
|
|
218
|
+
**File:** `exclusive.ts` (`expandOrConditions`)
|
|
219
|
+
|
|
220
|
+
### What It Does
|
|
221
|
+
|
|
222
|
+
Runs **before** `buildExclusiveConditions`. Splits any user-authored OR in a parsed entry's condition into multiple sibling entries, each made exclusive against the OR branches that come before it.
|
|
223
|
+
|
|
224
|
+
### How It Works
|
|
225
|
+
|
|
226
|
+
For an entry with condition `A | B | C`:
|
|
227
|
+
|
|
228
|
+
```
|
|
229
|
+
A (first branch, no prior)
|
|
230
|
+
B & !A (second branch exclusive from first)
|
|
231
|
+
C & !A & !B (third branch exclusive from first two)
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
Each expanded branch gets a `stateKey` suffix like `[0]`, `[1]`, `[2]`. Branches that simplify to `FALSE` are dropped. Branches inherit the original entry's priority.
|
|
235
|
+
|
|
236
|
+
This pass does **not** sort branches — user ORs are authored in the natural order they appear and aren't the product of De Morgan negation, so at-rule-aware sorting isn't required here (that's Stage 3's job).
|
|
237
|
+
|
|
238
|
+
### Why
|
|
239
|
+
|
|
240
|
+
Running this before exclusive building means the Stage 2b negation cascade sees one branch per entry and never has to reason about nested ORs while computing `!prior`. It also avoids emitting overlapping CSS rules: `{ 'compact | @media(dark)': 'red' }` becomes two mutually exclusive entries rather than one rule whose two branches could both match simultaneously.
|
|
241
|
+
|
|
242
|
+
---
|
|
243
|
+
|
|
244
|
+
## Stage 2b: Build Exclusive Conditions
|
|
245
|
+
|
|
246
|
+
**File:** `exclusive.ts` (`buildExclusiveConditions`)
|
|
247
|
+
|
|
248
|
+
### What It Does
|
|
249
|
+
|
|
250
|
+
Ensures each style entry applies in exactly one scenario by ANDing each condition with the negation of all higher-priority conditions.
|
|
251
|
+
|
|
252
|
+
### How It Works
|
|
253
|
+
|
|
254
|
+
Given entries ordered by priority (highest first):
|
|
255
|
+
```
|
|
256
|
+
A: value1 (priority 2)
|
|
257
|
+
B: value2 (priority 1)
|
|
258
|
+
C: value3 (priority 0)
|
|
259
|
+
```
|
|
260
|
+
|
|
261
|
+
Produces:
|
|
262
|
+
```
|
|
263
|
+
A: A (highest priority, no negation needed)
|
|
264
|
+
B: B & !A (applies only when A doesn't)
|
|
265
|
+
C: C & !A & !B (applies only when neither A nor B)
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
Each exclusive condition is passed through `simplifyCondition`. Entries that simplify to `FALSE` (impossible) are filtered out. The default state (`''` → `TrueCondition`) is not added to the “prior” list for negation (see `buildExclusiveConditions`).
|
|
269
|
+
|
|
270
|
+
### Why
|
|
271
|
+
|
|
272
|
+
This eliminates CSS specificity wars. Instead of relying on cascade order, each CSS rule matches in exactly one scenario. Benefits:
|
|
273
|
+
- Predictable styling regardless of rule order
|
|
274
|
+
- No conflicts from overlapping conditions
|
|
275
|
+
- Easier debugging (each rule is mutually exclusive)
|
|
276
|
+
|
|
277
|
+
### Example
|
|
278
|
+
|
|
279
|
+
```typescript
|
|
280
|
+
// Style value mapping
|
|
281
|
+
{ padding: { '': '2x', 'compact': '1x', '@media(w < 768px)': '0.5x' } }
|
|
282
|
+
|
|
283
|
+
// After exclusive building (highest priority first):
|
|
284
|
+
// @media(w < 768px): applies when media matches
|
|
285
|
+
// compact & !@media(w < 768px): applies when compact but NOT media
|
|
286
|
+
// !compact & !@media(w < 768px): default, applies when neither
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
---
|
|
290
|
+
|
|
291
|
+
## Stage 3: Expand De Morgan OR Branches
|
|
292
|
+
|
|
293
|
+
**File:** `exclusive.ts` (`expandExclusiveOrs`, `sortOrBranchesForExpansion`)
|
|
294
|
+
|
|
295
|
+
### What It Does
|
|
296
|
+
|
|
297
|
+
Runs **after** `buildExclusiveConditions`. Handles ORs that arise **during** exclusive building from De Morgan negation — e.g. when a higher-priority condition `A & B` gets negated into the next entry's exclusive as `!(A & B) = !A | !B`. When such an OR mixes **at-rule** context (`media`, `container`, `supports`, `starting`) with other branches, each branch needs to keep its own at-rule wrapping.
|
|
298
|
+
|
|
299
|
+
This is the companion to **Stage 2a** (user-OR expansion). The split exists because the two passes have different data and different correctness needs:
|
|
300
|
+
|
|
301
|
+
| Stage | Runs on | Sees ORs from | Sorts branches? |
|
|
302
|
+
|---|---|---|---|
|
|
303
|
+
| 2a `expandOrConditions` | `ParsedStyleEntry.condition` | User-authored `|` in state keys | No — user order is stable |
|
|
304
|
+
| 3 `expandExclusiveOrs` | `ExclusiveStyleEntry.exclusiveCondition` | De Morgan negation inside `buildExclusiveConditions` | Yes — at-rule branches first |
|
|
305
|
+
|
|
306
|
+
### How It Works
|
|
307
|
+
|
|
308
|
+
1. Collect top-level OR branches of `exclusiveCondition`.
|
|
309
|
+
2. If there is no OR (single branch), the entry is unchanged. Pure selector ORs with no at-rule context are also left alone (materialization handles them via `:is()` / variant merging).
|
|
310
|
+
3. Otherwise `sortOrBranchesForExpansion` reorders branches so at-rule-heavy branches come first. This is load-bearing for correctness (see below).
|
|
311
|
+
4. Each branch is made exclusive against prior branches: `branch & !prior[0] & !prior[1] & ...`, then simplified.
|
|
312
|
+
5. Impossible branches are dropped; expanded entries get a synthetic `stateKey` suffix like `[or:0]`.
|
|
313
|
+
|
|
314
|
+
### Why the sort matters
|
|
315
|
+
|
|
316
|
+
Consider `!A | !B` where A is an at-rule (e.g. `@supports(grid)`) and B is a modifier (e.g. `:has(foo)`):
|
|
317
|
+
|
|
318
|
+
- **With at-rule-first sort** (`!A`, then `!B & A`): the first branch emits "outside `@supports`", the second emits "inside `@supports` with `:not(:has(foo))`". Full coverage.
|
|
319
|
+
- **Without the sort** (`!B`, then `!A & B`): the first branch emits `:not(:has(foo))` as a bare selector with no at-rule context — leaking the rule outside `@supports`. The second is incomplete.
|
|
320
|
+
|
|
321
|
+
The pre-build Stage 2a pass doesn't need this because user-authored ORs aren't produced by negation and their branches are expected to apply in each branch's own scope.
|
|
322
|
+
|
|
323
|
+
### Example (conceptual)
|
|
324
|
+
|
|
325
|
+
See the comment block in `exclusive.ts:500-523`: a default value whose higher-priority sibling is `@supports(...) & :has(...)` gets an exclusive of `!@supports | !:has`. Expansion yields one branch under `@supports (not ...)` and another under `@supports (...) { :not(:has()) }` instead of a bare `:not(:has())` rule.
|
|
326
|
+
|
|
327
|
+
---
|
|
328
|
+
|
|
329
|
+
## Stage 4: Compute State Combinations
|
|
330
|
+
|
|
331
|
+
**File:** `index.ts` (`computeStateCombinations`)
|
|
332
|
+
|
|
333
|
+
### What It Does
|
|
334
|
+
|
|
335
|
+
Computes the Cartesian product of all style entries for a handler, creating snapshots of which value each style has for each possible state combination.
|
|
336
|
+
|
|
337
|
+
### How It Works
|
|
338
|
+
|
|
339
|
+
1. Collect exclusive entries for each style the handler uses
|
|
340
|
+
2. Compute Cartesian product: every combination of entries
|
|
341
|
+
3. For each combination:
|
|
342
|
+
- AND all `exclusiveCondition` values together
|
|
343
|
+
- `simplifyCondition` the result
|
|
344
|
+
- Skip if simplified to `FALSE`
|
|
345
|
+
- Record the values for each style
|
|
346
|
+
|
|
347
|
+
### Why
|
|
348
|
+
|
|
349
|
+
Style handlers often depend on multiple style properties (e.g., `padding` might look at both `padding` and `gap`). By computing all valid combinations, we can call the handler once per unique state and get the correct CSS output.
|
|
350
|
+
|
|
351
|
+
### Example
|
|
352
|
+
|
|
353
|
+
```typescript
|
|
354
|
+
// Handler looks up: ['padding', 'size']
|
|
355
|
+
// padding has entries: [{ value: '2x', condition: A }, { value: '1x', condition: B }]
|
|
356
|
+
// size has entries: [{ value: 'large', condition: C }, { value: 'small', condition: D }]
|
|
357
|
+
|
|
358
|
+
// Combinations:
|
|
359
|
+
// { padding: '2x', size: 'large', condition: A & C }
|
|
360
|
+
// { padding: '2x', size: 'small', condition: A & D }
|
|
361
|
+
// { padding: '1x', size: 'large', condition: B & C }
|
|
362
|
+
// { padding: '1x', size: 'small', condition: B & D }
|
|
363
|
+
```
|
|
364
|
+
|
|
365
|
+
---
|
|
366
|
+
|
|
367
|
+
## Stage 5: Call Handlers
|
|
368
|
+
|
|
369
|
+
**File:** `index.ts` (within `processStyles`)
|
|
370
|
+
|
|
371
|
+
### What It Does
|
|
372
|
+
|
|
373
|
+
Invokes style handlers with computed value snapshots to produce CSS declarations.
|
|
374
|
+
|
|
375
|
+
### How It Works
|
|
376
|
+
|
|
377
|
+
1. For each state snapshot (condition + values):
|
|
378
|
+
- Call the handler with the values
|
|
379
|
+
- Handler returns CSS properties (e.g., `{ 'padding-top': '16px', 'padding-bottom': '16px' }`)
|
|
380
|
+
- Handler may also return `$` (selector suffix) for pseudo-elements
|
|
381
|
+
2. Create computed rules with the condition, declarations, and selector suffix
|
|
382
|
+
|
|
383
|
+
### Why
|
|
384
|
+
|
|
385
|
+
Style handlers encapsulate the logic for translating design tokens (like `'2x'`) to actual CSS values (like `'16px'`). They can also handle complex multi-property styles (e.g., `padding` → `padding-top`, `padding-right`, etc.).
|
|
386
|
+
|
|
387
|
+
---
|
|
388
|
+
|
|
389
|
+
## Stage 6: Merge By Value
|
|
390
|
+
|
|
391
|
+
**File:** `index.ts` (`mergeByValue`)
|
|
392
|
+
|
|
393
|
+
### What It Does
|
|
394
|
+
|
|
395
|
+
Combines rules that have identical CSS output into a single rule with an OR condition.
|
|
396
|
+
|
|
397
|
+
### How It Works
|
|
398
|
+
|
|
399
|
+
1. Group rules by `selectorSuffix` plus a stable string for declarations (JSON via an internal `declStringCache` `WeakMap` on declaration objects)
|
|
400
|
+
2. For rules in the same group:
|
|
401
|
+
- Merge their conditions with OR
|
|
402
|
+
- `simplifyCondition` the resulting condition
|
|
403
|
+
3. Output one rule per group
|
|
404
|
+
|
|
405
|
+
### Why
|
|
406
|
+
|
|
407
|
+
Different state combinations might produce the same CSS output. Rather than emitting duplicate CSS, we combine them into a single rule. This reduces CSS size and improves performance.
|
|
408
|
+
|
|
409
|
+
### Example
|
|
410
|
+
|
|
411
|
+
```typescript
|
|
412
|
+
// Before merging:
|
|
413
|
+
// condition: A → { color: 'red' }
|
|
414
|
+
// condition: B → { color: 'red' }
|
|
415
|
+
|
|
416
|
+
// After merging:
|
|
417
|
+
// condition: A | B → { color: 'red' }
|
|
418
|
+
```
|
|
419
|
+
|
|
420
|
+
---
|
|
421
|
+
|
|
422
|
+
## Stage 7: Materialize CSS
|
|
423
|
+
|
|
424
|
+
**File:** `materialize.ts` (`conditionToCSS`, `materializeComputedRule` in `index.ts`)
|
|
425
|
+
|
|
426
|
+
### What It Does
|
|
427
|
+
|
|
428
|
+
Converts condition trees into actual CSS selectors and at-rules.
|
|
429
|
+
|
|
430
|
+
### How It Works
|
|
431
|
+
|
|
432
|
+
1. **Condition to CSS components** (`conditionToCSS`): Walk the condition tree and build `SelectorVariant` data:
|
|
433
|
+
- `ModifierCondition` → attribute selectors (e.g. `[data-hovered]`); optional `operator` (`=`, `^=`, `$=`, `*=`)
|
|
434
|
+
- `PseudoCondition` → pseudo-class (e.g. `:hover`)
|
|
435
|
+
- `MediaCondition` → `@media` (dimension, feature, or type)
|
|
436
|
+
- `ContainerCondition` → `@container` (dimension, style query, or raw)
|
|
437
|
+
- `RootCondition` → `rootGroups` / root prefix fragments
|
|
438
|
+
- `ParentCondition` → `parentGroups` / ancestor selectors (`direct` → child combinator path)
|
|
439
|
+
- `OwnCondition` → `ownGroups` on the **styled** element (sub-element / `&` scope), optimized with `optimizeGroups`
|
|
440
|
+
- `SupportsCondition` → `@supports` at-rules
|
|
441
|
+
- `StartingCondition` → `@starting-style` wrapper
|
|
442
|
+
|
|
443
|
+
2. **AND / OR on variants**: AND merges variant dimensions; OR yields multiple variants (later merged into `:is()` / `:not()` groups where appropriate).
|
|
444
|
+
|
|
445
|
+
3. **Contradiction detection**: During variant merging, impossible combinations are dropped (e.g. conflicting media, root, or modifier negations).
|
|
446
|
+
|
|
447
|
+
4. **`materializeComputedRule`**: Groups variants by sorted at-rules plus root-prefix key; within each group, `mergeVariantsIntoSelectorGroups` merges variants that differ only in flat modifier/pseudo parts; builds selector strings and emits one or more `CSSRule` objects.
|
|
448
|
+
|
|
449
|
+
### Why
|
|
450
|
+
|
|
451
|
+
CSS has different mechanisms for different condition types:
|
|
452
|
+
- Modifiers → attribute selectors
|
|
453
|
+
- Media queries → `@media` blocks
|
|
454
|
+
- Container queries → `@container` blocks
|
|
455
|
+
- Root state → `:root` / root groups
|
|
456
|
+
- Supports → `@supports` blocks
|
|
457
|
+
|
|
458
|
+
The materialization layer handles these differences while maintaining the logical semantics of the condition tree.
|
|
459
|
+
|
|
460
|
+
### Output Structure
|
|
461
|
+
|
|
462
|
+
```typescript
|
|
463
|
+
interface CSSRule {
|
|
464
|
+
selector: string | string[]; // Selector fragment(s); array when OR’d selector branches
|
|
465
|
+
declarations: string; // CSS declarations (e.g. 'color: red;')
|
|
466
|
+
atRules?: string[]; // Wrapping at-rules
|
|
467
|
+
rootPrefix?: string; // Root state prefix
|
|
468
|
+
}
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
When `renderStylesPipeline` runs **without** a class name, returned rules include `needsClassName: true` (compatibility field for the injector); that flag is not part of `CSSRule` inside `materialize.ts`.
|
|
472
|
+
|
|
473
|
+
---
|
|
474
|
+
|
|
475
|
+
## Condition Types
|
|
476
|
+
|
|
477
|
+
**File:** `conditions.ts`
|
|
478
|
+
|
|
479
|
+
### ConditionNode Hierarchy
|
|
480
|
+
|
|
481
|
+
```
|
|
482
|
+
ConditionNode
|
|
483
|
+
├── TrueCondition (matches everything)
|
|
484
|
+
├── FalseCondition (matches nothing)
|
|
485
|
+
├── CompoundCondition (AND/OR of children)
|
|
486
|
+
└── StateCondition
|
|
487
|
+
├── ModifierCondition (data attributes; optional value + match operator)
|
|
488
|
+
├── PseudoCondition (CSS pseudo-classes: :hover)
|
|
489
|
+
├── MediaCondition (subtype: dimension | feature | type)
|
|
490
|
+
├── ContainerCondition (subtype: dimension | style | raw)
|
|
491
|
+
├── RootCondition (inner condition under :root)
|
|
492
|
+
├── ParentCondition (@parent(...); optional direct parent)
|
|
493
|
+
├── OwnCondition (@own(...); scoped to styled / sub-element)
|
|
494
|
+
├── SupportsCondition (@supports(...))
|
|
495
|
+
└── StartingCondition (@starting-style wrapper)
|
|
496
|
+
```
|
|
497
|
+
|
|
498
|
+
### Key Operations
|
|
499
|
+
|
|
500
|
+
- `and(...conditions)`: Create AND with short-circuit and flattening
|
|
501
|
+
- `or(...conditions)`: Create OR with short-circuit and flattening
|
|
502
|
+
- `not(condition)`: Negate with De Morgan's law support
|
|
503
|
+
- `getConditionUniqueId(condition)`: Get canonical ID for comparison
|
|
504
|
+
|
|
505
|
+
---
|
|
506
|
+
|
|
507
|
+
## Simplification
|
|
508
|
+
|
|
509
|
+
**File:** `simplify.ts`
|
|
510
|
+
|
|
511
|
+
### What It Does
|
|
512
|
+
|
|
513
|
+
Applies boolean algebra rules to reduce condition complexity and detect impossible combinations.
|
|
514
|
+
|
|
515
|
+
### Rules Applied
|
|
516
|
+
|
|
517
|
+
1. **Identity Laws**:
|
|
518
|
+
- `A & TRUE = A`
|
|
519
|
+
- `A | FALSE = A`
|
|
520
|
+
|
|
521
|
+
2. **Annihilator Laws**:
|
|
522
|
+
- `A & FALSE = FALSE`
|
|
523
|
+
- `A | TRUE = TRUE`
|
|
524
|
+
|
|
525
|
+
3. **Contradiction Detection**:
|
|
526
|
+
- `A & !A = FALSE`
|
|
527
|
+
|
|
528
|
+
4. **Tautology Detection**:
|
|
529
|
+
- `A | !A = TRUE`
|
|
530
|
+
|
|
531
|
+
5. **Idempotent Laws** (via deduplication):
|
|
532
|
+
- `A & A = A`
|
|
533
|
+
- `A | A = A`
|
|
534
|
+
|
|
535
|
+
6. **Absorption Laws**:
|
|
536
|
+
- `A & (A | B) = A`
|
|
537
|
+
- `A | (A & B) = A`
|
|
538
|
+
|
|
539
|
+
7. **Range intersection**: For **media and container** dimension queries, impossible ranges simplify to `FALSE` (e.g. `@media(w > 400px) & @media(w < 300px)`). Ranges with compatible bounds are also merged in place (`w >= 400 & w <= 800` → a single bounded range).
|
|
540
|
+
|
|
541
|
+
8. **Container style queries**: Conflicting or redundant `@container` style conditions on the same property can be reduced (see `simplify.ts` around the container-style conflict pass).
|
|
542
|
+
|
|
543
|
+
9. **Attribute conflict detection**:
|
|
544
|
+
- `[data-theme="dark"] & [data-theme="light"] = FALSE`
|
|
545
|
+
|
|
546
|
+
10. **Complementary factoring** (OR context): `(A & B) | (A & !B) = A`. Also works on **compound complements** — if two AND-clauses differ only by a child that is a compound negation of the other (e.g. `X` vs `!X` where X is itself `(P & Q)`), the clauses factor correctly.
|
|
547
|
+
|
|
548
|
+
11. **Consensus / resolution** (AND context, dual of #10): `(A | B) & (A | !B) = A`. Added in commit f9038bd to eliminate overlapping CSS selectors from compound-state OR branches.
|
|
549
|
+
|
|
550
|
+
### Why
|
|
551
|
+
|
|
552
|
+
Simplification reduces CSS output size and catches impossible combinations early, preventing invalid CSS rules from being generated. Every `simplifyCondition` call is memoized by the condition's unique id, so the cost of running it many times across stages is negligible after the first hit.
|
|
553
|
+
|
|
554
|
+
---
|
|
555
|
+
|
|
556
|
+
## Caching Strategy
|
|
557
|
+
|
|
558
|
+
LRU and small auxiliary caches:
|
|
559
|
+
|
|
560
|
+
| Cache | Size | Key | Purpose |
|
|
561
|
+
|-------|------|-----|---------|
|
|
562
|
+
| `pipelineCache` | 5000 | `pipelineCacheKey \|\| stringifyStyles(styles)` | Skip full pipeline for identical styles |
|
|
563
|
+
| `parseCache` | 5000 | `trimmedStateKey + '\\0' + isSubElement + '\\0' + JSON.stringify(localPredefinedStates)` | Skip re-parsing identical state keys in context |
|
|
564
|
+
| `simplifyCache` | 5000 | `getConditionUniqueId(node)` | Skip re-simplifying identical conditions |
|
|
565
|
+
| `conditionCache` | 3000 | `getConditionUniqueId(node)` in `conditionToCSS` | Skip re-materializing identical conditions |
|
|
566
|
+
| `variantKeyCache` | — | `WeakMap<SelectorVariant, string>` | Stable string keys for variants during materialization |
|
|
567
|
+
| `declStringCache` | — | `WeakMap<Record<string,string>, string>` | Stable JSON keys for declaration objects in `mergeByValue` |
|
|
568
|
+
|
|
569
|
+
---
|
|
570
|
+
|
|
571
|
+
## Example Walkthrough
|
|
572
|
+
|
|
573
|
+
### Input
|
|
574
|
+
|
|
575
|
+
```typescript
|
|
576
|
+
const styles = {
|
|
577
|
+
color: {
|
|
578
|
+
'': '#white',
|
|
579
|
+
'@media(prefers-color-scheme: dark)': '#dark',
|
|
580
|
+
hovered: '#highlight',
|
|
581
|
+
},
|
|
582
|
+
};
|
|
583
|
+
```
|
|
584
|
+
|
|
585
|
+
### Stage 1: Parse Conditions
|
|
586
|
+
|
|
587
|
+
```
|
|
588
|
+
'' → TrueCondition
|
|
589
|
+
'@media(prefers-color-scheme: dark)' → MediaCondition(subtype: 'feature', feature: 'prefers-color-scheme', featureValue: 'dark')
|
|
590
|
+
'hovered' → ModifierCondition(attribute: 'data-hovered')
|
|
591
|
+
```
|
|
592
|
+
|
|
593
|
+
### Stage 0 + 1b: Normalization
|
|
594
|
+
|
|
595
|
+
No compound AND keys, no same-value duplicates — the value map is unchanged.
|
|
596
|
+
|
|
597
|
+
### Stage 1 + 2a: Parse and expand user ORs
|
|
598
|
+
|
|
599
|
+
No user ORs — three entries pass through unchanged.
|
|
600
|
+
|
|
601
|
+
### Stage 2b + 3: Exclusive conditions + De Morgan expansion
|
|
602
|
+
|
|
603
|
+
Processing order (highest priority first): `hovered`, `@media(dark)`, default.
|
|
604
|
+
|
|
605
|
+
```
|
|
606
|
+
hovered: [data-hovered]
|
|
607
|
+
@media(dark) & !hovered: @media(dark) & :not([data-hovered])
|
|
608
|
+
!hovered & !@media(dark): :not([data-hovered]) & not @media(dark)
|
|
609
|
+
```
|
|
610
|
+
|
|
611
|
+
The default entry's exclusive is `!hovered & !@media(dark)` — no top-level OR, so Stage 3 expansion does nothing. If a higher-priority entry had been `@media(dark) & :has(foo)`, the default's exclusive would have expanded via De Morgan into two at-rule-aware branches (see Stage 3 for that scenario).
|
|
612
|
+
|
|
613
|
+
### Stages 4–5: Compute combinations and call handler
|
|
614
|
+
|
|
615
|
+
Single style, three snapshots; the `color` handler emits `color` plus `--current-color*` variables.
|
|
616
|
+
|
|
617
|
+
### Stage 6: Merge by value
|
|
618
|
+
|
|
619
|
+
Each snapshot yields distinct declarations; no merge.
|
|
620
|
+
|
|
621
|
+
### Stage 7: Materialize CSS
|
|
622
|
+
|
|
623
|
+
Using `renderStyles(styles, '.t1')` (single class prefix; `renderStylesPipeline` doubles the class for specificity when a class name is supplied):
|
|
624
|
+
|
|
625
|
+
```css
|
|
626
|
+
.t1[data-hovered] {
|
|
627
|
+
color: var(--highlight-color);
|
|
628
|
+
--current-color: var(--highlight-color);
|
|
629
|
+
--current-color-oklch: var(--highlight-color-oklch);
|
|
630
|
+
}
|
|
631
|
+
@media (prefers-color-scheme: dark) {
|
|
632
|
+
.t1:not([data-hovered]) {
|
|
633
|
+
color: var(--dark-color);
|
|
634
|
+
--current-color: var(--dark-color);
|
|
635
|
+
--current-color-oklch: var(--dark-color-oklch);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
@media (not (prefers-color-scheme: dark)) {
|
|
639
|
+
.t1:not([data-hovered]) {
|
|
640
|
+
color: var(--white-color);
|
|
641
|
+
--current-color: var(--white-color);
|
|
642
|
+
--current-color-oklch: var(--white-color-oklch);
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
```
|
|
646
|
+
|
|
647
|
+
---
|
|
648
|
+
|
|
649
|
+
## Key Design Decisions
|
|
650
|
+
|
|
651
|
+
### 1. Exclusive Conditions Over CSS Specificity
|
|
652
|
+
|
|
653
|
+
Rather than relying on CSS cascade rules, we generate mutually exclusive selectors. This makes styling predictable and debuggable.
|
|
654
|
+
|
|
655
|
+
### 2. OR Handling in Three Layers
|
|
656
|
+
|
|
657
|
+
Boolean OR appears in three different shapes during the pipeline, and each is handled where it's cheapest to get right:
|
|
658
|
+
|
|
659
|
+
1. **User-authored ORs in state keys** (Stage 2a, `expandOrConditions`): A user-authored condition like `'compact | @media(w < 768px)'` is split into multiple exclusive entries **before** exclusive building so the negation cascade doesn't have to reason about nested ORs.
|
|
660
|
+
|
|
661
|
+
2. **De Morgan ORs from negation** (Stage 3, `expandExclusiveOrs`): When `buildExclusiveConditions` negates a higher-priority compound like `A & B`, the result is `!A | !B`. If branches involve at-rules, they're split with `sortOrBranchesForExpansion` so at-rule context is preserved per branch.
|
|
662
|
+
|
|
663
|
+
3. **Pure selector ORs** (materialization): ORs that only mention modifiers/pseudos are kept intact until the `conditionToCSS` layer, where they're merged into `:is()` / `:not()` groups or emitted as comma-separated selectors. There's no gain from expanding these earlier — CSS already has compact syntax for selector-only disjunction.
|
|
664
|
+
|
|
665
|
+
Ultimately every emitted CSS rule corresponds to one conjunctive clause (DNF), produced by whichever of the three paths handled the OR.
|
|
666
|
+
|
|
667
|
+
### 3. Early Contradiction Detection
|
|
668
|
+
|
|
669
|
+
Impossible combinations are detected at multiple levels (simplification, variant merging) to avoid generating invalid CSS.
|
|
670
|
+
|
|
671
|
+
### 4. Aggressive Caching
|
|
672
|
+
|
|
673
|
+
Parse, simplify, condition-to-CSS, and full-pipeline results are cached independently, enabling fast re-rendering when only parts of the style object change.
|