Skip to content

Commit 7411704

Browse files
committed
docs: add Tailwind CSS integration guide
1 parent 24892e0 commit 7411704

File tree

1 file changed

+214
-0
lines changed

1 file changed

+214
-0
lines changed
Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,214 @@
1+
---
2+
title: Tailwind CSS Integration
3+
description: Using Tailwind CSS with React Native SwiftUI
4+
---
5+
6+
This guide shows how to integrate React Native SwiftUI with [@mgcrea/react-native-tailwind](https://github.com/mgcrea/react-native-tailwind) using TypeScript declaration merging.
7+
8+
## Installation
9+
10+
Install the Tailwind package:
11+
12+
```bash
13+
npm install @mgcrea/react-native-tailwind
14+
```
15+
16+
Add the Babel plugin to your `babel.config.js`:
17+
18+
```javascript
19+
module.exports = {
20+
presets: ['module:@react-native/babel-preset'],
21+
plugins: ['@mgcrea/react-native-tailwind/babel'],
22+
};
23+
```
24+
25+
## TypeScript Setup
26+
27+
### 1. Enable React Native className support
28+
29+
Create `src/types/react-native-tailwind.d.ts`:
30+
31+
```tsx
32+
import "@mgcrea/react-native-tailwind/react-native";
33+
```
34+
35+
### 2. Enable SwiftUI className support
36+
37+
Create `src/types/react-native-swiftui.d.ts`:
38+
39+
```tsx
40+
/**
41+
* TypeScript declarations to add className props to React Native SwiftUI components
42+
* This provides module augmentation for @mgcrea/react-native-swiftui
43+
*/
44+
45+
import "@mgcrea/react-native-swiftui";
46+
47+
declare module "@mgcrea/react-native-swiftui" {
48+
interface NativeViewStyleProps {
49+
className?: string;
50+
}
51+
interface NativeTextStyleProps {
52+
className?: string;
53+
}
54+
interface NativeLabelStyleProps {
55+
labelClassName?: string;
56+
}
57+
}
58+
```
59+
60+
Make sure your `tsconfig.json` includes these files:
61+
62+
```json
63+
{
64+
"include": ["src/**/*"]
65+
}
66+
```
67+
68+
## Available Interfaces
69+
70+
The library exports these interfaces for declaration merging:
71+
72+
| Interface | Used By | Description |
73+
|-----------|---------|-------------|
74+
| `NativeViewStyleProps` | Container components | Base style props for VStack, HStack, Form, etc. |
75+
| `NativeTextStyleProps` | Text components | Extended props for Text, Button, TextField, etc. |
76+
| `NativeLabelStyleProps` | Labeled components | Label style props for Toggle, Picker, Slider, etc. |
77+
78+
## Basic Usage
79+
80+
After setup, you can use Tailwind classes directly on SwiftUI components:
81+
82+
```tsx
83+
import { useState } from 'react';
84+
import { View } from 'react-native';
85+
import { SwiftUI } from '@mgcrea/react-native-swiftui';
86+
87+
export function MyComponent() {
88+
const [name, setName] = useState('');
89+
const [notifications, setNotifications] = useState(false);
90+
91+
return (
92+
<View className="flex-1">
93+
<SwiftUI className="flex-1">
94+
<SwiftUI.Form className="bg-gray-100">
95+
<SwiftUI.Section header="Profile">
96+
<SwiftUI.TextField
97+
label="Name"
98+
className="text-lg"
99+
labelClassName="font-semibold text-blue-600"
100+
text={name}
101+
onChange={setName}
102+
/>
103+
<SwiftUI.Toggle
104+
label="Notifications"
105+
labelClassName="text-gray-700"
106+
isOn={notifications}
107+
onChange={setNotifications}
108+
/>
109+
</SwiftUI.Section>
110+
</SwiftUI.Form>
111+
</SwiftUI>
112+
</View>
113+
);
114+
}
115+
```
116+
117+
## Supported Features
118+
119+
The `@mgcrea/react-native-tailwind` package supports:
120+
121+
- **Zero runtime overhead** - All transformations happen at compile time via Babel
122+
- **State modifiers** - `active:`, `hover:`, `focus:`, `disabled:` variants
123+
- **Platform-specific** - `ios:`, `android:`, `web:` prefixes
124+
- **Dark mode** - `dark:` and `light:` modifiers
125+
- **Arbitrary values** - Custom sizing like `w-[123px]`
126+
127+
## Complete Example
128+
129+
```tsx
130+
import { useState } from 'react';
131+
import { View } from 'react-native';
132+
import { SwiftUI } from '@mgcrea/react-native-swiftui';
133+
134+
export function SettingsForm() {
135+
const [name, setName] = useState('');
136+
const [email, setEmail] = useState('');
137+
const [darkMode, setDarkMode] = useState(false);
138+
const [fontSize, setFontSize] = useState(16);
139+
140+
return (
141+
<View className="flex-1 bg-white dark:bg-gray-900">
142+
<SwiftUI className="flex-1">
143+
<SwiftUI.Form>
144+
<SwiftUI.Section header="Account">
145+
<SwiftUI.TextField
146+
label="Name"
147+
labelClassName="font-medium"
148+
text={name}
149+
onChange={setName}
150+
/>
151+
<SwiftUI.TextField
152+
label="Email"
153+
labelClassName="font-medium"
154+
keyboardType="emailAddress"
155+
text={email}
156+
onChange={setEmail}
157+
/>
158+
</SwiftUI.Section>
159+
160+
<SwiftUI.Section header="Appearance">
161+
<SwiftUI.Toggle
162+
label="Dark Mode"
163+
labelClassName="text-gray-800 dark:text-gray-200"
164+
isOn={darkMode}
165+
onChange={setDarkMode}
166+
/>
167+
<SwiftUI.Slider
168+
label={`Font Size: ${fontSize}px`}
169+
labelClassName="text-sm text-gray-600"
170+
value={fontSize}
171+
minimum={12}
172+
maximum={24}
173+
step={1}
174+
onChange={setFontSize}
175+
/>
176+
</SwiftUI.Section>
177+
</SwiftUI.Form>
178+
</SwiftUI>
179+
</View>
180+
);
181+
}
182+
```
183+
184+
## Extending Other Props
185+
186+
You can extend the interfaces for other use cases beyond Tailwind:
187+
188+
```tsx
189+
declare module "@mgcrea/react-native-swiftui" {
190+
// Add testID for testing
191+
interface NativeViewStyleProps {
192+
testID?: string;
193+
}
194+
195+
// Add accessibility props
196+
interface NativeTextStyleProps {
197+
accessibilityLabel?: string;
198+
accessibilityHint?: string;
199+
}
200+
201+
// Add custom theming props
202+
interface NativeLabelStyleProps {
203+
labelVariant?: 'primary' | 'secondary' | 'muted';
204+
}
205+
}
206+
```
207+
208+
## Tips
209+
210+
1. **Keep declarations in sync** - Update your declaration file when the library adds new style interfaces
211+
2. **Use a types directory** - Keep all custom declarations organized in a `src/types/` folder
212+
3. **Type safety** - Declaration merging preserves full TypeScript support
213+
4. **IDE support** - Your IDE will autocomplete the new props after declaration merging
214+
5. **Platform modifiers** - Use `ios:` prefix for iOS-specific styles that work well with SwiftUI

0 commit comments

Comments
 (0)