@mgcrea/react-native-tailwind 0.3.0 → 0.4.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 +16 -26
- package/dist/babel/index.cjs +6 -5
- package/dist/babel/index.ts +9 -6
- package/package.json +1 -1
- package/src/babel/index.ts +9 -6
package/README.md
CHANGED
|
@@ -95,11 +95,11 @@ The Babel plugin transforms your code at compile time:
|
|
|
95
95
|
```tsx
|
|
96
96
|
import { StyleSheet } from "react-native";
|
|
97
97
|
|
|
98
|
-
<View style={
|
|
99
|
-
<ScrollView contentContainerStyle={
|
|
100
|
-
<FlatList columnWrapperStyle={
|
|
98
|
+
<View style={_twStyles._bg_blue_500_m_4_p_2_rounded_lg} />;
|
|
99
|
+
<ScrollView contentContainerStyle={_twStyles._gap_4_items_center} />;
|
|
100
|
+
<FlatList columnWrapperStyle={_twStyles._gap_4} ListHeaderComponentStyle={_twStyles._bg_gray_100_p_4} />;
|
|
101
101
|
|
|
102
|
-
const
|
|
102
|
+
const _twStyles = StyleSheet.create({
|
|
103
103
|
_bg_blue_500_m_4_p_2_rounded_lg: {
|
|
104
104
|
margin: 16,
|
|
105
105
|
padding: 8,
|
|
@@ -279,18 +279,16 @@ export function ToggleButton() {
|
|
|
279
279
|
```tsx
|
|
280
280
|
<Pressable
|
|
281
281
|
onPress={() => setIsActive(!isActive)}
|
|
282
|
-
style={isActive ?
|
|
282
|
+
style={isActive ? _twStyles._bg_green_500_p_4 : _twStyles._bg_red_500_p_4}
|
|
283
283
|
>
|
|
284
|
-
<Text style={
|
|
284
|
+
<Text style={_twStyles._text_white}>{isActive ? "Active" : "Inactive"}</Text>
|
|
285
285
|
</Pressable>
|
|
286
286
|
```
|
|
287
287
|
|
|
288
288
|
**Template Literal (Static + Dynamic):**
|
|
289
289
|
|
|
290
290
|
```tsx
|
|
291
|
-
<Pressable
|
|
292
|
-
className={`border-2 rounded-lg ${isActive ? "bg-blue-500" : "bg-gray-300"} p-4`}
|
|
293
|
-
>
|
|
291
|
+
<Pressable className={`border-2 rounded-lg ${isActive ? "bg-blue-500" : "bg-gray-300"} p-4`}>
|
|
294
292
|
<Text className="text-white">Click Me</Text>
|
|
295
293
|
</Pressable>
|
|
296
294
|
```
|
|
@@ -300,13 +298,13 @@ export function ToggleButton() {
|
|
|
300
298
|
```tsx
|
|
301
299
|
<Pressable
|
|
302
300
|
style={[
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
isActive ?
|
|
306
|
-
|
|
301
|
+
_twStyles._border_2,
|
|
302
|
+
_twStyles._rounded_lg,
|
|
303
|
+
isActive ? _twStyles._bg_blue_500 : _twStyles._bg_gray_300,
|
|
304
|
+
_twStyles._p_4,
|
|
307
305
|
]}
|
|
308
306
|
>
|
|
309
|
-
<Text style={
|
|
307
|
+
<Text style={_twStyles._text_white}>Click Me</Text>
|
|
310
308
|
</Pressable>
|
|
311
309
|
```
|
|
312
310
|
|
|
@@ -321,13 +319,7 @@ export function ToggleButton() {
|
|
|
321
319
|
**Transforms to:**
|
|
322
320
|
|
|
323
321
|
```tsx
|
|
324
|
-
<View
|
|
325
|
-
style={[
|
|
326
|
-
styles._p_4,
|
|
327
|
-
styles._bg_gray_100,
|
|
328
|
-
isActive && styles._border_4_border_purple_500
|
|
329
|
-
]}
|
|
330
|
-
>
|
|
322
|
+
<View style={[_twStyles._p_4, _twStyles._bg_gray_100, isActive && _twStyles._border_4_border_purple_500]}>
|
|
331
323
|
<Text>Content</Text>
|
|
332
324
|
</View>
|
|
333
325
|
```
|
|
@@ -335,9 +327,7 @@ export function ToggleButton() {
|
|
|
335
327
|
**Multiple Conditionals:**
|
|
336
328
|
|
|
337
329
|
```tsx
|
|
338
|
-
<View
|
|
339
|
-
className={`${size === "lg" ? "p-8" : "p-4"} ${isActive ? "bg-blue-500" : "bg-gray-400"}`}
|
|
340
|
-
>
|
|
330
|
+
<View className={`${size === "lg" ? "p-8" : "p-4"} ${isActive ? "bg-blue-500" : "bg-gray-400"}`}>
|
|
341
331
|
<Text>Dynamic Size & Color</Text>
|
|
342
332
|
</View>
|
|
343
333
|
```
|
|
@@ -373,7 +363,7 @@ You can use inline `style` prop alongside `className`:
|
|
|
373
363
|
The Babel plugin will merge them:
|
|
374
364
|
|
|
375
365
|
```tsx
|
|
376
|
-
<View style={[
|
|
366
|
+
<View style={[_twStyles._className_styles, { paddingTop: safeAreaInsets.top }]}>
|
|
377
367
|
<Text>Content</Text>
|
|
378
368
|
</View>
|
|
379
369
|
```
|
|
@@ -567,7 +557,7 @@ Access the parser and constants programmatically:
|
|
|
567
557
|
import { parseClassName, COLORS, SPACING_SCALE } from "@mgcrea/react-native-tailwind";
|
|
568
558
|
|
|
569
559
|
// Parse className strings
|
|
570
|
-
const
|
|
560
|
+
const _twStyles = parseClassName("m-4 p-2 bg-blue-500");
|
|
571
561
|
// Returns: { margin: 16, padding: 8, backgroundColor: '#3B82F6' }
|
|
572
562
|
|
|
573
563
|
// Access default scales
|
package/dist/babel/index.cjs
CHANGED
|
@@ -950,6 +950,7 @@ function extractCustomColors(filename) {
|
|
|
950
950
|
}
|
|
951
951
|
|
|
952
952
|
// src/babel/index.ts
|
|
953
|
+
var STYLES_IDENTIFIER = "_twStyles";
|
|
953
954
|
var SUPPORTED_CLASS_ATTRIBUTES = [
|
|
954
955
|
"className",
|
|
955
956
|
"contentContainerClassName",
|
|
@@ -997,7 +998,7 @@ function processTemplateLiteral(node, state, t) {
|
|
|
997
998
|
const styleKey = generateStyleKey2(cls);
|
|
998
999
|
state.styleRegistry.set(styleKey, styleObject);
|
|
999
1000
|
staticParts.push(cls);
|
|
1000
|
-
parts.push(t.memberExpression(t.identifier(
|
|
1001
|
+
parts.push(t.memberExpression(t.identifier(STYLES_IDENTIFIER), t.identifier(styleKey)));
|
|
1001
1002
|
}
|
|
1002
1003
|
}
|
|
1003
1004
|
if (i < node.expressions.length) {
|
|
@@ -1052,7 +1053,7 @@ function processStringOrExpression(node, state, t) {
|
|
|
1052
1053
|
const styleObject = parseClassName2(className, state.customColors);
|
|
1053
1054
|
const styleKey = generateStyleKey2(className);
|
|
1054
1055
|
state.styleRegistry.set(styleKey, styleObject);
|
|
1055
|
-
return t.memberExpression(t.identifier(
|
|
1056
|
+
return t.memberExpression(t.identifier(STYLES_IDENTIFIER), t.identifier(styleKey));
|
|
1056
1057
|
}
|
|
1057
1058
|
if (t.isConditionalExpression(node)) {
|
|
1058
1059
|
const result = processConditionalExpression(node, state, t);
|
|
@@ -1187,14 +1188,14 @@ function addStyleSheetImport(path2, t) {
|
|
|
1187
1188
|
function replaceWithStyleAttribute(classNamePath, styleKey, targetStyleProp, t) {
|
|
1188
1189
|
const styleAttribute = t.jsxAttribute(
|
|
1189
1190
|
t.jsxIdentifier(targetStyleProp),
|
|
1190
|
-
t.jsxExpressionContainer(t.memberExpression(t.identifier(
|
|
1191
|
+
t.jsxExpressionContainer(t.memberExpression(t.identifier(STYLES_IDENTIFIER), t.identifier(styleKey)))
|
|
1191
1192
|
);
|
|
1192
1193
|
classNamePath.replaceWith(styleAttribute);
|
|
1193
1194
|
}
|
|
1194
1195
|
function mergeStyleAttribute(classNamePath, styleAttribute, styleKey, t) {
|
|
1195
1196
|
const existingStyle = styleAttribute.value.expression;
|
|
1196
1197
|
const styleArray = t.arrayExpression([
|
|
1197
|
-
t.memberExpression(t.identifier(
|
|
1198
|
+
t.memberExpression(t.identifier(STYLES_IDENTIFIER), t.identifier(styleKey)),
|
|
1198
1199
|
existingStyle
|
|
1199
1200
|
]);
|
|
1200
1201
|
styleAttribute.value = t.jsxExpressionContainer(styleArray);
|
|
@@ -1236,7 +1237,7 @@ function injectStyles(path2, styleRegistry, t) {
|
|
|
1236
1237
|
}
|
|
1237
1238
|
const styleSheet = t.variableDeclaration("const", [
|
|
1238
1239
|
t.variableDeclarator(
|
|
1239
|
-
t.identifier(
|
|
1240
|
+
t.identifier(STYLES_IDENTIFIER),
|
|
1240
1241
|
t.callExpression(t.memberExpression(t.identifier("StyleSheet"), t.identifier("create")), [
|
|
1241
1242
|
t.objectExpression(styleProperties)
|
|
1242
1243
|
])
|
package/dist/babel/index.ts
CHANGED
|
@@ -22,6 +22,9 @@ type PluginState = PluginPass & {
|
|
|
22
22
|
customColors: Record<string, string>;
|
|
23
23
|
};
|
|
24
24
|
|
|
25
|
+
// Use a unique identifier to avoid conflicts with user's own styles
|
|
26
|
+
const STYLES_IDENTIFIER = "_twStyles";
|
|
27
|
+
|
|
25
28
|
/**
|
|
26
29
|
* Supported className-like attributes
|
|
27
30
|
*/
|
|
@@ -117,7 +120,7 @@ function processTemplateLiteral(
|
|
|
117
120
|
staticParts.push(cls);
|
|
118
121
|
|
|
119
122
|
// Add to parts array
|
|
120
|
-
parts.push(t.memberExpression(t.identifier(
|
|
123
|
+
parts.push(t.memberExpression(t.identifier(STYLES_IDENTIFIER), t.identifier(styleKey)));
|
|
121
124
|
}
|
|
122
125
|
}
|
|
123
126
|
|
|
@@ -216,7 +219,7 @@ function processStringOrExpression(node: any, state: PluginState, t: typeof Babe
|
|
|
216
219
|
const styleKey = generateStyleKey(className);
|
|
217
220
|
state.styleRegistry.set(styleKey, styleObject);
|
|
218
221
|
|
|
219
|
-
return t.memberExpression(t.identifier(
|
|
222
|
+
return t.memberExpression(t.identifier(STYLES_IDENTIFIER), t.identifier(styleKey));
|
|
220
223
|
}
|
|
221
224
|
|
|
222
225
|
// Handle nested expressions recursively
|
|
@@ -426,7 +429,7 @@ function replaceWithStyleAttribute(
|
|
|
426
429
|
) {
|
|
427
430
|
const styleAttribute = t.jsxAttribute(
|
|
428
431
|
t.jsxIdentifier(targetStyleProp),
|
|
429
|
-
t.jsxExpressionContainer(t.memberExpression(t.identifier(
|
|
432
|
+
t.jsxExpressionContainer(t.memberExpression(t.identifier(STYLES_IDENTIFIER), t.identifier(styleKey))),
|
|
430
433
|
);
|
|
431
434
|
|
|
432
435
|
classNamePath.replaceWith(styleAttribute);
|
|
@@ -446,7 +449,7 @@ function mergeStyleAttribute(
|
|
|
446
449
|
// Create array with className styles first, then existing styles
|
|
447
450
|
// This allows existing styles to override className styles
|
|
448
451
|
const styleArray = t.arrayExpression([
|
|
449
|
-
t.memberExpression(t.identifier(
|
|
452
|
+
t.memberExpression(t.identifier(STYLES_IDENTIFIER), t.identifier(styleKey)),
|
|
450
453
|
existingStyle,
|
|
451
454
|
]);
|
|
452
455
|
|
|
@@ -531,10 +534,10 @@ function injectStyles(
|
|
|
531
534
|
styleProperties.push(t.objectProperty(t.identifier(key), t.objectExpression(properties)));
|
|
532
535
|
}
|
|
533
536
|
|
|
534
|
-
// Create: const
|
|
537
|
+
// Create: const _tailwindStyles = StyleSheet.create({ ... })
|
|
535
538
|
const styleSheet = t.variableDeclaration("const", [
|
|
536
539
|
t.variableDeclarator(
|
|
537
|
-
t.identifier(
|
|
540
|
+
t.identifier(STYLES_IDENTIFIER),
|
|
538
541
|
t.callExpression(t.memberExpression(t.identifier("StyleSheet"), t.identifier("create")), [
|
|
539
542
|
t.objectExpression(styleProperties),
|
|
540
543
|
]),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mgcrea/react-native-tailwind",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Compile-time Tailwind CSS for React Native with zero runtime overhead",
|
|
5
5
|
"author": "Olivier Louvignes <olivier@mgcrea.io> (https://github.com/mgcrea)",
|
|
6
6
|
"homepage": "https://github.com/mgcrea/react-native-tailwind#readme",
|
package/src/babel/index.ts
CHANGED
|
@@ -22,6 +22,9 @@ type PluginState = PluginPass & {
|
|
|
22
22
|
customColors: Record<string, string>;
|
|
23
23
|
};
|
|
24
24
|
|
|
25
|
+
// Use a unique identifier to avoid conflicts with user's own styles
|
|
26
|
+
const STYLES_IDENTIFIER = "_twStyles";
|
|
27
|
+
|
|
25
28
|
/**
|
|
26
29
|
* Supported className-like attributes
|
|
27
30
|
*/
|
|
@@ -117,7 +120,7 @@ function processTemplateLiteral(
|
|
|
117
120
|
staticParts.push(cls);
|
|
118
121
|
|
|
119
122
|
// Add to parts array
|
|
120
|
-
parts.push(t.memberExpression(t.identifier(
|
|
123
|
+
parts.push(t.memberExpression(t.identifier(STYLES_IDENTIFIER), t.identifier(styleKey)));
|
|
121
124
|
}
|
|
122
125
|
}
|
|
123
126
|
|
|
@@ -216,7 +219,7 @@ function processStringOrExpression(node: any, state: PluginState, t: typeof Babe
|
|
|
216
219
|
const styleKey = generateStyleKey(className);
|
|
217
220
|
state.styleRegistry.set(styleKey, styleObject);
|
|
218
221
|
|
|
219
|
-
return t.memberExpression(t.identifier(
|
|
222
|
+
return t.memberExpression(t.identifier(STYLES_IDENTIFIER), t.identifier(styleKey));
|
|
220
223
|
}
|
|
221
224
|
|
|
222
225
|
// Handle nested expressions recursively
|
|
@@ -426,7 +429,7 @@ function replaceWithStyleAttribute(
|
|
|
426
429
|
) {
|
|
427
430
|
const styleAttribute = t.jsxAttribute(
|
|
428
431
|
t.jsxIdentifier(targetStyleProp),
|
|
429
|
-
t.jsxExpressionContainer(t.memberExpression(t.identifier(
|
|
432
|
+
t.jsxExpressionContainer(t.memberExpression(t.identifier(STYLES_IDENTIFIER), t.identifier(styleKey))),
|
|
430
433
|
);
|
|
431
434
|
|
|
432
435
|
classNamePath.replaceWith(styleAttribute);
|
|
@@ -446,7 +449,7 @@ function mergeStyleAttribute(
|
|
|
446
449
|
// Create array with className styles first, then existing styles
|
|
447
450
|
// This allows existing styles to override className styles
|
|
448
451
|
const styleArray = t.arrayExpression([
|
|
449
|
-
t.memberExpression(t.identifier(
|
|
452
|
+
t.memberExpression(t.identifier(STYLES_IDENTIFIER), t.identifier(styleKey)),
|
|
450
453
|
existingStyle,
|
|
451
454
|
]);
|
|
452
455
|
|
|
@@ -531,10 +534,10 @@ function injectStyles(
|
|
|
531
534
|
styleProperties.push(t.objectProperty(t.identifier(key), t.objectExpression(properties)));
|
|
532
535
|
}
|
|
533
536
|
|
|
534
|
-
// Create: const
|
|
537
|
+
// Create: const _tailwindStyles = StyleSheet.create({ ... })
|
|
535
538
|
const styleSheet = t.variableDeclaration("const", [
|
|
536
539
|
t.variableDeclarator(
|
|
537
|
-
t.identifier(
|
|
540
|
+
t.identifier(STYLES_IDENTIFIER),
|
|
538
541
|
t.callExpression(t.memberExpression(t.identifier("StyleSheet"), t.identifier("create")), [
|
|
539
542
|
t.objectExpression(styleProperties),
|
|
540
543
|
]),
|