|
| 1 | +--- |
| 2 | +title: Bottom Tabs meet Native |
| 3 | +authors: oskar |
| 4 | +tags: [tutorial, react-native-paper] |
| 5 | +--- |
| 6 | + |
| 7 | +This is a guest post by Oskar Kwaśniewski, creator of `react-native-bottom-tabs`, a library exposing native tab primitives that integrates with React Navigation. If you like this guide, check out the `react-native-bottom-tabs` [documentation](https://callstackincubator.github.io/react-native-bottom-tabs/) for more! |
| 8 | + |
| 9 | +This blog post will explain the differences between the JavaScript Bottom Tabs navigator and provide a step-by-step guide for integrating React Navigation with the Native Bottom Tabs Navigator. |
| 10 | + |
| 11 | +## Introduction |
| 12 | + |
| 13 | +React Navigation comes with many navigators out of the box. We've got Stack, Native Stack, Drawer, and Bottom Tabs, but there were no Native Bottom Tabs until today! |
| 14 | + |
| 15 | +Both Android and iOS have predefined native components for handling bottom navigation. For iOS it's SwiftUI's `TabView` component and for Android it's `BottomNavigationView`. The native approach gives us an appropriate appearance no matter the platform we are running on. Native Bottom Tabs is a navigator that wraps the native `TabView` and `BottomNavigationView` - so you can use them with React Navigation. |
| 16 | + |
| 17 | +Let's dive into the details of this navigator. |
| 18 | + |
| 19 | +Note: Native Bottom Tabs navigator is a standalone package, not released as part of React Navigation. |
| 20 | + |
| 21 | +## Overview |
| 22 | + |
| 23 | +You still might be wondering the difference between `@react-navigation/bottom-tabs` and `react-native-bottom-tabs`. |
| 24 | + |
| 25 | +Let's go over the main differences: |
| 26 | + |
| 27 | +- JS Bottom Tabs recreate the UI as closely as possible while **Native Bottom Tabs use native platform primitives** to create the tabs. This makes your tab navigation indistinguishable from Native Apps as they use the same components under the hood. |
| 28 | +- Native Bottom Tabs **adapt to interfaces of a given platform** for example: tvOS and visionOS show tabs as a sidebar on iPadOS they appear at the top, while JS Bottom Tabs are always at the bottom. |
| 29 | + |
| 30 | +### Distinctive features of Native Bottom Tabs |
| 31 | + |
| 32 | +#### Multi-platform support |
| 33 | + |
| 34 | +Native Bottom tabs adapt to the appearance of multiple platforms. You always get natively-looking tabs! |
| 35 | + |
| 36 | +<img src="/assets/blog/native-bottom-tabs/ios.png" alt="Native Tabs on iOS" /> |
| 37 | + |
| 38 | +Bottom Navigation on iOS, with native blur. |
| 39 | + |
| 40 | +<img src="/assets/blog/native-bottom-tabs/android.png" alt="Native Tabs on Android" /> |
| 41 | + |
| 42 | +Bottom Navigation on Android, following Material Design 3 styling. |
| 43 | + |
| 44 | +<img src="/assets/blog/native-bottom-tabs/ipados.png" alt="Native Tabs on iPadOS" /> |
| 45 | + |
| 46 | +On iPadOS tabs appear at the top with a button allowing you to go into the sidebar mode. |
| 47 | + |
| 48 | +<img src="/assets/blog/native-bottom-tabs/visionos.png" alt="Native Tabs on visionOS" /> |
| 49 | + |
| 50 | +On visionOS, the tabs appear on the left side, attached outside of the window. |
| 51 | + |
| 52 | +<img src="/assets/blog/native-bottom-tabs/tvos.png" alt="Native Tabs on tvOS" /> |
| 53 | + |
| 54 | +On tvOS tabs appear on the top, making navigation with the TV remote a breeze. |
| 55 | + |
| 56 | +<img src="/assets/blog/native-bottom-tabs/macos.png" alt="Native Tabs on macOS" /> |
| 57 | + |
| 58 | +On macOS, tabs appear on the left side, following the design of the Finder app. |
| 59 | + |
| 60 | +#### Automatic scroll to the top |
| 61 | + |
| 62 | +iOS TabView automatically scrolls to the top when ScrollView is embedded inside of it. |
| 63 | + |
| 64 | +#### Automatic PiP avoidance |
| 65 | + |
| 66 | +The operating system recognizes navigation in your app making the Picture in Picture window automatically avoid bottom navigation. |
| 67 | + |
| 68 | +#### Platform-specific styling |
| 69 | + |
| 70 | +For iOS bottom navigation has a built-in blur making your app stand out. For Android, you can choose between Material 2 and Material 3 and leverage Material You system styling. |
| 71 | + |
| 72 | +#### Sidebar |
| 73 | + |
| 74 | +TabView can turn in to a side bar on tvOS, iPadOS and macOS. The `sidebarAdaptable` prop controls this. |
| 75 | + |
| 76 | +## Getting started |
| 77 | + |
| 78 | +To get started follow the installation instructions in the `react-native-bottom-tabs` [documentation](https://callstackincubator.github.io/react-native-bottom-tabs/docs/getting-started/quick-start.html). |
| 79 | + |
| 80 | +Native Bottom Tabs Navigation resembles JavaScript Tabs API as closely as possible. Making your migration straightforward. |
| 81 | + |
| 82 | +As mentioned before, Native Bottom Tabs use native primitives to create the tabs. This approach also has some downsides: Native components enforce certain constraints that we need to follow. |
| 83 | + |
| 84 | +There are a few differences between the APIs worth noting. One of the biggest is how native tabs handle images. In JavaScript tabs, you can render React components as icons, in native tabs unfortunately it’s not possible. Instead, you have to provide one of the following options: |
| 85 | + |
| 86 | +```tsx |
| 87 | +<Tab.Screen |
| 88 | + name="Albums" |
| 89 | + component={Albums} |
| 90 | + options={{ |
| 91 | + tabBarIcon: () => require('person.png'), |
| 92 | + // SVG is also supported |
| 93 | + tabBarIcon: () => require('person.svg'), |
| 94 | + // or |
| 95 | + tabBarIcon: () => ({ sfSymbol: 'person' }), |
| 96 | + // You can also pass a URL |
| 97 | + tabBarIcon: () => ({ uri: 'https://example.com/icon.png' }), |
| 98 | + }} |
| 99 | +/> |
| 100 | +``` |
| 101 | + |
| 102 | +So if you need full customizability like providing custom tab bar icons, and advanced styling that goes beyond what’s possible with native components you should use JavaScript bottom tabs. |
| 103 | + |
| 104 | +On top of that, the scope of this library doesn’t include the web so for that platform, you should use JavaScript Tabs. |
| 105 | + |
| 106 | +To get started you can import `createNativeBottomTabNavigator` from `@bottom-tabs/react-navigation` and use it the same way as JavaScript Bottom Tabs. |
| 107 | + |
| 108 | +### Example usage |
| 109 | + |
| 110 | +```tsx |
| 111 | +import { createNativeBottomTabNavigator } from '@bottom-tabs/react-navigation'; |
| 112 | + |
| 113 | +const Tabs = createNativeBottomTabNavigator(); |
| 114 | + |
| 115 | +function NativeBottomTabs() { |
| 116 | + return ( |
| 117 | + <Tabs.Navigator> |
| 118 | + <Tabs.Screen |
| 119 | + name="index" |
| 120 | + options={{ |
| 121 | + title: 'Home', |
| 122 | + tabBarIcon: () => ({ uri: 'https://example.com/icon.png' }), |
| 123 | + }} |
| 124 | + /> |
| 125 | + <Tabs.Screen |
| 126 | + name="explore" |
| 127 | + options={{ |
| 128 | + title: 'Explore', |
| 129 | + tabBarIcon: () => ({ uri: 'https://example.com/icon.png' }), |
| 130 | + }} |
| 131 | + /> |
| 132 | + </Tabs.Navigator> |
| 133 | + ); |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +<img src="/assets/blog/native-bottom-tabs/result.png" alt="Native Tabs" /> |
| 138 | + |
| 139 | +You can check out the project [here](https://github.com/callstackincubator/react-native-bottom-tabs). |
| 140 | + |
| 141 | +Thanks for reading! |
0 commit comments