|
| 1 | +# Web Platform Support |
| 2 | + |
| 3 | +React Native Bottom Tabs uses native platform primitives (SwiftUI on iOS and Material Design on Android) which are not available on web. For web applications, you'll need to use JavaScript-based tab implementations as a fallback. |
| 4 | + |
| 5 | +## Why Web Requires a Different Approach |
| 6 | + |
| 7 | +Native bottom tabs rely on platform-specific UI components that don't exist in web browsers. React Native Web translates React Native components to web equivalents, but native modules require explicit web implementations. |
| 8 | + |
| 9 | +:::note |
| 10 | + |
| 11 | +Web support requires using platform-specific files or a custom tab bar implementation. This guide shows you both approaches. |
| 12 | + |
| 13 | +::: |
| 14 | + |
| 15 | +## Approach 1: Platform-Specific Files (Recommended) |
| 16 | + |
| 17 | +React Native's Metro bundler automatically resolves platform-specific files. You can create separate implementations for native platforms and web. |
| 18 | + |
| 19 | +### File Structure |
| 20 | + |
| 21 | +``` |
| 22 | +src/ |
| 23 | +├── navigation/ |
| 24 | +│ ├── TabNavigator.native.tsx # Used on iOS/Android |
| 25 | +│ └── TabNavigator.web.tsx # Used on web |
| 26 | +``` |
| 27 | + |
| 28 | +### Native Implementation |
| 29 | + |
| 30 | +```tsx title="TabNavigator.native.tsx" |
| 31 | +import { createNativeBottomTabNavigator } from '@bottom-tabs/react-navigation'; |
| 32 | +import { HomeScreen } from '../screens/HomeScreen'; |
| 33 | +import { SettingsScreen } from '../screens/SettingsScreen'; |
| 34 | + |
| 35 | +const Tabs = createNativeBottomTabNavigator(); |
| 36 | + |
| 37 | +export function TabNavigator() { |
| 38 | + return ( |
| 39 | + <Tabs.Navigator> |
| 40 | + <Tabs.Screen |
| 41 | + name="Home" |
| 42 | + component={HomeScreen} |
| 43 | + options={{ |
| 44 | + tabBarIcon: () => ({ sfSymbol: 'house' }), |
| 45 | + }} |
| 46 | + /> |
| 47 | + <Tabs.Screen |
| 48 | + name="Settings" |
| 49 | + component={SettingsScreen} |
| 50 | + options={{ |
| 51 | + tabBarIcon: () => ({ sfSymbol: 'gear' }), |
| 52 | + }} |
| 53 | + /> |
| 54 | + </Tabs.Navigator> |
| 55 | + ); |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +### Web Implementation |
| 60 | + |
| 61 | +For web, use `@react-navigation/bottom-tabs` which provides a JavaScript-based implementation: |
| 62 | + |
| 63 | +```bash |
| 64 | +npm install @react-navigation/bottom-tabs |
| 65 | +``` |
| 66 | + |
| 67 | +```tsx title="TabNavigator.web.tsx" |
| 68 | +import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'; |
| 69 | +import { HomeScreen } from '../screens/HomeScreen'; |
| 70 | +import { SettingsScreen } from '../screens/SettingsScreen'; |
| 71 | + |
| 72 | +const Tab = createBottomTabNavigator(); |
| 73 | + |
| 74 | +export function TabNavigator() { |
| 75 | + return ( |
| 76 | + <Tab.Navigator> |
| 77 | + <Tab.Screen |
| 78 | + name="Home" |
| 79 | + component={HomeScreen} |
| 80 | + options={{ |
| 81 | + tabBarIcon: ({ color, size }) => ( |
| 82 | + <Icon name="home" size={size} color={color} /> |
| 83 | + ), |
| 84 | + }} |
| 85 | + /> |
| 86 | + <Tab.Screen |
| 87 | + name="Settings" |
| 88 | + component={SettingsScreen} |
| 89 | + options={{ |
| 90 | + tabBarIcon: ({ color, size }) => ( |
| 91 | + <Icon name="settings" size={size} color={color} /> |
| 92 | + ), |
| 93 | + }} |
| 94 | + /> |
| 95 | + </Tab.Navigator> |
| 96 | + ); |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +### Using in Your App |
| 101 | + |
| 102 | +Import the component normally - React Native will automatically use the correct file: |
| 103 | + |
| 104 | +```tsx title="App.tsx" |
| 105 | +import { NavigationContainer } from '@react-navigation/native'; |
| 106 | +import { TabNavigator } from './navigation/TabNavigator'; |
| 107 | + |
| 108 | +export default function App() { |
| 109 | + return ( |
| 110 | + <NavigationContainer> |
| 111 | + <TabNavigator /> |
| 112 | + </NavigationContainer> |
| 113 | + ); |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +## Approach 2: Custom Tab Bar with Standalone Usage |
| 118 | + |
| 119 | +If you're using the standalone `TabView` component, you can provide a custom `tabBar` prop that renders a web-compatible implementation. |
| 120 | + |
| 121 | +```tsx |
| 122 | +import { Platform } from 'react-native'; |
| 123 | +import TabView from 'react-native-bottom-tabs'; |
| 124 | + |
| 125 | +function MyTabs() { |
| 126 | + const [index, setIndex] = React.useState(0); |
| 127 | + const [routes] = React.useState([ |
| 128 | + { key: 'home', title: 'Home' }, |
| 129 | + { key: 'settings', title: 'Settings' }, |
| 130 | + ]); |
| 131 | + |
| 132 | + const renderScene = SceneMap({ |
| 133 | + home: HomeScreen, |
| 134 | + settings: SettingsScreen, |
| 135 | + }); |
| 136 | + |
| 137 | + return ( |
| 138 | + <TabView |
| 139 | + navigationState={{ index, routes }} |
| 140 | + renderScene={renderScene} |
| 141 | + onIndexChange={setIndex} |
| 142 | + tabBar={Platform.OS === 'web' ? CustomWebTabBar : undefined} |
| 143 | + /> |
| 144 | + ); |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +### Example Custom Web Tab Bar |
| 149 | + |
| 150 | +```tsx |
| 151 | +function CustomWebTabBar() { |
| 152 | + return ( |
| 153 | + <View style={styles.tabBar}> |
| 154 | + <TouchableOpacity style={styles.tab}> |
| 155 | + <Text>Home</Text> |
| 156 | + </TouchableOpacity> |
| 157 | + <TouchableOpacity style={styles.tab}> |
| 158 | + <Text>Settings</Text> |
| 159 | + </TouchableOpacity> |
| 160 | + </View> |
| 161 | + ); |
| 162 | +} |
| 163 | + |
| 164 | +const styles = StyleSheet.create({ |
| 165 | + tabBar: { |
| 166 | + flexDirection: 'row', |
| 167 | + backgroundColor: '#fff', |
| 168 | + borderTopWidth: 1, |
| 169 | + borderTopColor: '#e0e0e0', |
| 170 | + height: 60, |
| 171 | + }, |
| 172 | + tab: { |
| 173 | + flex: 1, |
| 174 | + justifyContent: 'center', |
| 175 | + alignItems: 'center', |
| 176 | + }, |
| 177 | +}); |
| 178 | +``` |
| 179 | + |
| 180 | +## Approach 3: Conditional Platform Rendering |
| 181 | + |
| 182 | +For simpler cases, you can conditionally render different navigators based on platform: |
| 183 | + |
| 184 | +```tsx |
| 185 | +import { Platform } from 'react-native'; |
| 186 | +import { createNativeBottomTabNavigator } from '@bottom-tabs/react-navigation'; |
| 187 | +import { createBottomTabNavigator } from '@react-navigation/bottom-tabs'; |
| 188 | + |
| 189 | +const NativeTabs = createNativeBottomTabNavigator(); |
| 190 | +const JSTabs = createBottomTabNavigator(); |
| 191 | + |
| 192 | +const Tabs = Platform.OS === 'web' ? JSTabs : NativeTabs; |
| 193 | + |
| 194 | +export function TabNavigator() { |
| 195 | + return ( |
| 196 | + <Tabs.Navigator> |
| 197 | + <Tabs.Screen name="Home" component={HomeScreen} /> |
| 198 | + <Tabs.Screen name="Settings" component={SettingsScreen} /> |
| 199 | + </Tabs.Navigator> |
| 200 | + ); |
| 201 | +} |
| 202 | +``` |
| 203 | + |
| 204 | +:::warning |
| 205 | + |
| 206 | +This approach requires installing both `@bottom-tabs/react-navigation` and `@react-navigation/bottom-tabs`, which increases bundle size for all platforms. |
| 207 | + |
| 208 | +::: |
| 209 | + |
| 210 | +## Styling Considerations |
| 211 | + |
| 212 | +When implementing web tabs, consider these styling differences: |
| 213 | + |
| 214 | +### Native Platforms |
| 215 | +- Use `sfSymbol` for iOS icons |
| 216 | +- Platform-specific appearance attributes |
| 217 | +- Native gestures and animations |
| 218 | + |
| 219 | +### Web Platform |
| 220 | +- Use web-compatible icon libraries (e.g., `react-icons`, `@expo/vector-icons`) |
| 221 | +- CSS-based styling and animations |
| 222 | +- Standard web accessibility practices |
| 223 | + |
| 224 | +## Icon Libraries for Web |
| 225 | + |
| 226 | +For web compatibility, use icon libraries that work across all platforms: |
| 227 | + |
| 228 | +### Expo Vector Icons |
| 229 | + |
| 230 | +```tsx |
| 231 | +import { Ionicons } from '@expo/vector-icons'; |
| 232 | + |
| 233 | +options={{ |
| 234 | + tabBarIcon: ({ color, size }) => ( |
| 235 | + <Ionicons name="home" size={size} color={color} /> |
| 236 | + ), |
| 237 | +}} |
| 238 | +``` |
| 239 | + |
| 240 | +### React Icons |
| 241 | + |
| 242 | +```tsx |
| 243 | +import { FiHome } from 'react-icons/fi'; |
| 244 | + |
| 245 | +options={{ |
| 246 | + tabBarIcon: ({ color }) => <FiHome color={color} />, |
| 247 | +}} |
| 248 | +``` |
| 249 | + |
| 250 | +## Summary |
| 251 | + |
| 252 | +| Approach | Pros | Cons | |
| 253 | +|----------|------|------| |
| 254 | +| **Platform-Specific Files** | Clean separation, optimal bundle size | Requires maintaining two implementations | |
| 255 | +| **Custom Tab Bar** | Full control, single codebase | More code to maintain | |
| 256 | +| **Conditional Rendering** | Simple to understand | Both libraries in bundle | |
| 257 | + |
| 258 | +For most projects, **platform-specific files** provide the best balance of code organization, bundle size, and developer experience. |
| 259 | + |
| 260 | +## Additional Resources |
| 261 | + |
| 262 | +- [React Navigation Bottom Tabs](https://reactnavigation.org/docs/bottom-tab-navigator/) |
| 263 | +- [React Native Web Documentation](https://necolas.github.io/react-native-web/) |
| 264 | +- [Metro Bundler Platform-Specific Extensions](https://reactnative.dev/docs/platform-specific-code#platform-specific-extensions) |
0 commit comments