@hozo/engine 0.3.0 → 0.5.0

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.
Files changed (179) hide show
  1. package/CHANGELOG.md +83 -0
  2. package/README.md +8 -4
  3. package/dist/authored-edges.d.ts +24 -0
  4. package/dist/authored-edges.d.ts.map +1 -0
  5. package/dist/authored-edges.js +73 -0
  6. package/dist/authored-edges.js.map +1 -0
  7. package/dist/authored-gap.d.ts +11 -0
  8. package/dist/authored-gap.d.ts.map +1 -0
  9. package/dist/authored-gap.js +45 -0
  10. package/dist/authored-gap.js.map +1 -0
  11. package/dist/class-name.native.d.ts +6 -0
  12. package/dist/class-name.native.d.ts.map +1 -1
  13. package/dist/class-name.native.js.map +1 -1
  14. package/dist/class-resolver.d.ts +77 -4
  15. package/dist/class-resolver.d.ts.map +1 -1
  16. package/dist/class-resolver.js +461 -34
  17. package/dist/class-resolver.js.map +1 -1
  18. package/dist/class-transition.d.ts +32 -0
  19. package/dist/class-transition.d.ts.map +1 -0
  20. package/dist/class-transition.js +48 -0
  21. package/dist/class-transition.js.map +1 -0
  22. package/dist/color-transition.d.ts +2 -0
  23. package/dist/color-transition.d.ts.map +1 -1
  24. package/dist/color-transition.js +4 -0
  25. package/dist/color-transition.js.map +1 -1
  26. package/dist/cx.d.ts +4 -0
  27. package/dist/cx.d.ts.map +1 -0
  28. package/dist/cx.js +17 -0
  29. package/dist/cx.js.map +1 -0
  30. package/dist/dom-style.d.ts +1 -0
  31. package/dist/dom-style.d.ts.map +1 -1
  32. package/dist/dom-style.js +4 -0
  33. package/dist/dom-style.js.map +1 -1
  34. package/dist/filter-slots.d.ts +11 -0
  35. package/dist/filter-slots.d.ts.map +1 -0
  36. package/dist/filter-slots.js +57 -0
  37. package/dist/filter-slots.js.map +1 -0
  38. package/dist/gap-styles.d.ts +3 -0
  39. package/dist/gap-styles.d.ts.map +1 -0
  40. package/dist/gap-styles.js +7 -0
  41. package/dist/gap-styles.js.map +1 -0
  42. package/dist/generated/animated.d.ts +3 -0
  43. package/dist/generated/animated.d.ts.map +1 -0
  44. package/dist/generated/animated.js +7 -0
  45. package/dist/generated/animated.js.map +1 -0
  46. package/dist/generated/authored-edges.d.ts +2 -0
  47. package/dist/generated/authored-edges.d.ts.map +1 -0
  48. package/dist/generated/authored-edges.js +3 -0
  49. package/dist/generated/authored-edges.js.map +1 -0
  50. package/dist/generated/class-resolver.d.ts +1 -1
  51. package/dist/generated/class-resolver.d.ts.map +1 -1
  52. package/dist/generated/class-resolver.js +1 -1
  53. package/dist/generated/class-resolver.js.map +1 -1
  54. package/dist/generated/filter-slots.d.ts +2 -0
  55. package/dist/generated/filter-slots.d.ts.map +1 -0
  56. package/dist/generated/filter-slots.js +3 -0
  57. package/dist/generated/filter-slots.js.map +1 -0
  58. package/dist/generated/gap-styles.d.ts +3 -0
  59. package/dist/generated/gap-styles.d.ts.map +1 -0
  60. package/dist/generated/gap-styles.js +4 -0
  61. package/dist/generated/gap-styles.js.map +1 -0
  62. package/dist/generated/gradient-slots.d.ts +2 -0
  63. package/dist/generated/gradient-slots.d.ts.map +1 -0
  64. package/dist/generated/gradient-slots.js +3 -0
  65. package/dist/generated/gradient-slots.js.map +1 -0
  66. package/dist/generated/safe-area.d.ts +1 -1
  67. package/dist/generated/safe-area.d.ts.map +1 -1
  68. package/dist/generated/safe-area.js +1 -1
  69. package/dist/generated/safe-area.js.map +1 -1
  70. package/dist/generated/shadow-slots.d.ts +2 -0
  71. package/dist/generated/shadow-slots.d.ts.map +1 -0
  72. package/dist/generated/shadow-slots.js +3 -0
  73. package/dist/generated/shadow-slots.js.map +1 -0
  74. package/dist/generated/spread-styles.d.ts +2 -0
  75. package/dist/generated/spread-styles.d.ts.map +1 -0
  76. package/dist/generated/spread-styles.js +3 -0
  77. package/dist/generated/spread-styles.js.map +1 -0
  78. package/dist/generated/text-inheritance-protocol.d.ts +3 -0
  79. package/dist/generated/text-inheritance-protocol.d.ts.map +1 -0
  80. package/dist/generated/text-inheritance-protocol.js +3 -0
  81. package/dist/generated/text-inheritance-protocol.js.map +1 -0
  82. package/dist/generated/text-size-context.d.ts +2 -0
  83. package/dist/generated/text-size-context.d.ts.map +1 -0
  84. package/dist/generated/text-size-context.js +3 -0
  85. package/dist/generated/text-size-context.js.map +1 -0
  86. package/dist/gradient-slots.d.ts +19 -0
  87. package/dist/gradient-slots.d.ts.map +1 -0
  88. package/dist/gradient-slots.js +61 -0
  89. package/dist/gradient-slots.js.map +1 -0
  90. package/dist/hooks.native.js +1 -1
  91. package/dist/index.d.ts +6 -3
  92. package/dist/index.d.ts.map +1 -1
  93. package/dist/index.js +6 -3
  94. package/dist/index.js.map +1 -1
  95. package/dist/inline-edges.d.ts +8 -0
  96. package/dist/inline-edges.d.ts.map +1 -0
  97. package/dist/inline-edges.js +13 -0
  98. package/dist/inline-edges.js.map +1 -0
  99. package/dist/presence-context.d.ts +15 -0
  100. package/dist/presence-context.d.ts.map +1 -0
  101. package/dist/presence-context.js +11 -0
  102. package/dist/presence-context.js.map +1 -0
  103. package/dist/project.d.ts +8 -0
  104. package/dist/project.d.ts.map +1 -1
  105. package/dist/project.js +7 -0
  106. package/dist/project.js.map +1 -1
  107. package/dist/project.native.d.ts +1 -1
  108. package/dist/project.native.d.ts.map +1 -1
  109. package/dist/project.native.js +1 -1
  110. package/dist/project.native.js.map +1 -1
  111. package/dist/responder.d.ts +34 -0
  112. package/dist/responder.d.ts.map +1 -1
  113. package/dist/responder.js +52 -0
  114. package/dist/responder.js.map +1 -1
  115. package/dist/runtime-transition.native.d.ts +37 -0
  116. package/dist/runtime-transition.native.d.ts.map +1 -0
  117. package/dist/runtime-transition.native.js +252 -0
  118. package/dist/runtime-transition.native.js.map +1 -0
  119. package/dist/safe-area.native.d.ts +12 -0
  120. package/dist/safe-area.native.d.ts.map +1 -1
  121. package/dist/safe-area.native.js +44 -21
  122. package/dist/safe-area.native.js.map +1 -1
  123. package/dist/shadow-slots.d.ts +26 -0
  124. package/dist/shadow-slots.d.ts.map +1 -0
  125. package/dist/shadow-slots.js +59 -0
  126. package/dist/shadow-slots.js.map +1 -0
  127. package/dist/spread-styles.d.ts +16 -0
  128. package/dist/spread-styles.d.ts.map +1 -0
  129. package/dist/spread-styles.js +57 -0
  130. package/dist/spread-styles.js.map +1 -0
  131. package/dist/text-inheritance.d.ts +30 -0
  132. package/dist/text-inheritance.d.ts.map +1 -0
  133. package/dist/text-inheritance.js +42 -0
  134. package/dist/text-inheritance.js.map +1 -0
  135. package/dist/text-style.d.ts +8 -0
  136. package/dist/text-style.d.ts.map +1 -0
  137. package/dist/text-style.js +34 -0
  138. package/dist/text-style.js.map +1 -0
  139. package/dist/transform-slots.d.ts +1 -1
  140. package/dist/transform-slots.d.ts.map +1 -1
  141. package/dist/transform-slots.js +12 -5
  142. package/dist/transform-slots.js.map +1 -1
  143. package/package.json +38 -2
  144. package/src/authored-edges.ts +94 -0
  145. package/src/authored-gap.ts +41 -0
  146. package/src/class-name.native.ts +10 -0
  147. package/src/class-resolver.ts +574 -20
  148. package/src/class-transition.ts +71 -0
  149. package/src/color-transition.ts +5 -0
  150. package/src/cx.ts +18 -0
  151. package/src/dom-style.ts +3 -0
  152. package/src/filter-slots.ts +67 -0
  153. package/src/gap-styles.ts +11 -0
  154. package/src/generated/animated.ts +15 -0
  155. package/src/generated/authored-edges.ts +2 -0
  156. package/src/generated/class-resolver.ts +8 -1
  157. package/src/generated/filter-slots.ts +2 -0
  158. package/src/generated/gap-styles.ts +3 -0
  159. package/src/generated/gradient-slots.ts +2 -0
  160. package/src/generated/safe-area.ts +5 -1
  161. package/src/generated/shadow-slots.ts +2 -0
  162. package/src/generated/spread-styles.ts +2 -0
  163. package/src/generated/text-inheritance-protocol.ts +9 -0
  164. package/src/generated/text-size-context.ts +2 -0
  165. package/src/gradient-slots.ts +73 -0
  166. package/src/hooks.native.ts +1 -1
  167. package/src/index.ts +17 -2
  168. package/src/inline-edges.ts +14 -0
  169. package/src/presence-context.ts +27 -0
  170. package/src/project.native.ts +1 -1
  171. package/src/project.ts +9 -0
  172. package/src/responder.ts +52 -0
  173. package/src/runtime-transition.native.tsx +324 -0
  174. package/src/safe-area.native.ts +59 -24
  175. package/src/shadow-slots.ts +78 -0
  176. package/src/spread-styles.ts +81 -0
  177. package/src/text-inheritance.ts +60 -0
  178. package/src/text-style.ts +35 -0
  179. package/src/transform-slots.ts +11 -3
@@ -1,12 +1,102 @@
1
+ import type { FilterSlots } from './filter-slots.ts'
2
+ import type { GradientSlots } from './gradient-slots.ts'
3
+ import { INLINE_EDGES, type InlineFamily } from './inline-edges.ts'
4
+ import type { ShadowSlots } from './shadow-slots.ts'
5
+ import type { TransformSlots } from './transform-slots.ts'
6
+
1
7
  /** A React Native style object, as the compiler emits it. */
2
8
  export type StyleObject = Record<string, unknown>
3
9
 
10
+ /**
11
+ * Compiler-owned text-size fallbacks are not explicit line heights. CSS's
12
+ * --tw-leading register survives a later font-size rule; RN has one numeric
13
+ * slot. The map marks actual generated objects, never class names or private
14
+ * style keys. Only maps containing these fallbacks install this composition.
15
+ */
16
+ export function hozoTextMetricStyles(
17
+ styles: StyleObject[],
18
+ defaults: ReadonlyMap<StyleObject, StyleObject>,
19
+ registers?: ReadonlyMap<StyleObject, StyleObject>,
20
+ ): StyleObject[] {
21
+ if (registers && styles.some((style) => registers.has(style))) {
22
+ let leading: StyleObject | undefined
23
+ let hasDefault = false
24
+ let hasExplicit = false
25
+ for (const style of styles) {
26
+ if (defaults.has(style)) hasDefault = true
27
+ else if (Object.hasOwn(style, 'lineHeight')) hasExplicit = true
28
+ else leading = registers.get(style) ?? leading
29
+ }
30
+ // Until the direct declaration and the reader have a shared assessed
31
+ // order, keep the existing explicit-leading projection. A later
32
+ // register cannot become a direct line-height just by appearing last.
33
+ if (hasDefault && !hasExplicit && leading) {
34
+ return [...styles.map((style) => defaults.get(style) ?? style), leading]
35
+ }
36
+ }
37
+ if (
38
+ !styles.some((style) => defaults.has(style)) ||
39
+ !styles.some((style) => Object.hasOwn(style, 'lineHeight') && !defaults.has(style))
40
+ )
41
+ return styles
42
+ // Default-free objects are built once by the generated factory. A state
43
+ // update selects them; it never clones a style object or inspects ranks.
44
+ return styles.map((style) => defaults.get(style) ?? style)
45
+ }
46
+
47
+ type TextMetricStyle = StyleObject | readonly TextMetricStyle[] | false | null | undefined
48
+
49
+ /**
50
+ * A compiled text owner sees both its own entries and selected caller entries.
51
+ * Keep the array order for every other field, and keep the last explicit
52
+ * leading if a later font-size fallback would cover it.
53
+ */
54
+ export function hozoMixedTextMetricStyles(
55
+ styles: readonly TextMetricStyle[],
56
+ ownDefaults: readonly StyleObject[],
57
+ candidateDefaults?: ReadonlySet<StyleObject> | ReadonlyMap<StyleObject, StyleObject>,
58
+ candidateRegisters?: ReadonlyMap<StyleObject, StyleObject>,
59
+ ownRegisters?: readonly (readonly [StyleObject, number])[],
60
+ ): readonly TextMetricStyle[] {
61
+ let explicit: StyleObject | undefined
62
+ let endsWithDefault = false
63
+ let hasDefault = false
64
+ let register: number | undefined
65
+ function visit(value: TextMetricStyle): void {
66
+ if (!value || typeof value !== 'object') return
67
+ if (Array.isArray(value)) {
68
+ for (const part of value) visit(part)
69
+ return
70
+ }
71
+ const style = value as StyleObject
72
+ const captured =
73
+ ownRegisters?.find(([entry]) => entry === style)?.[1] ??
74
+ (candidateRegisters?.get(style)?.lineHeight as number | undefined)
75
+ if (captured !== undefined) register = captured
76
+ if (!Object.hasOwn(style, 'lineHeight')) return
77
+ endsWithDefault = ownDefaults.includes(style) || candidateDefaults?.has(style) === true
78
+ hasDefault ||= endsWithDefault
79
+ if (!endsWithDefault) {
80
+ explicit = style
81
+ }
82
+ }
83
+ for (const style of styles) visit(style)
84
+ if (hasDefault && !explicit && register !== undefined)
85
+ return [...styles, { lineHeight: register }]
86
+ return endsWithDefault && explicit ? [...styles, { lineHeight: explicit.lineHeight }] : styles
87
+ }
88
+
4
89
  /**
5
90
  * The state an element is in when its runtime classes are resolved: what
6
91
  * its pressable reports (`pressed`, `hovered`, `focused`, `focusVisible`)
7
92
  * and what its props say: `disabled`, and `aria-checked`, `aria-selected` and
8
93
  * the rest under their attribute names, so `aria-pressed` cannot be read as
9
- * the pointer's `pressed`.
94
+ * the pointer's `pressed`. Plain data-presence conditions use `data-foo`
95
+ * booleans supplied by the actual owner: true means the attribute is present,
96
+ * false means absent, and a missing key is unassessed (and diagnosed).
97
+ * Simple equality predicates use `data-foo=value` booleans, with case-sensitive
98
+ * safe token values normalized at build time. Presence cannot satisfy equality.
99
+ * These are normalized facts, not raw HTML attribute values or selectors.
10
100
  */
11
101
  export type ClassState = Readonly<Record<string, unknown>>
12
102
 
@@ -14,8 +104,12 @@ export type ClassState = Readonly<Record<string, unknown>>
14
104
  * A class that applies only in a state: the state keys that all have to
15
105
  * hold, and the style it applies then. `active:opacity-80` is
16
106
  * `[['pressed'], { opacity: 0.8 }]`.
107
+ * Generated Tailwind owner states may append a cached cascade rank; legacy
108
+ * two-entry groups and mixed/unranked conditional domains keep their order.
109
+ * `!aria-…` is compiler-owned negative-predicate data. It still requires the
110
+ * underlying owner fact; an absent fact is not a measured false value.
17
111
  */
18
- export type StateGroup = readonly [readonly string[], StyleObject]
112
+ export type StateGroup = readonly [readonly string[], StyleObject, number?]
19
113
 
20
114
  /**
21
115
  * Resolves whitespace-separated class names to React Native style objects.
@@ -28,16 +122,207 @@ export type StateGroup = readonly [readonly string[], StyleObject]
28
122
  * in a state is reported as it always was; with it, that class applies
29
123
  * when its state holds, after the unconditional ones.
30
124
  */
31
- export type ClassResolver = (value: unknown, state?: ClassState) => StyleObject[]
125
+ type Composer = (styles: StyleObject[], deferred?: number) => StyleObject[]
126
+
127
+ export type ClassResolver = ((
128
+ value: unknown,
129
+ state?: ClassState,
130
+ deferred?: number,
131
+ ) => StyleObject[]) & {
132
+ /** @internal Compiler-generated identities for mixed effect boundaries. */
133
+ shadowSlots?: ReadonlyMap<StyleObject, ShadowSlots>
134
+ gradientSlots?: ReadonlyMap<StyleObject, GradientSlots>
135
+ transformSlots?: ReadonlyMap<StyleObject, TransformSlots>
136
+ filterSlots?: ReadonlyMap<StyleObject, FilterSlots>
137
+ /** @internal Identity of implicit leading, including on maps without explicit leading. */
138
+ fontDefaults?: ReadonlySet<StyleObject> | ReadonlyMap<StyleObject, StyleObject>
139
+ /** @internal Captured register writes keyed by original generated style. */
140
+ leadingRegisters?: ReadonlyMap<StyleObject, StyleObject>
141
+ /** @internal Bit mask of this namespace's installed deferrable composers. */
142
+ effectMask?: number
143
+ /**
144
+ * @internal Development only. Warn once about each name absent from the map,
145
+ * unless `markers` declares it: an exact name, or a prefix ending in `*`.
146
+ * The bundler calls it behind `__DEV__`, so a release build never does.
147
+ */
148
+ warnUnknownClasses?: (markers: readonly string[]) => void
149
+ }
150
+
151
+ /** @internal Generated-data ABI, not a utility parser or a public cascade API. */
152
+ export interface ClassResolverPlan {
153
+ schema: 1
154
+ styles: Record<string, StyleObject>
155
+ unsupported?: Record<string, string>
156
+ states?: Record<string, readonly StateGroup[]>
157
+ ambient?: ClassState
158
+ compose?: Composer
159
+ /** Ordinals from one complete compiler namespace. Missing means barrier. */
160
+ ordering: {
161
+ schema: 1
162
+ ranks: Readonly<Record<string, readonly number[]>>
163
+ /** Compiler-proven disjoint sets: unranked colours and admitted ranked scalars. */
164
+ independentColors?: readonly [barriers: string, independent: string]
165
+ /** Lists disjoint colour writers. Older schema-1 maps may send a redundant true. */
166
+ rankedStates?: true | string
167
+ }
168
+ }
169
+
170
+ interface TemplateEntry {
171
+ keys: readonly string[]
172
+ style: StyleObject
173
+ name: string
174
+ ownerRank: number | undefined
175
+ }
176
+
177
+ interface RankedEntry extends TemplateEntry {
178
+ rank: number | undefined
179
+ }
180
+
181
+ /**
182
+ * The families whose physical writes these styles need as logical ones: one
183
+ * style writes the two physical sides unevenly (only one, or two values),
184
+ * which the RTL swap would move (#2050), or, when `mixed` counts, the
185
+ * styles write the family both logically and physically (#2018).
186
+ */
187
+ function inlineEdgesToProject(styles: Iterable<StyleObject>, mixed: boolean): InlineFamily[] {
188
+ let logical = 0
189
+ let physical = 0
190
+ let uneven = 0
191
+ for (const style of styles) {
192
+ INLINE_EDGES.forEach(([start, end, left, right], family) => {
193
+ if (Object.hasOwn(style, start) || Object.hasOwn(style, end)) logical |= 1 << family
194
+ const hasLeft = Object.hasOwn(style, left)
195
+ const hasRight = Object.hasOwn(style, right)
196
+ if (hasLeft || hasRight) physical |= 1 << family
197
+ if (hasLeft !== hasRight || style[left] !== style[right]) uneven |= 1 << family
198
+ })
199
+ }
200
+ const project = uneven | (mixed ? logical & physical : 0)
201
+ return INLINE_EDGES.filter((_, family) => project & (1 << family))
202
+ }
203
+
204
+ /**
205
+ * Tailwind lets the later of `px-8 pl-1` win; React Native does not. Without
206
+ * `swapLeftAndRightInRTL` Yoga prefers `paddingStart` to `paddingLeft` in any
207
+ * order, and with it -- the default -- RTL turns `paddingLeft` into
208
+ * `paddingStart`, overwriting it, so the physical write wins on the other
209
+ * side. Start and end mean the same under both, so in a family the class
210
+ * string writes both ways, physical writes are renamed to the logical side
211
+ * they are in this direction, and array order decides as the cascade did
212
+ * (#2018). The family is chosen per class string, states included, as the
213
+ * static compiler chooses it per element: `pl-1` must not change sides when
214
+ * `hover:ps-8` beside it stops applying. A physical utility on its own
215
+ * keeps its side too (`pl-4` stays on the left in RTL, as in Tailwind), so a
216
+ * family one style writes unevenly is renamed the same way.
217
+ */
218
+ function projectInlineEdges(
219
+ styles: StyleObject[],
220
+ mixed: readonly InlineFamily[] | undefined,
221
+ rtl: boolean,
222
+ ): StyleObject[] {
223
+ if (!mixed?.length) return styles
224
+ return styles.map((style) => {
225
+ let projected: StyleObject | undefined
226
+ for (const [start, end, left, right] of mixed) {
227
+ for (const [physical, logical] of [
228
+ [left, rtl ? end : start],
229
+ [right, rtl ? start : end],
230
+ ] as const) {
231
+ if (!Object.hasOwn(style, physical)) continue
232
+ projected ??= { ...style }
233
+ const value = projected[physical]
234
+ delete projected[physical]
235
+ projected[logical] = value
236
+ }
237
+ }
238
+ return projected ?? style
239
+ })
240
+ }
241
+
242
+ /** An assessed run may not move across a Native specificity boundary or
243
+ * unknown write. The compiler can separately prove that specific holes do
244
+ * not interact with the whole tier. */
245
+ function orderRankedRuns(
246
+ entries: RankedEntry[],
247
+ sharedTier: boolean,
248
+ disjointHoles: boolean,
249
+ start = 0,
250
+ limit = entries.length,
251
+ ) {
252
+ while (start < limit - 1) {
253
+ let end = start
254
+ let previous = -1
255
+ let inverted = false
256
+ const specificity = entries[start]!.ownerRank
257
+ for (; end < limit; end++) {
258
+ const { rank, ownerRank } = entries[end]!
259
+ if (!sharedTier && ownerRank !== specificity) break
260
+ if (rank === undefined) {
261
+ if (!disjointHoles) break
262
+ continue
263
+ }
264
+ inverted ||= previous > rank
265
+ previous = rank
266
+ }
267
+ if (inverted && end === start + 2) {
268
+ // Both entries had ranks, otherwise no inversion was possible.
269
+ const first = entries[start]!
270
+ entries[start] = entries[start + 1]!
271
+ entries[start + 1] = first
272
+ } else if (inverted) {
273
+ const run = entries.slice(start, end)
274
+ const ranked = disjointHoles ? run.filter((entry) => entry.rank !== undefined) : run
275
+ ranked.sort((a, b) => a.rank! - b.rank!)
276
+ let next = 0
277
+ for (let index = start; index < end; index++)
278
+ if (entries[index]!.rank !== undefined) entries[index] = ranked[next++]!
279
+ }
280
+ // A different specificity starts a new run; an unranked write pins
281
+ // its own position and can be skipped.
282
+ start = end + (end < limit && entries[end]!.rank === undefined ? 1 : 0)
283
+ }
284
+ }
285
+
286
+ function orderRestingRuns(
287
+ entries: RankedEntry[],
288
+ barriers: ReadonlySet<string> | undefined,
289
+ independent: ReadonlySet<string> | undefined,
290
+ disjoint: boolean,
291
+ ) {
292
+ if (disjoint || !independent || !barriers) {
293
+ orderRankedRuns(entries, true, disjoint)
294
+ return
295
+ }
296
+ // The full input did not qualify, but the same compiler proof can
297
+ // admit an island bounded by an unrelated or overlapping write.
298
+ let start = 0
299
+ let hasColour = false
300
+ for (let end = 0; end <= entries.length; end++) {
301
+ const entry = entries[end]
302
+ if (entry && (entry.rank === undefined ? barriers : independent).has(entry.name)) {
303
+ hasColour ||= entry.rank === undefined
304
+ continue
305
+ }
306
+ if (hasColour) orderRankedRuns(entries, true, true, start, end)
307
+ start = end + 1
308
+ hasColour = false
309
+ }
310
+ orderRankedRuns(entries, true, false)
311
+ }
32
312
 
33
313
  interface Resolved {
34
314
  /** The unconditional styles, composed: what a string with no state classes returns. */
35
315
  base: StyleObject[]
36
316
  /** The same, before `compose`, for composing again with state styles. */
37
317
  raw: StyleObject[]
38
- groups: StateGroup[]
318
+ /** Inline families this string writes both ways, states included (#2018). */
319
+ edges?: readonly InlineFamily[]
320
+ /** Only materialized for a mixed compiler boundary, using the same selection. */
321
+ deferredBases?: (StyleObject[] | undefined)[]
39
322
  /** State classes in this string, named if it is resolved without a state. */
40
323
  stateClasses: string[]
324
+ /** Cached in final order. Fallback orders all resting writes before it. */
325
+ template?: { entries: TemplateEntry[]; appendToBase: boolean }
41
326
  }
42
327
 
43
328
  /**
@@ -58,55 +343,286 @@ interface Resolved {
58
343
  * state (#886). Those need no tracking here: the pressable that renders
59
344
  * the element already tracks it and hands it in, so this stays a function
60
345
  * of the class string and that state.
346
+ *
347
+ * `ambient` holds facts that cannot change while the app runs, so no
348
+ * element has to supply them: `rtl` and `ltr`, because React Native needs
349
+ * a reload for a direction change to take effect. A class conditioned on
350
+ * them alone is decided once, when its string is first resolved, and then
351
+ * cached like an unconditional one -- after the unconditional ones, which is
352
+ * where Tailwind puts a variant too.
61
353
  */
354
+ export function createClassResolver(plan: ClassResolverPlan): ClassResolver
62
355
  export function createClassResolver(
63
356
  styles: Record<string, StyleObject>,
357
+ unsupported?: Record<string, string>,
358
+ compose?: Composer,
359
+ states?: Record<string, readonly StateGroup[]>,
360
+ ambient?: ClassState,
361
+ ): ClassResolver
362
+ export function createClassResolver(
363
+ stylesOrPlan: Record<string, StyleObject> | ClassResolverPlan,
64
364
  unsupported: Record<string, string> = {},
65
- compose?: (styles: StyleObject[]) => StyleObject[],
365
+ compose?: Composer,
66
366
  states: Record<string, readonly StateGroup[]> = {},
367
+ ambient: ClassState = {},
67
368
  ): ClassResolver {
369
+ let styles: Record<string, StyleObject>
370
+ let ranks: ClassResolverPlan['ordering']['ranks'] | undefined
371
+ let independentColors: ClassResolverPlan['ordering']['independentColors']
372
+ let stateColors: Set<string> | undefined
373
+ if (typeof stylesOrPlan.schema === 'number') {
374
+ const plan = stylesOrPlan as ClassResolverPlan
375
+ if (
376
+ plan.schema !== 1 ||
377
+ plan.ordering?.schema !== 1 ||
378
+ !plan.ordering.ranks ||
379
+ typeof plan.ordering.ranks !== 'object'
380
+ )
381
+ throw new Error('Unsupported Hozo class plan schema')
382
+ styles = plan.styles
383
+ unsupported = plan.unsupported ?? {}
384
+ compose = plan.compose
385
+ states = plan.states ?? {}
386
+ ambient = plan.ambient ?? {}
387
+ ranks = plan.ordering.ranks
388
+ independentColors = plan.ordering.independentColors
389
+ if (typeof plan.ordering.rankedStates === 'string')
390
+ stateColors = new Set(plan.ordering.rankedStates.split(' '))
391
+ } else styles = stylesOrPlan as Record<string, StyleObject>
392
+ // Only mixed namespaces carrying the build-time proof allocate these.
393
+ const barriers = independentColors && new Set(independentColors[0].split(' '))
394
+ const independent = independentColors && new Set(independentColors[1].split(' '))
395
+ const isAmbient = (key: string) => Object.hasOwn(ambient, key)
396
+ // Checked once per map: most never write an inline family both ways or
397
+ // unevenly, and then no class string can, so none is scanned. A mixed
398
+ // family counts only in a ranked map: an unranked one keeps its written
399
+ // order and Yoga's own precedence.
400
+ const mixed = ranks !== undefined
401
+ const edges =
402
+ inlineEdgesToProject(
403
+ [
404
+ ...Object.values(styles),
405
+ ...Object.values(states).flatMap((groups) => groups.map((group) => group[1])),
406
+ ],
407
+ mixed,
408
+ ).length > 0
409
+ const rtl = Boolean(ambient.rtl)
410
+ const stringEdges = (entries: readonly { style: StyleObject }[], raw: readonly StyleObject[]) =>
411
+ edges
412
+ ? inlineEdgesToProject([...raw, ...entries.map((entry) => entry.style)], mixed)
413
+ : undefined
414
+ const project = (list: StyleObject[], resolved: { edges?: readonly InlineFamily[] }) =>
415
+ projectInlineEdges(list, resolved.edges, rtl)
68
416
  // Class strings repeat constantly across renders and list items, and the
69
417
  // result is immutable, so each distinct string is resolved once.
70
418
  const cache = new Map<string, Resolved>()
71
419
  // Warn once per class, not once per render -- a warning inside a list's
72
420
  // render path would otherwise repeat for every row, every frame.
73
421
  const warned = new Set<string>()
422
+ // Set only by `warnUnknownClasses`, which only development builds call.
423
+ let unknownIsMissing: ((name: string) => boolean) | undefined
74
424
  const warn = (name: string, reason: string) => {
75
425
  if (warned.has(name)) return
76
426
  warned.add(name)
77
427
  // eslint-disable-next-line no-console
78
428
  console.warn(`[hozo] ${reason}`)
79
429
  }
430
+ // Absent is not false. A focused field must not silently claim that it
431
+ // measured hover/press, or that a spread supplied a disabled driver.
432
+ const supplied = (keys: readonly string[], name: string, facts: ClassState) => {
433
+ if (!keys.every((key) => Object.hasOwn(facts, key))) {
434
+ if (!warned.has(name)) {
435
+ const missing = keys.filter((key) => !Object.hasOwn(facts, key))
436
+ warn(name, `\`${name}\` needs state (${missing.join(', ')}) this element does not supply.`)
437
+ }
438
+ return false
439
+ }
440
+ return true
441
+ }
442
+ const positive = (keys: readonly string[], name: string, facts: ClassState) =>
443
+ supplied(keys, name, facts) && keys.every((key) => Boolean(facts[key]))
444
+ // Prepare only negative maps' exceptional groups once, not during state
445
+ // updates or for every authored class lookup. Positive-only maps retain
446
+ // their existing boolean matcher and allocate no predicate side table.
447
+ let negative: Map<readonly string[], { keys: string[]; expected: boolean[] }> | undefined
448
+ for (const groups of Object.values(states))
449
+ for (const [keys] of groups)
450
+ if (keys.some((key) => key.startsWith('!aria-')))
451
+ (negative ??= new Map()).set(keys, {
452
+ keys: keys.map((key) => (key.startsWith('!aria-') ? key.slice(1) : key)),
453
+ expected: keys.map((key) => !key.startsWith('!aria-')),
454
+ })
455
+ const negativePredicates = negative
456
+ const matches = negativePredicates
457
+ ? (keys: readonly string[], name: string, facts: ClassState) => {
458
+ const predicate = negativePredicates.get(keys)
459
+ return predicate
460
+ ? supplied(predicate.keys, name, facts) &&
461
+ predicate.keys.every(
462
+ (key, index) => Boolean(facts[key]) === predicate.expected[index],
463
+ )
464
+ : positive(keys, name, facts)
465
+ }
466
+ : positive
80
467
 
81
468
  function resolve(value: string): Resolved {
82
469
  const base: StyleObject[] = []
83
- const groups: StateGroup[] = []
470
+ const resting: RankedEntry[] | undefined = ranks ? [] : undefined
471
+ const groups: RankedEntry[] = []
84
472
  const stateClasses: string[] = []
473
+ const decided: RankedEntry[] = []
474
+ // A compact plan orders all writes together; an unassessed object
475
+ // without a disjointness proof keeps the existing selection tiers.
476
+ let ranked: RankedEntry[] | undefined = ranks ? [] : undefined
477
+ let canKeepRanks = independent !== undefined
478
+ let restingHolesDisjoint = canKeepRanks
479
+ let hasUnrankedState = false
480
+ let colors: TemplateEntry[] | undefined
481
+ const rankFor = (name: string, index: number) => {
482
+ const rank = ranks?.[name]?.[index]
483
+ if (rank !== undefined && (!Number.isSafeInteger(rank) || rank < 0))
484
+ throw new Error('Invalid Hozo class plan rank')
485
+ return rank
486
+ }
487
+ const take = ranks
488
+ ? (entry: RankedEntry) => {
489
+ if (!ranked) return
490
+ if (entry.rank === undefined) {
491
+ if (canKeepRanks && barriers?.has(entry.name)) {
492
+ // Their colours still compete in fallback order. The ranked
493
+ // entries are only independent of these writes, not of one another.
494
+ ;(colors ??= []).push(entry)
495
+ } else ranked = undefined
496
+ return
497
+ }
498
+ if (canKeepRanks && !independent?.has(entry.name)) canKeepRanks = false
499
+ ranked.push(entry)
500
+ }
501
+ : undefined
85
502
  for (const name of value.split(/\s+/)) {
86
503
  if (name === '') {
87
504
  continue
88
505
  }
89
506
  const style = styles[name]
90
507
  if (style) {
91
- base.push(style)
508
+ if (resting) {
509
+ const rank = rankFor(name, 0)
510
+ const entry = { name, keys: [], style, ownerRank: undefined, rank }
511
+ resting.push(entry)
512
+ if (restingHolesDisjoint && !(rank === undefined ? barriers : independent)?.has(name))
513
+ restingHolesDisjoint = false
514
+ take?.(entry)
515
+ } else base.push(style)
92
516
  continue
93
517
  }
94
518
  const conditional = states[name]
95
519
  if (conditional) {
96
- groups.push(...conditional)
97
- stateClasses.push(name)
520
+ let needsState = false
521
+ for (let index = 0; index < conditional.length; index++) {
522
+ const group = conditional[index]!
523
+ const rank = rankFor(name, index)
524
+ const entry = {
525
+ name,
526
+ keys: group[0],
527
+ style: group[1],
528
+ ownerRank: group[2],
529
+ rank,
530
+ }
531
+ take?.(entry)
532
+ if ((negativePredicates?.get(entry.keys)?.keys ?? entry.keys).every(isAmbient)) {
533
+ if (matches(entry.keys, name, ambient)) decided.push(entry)
534
+ continue
535
+ }
536
+ groups.push(entry)
537
+ if (rank === undefined) {
538
+ hasUnrankedState = true
539
+ if (stateColors && !stateColors.has(name))
540
+ throw new Error('Invalid Hozo class plan state rank')
541
+ }
542
+ needsState = true
543
+ }
544
+ if (needsState) stateClasses.push(name)
98
545
  continue
99
546
  }
100
547
  // An unknown class isn't necessarily a Hozo problem -- it may be an
101
- // app's own non-Tailwind class, or a testID-ish marker -- so only the
102
- // ones the compiler recognized and refused are reported.
548
+ // app's own non-Tailwind class, or a testID-ish marker. So by default
549
+ // only the ones the compiler recognized and refused are reported. In
550
+ // development the bundler can switch on the rest, with the project's
551
+ // declared markers (`markerClasses`) excluded (#2073).
103
552
  const reason = unsupported[name]
104
553
  if (reason) warn(name, reason)
554
+ else if (unknownIsMissing?.(name))
555
+ warn(
556
+ name,
557
+ `\`${name}\` is not in this app's class map, so it does not apply. If it is a ` +
558
+ "Tailwind class, it was assembled at run time or written in a file the bundler's scan " +
559
+ "did not read: write it out whole in a file the project's `content` covers. If it is " +
560
+ 'not meant to style anything, list it in `markerClasses`.',
561
+ )
562
+ }
563
+ if (ranked && (!colors || canKeepRanks)) {
564
+ // Stable ties keep input order. Cache the selected entry order; hot
565
+ // state changes only test guards, never read or sort priorities.
566
+ ranked.sort((a, b) => a.rank! - b.rank!)
567
+ const ordered: TemplateEntry[] = colors ?? []
568
+ for (const entry of ranked) {
569
+ if ((negativePredicates?.get(entry.keys)?.keys ?? entry.keys).every(isAmbient)) {
570
+ if (matches(entry.keys, entry.name, ambient)) ordered.push({ ...entry, keys: [] })
571
+ } else ordered.push(entry)
572
+ }
573
+ const raw = ordered.filter((entry) => !entry.keys.length).map((entry) => entry.style)
574
+ const edges = stringEdges(ordered, [])
575
+ return {
576
+ base: projectInlineEdges(compose ? compose(raw) : raw, edges, rtl),
577
+ raw,
578
+ edges,
579
+ stateClasses,
580
+ template: groups.length ? { entries: ordered, appendToBase: false } : undefined,
581
+ }
582
+ }
583
+ // The selected tier (not the whole map) can already have exact CSS
584
+ // priorities. Use Native weights only when there is a real hole.
585
+ const sharedTier = ranks !== undefined && (!hasUnrankedState || stateColors !== undefined)
586
+ // Native can linearize known selector weights, but it must not order
587
+ // unknown weights by pretending they have zero specificity.
588
+ if (
589
+ !sharedTier &&
590
+ groups.length > 1 &&
591
+ groups.every((group) => group.ownerRank !== undefined) &&
592
+ groups.some((group, index) => index > 0 && groups[index - 1]!.ownerRank! > group.ownerRank!)
593
+ ) {
594
+ groups.sort((a, b) => a.ownerRank! - b.ownerRank!)
595
+ }
596
+ if (ranks) {
597
+ // Ambient guards have already been evaluated. They keep their
598
+ // resting tier, but it must not throw away known Tailwind order.
599
+ orderRankedRuns(decided, true, false)
600
+ orderRankedRuns(groups, sharedTier, stateColors !== undefined)
601
+ }
602
+ if (resting) {
603
+ orderRestingRuns(resting, barriers, independent, restingHolesDisjoint)
604
+ for (const { style } of resting) base.push(style)
605
+ }
606
+ for (const { style } of decided) base.push(style)
607
+ const edges = stringEdges(groups, base)
608
+ return {
609
+ base: projectInlineEdges(compose ? compose(base) : base, edges, rtl),
610
+ raw: base,
611
+ edges,
612
+ stateClasses,
613
+ template: groups.length ? { entries: groups, appendToBase: true } : undefined,
105
614
  }
106
- return { base: compose ? compose(base) : base, raw: base, groups, stateClasses }
107
615
  }
108
616
 
109
- return function hozoClasses(value: unknown, state?: ClassState): StyleObject[] {
617
+ function restingWithoutEffects(resolved: Resolved, deferred: number): StyleObject[] {
618
+ const bases = (resolved.deferredBases ??= [])
619
+ return (bases[deferred] ??= project(
620
+ compose ? compose(resolved.raw, deferred) : resolved.raw,
621
+ resolved,
622
+ ))
623
+ }
624
+
625
+ const hozoClasses: ClassResolver = (value, state, deferred) => {
110
626
  // Falsy is the normal case, not an error: `cond && 'p-4'` yields
111
627
  // `false` whenever the condition is off.
112
628
  if (typeof value !== 'string' || value === '') {
@@ -118,24 +634,62 @@ export function createClassResolver(
118
634
  resolved = resolve(value)
119
635
  cache.set(value, resolved)
120
636
  }
121
- if (resolved.groups.length === 0) return resolved.base
637
+ if (deferred) deferred &= hozoClasses.effectMask ?? 0
638
+ const template = resolved.template
639
+ if (!template) return deferred ? restingWithoutEffects(resolved, deferred) : resolved.base
122
640
  if (state === undefined) {
123
641
  for (const name of resolved.stateClasses) {
124
642
  warn(
125
643
  name,
126
644
  `\`${name}\` applies in a state, and this element hands its classes no state to ` +
127
- 'resolve it against. Put it on a Pressable, Button or Link, or write it as a static ' +
645
+ 'resolve it against. Put it on an owner that supplies this state, or write it as a static ' +
128
646
  'className so it compiles to a real style variant.',
129
647
  )
130
648
  }
131
- return resolved.base
649
+ return deferred ? restingWithoutEffects(resolved, deferred) : resolved.base
132
650
  }
133
- const active = resolved.groups.filter(([keys]) => keys.every((key) => Boolean(state[key])))
134
- if (active.length === 0) return resolved.base
135
- const all = [...resolved.raw, ...active.map(([, style]) => style)]
651
+ // A group can name an ambient fact beside the element's own, as
652
+ // `rtl:hover:` does; the element supplies only its part.
653
+ const facts = { ...ambient, ...state }
654
+ // Fully ranked writes may interleave with resting styles. The fallback
655
+ // template has only state entries, so delay copying its resting prefix
656
+ // until an actual state matches. Both routes select by the same loop.
657
+ const all: StyleObject[] = []
658
+ let active = false
659
+ for (const entry of template.entries) {
660
+ if (!entry.keys.length) all.push(entry.style)
661
+ else if (matches(entry.keys, entry.name, facts)) {
662
+ if (!active && template.appendToBase) {
663
+ for (const style of resolved.raw) all.push(style)
664
+ }
665
+ all.push(entry.style)
666
+ active = true
667
+ }
668
+ }
669
+ if (!active) return deferred ? restingWithoutEffects(resolved, deferred) : resolved.base
136
670
  // A state style can carry a transform, which `compose` folds into one;
137
671
  // so it runs over the whole list, and the result is per render rather
138
672
  // than cached -- it changes with the state.
139
- return compose ? compose(all) : all
673
+ return project(compose ? compose(all, deferred) : all, resolved)
140
674
  }
675
+ hozoClasses.warnUnknownClasses = (markers) => {
676
+ const names = new Set<string>(BUILTIN_MARKERS)
677
+ const prefixes = [...BUILTIN_MARKER_PREFIXES]
678
+ for (const marker of markers) {
679
+ if (marker.endsWith('*')) prefixes.push(marker.slice(0, -1))
680
+ else names.add(marker)
681
+ }
682
+ unknownIsMissing = (name) =>
683
+ !names.has(name) && !prefixes.some((prefix) => name.startsWith(prefix))
684
+ // A string resolved before this call would otherwise keep its silence.
685
+ cache.clear()
686
+ }
687
+ return hozoClasses
141
688
  }
689
+
690
+ /**
691
+ * Tailwind's own non-styling names: a `group` or `peer` marks an element for
692
+ * its descendants or siblings and writes no style of its own.
693
+ */
694
+ const BUILTIN_MARKERS = ['group', 'peer']
695
+ const BUILTIN_MARKER_PREFIXES = ['group/', 'peer/']