@mgcrea/react-native-tailwind 0.9.1 → 0.10.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 (39) hide show
  1. package/README.md +356 -30
  2. package/dist/babel/config-loader.test.ts +152 -0
  3. package/dist/babel/index.cjs +547 -47
  4. package/dist/babel/plugin.d.ts +21 -0
  5. package/dist/babel/plugin.test.ts +331 -0
  6. package/dist/babel/plugin.ts +258 -28
  7. package/dist/babel/utils/colorSchemeModifierProcessing.d.ts +34 -0
  8. package/dist/babel/utils/colorSchemeModifierProcessing.ts +89 -0
  9. package/dist/babel/utils/dynamicProcessing.d.ts +33 -2
  10. package/dist/babel/utils/dynamicProcessing.ts +352 -33
  11. package/dist/babel/utils/styleInjection.d.ts +13 -0
  12. package/dist/babel/utils/styleInjection.ts +101 -0
  13. package/dist/babel/utils/styleTransforms.test.ts +56 -0
  14. package/dist/babel/utils/twProcessing.d.ts +2 -0
  15. package/dist/babel/utils/twProcessing.ts +22 -1
  16. package/dist/parser/index.d.ts +2 -2
  17. package/dist/parser/index.js +1 -1
  18. package/dist/parser/modifiers.d.ts +48 -2
  19. package/dist/parser/modifiers.js +1 -1
  20. package/dist/parser/modifiers.test.js +1 -1
  21. package/dist/runtime.cjs +1 -1
  22. package/dist/runtime.cjs.map +3 -3
  23. package/dist/runtime.js +1 -1
  24. package/dist/runtime.js.map +3 -3
  25. package/dist/types/config.d.ts +7 -0
  26. package/dist/types/config.js +0 -0
  27. package/package.json +3 -2
  28. package/src/babel/config-loader.test.ts +152 -0
  29. package/src/babel/plugin.test.ts +331 -0
  30. package/src/babel/plugin.ts +258 -28
  31. package/src/babel/utils/colorSchemeModifierProcessing.ts +89 -0
  32. package/src/babel/utils/dynamicProcessing.ts +352 -33
  33. package/src/babel/utils/styleInjection.ts +101 -0
  34. package/src/babel/utils/styleTransforms.test.ts +56 -0
  35. package/src/babel/utils/twProcessing.ts +22 -1
  36. package/src/parser/index.ts +12 -1
  37. package/src/parser/modifiers.test.ts +151 -1
  38. package/src/parser/modifiers.ts +139 -4
  39. package/src/types/config.ts +7 -0
@@ -5,6 +5,8 @@
5
5
  import type { NodePath } from "@babel/core";
6
6
  import type * as BabelTypes from "@babel/types";
7
7
  import type { ModifierType, ParsedModifier } from "../../parser/index.js";
8
+ import { expandSchemeModifier, isSchemeModifier } from "../../parser/index.js";
9
+ import type { SchemeModifierConfig } from "../../types/config.js";
8
10
  import type { StyleObject } from "../../types/core.js";
9
11
 
10
12
  /**
@@ -14,6 +16,7 @@ import type { StyleObject } from "../../types/core.js";
14
16
  export interface TwProcessingState {
15
17
  styleRegistry: Map<string, StyleObject>;
16
18
  customColors: Record<string, string>;
19
+ schemeModifierConfig: SchemeModifierConfig;
17
20
  stylesIdentifier: string;
18
21
  }
19
22
 
@@ -30,7 +33,25 @@ export function processTwCall(
30
33
  splitModifierClasses: (className: string) => { baseClasses: string[]; modifierClasses: ParsedModifier[] },
31
34
  t: typeof BabelTypes,
32
35
  ): void {
33
- const { baseClasses, modifierClasses } = splitModifierClasses(className);
36
+ const { baseClasses, modifierClasses: rawModifierClasses } = splitModifierClasses(className);
37
+
38
+ // Expand scheme: modifiers into dark: and light: modifiers
39
+ const modifierClasses: ParsedModifier[] = [];
40
+ for (const modifier of rawModifierClasses) {
41
+ if (isSchemeModifier(modifier.modifier)) {
42
+ // Expand scheme: into dark: and light:
43
+ const expanded = expandSchemeModifier(
44
+ modifier,
45
+ state.customColors,
46
+ state.schemeModifierConfig.darkSuffix ?? "-dark",
47
+ state.schemeModifierConfig.lightSuffix ?? "-light",
48
+ );
49
+ modifierClasses.push(...expanded);
50
+ } else {
51
+ // Keep other modifiers as-is
52
+ modifierClasses.push(modifier);
53
+ }
54
+ }
34
55
 
35
56
  // Build TwStyle object properties
36
57
  const objectProperties: BabelTypes.ObjectProperty[] = [];
@@ -84,10 +84,21 @@ export { parseTypography } from "./typography";
84
84
 
85
85
  // Re-export modifier utilities
86
86
  export {
87
+ expandSchemeModifier,
87
88
  hasModifier,
89
+ isColorClass,
90
+ isColorSchemeModifier,
88
91
  isPlatformModifier,
92
+ isSchemeModifier,
89
93
  isStateModifier,
90
94
  parseModifier,
91
95
  splitModifierClasses,
92
96
  } from "./modifiers";
93
- export type { ModifierType, ParsedModifier, PlatformModifierType, StateModifierType } from "./modifiers";
97
+ export type {
98
+ ColorSchemeModifierType,
99
+ ModifierType,
100
+ ParsedModifier,
101
+ PlatformModifierType,
102
+ SchemeModifierType,
103
+ StateModifierType,
104
+ } from "./modifiers";
@@ -1,6 +1,13 @@
1
1
  import { describe, expect, it } from "vitest";
2
2
  import type { ModifierType, ParsedModifier } from "./modifiers";
3
- import { hasModifier, parseModifier, splitModifierClasses } from "./modifiers";
3
+ import {
4
+ expandSchemeModifier,
5
+ hasModifier,
6
+ isColorClass,
7
+ isSchemeModifier,
8
+ parseModifier,
9
+ splitModifierClasses,
10
+ } from "./modifiers";
4
11
 
5
12
  describe("parseModifier - basic functionality", () => {
6
13
  it("should parse active modifier", () => {
@@ -373,3 +380,146 @@ describe("type safety", () => {
373
380
  }
374
381
  });
375
382
  });
383
+
384
+ describe("isSchemeModifier", () => {
385
+ it("should return true for scheme modifier", () => {
386
+ expect(isSchemeModifier("scheme")).toBe(true);
387
+ });
388
+
389
+ it("should return false for non-scheme modifiers", () => {
390
+ expect(isSchemeModifier("dark")).toBe(false);
391
+ expect(isSchemeModifier("light")).toBe(false);
392
+ expect(isSchemeModifier("active")).toBe(false);
393
+ expect(isSchemeModifier("ios")).toBe(false);
394
+ });
395
+ });
396
+
397
+ describe("isColorClass", () => {
398
+ it("should return true for text color classes", () => {
399
+ expect(isColorClass("text-red-500")).toBe(true);
400
+ expect(isColorClass("text-systemGray")).toBe(true);
401
+ expect(isColorClass("text-blue-50")).toBe(true);
402
+ });
403
+
404
+ it("should return true for background color classes", () => {
405
+ expect(isColorClass("bg-red-500")).toBe(true);
406
+ expect(isColorClass("bg-systemGray")).toBe(true);
407
+ expect(isColorClass("bg-transparent")).toBe(true);
408
+ });
409
+
410
+ it("should return true for border color classes", () => {
411
+ expect(isColorClass("border-red-500")).toBe(true);
412
+ expect(isColorClass("border-systemGray")).toBe(true);
413
+ expect(isColorClass("border-black")).toBe(true);
414
+ });
415
+
416
+ it("should return false for non-color classes", () => {
417
+ expect(isColorClass("m-4")).toBe(false);
418
+ expect(isColorClass("p-2")).toBe(false);
419
+ expect(isColorClass("flex")).toBe(false);
420
+ expect(isColorClass("rounded-lg")).toBe(false);
421
+ expect(isColorClass("font-bold")).toBe(false);
422
+ });
423
+ });
424
+
425
+ describe("expandSchemeModifier", () => {
426
+ const customColors = {
427
+ "systemGray-dark": "#333333",
428
+ "systemGray-light": "#CCCCCC",
429
+ "primary-dark": "#1E40AF",
430
+ "primary-light": "#BFDBFE",
431
+ "accent-dark": "#DC2626",
432
+ "accent-light": "#FECACA",
433
+ };
434
+
435
+ it("should expand text color scheme modifier with default suffixes", () => {
436
+ const modifier = { modifier: "scheme" as const, baseClass: "text-systemGray" };
437
+ const result = expandSchemeModifier(modifier, customColors);
438
+
439
+ expect(result).toHaveLength(2);
440
+ expect((result as [ParsedModifier, ParsedModifier])[0]).toEqual({
441
+ modifier: "dark",
442
+ baseClass: "text-systemGray-dark",
443
+ });
444
+ expect((result as [ParsedModifier, ParsedModifier])[1]).toEqual({
445
+ modifier: "light",
446
+ baseClass: "text-systemGray-light",
447
+ });
448
+ });
449
+
450
+ it("should expand background color scheme modifier", () => {
451
+ const modifier = { modifier: "scheme" as const, baseClass: "bg-primary" };
452
+ const result = expandSchemeModifier(modifier, customColors);
453
+
454
+ expect(result).toHaveLength(2);
455
+ expect((result as [ParsedModifier, ParsedModifier])[0]).toEqual({
456
+ modifier: "dark",
457
+ baseClass: "bg-primary-dark",
458
+ });
459
+ expect((result as [ParsedModifier, ParsedModifier])[1]).toEqual({
460
+ modifier: "light",
461
+ baseClass: "bg-primary-light",
462
+ });
463
+ });
464
+
465
+ it("should expand border color scheme modifier", () => {
466
+ const modifier = { modifier: "scheme" as const, baseClass: "border-accent" };
467
+ const result = expandSchemeModifier(modifier, customColors);
468
+
469
+ expect(result).toHaveLength(2);
470
+ expect((result as [ParsedModifier, ParsedModifier])[0]).toEqual({
471
+ modifier: "dark",
472
+ baseClass: "border-accent-dark",
473
+ });
474
+ expect((result as [ParsedModifier, ParsedModifier])[1]).toEqual({
475
+ modifier: "light",
476
+ baseClass: "border-accent-light",
477
+ });
478
+ });
479
+
480
+ it("should use custom suffixes when provided", () => {
481
+ const modifier = { modifier: "scheme" as const, baseClass: "text-systemGray" };
482
+ const _result = expandSchemeModifier(modifier, customColors, "-darkMode", "-lightMode");
483
+
484
+ const expectedColors = {
485
+ "systemGray-darkMode": "#333333",
486
+ "systemGray-lightMode": "#CCCCCC",
487
+ };
488
+
489
+ expect(expandSchemeModifier(modifier, expectedColors, "-darkMode", "-lightMode")).toHaveLength(2);
490
+ });
491
+
492
+ it("should return empty array for non-color classes", () => {
493
+ const modifier = { modifier: "scheme" as const, baseClass: "m-4" };
494
+ const result = expandSchemeModifier(modifier, customColors);
495
+
496
+ expect(result).toEqual([]);
497
+ });
498
+
499
+ it("should return empty array when dark color variant is missing", () => {
500
+ const modifier = { modifier: "scheme" as const, baseClass: "text-missing" };
501
+ const incompleteColors = {
502
+ "missing-light": "#FFFFFF",
503
+ };
504
+ const result = expandSchemeModifier(modifier, incompleteColors);
505
+
506
+ expect(result).toEqual([]);
507
+ });
508
+
509
+ it("should return empty array when light color variant is missing", () => {
510
+ const modifier = { modifier: "scheme" as const, baseClass: "text-missing" };
511
+ const incompleteColors = {
512
+ "missing-dark": "#000000",
513
+ };
514
+ const result = expandSchemeModifier(modifier, incompleteColors);
515
+
516
+ expect(result).toEqual([]);
517
+ });
518
+
519
+ it("should return empty array when both color variants are missing", () => {
520
+ const modifier = { modifier: "scheme" as const, baseClass: "text-missing" };
521
+ const result = expandSchemeModifier(modifier, customColors);
522
+
523
+ expect(result).toEqual([]);
524
+ });
525
+ });
@@ -1,12 +1,19 @@
1
1
  /**
2
- * Modifier parsing utilities for state-based and platform-specific class names
2
+ * Modifier parsing utilities for state-based, platform-specific, and color scheme class names
3
3
  * - State modifiers: active:, hover:, focus:, disabled:, placeholder:
4
4
  * - Platform modifiers: ios:, android:, web:
5
+ * - Color scheme modifiers: dark:, light:
5
6
  */
6
7
 
7
8
  export type StateModifierType = "active" | "hover" | "focus" | "disabled" | "placeholder";
8
9
  export type PlatformModifierType = "ios" | "android" | "web";
9
- export type ModifierType = StateModifierType | PlatformModifierType;
10
+ export type ColorSchemeModifierType = "dark" | "light";
11
+ export type SchemeModifierType = "scheme";
12
+ export type ModifierType =
13
+ | StateModifierType
14
+ | PlatformModifierType
15
+ | ColorSchemeModifierType
16
+ | SchemeModifierType;
10
17
 
11
18
  export type ParsedModifier = {
12
19
  modifier: ModifierType;
@@ -30,9 +37,24 @@ const STATE_MODIFIERS: readonly StateModifierType[] = [
30
37
  const PLATFORM_MODIFIERS: readonly PlatformModifierType[] = ["ios", "android", "web"] as const;
31
38
 
32
39
  /**
33
- * All supported modifiers (state + platform)
40
+ * Supported color scheme modifiers that map to Appearance.colorScheme values
34
41
  */
35
- const SUPPORTED_MODIFIERS: readonly ModifierType[] = [...STATE_MODIFIERS, ...PLATFORM_MODIFIERS] as const;
42
+ const COLOR_SCHEME_MODIFIERS: readonly ColorSchemeModifierType[] = ["dark", "light"] as const;
43
+
44
+ /**
45
+ * Scheme modifier that expands to both dark: and light: modifiers
46
+ */
47
+ const SCHEME_MODIFIERS: readonly SchemeModifierType[] = ["scheme"] as const;
48
+
49
+ /**
50
+ * All supported modifiers (state + platform + color scheme + scheme)
51
+ */
52
+ const SUPPORTED_MODIFIERS: readonly ModifierType[] = [
53
+ ...STATE_MODIFIERS,
54
+ ...PLATFORM_MODIFIERS,
55
+ ...COLOR_SCHEME_MODIFIERS,
56
+ ...SCHEME_MODIFIERS,
57
+ ] as const;
36
58
 
37
59
  /**
38
60
  * Parse a class name to detect and extract modifiers
@@ -107,6 +129,119 @@ export function isPlatformModifier(modifier: ModifierType): modifier is Platform
107
129
  return PLATFORM_MODIFIERS.includes(modifier as PlatformModifierType);
108
130
  }
109
131
 
132
+ /**
133
+ * Check if a modifier is a color scheme modifier (dark, light)
134
+ *
135
+ * @param modifier - Modifier type to check
136
+ * @returns true if modifier is a color scheme modifier
137
+ */
138
+ export function isColorSchemeModifier(modifier: ModifierType): modifier is ColorSchemeModifierType {
139
+ return COLOR_SCHEME_MODIFIERS.includes(modifier as ColorSchemeModifierType);
140
+ }
141
+
142
+ /**
143
+ * Check if a modifier is a scheme modifier (scheme)
144
+ *
145
+ * @param modifier - Modifier type to check
146
+ * @returns true if modifier is a scheme modifier
147
+ */
148
+ export function isSchemeModifier(modifier: ModifierType): modifier is SchemeModifierType {
149
+ return SCHEME_MODIFIERS.includes(modifier as SchemeModifierType);
150
+ }
151
+
152
+ /**
153
+ * Check if a class name is a color-based utility class
154
+ *
155
+ * @param className - Class name to check
156
+ * @returns true if class is color-based (text-*, bg-*, border-*)
157
+ */
158
+ export function isColorClass(className: string): boolean {
159
+ return className.startsWith("text-") || className.startsWith("bg-") || className.startsWith("border-");
160
+ }
161
+
162
+ /**
163
+ * Expand scheme modifier into dark and light modifiers
164
+ *
165
+ * @param schemeModifier - Parsed scheme modifier
166
+ * @param customColors - Custom colors from config
167
+ * @param darkSuffix - Suffix for dark variant (default: "-dark")
168
+ * @param lightSuffix - Suffix for light variant (default: "-light")
169
+ * @returns Array of expanded modifiers (dark: and light:), or empty array if validation fails
170
+ *
171
+ * @example
172
+ * expandSchemeModifier(
173
+ * { modifier: "scheme", baseClass: "text-systemGray" },
174
+ * { "systemGray-dark": "#333", "systemGray-light": "#ccc" },
175
+ * "-dark",
176
+ * "-light"
177
+ * )
178
+ * // Returns: [
179
+ * // { modifier: "dark", baseClass: "text-systemGray-dark" },
180
+ * // { modifier: "light", baseClass: "text-systemGray-light" }
181
+ * // ]
182
+ */
183
+ export function expandSchemeModifier(
184
+ schemeModifier: ParsedModifier,
185
+ customColors: Record<string, string>,
186
+ darkSuffix = "-dark",
187
+ lightSuffix = "-light",
188
+ ): ParsedModifier[] {
189
+ const { baseClass } = schemeModifier;
190
+
191
+ // Only process color-based classes
192
+ if (!isColorClass(baseClass)) {
193
+ if (process.env.NODE_ENV !== "production") {
194
+ console.warn(
195
+ `[react-native-tailwind] scheme: modifier only supports color classes (text-*, bg-*, border-*). ` +
196
+ `Found: "${baseClass}". This modifier will be ignored.`,
197
+ );
198
+ }
199
+ return [];
200
+ }
201
+
202
+ // Extract the color name from the class
203
+ // e.g., "text-systemGray" -> "systemGray"
204
+ const match = baseClass.match(/^(text|bg|border)-(.+)$/);
205
+ if (!match) {
206
+ return [];
207
+ }
208
+
209
+ const [, prefix, colorName] = match;
210
+
211
+ // Build variant class names
212
+ const darkColorName = `${colorName}${darkSuffix}`;
213
+ const lightColorName = `${colorName}${lightSuffix}`;
214
+
215
+ // Validate that both color variants exist
216
+ const darkColorExists = customColors[darkColorName] !== undefined;
217
+ const lightColorExists = customColors[lightColorName] !== undefined;
218
+
219
+ if (!darkColorExists || !lightColorExists) {
220
+ if (process.env.NODE_ENV !== "production") {
221
+ const missing = [];
222
+ if (!darkColorExists) missing.push(`${colorName}${darkSuffix}`);
223
+ if (!lightColorExists) missing.push(`${colorName}${lightSuffix}`);
224
+ console.warn(
225
+ `[react-native-tailwind] scheme:${baseClass} requires both color variants to exist. ` +
226
+ `Missing: ${missing.join(", ")}. This modifier will be ignored.`,
227
+ );
228
+ }
229
+ return [];
230
+ }
231
+
232
+ // Expand to dark: and light: modifiers
233
+ return [
234
+ {
235
+ modifier: "dark" as ColorSchemeModifierType,
236
+ baseClass: `${prefix}-${darkColorName}`,
237
+ },
238
+ {
239
+ modifier: "light" as ColorSchemeModifierType,
240
+ baseClass: `${prefix}-${lightColorName}`,
241
+ },
242
+ ];
243
+ }
244
+
110
245
  /**
111
246
  * Split a space-separated className string into base and modifier classes
112
247
  *
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Configuration for the scheme: modifier
3
+ */
4
+ export type SchemeModifierConfig = {
5
+ darkSuffix?: string;
6
+ lightSuffix?: string;
7
+ };