@mgcrea/react-native-tailwind 0.13.0 → 0.15.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.
- package/README.md +33 -30
- package/dist/babel/config-loader.d.ts +10 -0
- package/dist/babel/config-loader.test.ts +75 -21
- package/dist/babel/config-loader.ts +100 -2
- package/dist/babel/index.cjs +439 -46
- package/dist/babel/plugin/state.d.ts +4 -0
- package/dist/babel/plugin/state.ts +8 -0
- package/dist/babel/plugin/visitors/className.test.ts +313 -0
- package/dist/babel/plugin/visitors/className.ts +36 -8
- package/dist/babel/plugin/visitors/imports.ts +16 -1
- package/dist/babel/plugin/visitors/program.ts +19 -2
- package/dist/babel/plugin/visitors/tw.test.ts +151 -0
- package/dist/babel/utils/directionalModifierProcessing.d.ts +34 -0
- package/dist/babel/utils/directionalModifierProcessing.ts +99 -0
- package/dist/babel/utils/styleInjection.d.ts +16 -0
- package/dist/babel/utils/styleInjection.ts +138 -7
- package/dist/babel/utils/twProcessing.d.ts +2 -0
- package/dist/babel/utils/twProcessing.ts +92 -3
- package/dist/parser/borders.js +1 -1
- package/dist/parser/borders.test.js +1 -1
- package/dist/parser/index.d.ts +3 -2
- package/dist/parser/index.js +1 -1
- package/dist/parser/layout.d.ts +3 -1
- package/dist/parser/layout.js +1 -1
- package/dist/parser/layout.test.js +1 -1
- package/dist/parser/modifiers.d.ts +32 -2
- package/dist/parser/modifiers.js +1 -1
- package/dist/parser/modifiers.test.js +1 -1
- package/dist/parser/sizing.d.ts +3 -1
- package/dist/parser/sizing.js +1 -1
- package/dist/parser/sizing.test.js +1 -1
- package/dist/parser/spacing.d.ts +4 -2
- package/dist/parser/spacing.js +1 -1
- package/dist/parser/spacing.test.js +1 -1
- package/dist/parser/transforms.d.ts +3 -1
- package/dist/parser/transforms.js +1 -1
- package/dist/parser/transforms.test.js +1 -1
- package/dist/parser/typography.test.js +1 -1
- package/dist/runtime.cjs +1 -1
- package/dist/runtime.cjs.map +3 -3
- package/dist/runtime.d.ts +2 -0
- package/dist/runtime.js +1 -1
- package/dist/runtime.js.map +3 -3
- package/dist/runtime.test.js +1 -1
- package/package.json +6 -6
- package/src/babel/config-loader.test.ts +75 -21
- package/src/babel/config-loader.ts +100 -2
- package/src/babel/plugin/state.ts +8 -0
- package/src/babel/plugin/visitors/className.test.ts +313 -0
- package/src/babel/plugin/visitors/className.ts +36 -8
- package/src/babel/plugin/visitors/imports.ts +16 -1
- package/src/babel/plugin/visitors/program.ts +19 -2
- package/src/babel/plugin/visitors/tw.test.ts +151 -0
- package/src/babel/utils/directionalModifierProcessing.ts +99 -0
- package/src/babel/utils/styleInjection.ts +138 -7
- package/src/babel/utils/twProcessing.ts +92 -3
- package/src/parser/borders.test.ts +104 -0
- package/src/parser/borders.ts +50 -7
- package/src/parser/index.ts +8 -5
- package/src/parser/layout.test.ts +168 -0
- package/src/parser/layout.ts +107 -8
- package/src/parser/modifiers.test.ts +206 -0
- package/src/parser/modifiers.ts +62 -3
- package/src/parser/sizing.test.ts +56 -0
- package/src/parser/sizing.ts +20 -15
- package/src/parser/spacing.test.ts +123 -0
- package/src/parser/spacing.ts +30 -15
- package/src/parser/transforms.test.ts +57 -0
- package/src/parser/transforms.ts +7 -3
- package/src/parser/typography.test.ts +8 -0
- package/src/parser/typography.ts +4 -0
- package/src/runtime.test.ts +149 -0
- package/src/runtime.ts +53 -1
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for processing directional modifiers (rtl:, ltr:)
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type * as BabelTypes from "@babel/types";
|
|
6
|
+
import type { CustomTheme, DirectionalModifierType, ParsedModifier } from "../../parser/index.js";
|
|
7
|
+
import type { StyleObject } from "../../types/core.js";
|
|
8
|
+
import { hasRuntimeDimensions } from "./windowDimensionsProcessing.js";
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Plugin state interface (subset needed for directional modifier processing)
|
|
12
|
+
*/
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
|
|
14
|
+
export interface DirectionalModifierProcessingState {
|
|
15
|
+
styleRegistry: Map<string, StyleObject>;
|
|
16
|
+
customTheme: CustomTheme;
|
|
17
|
+
stylesIdentifier: string;
|
|
18
|
+
needsI18nManagerImport: boolean;
|
|
19
|
+
i18nManagerVariableName: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Process directional modifiers and generate conditional style expressions
|
|
24
|
+
*
|
|
25
|
+
* @param directionalModifiers - Array of parsed directional modifiers
|
|
26
|
+
* @param state - Plugin state
|
|
27
|
+
* @param parseClassName - Function to parse class names into style objects
|
|
28
|
+
* @param generateStyleKey - Function to generate unique style keys
|
|
29
|
+
* @param t - Babel types
|
|
30
|
+
* @returns Array of AST nodes for conditional expressions
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* Input: [{ modifier: "rtl", baseClass: "mr-4" }, { modifier: "ltr", baseClass: "ml-4" }]
|
|
34
|
+
* Output: [
|
|
35
|
+
* _twIsRTL && styles._rtl_mr_4,
|
|
36
|
+
* !_twIsRTL && styles._ltr_ml_4
|
|
37
|
+
* ]
|
|
38
|
+
*/
|
|
39
|
+
export function processDirectionalModifiers(
|
|
40
|
+
directionalModifiers: ParsedModifier[],
|
|
41
|
+
state: DirectionalModifierProcessingState,
|
|
42
|
+
parseClassName: (className: string, customTheme?: CustomTheme) => StyleObject,
|
|
43
|
+
generateStyleKey: (className: string) => string,
|
|
44
|
+
t: typeof BabelTypes,
|
|
45
|
+
): BabelTypes.Expression[] {
|
|
46
|
+
// Mark that we need I18nManager import
|
|
47
|
+
state.needsI18nManagerImport = true;
|
|
48
|
+
|
|
49
|
+
// Group modifiers by direction (rtl, ltr)
|
|
50
|
+
const modifiersByDirection = new Map<DirectionalModifierType, ParsedModifier[]>();
|
|
51
|
+
|
|
52
|
+
for (const mod of directionalModifiers) {
|
|
53
|
+
const direction = mod.modifier as DirectionalModifierType;
|
|
54
|
+
if (!modifiersByDirection.has(direction)) {
|
|
55
|
+
modifiersByDirection.set(direction, []);
|
|
56
|
+
}
|
|
57
|
+
const directionGroup = modifiersByDirection.get(direction);
|
|
58
|
+
if (directionGroup) {
|
|
59
|
+
directionGroup.push(mod);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Build conditional expressions for each direction
|
|
64
|
+
const conditionalExpressions: BabelTypes.Expression[] = [];
|
|
65
|
+
|
|
66
|
+
for (const [direction, modifiers] of modifiersByDirection) {
|
|
67
|
+
// Parse all classes for this direction together
|
|
68
|
+
const classNames = modifiers.map((m) => m.baseClass).join(" ");
|
|
69
|
+
const styleObject = parseClassName(classNames, state.customTheme);
|
|
70
|
+
|
|
71
|
+
// Check for runtime dimensions (w-screen, h-screen)
|
|
72
|
+
if (hasRuntimeDimensions(styleObject)) {
|
|
73
|
+
throw new Error(
|
|
74
|
+
`w-screen and h-screen cannot be combined with directional modifiers (rtl:, ltr:). ` +
|
|
75
|
+
`Found in: "${direction}:${classNames}". ` +
|
|
76
|
+
`Use w-screen/h-screen without modifiers instead.`,
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const styleKey = generateStyleKey(`${direction}_${classNames}`);
|
|
81
|
+
|
|
82
|
+
// Register style in the registry
|
|
83
|
+
state.styleRegistry.set(styleKey, styleObject);
|
|
84
|
+
|
|
85
|
+
// Create conditional:
|
|
86
|
+
// - For rtl: _twIsRTL && styles._rtl_...
|
|
87
|
+
// - For ltr: !_twIsRTL && styles._ltr_...
|
|
88
|
+
const rtlVariable = t.identifier(state.i18nManagerVariableName);
|
|
89
|
+
const directionCheck = direction === "rtl" ? rtlVariable : t.unaryExpression("!", rtlVariable);
|
|
90
|
+
|
|
91
|
+
const styleReference = t.memberExpression(t.identifier(state.stylesIdentifier), t.identifier(styleKey));
|
|
92
|
+
|
|
93
|
+
const conditionalExpression = t.logicalExpression("&&", directionCheck, styleReference);
|
|
94
|
+
|
|
95
|
+
conditionalExpressions.push(conditionalExpression);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return conditionalExpressions;
|
|
99
|
+
}
|
|
@@ -220,6 +220,128 @@ export function injectColorSchemeHook(
|
|
|
220
220
|
return true;
|
|
221
221
|
}
|
|
222
222
|
|
|
223
|
+
/**
|
|
224
|
+
* Add I18nManager import to the file or merge with existing react-native import
|
|
225
|
+
*/
|
|
226
|
+
export function addI18nManagerImport(path: NodePath<BabelTypes.Program>, t: typeof BabelTypes): void {
|
|
227
|
+
// Check if there's already a value import from react-native
|
|
228
|
+
const body = path.node.body;
|
|
229
|
+
let existingValueImport: BabelTypes.ImportDeclaration | null = null;
|
|
230
|
+
|
|
231
|
+
for (const statement of body) {
|
|
232
|
+
if (t.isImportDeclaration(statement) && statement.source.value === "react-native") {
|
|
233
|
+
// Skip type-only imports (they get erased at runtime)
|
|
234
|
+
if (statement.importKind === "type") {
|
|
235
|
+
continue;
|
|
236
|
+
}
|
|
237
|
+
// Skip namespace imports (import * as RN) - can't add named specifiers to them
|
|
238
|
+
const hasNamespaceImport = statement.specifiers.some((spec) => t.isImportNamespaceSpecifier(spec));
|
|
239
|
+
if (hasNamespaceImport) {
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
existingValueImport = statement;
|
|
243
|
+
break; // Found a value import, we can stop
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
if (existingValueImport) {
|
|
248
|
+
// Check if I18nManager is already imported
|
|
249
|
+
const hasI18nManager = existingValueImport.specifiers.some(
|
|
250
|
+
(spec) =>
|
|
251
|
+
t.isImportSpecifier(spec) &&
|
|
252
|
+
spec.imported.type === "Identifier" &&
|
|
253
|
+
spec.imported.name === "I18nManager",
|
|
254
|
+
);
|
|
255
|
+
|
|
256
|
+
if (!hasI18nManager) {
|
|
257
|
+
// Add I18nManager to existing value import
|
|
258
|
+
existingValueImport.specifiers.push(
|
|
259
|
+
t.importSpecifier(t.identifier("I18nManager"), t.identifier("I18nManager")),
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
} else {
|
|
263
|
+
// No value import exists - create a new one
|
|
264
|
+
// (Don't merge with type-only or namespace imports)
|
|
265
|
+
const importDeclaration = t.importDeclaration(
|
|
266
|
+
[t.importSpecifier(t.identifier("I18nManager"), t.identifier("I18nManager"))],
|
|
267
|
+
t.stringLiteral("react-native"),
|
|
268
|
+
);
|
|
269
|
+
path.unshiftContainer("body", importDeclaration);
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Inject I18nManager.isRTL variable at the top of the file (after imports and directives)
|
|
275
|
+
*
|
|
276
|
+
* Unlike hooks (useColorScheme, useWindowDimensions), I18nManager.isRTL is not a hook
|
|
277
|
+
* and can be accessed at module level. This is injected once per file.
|
|
278
|
+
*
|
|
279
|
+
* @param path - Program path
|
|
280
|
+
* @param variableName - Name for the RTL variable (e.g., '_twIsRTL')
|
|
281
|
+
* @param localIdentifier - Local identifier if I18nManager is already imported with an alias
|
|
282
|
+
* @param t - Babel types
|
|
283
|
+
*/
|
|
284
|
+
export function injectI18nManagerVariable(
|
|
285
|
+
path: NodePath<BabelTypes.Program>,
|
|
286
|
+
variableName: string,
|
|
287
|
+
localIdentifier: string | undefined,
|
|
288
|
+
t: typeof BabelTypes,
|
|
289
|
+
): void {
|
|
290
|
+
const body = path.node.body;
|
|
291
|
+
|
|
292
|
+
// Check if variable is already declared
|
|
293
|
+
for (const statement of body) {
|
|
294
|
+
if (
|
|
295
|
+
t.isVariableDeclaration(statement) &&
|
|
296
|
+
statement.declarations.length > 0 &&
|
|
297
|
+
t.isVariableDeclarator(statement.declarations[0])
|
|
298
|
+
) {
|
|
299
|
+
const declarator = statement.declarations[0];
|
|
300
|
+
if (t.isIdentifier(declarator.id) && declarator.id.name === variableName) {
|
|
301
|
+
return; // Already injected
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
// Use the local identifier if I18nManager was already imported with an alias,
|
|
307
|
+
// otherwise use 'I18nManager'
|
|
308
|
+
// e.g., import { I18nManager as RTL } → use RTL.isRTL
|
|
309
|
+
const identifierToUse = localIdentifier ?? "I18nManager";
|
|
310
|
+
|
|
311
|
+
// Create: const _twIsRTL = I18nManager.isRTL; (or aliased name if already imported)
|
|
312
|
+
const i18nVariable = t.variableDeclaration("const", [
|
|
313
|
+
t.variableDeclarator(
|
|
314
|
+
t.identifier(variableName),
|
|
315
|
+
t.memberExpression(t.identifier(identifierToUse), t.identifier("isRTL")),
|
|
316
|
+
),
|
|
317
|
+
]);
|
|
318
|
+
|
|
319
|
+
// Find the index to insert after all imports and directives ('use client', 'use strict', etc.)
|
|
320
|
+
let insertIndex = 0;
|
|
321
|
+
|
|
322
|
+
for (let i = 0; i < body.length; i++) {
|
|
323
|
+
const statement = body[i];
|
|
324
|
+
|
|
325
|
+
// Skip directives ('use client', 'use strict', etc.)
|
|
326
|
+
if (t.isExpressionStatement(statement) && t.isStringLiteral(statement.expression)) {
|
|
327
|
+
insertIndex = i + 1;
|
|
328
|
+
continue;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// Skip imports
|
|
332
|
+
if (t.isImportDeclaration(statement)) {
|
|
333
|
+
insertIndex = i + 1;
|
|
334
|
+
continue;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// Stop at the first non-directive, non-import statement
|
|
338
|
+
break;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// Insert after imports and directives
|
|
342
|
+
body.splice(insertIndex, 0, i18nVariable);
|
|
343
|
+
}
|
|
344
|
+
|
|
223
345
|
/**
|
|
224
346
|
* Add useWindowDimensions import to the file or merge with existing react-native import
|
|
225
347
|
*/
|
|
@@ -377,20 +499,29 @@ export function injectStylesAtTop(
|
|
|
377
499
|
),
|
|
378
500
|
]);
|
|
379
501
|
|
|
380
|
-
// Find the index to insert after all imports
|
|
502
|
+
// Find the index to insert after all imports and directives ('use client', 'use strict', etc.)
|
|
381
503
|
const body = path.node.body;
|
|
382
504
|
let insertIndex = 0;
|
|
383
505
|
|
|
384
|
-
// Find the last import statement
|
|
385
506
|
for (let i = 0; i < body.length; i++) {
|
|
386
|
-
|
|
507
|
+
const statement = body[i];
|
|
508
|
+
|
|
509
|
+
// Skip directives ('use client', 'use strict', etc.)
|
|
510
|
+
if (t.isExpressionStatement(statement) && t.isStringLiteral(statement.expression)) {
|
|
387
511
|
insertIndex = i + 1;
|
|
388
|
-
|
|
389
|
-
// Stop at the first non-import statement
|
|
390
|
-
break;
|
|
512
|
+
continue;
|
|
391
513
|
}
|
|
514
|
+
|
|
515
|
+
// Skip imports
|
|
516
|
+
if (t.isImportDeclaration(statement)) {
|
|
517
|
+
insertIndex = i + 1;
|
|
518
|
+
continue;
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
// Stop at the first non-directive, non-import statement
|
|
522
|
+
break;
|
|
392
523
|
}
|
|
393
524
|
|
|
394
|
-
// Insert StyleSheet.create after imports
|
|
525
|
+
// Insert StyleSheet.create after imports and directives
|
|
395
526
|
body.splice(insertIndex, 0, styleSheet);
|
|
396
527
|
}
|
|
@@ -8,12 +8,14 @@ import type { CustomTheme, ModifierType, ParsedModifier } from "../../parser/ind
|
|
|
8
8
|
import {
|
|
9
9
|
expandSchemeModifier,
|
|
10
10
|
isColorSchemeModifier,
|
|
11
|
+
isDirectionalModifier,
|
|
11
12
|
isPlatformModifier,
|
|
12
13
|
isSchemeModifier,
|
|
13
14
|
} from "../../parser/index.js";
|
|
14
15
|
import type { SchemeModifierConfig } from "../../types/config.js";
|
|
15
16
|
import type { StyleObject } from "../../types/core.js";
|
|
16
17
|
import { processColorSchemeModifiers } from "./colorSchemeModifierProcessing.js";
|
|
18
|
+
import { processDirectionalModifiers } from "./directionalModifierProcessing.js";
|
|
17
19
|
import { processPlatformModifiers } from "./platformModifierProcessing.js";
|
|
18
20
|
import { hasRuntimeDimensions } from "./windowDimensionsProcessing.js";
|
|
19
21
|
|
|
@@ -33,6 +35,9 @@ export interface TwProcessingState {
|
|
|
33
35
|
colorSchemeLocalIdentifier?: string;
|
|
34
36
|
// Platform support (for ios:/android:/web: modifiers)
|
|
35
37
|
needsPlatformImport: boolean;
|
|
38
|
+
// Directional support (for rtl:/ltr: modifiers)
|
|
39
|
+
needsI18nManagerImport: boolean;
|
|
40
|
+
i18nManagerVariableName: string;
|
|
36
41
|
}
|
|
37
42
|
|
|
38
43
|
/**
|
|
@@ -102,11 +107,15 @@ export function processTwCall(
|
|
|
102
107
|
objectProperties.push(t.objectProperty(t.identifier("style"), t.objectExpression([])));
|
|
103
108
|
}
|
|
104
109
|
|
|
105
|
-
// Separate color-scheme and
|
|
110
|
+
// Separate color-scheme, platform, and directional modifiers from other modifiers
|
|
106
111
|
const colorSchemeModifiers = modifierClasses.filter((m) => isColorSchemeModifier(m.modifier));
|
|
107
112
|
const platformModifiers = modifierClasses.filter((m) => isPlatformModifier(m.modifier));
|
|
113
|
+
const directionalModifiers = modifierClasses.filter((m) => isDirectionalModifier(m.modifier));
|
|
108
114
|
const otherModifiers = modifierClasses.filter(
|
|
109
|
-
(m) =>
|
|
115
|
+
(m) =>
|
|
116
|
+
!isColorSchemeModifier(m.modifier) &&
|
|
117
|
+
!isPlatformModifier(m.modifier) &&
|
|
118
|
+
!isDirectionalModifier(m.modifier),
|
|
110
119
|
);
|
|
111
120
|
|
|
112
121
|
// Check if we need color scheme support
|
|
@@ -293,7 +302,87 @@ export function processTwCall(
|
|
|
293
302
|
}
|
|
294
303
|
}
|
|
295
304
|
|
|
296
|
-
//
|
|
305
|
+
// Process directional modifiers if present
|
|
306
|
+
const hasDirectionalModifiers = directionalModifiers.length > 0;
|
|
307
|
+
|
|
308
|
+
if (hasDirectionalModifiers) {
|
|
309
|
+
// Mark that we need I18nManager import
|
|
310
|
+
state.needsI18nManagerImport = true;
|
|
311
|
+
|
|
312
|
+
// Generate directional conditional expressions
|
|
313
|
+
const directionalConditionals = processDirectionalModifiers(
|
|
314
|
+
directionalModifiers,
|
|
315
|
+
state,
|
|
316
|
+
parseClassName,
|
|
317
|
+
generateStyleKey,
|
|
318
|
+
t,
|
|
319
|
+
);
|
|
320
|
+
|
|
321
|
+
// If we already have a style array (from color scheme or platform modifiers), add to it
|
|
322
|
+
// Otherwise, convert style property to an array
|
|
323
|
+
const styleProperty = objectProperties.find(
|
|
324
|
+
(prop) => t.isIdentifier(prop.key) && prop.key.name === "style",
|
|
325
|
+
);
|
|
326
|
+
|
|
327
|
+
if (styleProperty && t.isArrayExpression(styleProperty.value)) {
|
|
328
|
+
// Already have style array, add directional conditionals to it
|
|
329
|
+
styleProperty.value.elements.push(...directionalConditionals);
|
|
330
|
+
} else {
|
|
331
|
+
// No existing array, create style array with base + directional conditionals
|
|
332
|
+
const styleArrayElements: BabelTypes.Expression[] = [];
|
|
333
|
+
|
|
334
|
+
// Add base style if present
|
|
335
|
+
if (baseClasses.length > 0) {
|
|
336
|
+
const baseClassName = baseClasses.join(" ");
|
|
337
|
+
const baseStyleObject = parseClassName(baseClassName, state.customTheme);
|
|
338
|
+
const baseStyleKey = generateStyleKey(baseClassName);
|
|
339
|
+
state.styleRegistry.set(baseStyleKey, baseStyleObject);
|
|
340
|
+
styleArrayElements.push(
|
|
341
|
+
t.memberExpression(t.identifier(state.stylesIdentifier), t.identifier(baseStyleKey)),
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// Add directional conditionals
|
|
346
|
+
styleArrayElements.push(...directionalConditionals);
|
|
347
|
+
|
|
348
|
+
// Replace style property with array
|
|
349
|
+
objectProperties[0] = t.objectProperty(t.identifier("style"), t.arrayExpression(styleArrayElements));
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
// Also add rtlStyle/ltrStyle properties for manual processing
|
|
353
|
+
const rtlModifiers = directionalModifiers.filter((m) => m.modifier === "rtl");
|
|
354
|
+
const ltrModifiers = directionalModifiers.filter((m) => m.modifier === "ltr");
|
|
355
|
+
|
|
356
|
+
if (rtlModifiers.length > 0) {
|
|
357
|
+
const rtlClassNames = rtlModifiers.map((m) => m.baseClass).join(" ");
|
|
358
|
+
const rtlStyleObject = parseClassName(rtlClassNames, state.customTheme);
|
|
359
|
+
const rtlStyleKey = generateStyleKey(`rtl_${rtlClassNames}`);
|
|
360
|
+
state.styleRegistry.set(rtlStyleKey, rtlStyleObject);
|
|
361
|
+
|
|
362
|
+
objectProperties.push(
|
|
363
|
+
t.objectProperty(
|
|
364
|
+
t.identifier("rtlStyle"),
|
|
365
|
+
t.memberExpression(t.identifier(state.stylesIdentifier), t.identifier(rtlStyleKey)),
|
|
366
|
+
),
|
|
367
|
+
);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
if (ltrModifiers.length > 0) {
|
|
371
|
+
const ltrClassNames = ltrModifiers.map((m) => m.baseClass).join(" ");
|
|
372
|
+
const ltrStyleObject = parseClassName(ltrClassNames, state.customTheme);
|
|
373
|
+
const ltrStyleKey = generateStyleKey(`ltr_${ltrClassNames}`);
|
|
374
|
+
state.styleRegistry.set(ltrStyleKey, ltrStyleObject);
|
|
375
|
+
|
|
376
|
+
objectProperties.push(
|
|
377
|
+
t.objectProperty(
|
|
378
|
+
t.identifier("ltrStyle"),
|
|
379
|
+
t.memberExpression(t.identifier(state.stylesIdentifier), t.identifier(ltrStyleKey)),
|
|
380
|
+
),
|
|
381
|
+
);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// Group other modifiers by type (non-color-scheme, non-platform, and non-directional modifiers)
|
|
297
386
|
const modifiersByType = new Map<ModifierType, ParsedModifier[]>();
|
|
298
387
|
for (const mod of otherModifiers) {
|
|
299
388
|
if (!modifiersByType.has(mod.modifier)) {
|
|
@@ -385,3 +385,107 @@ describe("parseBorder - color pattern detection", () => {
|
|
|
385
385
|
expect(parseBorder("border-l-[8px]")).toEqual({ borderLeftWidth: 8 });
|
|
386
386
|
});
|
|
387
387
|
});
|
|
388
|
+
|
|
389
|
+
describe("parseBorder - logical border width (RTL-aware)", () => {
|
|
390
|
+
it("should parse border start width", () => {
|
|
391
|
+
expect(parseBorder("border-s")).toEqual({ borderStartWidth: 1 });
|
|
392
|
+
expect(parseBorder("border-s-0")).toEqual({ borderStartWidth: 0 });
|
|
393
|
+
expect(parseBorder("border-s-2")).toEqual({ borderStartWidth: 2 });
|
|
394
|
+
expect(parseBorder("border-s-4")).toEqual({ borderStartWidth: 4 });
|
|
395
|
+
expect(parseBorder("border-s-8")).toEqual({ borderStartWidth: 8 });
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
it("should parse border end width", () => {
|
|
399
|
+
expect(parseBorder("border-e")).toEqual({ borderEndWidth: 1 });
|
|
400
|
+
expect(parseBorder("border-e-0")).toEqual({ borderEndWidth: 0 });
|
|
401
|
+
expect(parseBorder("border-e-2")).toEqual({ borderEndWidth: 2 });
|
|
402
|
+
expect(parseBorder("border-e-4")).toEqual({ borderEndWidth: 4 });
|
|
403
|
+
expect(parseBorder("border-e-8")).toEqual({ borderEndWidth: 8 });
|
|
404
|
+
});
|
|
405
|
+
|
|
406
|
+
it("should parse border start/end with arbitrary values", () => {
|
|
407
|
+
expect(parseBorder("border-s-[3px]")).toEqual({ borderStartWidth: 3 });
|
|
408
|
+
expect(parseBorder("border-s-[5]")).toEqual({ borderStartWidth: 5 });
|
|
409
|
+
expect(parseBorder("border-e-[3px]")).toEqual({ borderEndWidth: 3 });
|
|
410
|
+
expect(parseBorder("border-e-[5]")).toEqual({ borderEndWidth: 5 });
|
|
411
|
+
});
|
|
412
|
+
});
|
|
413
|
+
|
|
414
|
+
describe("parseBorder - logical border radius sides (RTL-aware)", () => {
|
|
415
|
+
it("should parse rounded start (both top and bottom start corners)", () => {
|
|
416
|
+
expect(parseBorder("rounded-s")).toEqual({
|
|
417
|
+
borderTopStartRadius: 4,
|
|
418
|
+
borderBottomStartRadius: 4,
|
|
419
|
+
});
|
|
420
|
+
expect(parseBorder("rounded-s-lg")).toEqual({
|
|
421
|
+
borderTopStartRadius: 8,
|
|
422
|
+
borderBottomStartRadius: 8,
|
|
423
|
+
});
|
|
424
|
+
expect(parseBorder("rounded-s-[12px]")).toEqual({
|
|
425
|
+
borderTopStartRadius: 12,
|
|
426
|
+
borderBottomStartRadius: 12,
|
|
427
|
+
});
|
|
428
|
+
});
|
|
429
|
+
|
|
430
|
+
it("should parse rounded end (both top and bottom end corners)", () => {
|
|
431
|
+
expect(parseBorder("rounded-e")).toEqual({
|
|
432
|
+
borderTopEndRadius: 4,
|
|
433
|
+
borderBottomEndRadius: 4,
|
|
434
|
+
});
|
|
435
|
+
expect(parseBorder("rounded-e-lg")).toEqual({
|
|
436
|
+
borderTopEndRadius: 8,
|
|
437
|
+
borderBottomEndRadius: 8,
|
|
438
|
+
});
|
|
439
|
+
expect(parseBorder("rounded-e-[12px]")).toEqual({
|
|
440
|
+
borderTopEndRadius: 12,
|
|
441
|
+
borderBottomEndRadius: 12,
|
|
442
|
+
});
|
|
443
|
+
});
|
|
444
|
+
});
|
|
445
|
+
|
|
446
|
+
describe("parseBorder - logical border radius corners (RTL-aware)", () => {
|
|
447
|
+
it("should parse rounded start-start (top-start corner)", () => {
|
|
448
|
+
expect(parseBorder("rounded-ss")).toEqual({ borderTopStartRadius: 4 });
|
|
449
|
+
expect(parseBorder("rounded-ss-lg")).toEqual({ borderTopStartRadius: 8 });
|
|
450
|
+
expect(parseBorder("rounded-ss-[12px]")).toEqual({
|
|
451
|
+
borderTopStartRadius: 12,
|
|
452
|
+
});
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
it("should parse rounded start-end (top-end corner)", () => {
|
|
456
|
+
expect(parseBorder("rounded-se")).toEqual({ borderTopEndRadius: 4 });
|
|
457
|
+
expect(parseBorder("rounded-se-lg")).toEqual({ borderTopEndRadius: 8 });
|
|
458
|
+
expect(parseBorder("rounded-se-[12px]")).toEqual({
|
|
459
|
+
borderTopEndRadius: 12,
|
|
460
|
+
});
|
|
461
|
+
});
|
|
462
|
+
|
|
463
|
+
it("should parse rounded end-start (bottom-start corner)", () => {
|
|
464
|
+
expect(parseBorder("rounded-es")).toEqual({ borderBottomStartRadius: 4 });
|
|
465
|
+
expect(parseBorder("rounded-es-lg")).toEqual({ borderBottomStartRadius: 8 });
|
|
466
|
+
expect(parseBorder("rounded-es-[12px]")).toEqual({
|
|
467
|
+
borderBottomStartRadius: 12,
|
|
468
|
+
});
|
|
469
|
+
});
|
|
470
|
+
|
|
471
|
+
it("should parse rounded end-end (bottom-end corner)", () => {
|
|
472
|
+
expect(parseBorder("rounded-ee")).toEqual({ borderBottomEndRadius: 4 });
|
|
473
|
+
expect(parseBorder("rounded-ee-lg")).toEqual({ borderBottomEndRadius: 8 });
|
|
474
|
+
expect(parseBorder("rounded-ee-[12px]")).toEqual({
|
|
475
|
+
borderBottomEndRadius: 12,
|
|
476
|
+
});
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
it("should parse all logical corners with different sizes", () => {
|
|
480
|
+
// Using full scale to verify all sizes work
|
|
481
|
+
expect(parseBorder("rounded-ss-none")).toEqual({ borderTopStartRadius: 0 });
|
|
482
|
+
expect(parseBorder("rounded-se-sm")).toEqual({ borderTopEndRadius: 2 });
|
|
483
|
+
expect(parseBorder("rounded-es-md")).toEqual({ borderBottomStartRadius: 6 });
|
|
484
|
+
expect(parseBorder("rounded-ee-xl")).toEqual({ borderBottomEndRadius: 12 });
|
|
485
|
+
expect(parseBorder("rounded-ss-2xl")).toEqual({ borderTopStartRadius: 16 });
|
|
486
|
+
expect(parseBorder("rounded-se-3xl")).toEqual({ borderTopEndRadius: 24 });
|
|
487
|
+
expect(parseBorder("rounded-es-full")).toEqual({
|
|
488
|
+
borderBottomStartRadius: 9999,
|
|
489
|
+
});
|
|
490
|
+
});
|
|
491
|
+
});
|
package/src/parser/borders.ts
CHANGED
|
@@ -35,10 +35,12 @@ const BORDER_WIDTH_PROP_MAP: Record<string, string> = {
|
|
|
35
35
|
r: "borderRightWidth",
|
|
36
36
|
b: "borderBottomWidth",
|
|
37
37
|
l: "borderLeftWidth",
|
|
38
|
+
s: "borderStartWidth",
|
|
39
|
+
e: "borderEndWidth",
|
|
38
40
|
};
|
|
39
41
|
|
|
40
42
|
/**
|
|
41
|
-
* Property mapping for border radius corners
|
|
43
|
+
* Property mapping for border radius corners (physical)
|
|
42
44
|
*/
|
|
43
45
|
const BORDER_RADIUS_CORNER_MAP: Record<string, string> = {
|
|
44
46
|
tl: "borderTopLeftRadius",
|
|
@@ -47,6 +49,18 @@ const BORDER_RADIUS_CORNER_MAP: Record<string, string> = {
|
|
|
47
49
|
br: "borderBottomRightRadius",
|
|
48
50
|
};
|
|
49
51
|
|
|
52
|
+
/**
|
|
53
|
+
* Property mapping for border radius corners (logical/RTL-aware)
|
|
54
|
+
* ss = start-start (top-start), se = start-end (top-end)
|
|
55
|
+
* es = end-start (bottom-start), ee = end-end (bottom-end)
|
|
56
|
+
*/
|
|
57
|
+
const BORDER_RADIUS_LOGICAL_CORNER_MAP: Record<string, string> = {
|
|
58
|
+
ss: "borderTopStartRadius",
|
|
59
|
+
se: "borderTopEndRadius",
|
|
60
|
+
es: "borderBottomStartRadius",
|
|
61
|
+
ee: "borderBottomEndRadius",
|
|
62
|
+
};
|
|
63
|
+
|
|
50
64
|
/**
|
|
51
65
|
* Property mapping for border radius sides (returns array of properties)
|
|
52
66
|
*/
|
|
@@ -55,6 +69,8 @@ const BORDER_RADIUS_SIDE_MAP: Record<string, string[]> = {
|
|
|
55
69
|
r: ["borderTopRightRadius", "borderBottomRightRadius"],
|
|
56
70
|
b: ["borderBottomLeftRadius", "borderBottomRightRadius"],
|
|
57
71
|
l: ["borderTopLeftRadius", "borderBottomLeftRadius"],
|
|
72
|
+
s: ["borderTopStartRadius", "borderBottomStartRadius"],
|
|
73
|
+
e: ["borderTopEndRadius", "borderBottomEndRadius"],
|
|
58
74
|
};
|
|
59
75
|
|
|
60
76
|
/**
|
|
@@ -141,16 +157,17 @@ export function parseBorder(cls: string, customColors?: Record<string, string>):
|
|
|
141
157
|
* @param customColors - Optional custom colors (passed to parseColor for pattern detection)
|
|
142
158
|
*/
|
|
143
159
|
function parseBorderWidth(cls: string, customColors?: Record<string, string>): StyleObject | null {
|
|
144
|
-
// Directional borders: border-t, border-t-2, border-t-[8px]
|
|
160
|
+
// Directional borders: border-t, border-t-2, border-t-[8px], border-s, border-e (RTL-aware)
|
|
145
161
|
// Note: border-x and border-y are handled by parseColor for colors only
|
|
146
|
-
const dirMatch = cls.match(/^border-([
|
|
162
|
+
const dirMatch = cls.match(/^border-([trblse])(?:-(.+))?$/);
|
|
147
163
|
if (dirMatch) {
|
|
148
164
|
const dir = dirMatch[1];
|
|
149
165
|
const valueStr = dirMatch[2] || ""; // empty string for border-t
|
|
150
166
|
|
|
151
167
|
// If it's a color pattern, let parseColor handle it
|
|
152
168
|
// Try to parse as color - if it succeeds, return null (let parseColor handle it)
|
|
153
|
-
|
|
169
|
+
// Note: We skip color check for s/e since React Native doesn't support borderStartColor/borderEndColor
|
|
170
|
+
if (valueStr && dir !== "s" && dir !== "e") {
|
|
154
171
|
const colorResult = parseColor(cls, customColors);
|
|
155
172
|
if (colorResult !== null) {
|
|
156
173
|
return null; // It's a color, let parseColor handle it
|
|
@@ -220,7 +237,7 @@ function parseBorderRadius(cls: string): StyleObject | null {
|
|
|
220
237
|
return null;
|
|
221
238
|
}
|
|
222
239
|
|
|
223
|
-
// Specific corners: rounded-tl, rounded-tl-lg, rounded-tl-[8px]
|
|
240
|
+
// Specific physical corners: rounded-tl, rounded-tl-lg, rounded-tl-[8px]
|
|
224
241
|
const cornerMatch = rest.match(/^(tl|tr|bl|br)(?:-(.+))?$/);
|
|
225
242
|
if (cornerMatch) {
|
|
226
243
|
const corner = cornerMatch[1];
|
|
@@ -244,8 +261,34 @@ function parseBorderRadius(cls: string): StyleObject | null {
|
|
|
244
261
|
return null;
|
|
245
262
|
}
|
|
246
263
|
|
|
247
|
-
//
|
|
248
|
-
|
|
264
|
+
// Logical corners (RTL-aware): rounded-ss, rounded-se, rounded-es, rounded-ee
|
|
265
|
+
// ss = start-start (top-start), se = start-end (top-end)
|
|
266
|
+
// es = end-start (bottom-start), ee = end-end (bottom-end)
|
|
267
|
+
const logicalCornerMatch = rest.match(/^(ss|se|es|ee)(?:-(.+))?$/);
|
|
268
|
+
if (logicalCornerMatch) {
|
|
269
|
+
const corner = logicalCornerMatch[1];
|
|
270
|
+
const valueStr = logicalCornerMatch[2] || ""; // empty string for rounded-ss
|
|
271
|
+
|
|
272
|
+
// Try arbitrary value first
|
|
273
|
+
if (valueStr.startsWith("[")) {
|
|
274
|
+
const arbitraryValue = parseArbitraryBorderRadius(valueStr);
|
|
275
|
+
if (arbitraryValue !== null) {
|
|
276
|
+
return { [BORDER_RADIUS_LOGICAL_CORNER_MAP[corner]]: arbitraryValue };
|
|
277
|
+
}
|
|
278
|
+
return null;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// Try preset scale
|
|
282
|
+
const scaleValue = BORDER_RADIUS_SCALE[valueStr];
|
|
283
|
+
if (scaleValue !== undefined) {
|
|
284
|
+
return { [BORDER_RADIUS_LOGICAL_CORNER_MAP[corner]]: scaleValue };
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
return null;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// Sides: rounded-t, rounded-t-lg, rounded-t-[8px], rounded-s, rounded-e (RTL-aware)
|
|
291
|
+
const sideMatch = rest.match(/^([trblse])(?:-(.+))?$/);
|
|
249
292
|
if (sideMatch) {
|
|
250
293
|
const side = sideMatch[1];
|
|
251
294
|
const valueStr = sideMatch[2] || ""; // empty string for rounded-t
|
package/src/parser/index.ts
CHANGED
|
@@ -21,6 +21,7 @@ export type CustomTheme = {
|
|
|
21
21
|
colors?: Record<string, string>;
|
|
22
22
|
fontFamily?: Record<string, string>;
|
|
23
23
|
fontSize?: Record<string, number>;
|
|
24
|
+
spacing?: Record<string, number>;
|
|
24
25
|
};
|
|
25
26
|
|
|
26
27
|
/**
|
|
@@ -50,17 +51,17 @@ export function parseClassName(className: string, customTheme?: CustomTheme): St
|
|
|
50
51
|
export function parseClass(cls: string, customTheme?: CustomTheme): StyleObject {
|
|
51
52
|
// Try each parser in order
|
|
52
53
|
// Note: parseBorder must come before parseColor to avoid border-[3px] being parsed as a color
|
|
53
|
-
//
|
|
54
|
+
// Parsers receive relevant custom theme properties
|
|
54
55
|
const parsers: Array<(cls: string) => StyleObject | null> = [
|
|
55
|
-
parseSpacing,
|
|
56
|
+
(cls: string) => parseSpacing(cls, customTheme?.spacing),
|
|
56
57
|
(cls: string) => parseBorder(cls, customTheme?.colors),
|
|
57
58
|
(cls: string) => parseColor(cls, customTheme?.colors),
|
|
58
|
-
parseLayout,
|
|
59
|
+
(cls: string) => parseLayout(cls, customTheme?.spacing),
|
|
59
60
|
(cls: string) => parseTypography(cls, customTheme?.fontFamily, customTheme?.fontSize),
|
|
60
|
-
parseSizing,
|
|
61
|
+
(cls: string) => parseSizing(cls, customTheme?.spacing),
|
|
61
62
|
parseShadow,
|
|
62
63
|
parseAspectRatio,
|
|
63
|
-
parseTransform,
|
|
64
|
+
(cls: string) => parseTransform(cls, customTheme?.spacing),
|
|
64
65
|
];
|
|
65
66
|
|
|
66
67
|
for (const parser of parsers) {
|
|
@@ -97,6 +98,7 @@ export {
|
|
|
97
98
|
hasModifier,
|
|
98
99
|
isColorClass,
|
|
99
100
|
isColorSchemeModifier,
|
|
101
|
+
isDirectionalModifier,
|
|
100
102
|
isPlatformModifier,
|
|
101
103
|
isSchemeModifier,
|
|
102
104
|
isStateModifier,
|
|
@@ -105,6 +107,7 @@ export {
|
|
|
105
107
|
} from "./modifiers";
|
|
106
108
|
export type {
|
|
107
109
|
ColorSchemeModifierType,
|
|
110
|
+
DirectionalModifierType,
|
|
108
111
|
ModifierType,
|
|
109
112
|
ParsedModifier,
|
|
110
113
|
PlatformModifierType,
|