rnwind 0.0.9 → 0.0.11

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 (47) hide show
  1. package/lib/cjs/core/parser/tw-parser.cjs +126 -3
  2. package/lib/cjs/core/parser/tw-parser.cjs.map +1 -1
  3. package/lib/cjs/core/parser/tw-parser.d.ts +22 -0
  4. package/lib/cjs/core/style-builder/build-style.cjs +12 -3
  5. package/lib/cjs/core/style-builder/build-style.cjs.map +1 -1
  6. package/lib/cjs/core/style-builder/build-style.d.ts +3 -1
  7. package/lib/cjs/core/style-builder/union-builder.cjs +9 -1
  8. package/lib/cjs/core/style-builder/union-builder.cjs.map +1 -1
  9. package/lib/cjs/core/style-builder/union-builder.d.ts +7 -0
  10. package/lib/cjs/runtime/hooks/use-scheme.cjs +8 -5
  11. package/lib/cjs/runtime/hooks/use-scheme.cjs.map +1 -1
  12. package/lib/cjs/runtime/index.cjs +1 -0
  13. package/lib/cjs/runtime/index.cjs.map +1 -1
  14. package/lib/cjs/runtime/index.d.ts +1 -1
  15. package/lib/cjs/runtime/lookup-css.cjs +27 -0
  16. package/lib/cjs/runtime/lookup-css.cjs.map +1 -1
  17. package/lib/cjs/runtime/lookup-css.d.ts +18 -0
  18. package/lib/cjs/testing/index.cjs +1 -1
  19. package/lib/cjs/testing/index.cjs.map +1 -1
  20. package/lib/esm/core/parser/color.mjs +1 -1
  21. package/lib/esm/core/parser/tw-parser.d.ts +22 -0
  22. package/lib/esm/core/parser/tw-parser.mjs +107 -2
  23. package/lib/esm/core/parser/tw-parser.mjs.map +1 -1
  24. package/lib/esm/core/style-builder/build-style.d.ts +3 -1
  25. package/lib/esm/core/style-builder/build-style.mjs +12 -3
  26. package/lib/esm/core/style-builder/build-style.mjs.map +1 -1
  27. package/lib/esm/core/style-builder/union-builder.d.ts +7 -0
  28. package/lib/esm/core/style-builder/union-builder.mjs +9 -1
  29. package/lib/esm/core/style-builder/union-builder.mjs.map +1 -1
  30. package/lib/esm/runtime/hooks/use-scheme.mjs +8 -5
  31. package/lib/esm/runtime/hooks/use-scheme.mjs.map +1 -1
  32. package/lib/esm/runtime/index.d.ts +1 -1
  33. package/lib/esm/runtime/index.mjs +1 -1
  34. package/lib/esm/runtime/index.mjs.map +1 -1
  35. package/lib/esm/runtime/lookup-css.d.ts +18 -0
  36. package/lib/esm/runtime/lookup-css.mjs +26 -1
  37. package/lib/esm/runtime/lookup-css.mjs.map +1 -1
  38. package/lib/esm/testing/index.mjs +2 -2
  39. package/lib/esm/testing/index.mjs.map +1 -1
  40. package/package.json +1 -1
  41. package/src/core/parser/tw-parser.ts +118 -1
  42. package/src/core/style-builder/build-style.ts +12 -1
  43. package/src/core/style-builder/union-builder.ts +10 -1
  44. package/src/runtime/hooks/use-scheme.ts +8 -4
  45. package/src/runtime/index.ts +1 -0
  46. package/src/runtime/lookup-css.ts +28 -0
  47. package/src/testing/index.ts +3 -0
@@ -1,3 +1,4 @@
1
+ import * as tailwindNode from '@tailwindcss/node';
1
2
  import { compile } from '@tailwindcss/node';
2
3
  import { Scanner } from '@tailwindcss/oxide';
3
4
  import { formatHex } from 'culori';
@@ -9,7 +10,7 @@ import { keyframesName, keyframeSelectorOffsets, pickAnimationName } from './key
9
10
  import { serializeInitialValue } from './property.mjs';
10
11
  import { classNameFromSelector } from './selector.mjs';
11
12
  import { extractThemeVars, extractSchemeAliases, extractCustomVariantSchemes, BASE_SCHEME, compileReadyTheme } from './theme-vars.mjs';
12
- import { serializeTokens, substituteThemeVars, coerceUnparsedValue } from './tokens.mjs';
13
+ import { substituteThemeVars, serializeTokens, coerceUnparsedValue } from './tokens.mjs';
13
14
  import { normalizeColorString } from './color.mjs';
14
15
 
15
16
  /**
@@ -59,6 +60,8 @@ class TailwindParser {
59
60
  config;
60
61
  scanner;
61
62
  compiler;
63
+ /** Full resolved base theme (built-in palette + user `@theme`), colors lowered to sRGB. Source for `useColor` / `useToken`. */
64
+ baseThemeTokens = null;
62
65
  themeSchemes;
63
66
  schemeAliases;
64
67
  /**
@@ -146,6 +149,42 @@ class TailwindParser {
146
149
  merged.set(k, v);
147
150
  return merged;
148
151
  }
152
+ /**
153
+ * Build the per-scheme theme token tables for `registerThemeTokens` —
154
+ * the data source for `useColor` / `useToken` / `useSize`. Emits one table
155
+ * per scheme the user wrote tokens under (`base` + each variant block /
156
+ * `.dark` override). `--color-*` values are lowered to sRGB (matching the
157
+ * className path) using `resolver` so a wide-gamut or `var()`-referencing
158
+ * token resolves to an RN-renderable string; other tokens pass through.
159
+ * @param resolver Full compiled `:root` table, for resolving `var()` refs.
160
+ * @returns Scheme → (token name → value) map.
161
+ */
162
+ buildThemeTokens(resolver) {
163
+ const out = {};
164
+ // BASE: the full resolved theme (built-in palette + user `@theme`). The
165
+ // runtime merges this under the active scheme, so `useColor('pink-500')`
166
+ // and `useColor('<your-token>')` both resolve in every scheme.
167
+ if (this.baseThemeTokens)
168
+ out[BASE_SCHEME] = this.baseThemeTokens;
169
+ // VARIANTS: only the per-scheme overrides the user wrote (`.dark { … }` /
170
+ // `@variant dark { … }`) — they layer on top of base at runtime.
171
+ for (const scheme of this.themeSchemes.keys()) {
172
+ if (scheme === BASE_SCHEME)
173
+ continue;
174
+ const userTable = this.themeSchemes.get(scheme);
175
+ if (!userTable || userTable.size === 0)
176
+ continue;
177
+ const schemeResolver = new Map(resolver);
178
+ for (const [k, v] of this.effectiveVars(scheme))
179
+ schemeResolver.set(k, v);
180
+ const table = {};
181
+ for (const [name, raw] of userTable) {
182
+ table[name] = name.startsWith('--color-') ? lowerColorToken(raw, schemeResolver) : raw;
183
+ }
184
+ out[scheme] = table;
185
+ }
186
+ return out;
187
+ }
149
188
  /**
150
189
  * Build the Tailwind compiler on first use and cache it. The theme CSS
151
190
  * gets a `theme(inline)` modifier on its `@import 'tailwindcss'` so
@@ -165,6 +204,13 @@ class TailwindParser {
165
204
  catch (error) {
166
205
  throw wrapThemeError(error);
167
206
  }
207
+ // Load the resolved design system ONCE to capture the FULL theme — the
208
+ // built-in palette (`pink-500`, …) plus the user's `@theme` tokens — so
209
+ // `useColor` / `useToken` resolve any theme value, not just the utilities
210
+ // a class happened to use (Tailwind tree-shakes `:root`, so the compiled
211
+ // CSS alone never carries the full palette). Best-effort: a load failure
212
+ // just narrows the hooks to the user's own tokens.
213
+ this.baseThemeTokens = await loadBaseThemeTokens(ready);
168
214
  return this.compiler;
169
215
  }
170
216
  /**
@@ -337,7 +383,65 @@ class TailwindParser {
337
383
  if (!referencedKeyframes.has(name))
338
384
  keyframes.delete(name);
339
385
  }
340
- return { atoms, keyframes, propertyDefaults, gradientAtoms, hapticAtoms, candidates: [...candidates], schemes, breakpoints };
386
+ const themeTokens = this.buildThemeTokens(compiledTheme);
387
+ return { atoms, keyframes, propertyDefaults, gradientAtoms, hapticAtoms, candidates: [...candidates], schemes, breakpoints, themeTokens };
388
+ }
389
+ }
390
+ /**
391
+ * Lower a `--color-*` token value to an RN-renderable sRGB string, matching
392
+ * the className path: resolve any `var()` ref via `resolver`, then lower a
393
+ * wide-gamut form (`oklch(…)`, `lab(…)`, `color(p3 …)`) to sRGB. Hex / rgb /
394
+ * named colors pass through unchanged.
395
+ * @param raw Raw token value.
396
+ * @param resolver Var name → value table for resolving `var()` references.
397
+ * @returns RN-safe color string.
398
+ */
399
+ function lowerColorToken(raw, resolver) {
400
+ const substituted = substituteThemeVars(raw, resolver);
401
+ return normalizeColorString(substituted) ?? substituted;
402
+ }
403
+ /** Theme token families excluded from the registered base table — pure Tailwind internals with no `useColor`/`useToken` value. */
404
+ const INTERNAL_TOKEN_PREFIXES = ['--tw-', '--default-'];
405
+ /**
406
+ * `@tailwindcss/node`'s `__unstable__loadDesignSystem` — exists at runtime but
407
+ * isn't in the package's published types, so it's accessed through a narrowed
408
+ * cast rather than a named import. Returns `undefined` when the (unstable) API
409
+ * isn't present, so {@link loadBaseThemeTokens} degrades gracefully.
410
+ */
411
+ const loadDesignSystem = tailwindNode.__unstable__loadDesignSystem;
412
+ /**
413
+ * Load the FULL resolved Tailwind theme (built-in palette + the user's
414
+ * `@theme`) via the design-system API and flatten it to an RN-safe token
415
+ * table — `--color-*` values lowered to sRGB, everything else passed through.
416
+ * This is what lets `useColor` / `useToken` resolve ANY theme token, including
417
+ * built-ins a class never used (Tailwind tree-shakes the compiled `:root`, so
418
+ * the compiled CSS alone can't supply the full palette). Internal `--tw-*` /
419
+ * `--default-*` families are dropped. Returns `null` on any failure so the
420
+ * caller degrades to the user's own `@theme` tokens.
421
+ * @param themeCss Compile-ready theme CSS (variants stripped, custom-variants added).
422
+ * @returns Flattened base token table, or null.
423
+ */
424
+ async function loadBaseThemeTokens(themeCss) {
425
+ if (typeof loadDesignSystem !== 'function')
426
+ return null;
427
+ try {
428
+ const design = await loadDesignSystem(themeCss, { base: process.cwd() });
429
+ const entries = design.theme?.entries?.();
430
+ if (!entries)
431
+ return null;
432
+ const table = {};
433
+ for (const [name, entry] of entries) {
434
+ const raw = entry?.value;
435
+ if (typeof raw !== 'string')
436
+ continue;
437
+ if (INTERNAL_TOKEN_PREFIXES.some((prefix) => name.startsWith(prefix)))
438
+ continue;
439
+ table[name] = name.startsWith('--color-') ? (normalizeColorString(raw) ?? raw) : raw;
440
+ }
441
+ return table;
442
+ }
443
+ catch {
444
+ return null;
341
445
  }
342
446
  }
343
447
  /**
@@ -386,6 +490,7 @@ function emptyOutput() {
386
490
  candidates: [],
387
491
  schemes: [BASE_SCHEME],
388
492
  breakpoints: new Map(),
493
+ themeTokens: {},
389
494
  };
390
495
  }
391
496
  /**