@mgcrea/react-native-tailwind 0.8.0 → 0.8.1

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.
@@ -0,0 +1,426 @@
1
+ import { parseSync } from "@babel/core";
2
+ import * as t from "@babel/types";
3
+ import { describe, expect, it } from "vitest";
4
+ import { getComponentModifierSupport, getStatePropertyForModifier } from "./componentSupport";
5
+
6
+ /**
7
+ * Helper to create a JSXOpeningElement from JSX code
8
+ */
9
+ function createJSXElement(code: string): t.JSXOpeningElement {
10
+ const ast = parseSync(code, {
11
+ sourceType: "module",
12
+ plugins: [["@babel/plugin-syntax-jsx", {}]],
13
+ filename: "test.tsx",
14
+ configFile: false,
15
+ babelrc: false,
16
+ });
17
+
18
+ if (!ast) {
19
+ throw new Error(`Failed to parse: ${code}`);
20
+ }
21
+
22
+ // Find the JSXOpeningElement in the AST
23
+ let element: t.JSXOpeningElement | null = null;
24
+
25
+ const traverse = (node: t.Node) => {
26
+ if (t.isJSXOpeningElement(node)) {
27
+ element = node;
28
+ return;
29
+ }
30
+ for (const key in node) {
31
+ // @ts-expect-error - Dynamic key access
32
+ if (node[key] && typeof node[key] === "object") {
33
+ // @ts-expect-error - Dynamic key access
34
+ traverse(node[key] as t.Node);
35
+ }
36
+ }
37
+ };
38
+
39
+ traverse(ast);
40
+
41
+ if (!element) {
42
+ throw new Error(`Could not find JSXOpeningElement in: ${code}`);
43
+ }
44
+
45
+ return element;
46
+ }
47
+
48
+ describe("getComponentModifierSupport", () => {
49
+ describe("Supported components", () => {
50
+ it("should recognize Pressable component", () => {
51
+ const element = createJSXElement("<Pressable />");
52
+ const result = getComponentModifierSupport(element, t);
53
+
54
+ expect(result).toEqual({
55
+ component: "Pressable",
56
+ supportedModifiers: ["active", "hover", "focus", "disabled"],
57
+ });
58
+ });
59
+
60
+ it("should recognize TextInput component", () => {
61
+ const element = createJSXElement("<TextInput />");
62
+ const result = getComponentModifierSupport(element, t);
63
+
64
+ expect(result).toEqual({
65
+ component: "TextInput",
66
+ supportedModifiers: ["focus", "disabled", "placeholder"],
67
+ });
68
+ });
69
+
70
+ it("should recognize Pressable with attributes", () => {
71
+ const element = createJSXElement('<Pressable className="m-4" onPress={handlePress} />');
72
+ const result = getComponentModifierSupport(element, t);
73
+
74
+ expect(result).toEqual({
75
+ component: "Pressable",
76
+ supportedModifiers: ["active", "hover", "focus", "disabled"],
77
+ });
78
+ });
79
+
80
+ it("should recognize TextInput with attributes", () => {
81
+ const element = createJSXElement('<TextInput className="border" placeholder="Email" />');
82
+ const result = getComponentModifierSupport(element, t);
83
+
84
+ expect(result).toEqual({
85
+ component: "TextInput",
86
+ supportedModifiers: ["focus", "disabled", "placeholder"],
87
+ });
88
+ });
89
+ });
90
+
91
+ describe("Member expressions", () => {
92
+ it("should recognize ReactNative.Pressable", () => {
93
+ const element = createJSXElement("<ReactNative.Pressable />");
94
+ const result = getComponentModifierSupport(element, t);
95
+
96
+ expect(result).toEqual({
97
+ component: "Pressable",
98
+ supportedModifiers: ["active", "hover", "focus", "disabled"],
99
+ });
100
+ });
101
+
102
+ it("should recognize RN.TextInput", () => {
103
+ const element = createJSXElement("<RN.TextInput />");
104
+ const result = getComponentModifierSupport(element, t);
105
+
106
+ expect(result).toEqual({
107
+ component: "TextInput",
108
+ supportedModifiers: ["focus", "disabled", "placeholder"],
109
+ });
110
+ });
111
+
112
+ it("should recognize nested member expressions", () => {
113
+ const element = createJSXElement("<Components.Input.TextInput />");
114
+ const result = getComponentModifierSupport(element, t);
115
+
116
+ // Should extract "TextInput" from the rightmost property
117
+ expect(result).toEqual({
118
+ component: "TextInput",
119
+ supportedModifiers: ["focus", "disabled", "placeholder"],
120
+ });
121
+ });
122
+
123
+ it("should recognize Pressable in namespaced imports", () => {
124
+ const element = createJSXElement("<UI.Pressable />");
125
+ const result = getComponentModifierSupport(element, t);
126
+
127
+ expect(result).toEqual({
128
+ component: "Pressable",
129
+ supportedModifiers: ["active", "hover", "focus", "disabled"],
130
+ });
131
+ });
132
+ });
133
+
134
+ describe("Unsupported components", () => {
135
+ it("should return null for View", () => {
136
+ const element = createJSXElement("<View />");
137
+ const result = getComponentModifierSupport(element, t);
138
+
139
+ expect(result).toBeNull();
140
+ });
141
+
142
+ it("should return null for TouchableOpacity", () => {
143
+ const element = createJSXElement("<TouchableOpacity />");
144
+ const result = getComponentModifierSupport(element, t);
145
+
146
+ expect(result).toBeNull();
147
+ });
148
+
149
+ it("should return null for custom components", () => {
150
+ const element = createJSXElement("<CustomButton />");
151
+ const result = getComponentModifierSupport(element, t);
152
+
153
+ expect(result).toBeNull();
154
+ });
155
+
156
+ it("should return null for Text", () => {
157
+ const element = createJSXElement("<Text />");
158
+ const result = getComponentModifierSupport(element, t);
159
+
160
+ expect(result).toBeNull();
161
+ });
162
+
163
+ it("should return null for Image", () => {
164
+ const element = createJSXElement("<Image />");
165
+ const result = getComponentModifierSupport(element, t);
166
+
167
+ expect(result).toBeNull();
168
+ });
169
+ });
170
+
171
+ describe("Edge cases", () => {
172
+ it("should be case-sensitive", () => {
173
+ // lowercase "pressable" should not match
174
+ const element = createJSXElement("<pressable />");
175
+ const result = getComponentModifierSupport(element, t);
176
+
177
+ expect(result).toBeNull();
178
+ });
179
+
180
+ it("should not match similar names", () => {
181
+ const element1 = createJSXElement("<PressableButton />");
182
+ const result1 = getComponentModifierSupport(element1, t);
183
+ expect(result1).toBeNull();
184
+
185
+ const element2 = createJSXElement("<MyPressable />");
186
+ const result2 = getComponentModifierSupport(element2, t);
187
+ expect(result2).toBeNull();
188
+
189
+ const element3 = createJSXElement("<TextInputField />");
190
+ const result3 = getComponentModifierSupport(element3, t);
191
+ expect(result3).toBeNull();
192
+ });
193
+
194
+ it("should handle self-closing tags", () => {
195
+ const element = createJSXElement("<Pressable />");
196
+ const result = getComponentModifierSupport(element, t);
197
+
198
+ expect(result).not.toBeNull();
199
+ expect(result?.component).toBe("Pressable");
200
+ });
201
+
202
+ it("should return null for non-JSXOpeningElement nodes", () => {
203
+ // Test with a random node type
204
+ const identifier = t.identifier("foo");
205
+ const result = getComponentModifierSupport(identifier, t);
206
+
207
+ expect(result).toBeNull();
208
+ });
209
+
210
+ it("should return null for JSXFragment", () => {
211
+ // JSXFragment doesn't have a JSXOpeningElement, so create a mock fragment
212
+ const fragment = {
213
+ type: "JSXFragment",
214
+ openingFragment: {},
215
+ closingFragment: {},
216
+ children: [],
217
+ };
218
+
219
+ const result = getComponentModifierSupport(fragment as t.Node, t);
220
+ expect(result).toBeNull();
221
+ });
222
+ });
223
+
224
+ describe("Modifier support differences", () => {
225
+ it("should show Pressable supports active modifier but TextInput does not", () => {
226
+ const pressable = createJSXElement("<Pressable />");
227
+ const textInput = createJSXElement("<TextInput />");
228
+
229
+ const pressableResult = getComponentModifierSupport(pressable, t);
230
+ const textInputResult = getComponentModifierSupport(textInput, t);
231
+
232
+ expect(pressableResult?.supportedModifiers).toContain("active");
233
+ expect(textInputResult?.supportedModifiers).not.toContain("active");
234
+ });
235
+
236
+ it("should show both support focus modifier", () => {
237
+ const pressable = createJSXElement("<Pressable />");
238
+ const textInput = createJSXElement("<TextInput />");
239
+
240
+ const pressableResult = getComponentModifierSupport(pressable, t);
241
+ const textInputResult = getComponentModifierSupport(textInput, t);
242
+
243
+ expect(pressableResult?.supportedModifiers).toContain("focus");
244
+ expect(textInputResult?.supportedModifiers).toContain("focus");
245
+ });
246
+
247
+ it("should show TextInput supports placeholder but Pressable does not", () => {
248
+ const pressable = createJSXElement("<Pressable />");
249
+ const textInput = createJSXElement("<TextInput />");
250
+
251
+ const pressableResult = getComponentModifierSupport(pressable, t);
252
+ const textInputResult = getComponentModifierSupport(textInput, t);
253
+
254
+ expect(pressableResult?.supportedModifiers).not.toContain("placeholder");
255
+ expect(textInputResult?.supportedModifiers).toContain("placeholder");
256
+ });
257
+
258
+ it("should show both support disabled modifier", () => {
259
+ const pressable = createJSXElement("<Pressable />");
260
+ const textInput = createJSXElement("<TextInput />");
261
+
262
+ const pressableResult = getComponentModifierSupport(pressable, t);
263
+ const textInputResult = getComponentModifierSupport(textInput, t);
264
+
265
+ expect(pressableResult?.supportedModifiers).toContain("disabled");
266
+ expect(textInputResult?.supportedModifiers).toContain("disabled");
267
+ });
268
+ });
269
+ });
270
+
271
+ describe("getStatePropertyForModifier", () => {
272
+ it("should map active to pressed", () => {
273
+ expect(getStatePropertyForModifier("active")).toBe("pressed");
274
+ });
275
+
276
+ it("should map hover to hovered", () => {
277
+ expect(getStatePropertyForModifier("hover")).toBe("hovered");
278
+ });
279
+
280
+ it("should map focus to focused", () => {
281
+ expect(getStatePropertyForModifier("focus")).toBe("focused");
282
+ });
283
+
284
+ it("should map disabled to disabled", () => {
285
+ expect(getStatePropertyForModifier("disabled")).toBe("disabled");
286
+ });
287
+
288
+ it("should return pressed as fallback for unknown modifiers", () => {
289
+ // @ts-expect-error - Testing fallback with invalid modifier
290
+ expect(getStatePropertyForModifier("unknown")).toBe("pressed");
291
+
292
+ // @ts-expect-error - Testing fallback with invalid modifier
293
+ expect(getStatePropertyForModifier("invalid")).toBe("pressed");
294
+
295
+ // @ts-expect-error - Testing fallback with invalid modifier
296
+ expect(getStatePropertyForModifier("")).toBe("pressed");
297
+ });
298
+
299
+ it("should handle all Pressable modifier states", () => {
300
+ // Pressable supports: active, hover, focus, disabled
301
+ const pressableModifiers: Array<"active" | "hover" | "focus" | "disabled"> = [
302
+ "active",
303
+ "hover",
304
+ "focus",
305
+ "disabled",
306
+ ];
307
+
308
+ const expectedMapping = {
309
+ active: "pressed",
310
+ hover: "hovered",
311
+ focus: "focused",
312
+ disabled: "disabled",
313
+ };
314
+
315
+ for (const modifier of pressableModifiers) {
316
+ expect(getStatePropertyForModifier(modifier)).toBe(expectedMapping[modifier]);
317
+ }
318
+ });
319
+
320
+ it("should handle all TextInput modifier states", () => {
321
+ // TextInput supports: focus, disabled, placeholder
322
+ // Note: placeholder doesn't have a state property (it's a prop, not state)
323
+ const textInputModifiers: Array<"focus" | "disabled"> = ["focus", "disabled"];
324
+
325
+ const expectedMapping = {
326
+ focus: "focused",
327
+ disabled: "disabled",
328
+ };
329
+
330
+ for (const modifier of textInputModifiers) {
331
+ expect(getStatePropertyForModifier(modifier)).toBe(expectedMapping[modifier]);
332
+ }
333
+ });
334
+ });
335
+
336
+ describe("Integration - Real-world scenarios", () => {
337
+ it("should correctly identify modifiers for a Pressable button", () => {
338
+ const element = createJSXElement(
339
+ '<Pressable className="active:bg-blue-700 hover:bg-blue-600 disabled:bg-gray-300" />',
340
+ );
341
+ const result = getComponentModifierSupport(element, t);
342
+
343
+ expect(result).not.toBeNull();
344
+ expect(result?.component).toBe("Pressable");
345
+
346
+ // Verify all used modifiers are supported
347
+ expect(result?.supportedModifiers).toContain("active");
348
+ expect(result?.supportedModifiers).toContain("hover");
349
+ expect(result?.supportedModifiers).toContain("disabled");
350
+ });
351
+
352
+ it("should correctly identify modifiers for a TextInput field", () => {
353
+ const element = createJSXElement(
354
+ '<TextInput className="focus:border-blue-500 disabled:bg-gray-100 placeholder:text-gray-400" />',
355
+ );
356
+ const result = getComponentModifierSupport(element, t);
357
+
358
+ expect(result).not.toBeNull();
359
+ expect(result?.component).toBe("TextInput");
360
+
361
+ // Verify all used modifiers are supported
362
+ expect(result?.supportedModifiers).toContain("focus");
363
+ expect(result?.supportedModifiers).toContain("disabled");
364
+ expect(result?.supportedModifiers).toContain("placeholder");
365
+ });
366
+
367
+ it("should handle namespaced components from imports", () => {
368
+ const element = createJSXElement('<RN.Pressable className="active:opacity-80" />');
369
+ const result = getComponentModifierSupport(element, t);
370
+
371
+ expect(result).not.toBeNull();
372
+ expect(result?.component).toBe("Pressable");
373
+ expect(result?.supportedModifiers).toContain("active");
374
+ });
375
+
376
+ it("should return null for unsupported components with modifiers", () => {
377
+ const element = createJSXElement('<View className="hover:bg-blue-500" />');
378
+ const result = getComponentModifierSupport(element, t);
379
+
380
+ // View doesn't support modifiers
381
+ expect(result).toBeNull();
382
+ });
383
+
384
+ it("should map all Pressable modifiers to correct state properties", () => {
385
+ const element = createJSXElement("<Pressable />");
386
+ const result = getComponentModifierSupport(element, t);
387
+
388
+ expect(result).not.toBeNull();
389
+
390
+ // Test each supported modifier maps correctly
391
+ const modifiers = result?.supportedModifiers as Array<"active" | "hover" | "focus" | "disabled">;
392
+ for (const modifier of modifiers) {
393
+ const stateProp = getStatePropertyForModifier(modifier);
394
+ expect(stateProp).toBeTruthy();
395
+ expect(typeof stateProp).toBe("string");
396
+ }
397
+
398
+ // Verify the mappings
399
+ expect(getStatePropertyForModifier("active")).toBe("pressed");
400
+ expect(getStatePropertyForModifier("hover")).toBe("hovered");
401
+ expect(getStatePropertyForModifier("focus")).toBe("focused");
402
+ expect(getStatePropertyForModifier("disabled")).toBe("disabled");
403
+ });
404
+
405
+ it("should map all TextInput modifiers to correct state properties", () => {
406
+ const element = createJSXElement("<TextInput />");
407
+ const result = getComponentModifierSupport(element, t);
408
+
409
+ expect(result).not.toBeNull();
410
+
411
+ // Filter out placeholder as it doesn't have a state property
412
+ const stateModifiers = result?.supportedModifiers.filter((m) => m !== "placeholder") as Array<
413
+ "focus" | "disabled"
414
+ >;
415
+
416
+ for (const modifier of stateModifiers) {
417
+ const stateProp = getStatePropertyForModifier(modifier);
418
+ expect(stateProp).toBeTruthy();
419
+ expect(typeof stateProp).toBe("string");
420
+ }
421
+
422
+ // Verify the mappings
423
+ expect(getStatePropertyForModifier("focus")).toBe("focused");
424
+ expect(getStatePropertyForModifier("disabled")).toBe("disabled");
425
+ });
426
+ });
@@ -168,6 +168,38 @@ describe("parseColor - custom colors", () => {
168
168
  expect(parseColor("bg-blue-500", overrideColors)).toEqual({ backgroundColor: "#FF0000" });
169
169
  });
170
170
 
171
+ it("should support custom colors with DEFAULT key from tailwind.config", () => {
172
+ // Simulates what flattenColors() produces from:
173
+ // { primary: { DEFAULT: "#1bacb5", 50: "#eefdfd", ... } }
174
+ const customColorsWithDefault = {
175
+ primary: "#1bacb5", // DEFAULT becomes the parent key
176
+ "primary-50": "#eefdfd",
177
+ "primary-100": "#d4f9f9",
178
+ "primary-500": "#1bacb5",
179
+ "primary-900": "#1e4f5b",
180
+ };
181
+
182
+ // Test that bg-primary uses the DEFAULT value
183
+ expect(parseColor("bg-primary", customColorsWithDefault)).toEqual({
184
+ backgroundColor: "#1bacb5",
185
+ });
186
+
187
+ // Test that bg-primary-50 uses the shade value
188
+ expect(parseColor("bg-primary-50", customColorsWithDefault)).toEqual({
189
+ backgroundColor: "#eefdfd",
190
+ });
191
+
192
+ // Test with text colors
193
+ expect(parseColor("text-primary", customColorsWithDefault)).toEqual({
194
+ color: "#1bacb5",
195
+ });
196
+
197
+ // Test with border colors
198
+ expect(parseColor("border-primary", customColorsWithDefault)).toEqual({
199
+ borderColor: "#1bacb5",
200
+ });
201
+ });
202
+
171
203
  it("should fallback to preset colors when custom color not found", () => {
172
204
  expect(parseColor("bg-red-500", customColors)).toEqual({ backgroundColor: COLORS["red-500"] });
173
205
  });
@@ -42,7 +42,7 @@ export function parseClass(cls: string, customColors?: Record<string, string>):
42
42
  // Try each parser in order
43
43
  // Note: parseBorder must come before parseColor to avoid border-[3px] being parsed as a color
44
44
  // parseColor gets custom colors, others don't need it
45
- const parsers: ((cls: string) => StyleObject | null)[] = [
45
+ const parsers: Array<(cls: string) => StyleObject | null> = [
46
46
  parseSpacing,
47
47
  parseBorder,
48
48
  (cls: string) => parseColor(cls, customColors),
@@ -358,4 +358,104 @@ describe("flattenColors", () => {
358
358
 
359
359
  expect(keys).toEqual(["z", "a", "m"]);
360
360
  });
361
+
362
+ it("should handle DEFAULT key in color scale objects", () => {
363
+ const colors = {
364
+ primary: {
365
+ "50": "#eefdfd",
366
+ "100": "#d4f9f9",
367
+ "200": "#aef2f3",
368
+ "500": "#1bacb5",
369
+ "900": "#1e4f5b",
370
+ DEFAULT: "#1bacb5",
371
+ },
372
+ };
373
+
374
+ expect(flattenColors(colors)).toEqual({
375
+ primary: "#1bacb5", // DEFAULT becomes the parent key
376
+ "primary-50": "#eefdfd",
377
+ "primary-100": "#d4f9f9",
378
+ "primary-200": "#aef2f3",
379
+ "primary-500": "#1bacb5",
380
+ "primary-900": "#1e4f5b",
381
+ });
382
+ });
383
+
384
+ it("should handle DEFAULT key with multiple color scales", () => {
385
+ const colors = {
386
+ primary: {
387
+ DEFAULT: "#1bacb5",
388
+ "500": "#1bacb5",
389
+ },
390
+ secondary: {
391
+ DEFAULT: "#ff6b6b",
392
+ "500": "#ff6b6b",
393
+ },
394
+ };
395
+
396
+ expect(flattenColors(colors)).toEqual({
397
+ primary: "#1bacb5",
398
+ "primary-500": "#1bacb5",
399
+ secondary: "#ff6b6b",
400
+ "secondary-500": "#ff6b6b",
401
+ });
402
+ });
403
+
404
+ it("should handle DEFAULT key in nested structures", () => {
405
+ const colors = {
406
+ brand: {
407
+ primary: {
408
+ DEFAULT: "#1bacb5",
409
+ light: "#d4f9f9",
410
+ dark: "#0e343e",
411
+ },
412
+ },
413
+ };
414
+
415
+ expect(flattenColors(colors)).toEqual({
416
+ "brand-primary": "#1bacb5", // DEFAULT uses parent key
417
+ "brand-primary-light": "#d4f9f9",
418
+ "brand-primary-dark": "#0e343e",
419
+ });
420
+ });
421
+
422
+ it("should handle DEFAULT at top level (edge case)", () => {
423
+ const colors = {
424
+ DEFAULT: "#000000",
425
+ primary: "#ff0000",
426
+ };
427
+
428
+ expect(flattenColors(colors)).toEqual({
429
+ DEFAULT: "#000000", // Top-level DEFAULT kept as-is (no parent)
430
+ primary: "#ff0000",
431
+ });
432
+ });
433
+
434
+ it("should handle mixed DEFAULT and regular keys", () => {
435
+ const colors = {
436
+ gray: {
437
+ "50": "#f9fafb",
438
+ "100": "#f3f4f6",
439
+ DEFAULT: "#6b7280",
440
+ "500": "#6b7280",
441
+ "900": "#111827",
442
+ },
443
+ white: "#ffffff",
444
+ brand: {
445
+ DEFAULT: "#ff6b6b",
446
+ accent: "#4ecdc4",
447
+ },
448
+ };
449
+
450
+ expect(flattenColors(colors)).toEqual({
451
+ "gray-50": "#f9fafb",
452
+ "gray-100": "#f3f4f6",
453
+ gray: "#6b7280", // DEFAULT becomes parent key
454
+ "gray-500": "#6b7280",
455
+ "gray-900": "#111827",
456
+ white: "#ffffff",
457
+ brand: "#ff6b6b", // DEFAULT becomes parent key
458
+ "brand-accent": "#4ecdc4",
459
+ });
460
+ });
361
461
  });
@@ -8,6 +8,7 @@ type NestedColors = {
8
8
  /**
9
9
  * Flatten nested color objects into flat key-value map
10
10
  * Example: { brand: { light: '#fff', dark: '#000' } } => { 'brand-light': '#fff', 'brand-dark': '#000' }
11
+ * Special handling for DEFAULT: { primary: { DEFAULT: '#000', 500: '#333' } } => { 'primary': '#000', 'primary-500': '#333' }
11
12
  *
12
13
  * @param colors - Nested color object where values can be strings or objects
13
14
  * @param prefix - Optional prefix for nested keys (used for recursion)
@@ -17,7 +18,8 @@ export function flattenColors(colors: NestedColors, prefix = ""): Record<string,
17
18
  const result: Record<string, string> = {};
18
19
 
19
20
  for (const [key, value] of Object.entries(colors)) {
20
- const newKey = prefix ? `${prefix}-${key}` : key;
21
+ // Special handling for DEFAULT key - use parent key without suffix
22
+ const newKey = key === "DEFAULT" && prefix ? prefix : prefix ? `${prefix}-${key}` : key;
21
23
 
22
24
  if (typeof value === "string") {
23
25
  result[newKey] = value;