|
| 1 | +--- |
| 2 | +title: The problem of Safe Areas |
| 3 | +authors: satya |
| 4 | +tags: [engineering] |
| 5 | +--- |
| 6 | + |
| 7 | +Recently I've been working on improving how React Navigation handles safe areas across different devices and platforms. |
| 8 | + |
| 9 | +In this post, I'll share various challenges I faced and the approach I plan to take to address them. |
| 10 | + |
| 11 | +{/* truncate */} |
| 12 | + |
| 13 | +## A bit of history |
| 14 | + |
| 15 | +When I started working with React Native back in 2015, the only thing we needed to handle was the status bar on iOS. But it changed over the years: |
| 16 | + |
| 17 | +- **2015**: The status bar was 20pt tall on all iPhones (40pt during a phone call, but let's not talk about that). It was opaque on Android, so there was nothing to handle. |
| 18 | +- **2016**: We got a [`StatusBar`](https://reactnative.dev/docs/statusbar) component with a `translucent` prop in [React Native 0.20](https://github.com/react/react-native/releases/tag/v0.20.0) (Android 5 Lollipop and later), then [`StatusBar.currentHeight`](https://reactnative.dev/docs/statusbar#currentheight-android) in [React Native 0.23](https://github.com/react/react-native/releases/tag/v0.23.0) to add padding for the status bar. |
| 19 | +- **2017**: Apple released the iPhone X. The status bar height now depended on the model (20pt on older iPhones and 44pt on the iPhone X), and we also had to handle the home indicator and the notch in landscape. |
| 20 | +- **2017**: We got [`contentInsetAdjustmentBehavior`](https://reactnative.dev/docs/scrollview#contentinsetadjustmentbehavior-ios) on `ScrollView` in [React Native 0.48](https://github.com/react/react-native/releases/tag/v0.48.0), and a built-in [`SafeAreaView`](https://reactnative.dev/docs/safeareaview) in [React Native 0.50](https://github.com/react/react-native/releases/tag/v0.50.0). But `SafeAreaView` only worked on iOS 11 and later, so we still needed to handle older iPhones and Android separately. |
| 21 | +- **2018**: Android devices started getting notches, hole-punch cameras, and other [display cutouts](https://developer.android.com/develop/ui/views/layout/display-cutout). |
| 22 | + |
| 23 | +Since React Navigation renders UI such as headers and tab bars which may overlap with the status bar, the notch, home indicator etc., we needed to handle this. Initially, we used [`react-native-iphone-x-helper`](https://github.com/ptelad/react-native-iphone-x-helper) for this. It detected the iPhone X from the screen dimensions, which worked at the time, but wasn't future proof. |
| 24 | + |
| 25 | +We needed a better solution. The obvious choice was to use the built-in `SafeAreaView` component on supported iOS versions, but it had a big problem. Back then, React Navigation was purely JS-based, using `transform` for animations. The `SafeAreaView` didn't work well with it, as the safe area insets depended on the transforms as well, so as a screen animated, the insets would change and it would cause a jump or stale layout. |
| 26 | + |
| 27 | +## The current state |
| 28 | + |
| 29 | +The [`react-native-safe-area-context`](https://github.com/th3rdwave/react-native-safe-area-context) library came out in August 2019. It solved all our problems: |
| 30 | + |
| 31 | +- It worked on both Android & iOS, so we had a unified API |
| 32 | +- The insets were relative to [`SafeAreaProvider`](https://rnsac.appandflow.com/docs/api/safe-area-provider), so we could render one at the root to avoid a jump or stale layout |
| 33 | +- It had a hook-based API with [`useSafeAreaInsets`](https://rnsac.appandflow.com/docs/api/use-safe-area-insets), which gave us actual inset values for calculations |
| 34 | + |
| 35 | +We started using it in React Navigation 5. It wasn't perfect, but it was close. |
| 36 | + |
| 37 | +## Why isn't it enough? |
| 38 | + |
| 39 | +In 2025, Apple added windowed mode to iPadOS. In windowed mode, apps have window controls (traffic light buttons) like on macOS, and can be freely resized. This added a new problem. Safe areas don't automatically avoid the traffic light buttons. There is a new [`cornerAdaptation`](<https://developer.apple.com/documentation/uikit/uiview/layoutregion/safearea(corneradaptation:)>) option that accounts for rounded corners and window controls. |
| 40 | + |
| 41 | +When working on React Navigation 8, I wanted to solve it. We mainly had 2 scenarios where the window controls covered our content: |
| 42 | + |
| 43 | +- The left side of headers |
| 44 | +- Scrollable content in left drawers |
| 45 | + |
| 46 | +<img |
| 47 | + src="/assets/blog/safe-areas/window-controls.png" |
| 48 | + alt="Window controls on iPadOS covering the header of a drawer" |
| 49 | + style={{ width: 604, height: 'auto' }} |
| 50 | +/> |
| 51 | + |
| 52 | +After some experimentation, I landed on an [`UNSTABLE_CornerInset`](/blog/2026/03/10/react-navigation-8.0-march-progress#unstable_cornerinset-for-better-ipados-windowed-mode-support) component with help from [Michał Osadnik](https://github.com/osdnk). It had to account for 2 things: |
| 53 | + |
| 54 | +- It needed to be dynamic, e.g. on a larger window size, the user could render a sidebar/permanent drawer, so the header shouldn't apply the insets. But it should apply the insets when this sidebar gets hidden. |
| 55 | +- It needed to stay static during animations, e.g. when pushing a new stack screen, we don't want the header insets to start from 0 and then jump to the actual inset as the header intersects with the window controls. |
| 56 | + |
| 57 | +<video playsInline autoPlay muted loop style={{ width: 604 }}> |
| 58 | + <source src="/assets/blog/safe-areas/sidebar.mp4" /> |
| 59 | +</video> |
| 60 | + |
| 61 | +Initially, we had the same problem as React Native's `SafeAreaView`, where the insets would change during animations and cause a jump or stale layout. After many iterations, the solution I arrived at was to: |
| 62 | + |
| 63 | +- Render a hidden view at the same bounds, but attached to the window, instead of getting the insets of the view itself. |
| 64 | +- Use the insets reported by UIKit for the hidden view, so they don't change during animations. |
| 65 | +- Listen to `transitionEnd` events of screens and window resize to re-calculate and re-apply the insets. |
| 66 | + |
| 67 | +It wasn't an ideal implementation, but it kind of worked. |
| 68 | + |
| 69 | +Then, Apple announced the iPhone Duo in September 2026. The status bar was now on the right side of the screen, and UI controls could be placed in the status bar region. I wanted to figure out how to support placing items in the status bar area of iPhone Duo without being overlapped by the status bar. This led me to 2 APIs: |
| 70 | + |
| 71 | +- [`UIView.ReservedRegion`](https://developer.apple.com/documentation/uikit/uiview/reservedregion) |
| 72 | +- [`UIView.LayoutRegion`](https://developer.apple.com/documentation/uikit/uiview/layoutregion) |
| 73 | + |
| 74 | +The reserved regions API was too low-level, which left `UIView.LayoutRegion`, specifically `UIView.LayoutRegion.bar`. So I worked on adding a component for that, but after some thinking, it made more sense to add everything supported by `UIView.LayoutRegion`: `bar`, `margins`, `safeArea`, `readableContent` instead of only `bar`. |
| 75 | + |
| 76 | +<img |
| 77 | + src="/assets/blog/safe-areas/bar-region.png" |
| 78 | + alt="A header button in the bar region below the status bar on the side of iPhone Duo" |
| 79 | + style={{ width: 330, height: 'auto' }} |
| 80 | +/> |
| 81 | + |
| 82 | +Now, there was overlap between `UNSTABLE_CornerInset` and `safeArea`, because `UNSTABLE_CornerInset` was essentially safe area with `cornerAdaptation`. So it was time to rethink the APIs we expose. |
| 83 | + |
| 84 | +I hadn't started experimenting with Android yet. But after talking to [Kacper Kafara](https://github.com/kkafar), I learned that we had a similar situation on Android with multiple types of insets with the [`WindowInsets`](https://developer.android.com/reference/android/view/WindowInsets.Type) API. |
| 85 | + |
| 86 | +## Our approach |
| 87 | + |
| 88 | +So far, `SafeAreaView` has been the main way safe area insets have been used in React Native. But as we get more and more form factors and layouts, I'm not sure it's the ideal API anymore, at least not if we want to have the same flexibility as the underlying platform APIs. |
| 89 | + |
| 90 | +It made more sense to have platform-specific components that expose all the different types of insets and regions from the underlying platform APIs, and maybe a cross-platform `SafeAreaView` could be built on top of them for simpler use cases. So I decided to work on 2 new components: |
| 91 | + |
| 92 | +- `LayoutRegionView`: based on `UIView.LayoutRegion` on iOS, with `cornerAdaptation` support, and `bar`, `margins`, `safeArea`, `readableContent` regions. |
| 93 | +- `WindowInsetsView`: based on `WindowInsets` on Android, with `systemBars`, `statusBars`, `navigationBars`, `captionBar`, `displayCutout`, `ime`, `systemGestures`, `mandatorySystemGestures`, `tappableElement` insets. |
| 94 | + |
| 95 | +The `LayoutRegionView` can be used as follows: |
| 96 | + |
| 97 | +```jsx |
| 98 | +<LayoutRegionView |
| 99 | + region="safeArea" |
| 100 | + cornerAdaptation="horizontal" |
| 101 | + edges={{ top: 'additive', start: 'additive' }} |
| 102 | +> |
| 103 | + {/* content */} |
| 104 | +</LayoutRegionView> |
| 105 | +``` |
| 106 | + |
| 107 | +The `WindowInsetsView` can be used as follows: |
| 108 | + |
| 109 | +```jsx |
| 110 | +<WindowInsetsView |
| 111 | + types={['systemBars', 'displayCutout']} |
| 112 | + edges={{ top: 'additive', bottom: 'additive' }} |
| 113 | +> |
| 114 | + {/* content */} |
| 115 | +</WindowInsetsView> |
| 116 | +``` |
| 117 | + |
| 118 | +Both of these components accept: |
| 119 | + |
| 120 | +- `edges`: to control which edges get the insets applied as padding, and whether they're added to the padding in style (`additive`) or the maximum of the inset and the padding in style is used (`maximum`) |
| 121 | +- `onInsetsChange`: to get the actual inset values if we need them for calculations, instead of wrapping the content and applying padding |
| 122 | + |
| 123 | +But exposing platform-specific insets isn't enough. We still had a set of requirements we needed to handle so these components work well with React Navigation and React Native: |
| 124 | + |
| 125 | +- Insets should reflect the view's position automatically, e.g. a view not covered by the status bar should not get a top inset. |
| 126 | +- Insets should stay stable during transforms to avoid a jump or stale layout. |
| 127 | +- Insets should work inside scroll views. While scroll views on iOS have automatic insets, we still need custom handling for `cornerAdaptation`. |
| 128 | +- Insets should include the translucent header and tab bar areas. |
| 129 | +- We should be able to provide our own insets, e.g. for custom headers, tab bars etc. |
| 130 | + |
| 131 | +For me, making it automatic was crucial. As a library, React Navigation's components could be used in many different scenarios, and they need to work well automatically without requiring users to manually tweak each case. Handling all the requirements while keeping it automatic was challenging, but we managed to achieve it with a few tricks. |
| 132 | + |
| 133 | +There was still one more thing. On iOS, the `safeArea` regions also account for native navigation bars, tab bars etc. And for custom overlays, iOS supports an [`additionalSafeAreaInsets`](https://developer.apple.com/documentation/uikit/uiviewcontroller/additionalsafeareainsets) property on view controllers to add additional insets. But none of the inset types on Android handle headers and tab bars. So I added a `custom` inset type to `WindowInsetsView` to provide insets for our own UI elements. |
| 134 | + |
| 135 | +To abstract these 2 mechanisms, I made an `OverlayInsetsProvider` component: |
| 136 | + |
| 137 | +```tsx |
| 138 | +<OverlayInsetsProvider insets={{ top: headerHeight, bottom: tabBarHeight }}> |
| 139 | + {/* content */} |
| 140 | +</OverlayInsetsProvider> |
| 141 | +``` |
| 142 | + |
| 143 | +I named it `OverlayInsetsProvider` since it provides the insets for overlays, and not only the iOS-specific `safeArea` region. |
| 144 | + |
| 145 | +This component sounds similar to `SafeAreaProvider`, but the main difference is that a `SafeAreaProvider` is also where the insets are measured, so your actual component may not be overlapped by the status bar, but it'll still get the insets if the `SafeAreaProvider` is overlapped by the status bar. With `OverlayInsetsProvider`, the insets still depend on where the component itself is located. |
| 146 | + |
| 147 | +<div style={{ display: 'flex', gap: 16 }}> |
| 148 | + <img |
| 149 | + src="/assets/blog/safe-areas/hook-insets.png" |
| 150 | + alt="Content below a header with padding from useSafeAreaInsets, leaving an extra gap for the status bar" |
| 151 | + style={{ width: 221, maxWidth: 'calc(50% - 8px)', height: 'auto' }} |
| 152 | + /> |
| 153 | + <img |
| 154 | + src="/assets/blog/safe-areas/region-insets.png" |
| 155 | + alt="Content below a header with LayoutRegionView, with no extra gap" |
| 156 | + style={{ width: 221, maxWidth: 'calc(50% - 8px)', height: 'auto' }} |
| 157 | + /> |
| 158 | +</div> |
| 159 | + |
| 160 | +My ideal API would be support for something like [CSS env() variables](https://developer.mozilla.org/en-US/docs/Web/CSS/env) in React Native, so we could do something like: |
| 161 | + |
| 162 | +```tsx |
| 163 | +<View |
| 164 | + style={{ |
| 165 | + paddingTop: env('safe-area-inset-top'), |
| 166 | + paddingBottom: env('safe-area-inset-bottom'), |
| 167 | + }} |
| 168 | +> |
| 169 | + {/* content */} |
| 170 | +</View> |
| 171 | +``` |
| 172 | + |
| 173 | +It could be combined with `calc()`, `max()` etc. for more complex use cases. It'd also solve a problem that a view such as `LayoutRegionView`, `WindowInsetsView`, `SafeAreaView` etc. can't handle: to specify insets in [`FlatList`](https://reactnative.dev/docs/flatlist) and other libraries where it's not possible to wrap the scrollable content. |
| 174 | + |
| 175 | +But for now, we have to work with the limitations of React Native. Hopefully the lower-level primitives like `LayoutRegionView` and `WindowInsetsView` will make it easier to build UIs that feel at home on each platform. |
| 176 | + |
| 177 | +## Future plans |
| 178 | + |
| 179 | +The React Native Screens team is also [working on a `SafeAreaView` component](https://github.com/software-mansion/react-native-screens/pull/3208) for iOS and Android. But it doesn't handle all the cases we need. So we plan to work with them to port our approach to React Native Screens instead, so there can be a unified solution for handling safe areas. The `SafeAreaView` component can then be an abstraction on top of `LayoutRegionView` and `WindowInsetsView`. |
0 commit comments