Skip to content

React Navigation details a new approach to safe area insets

updated

A new post on the React Navigation blog, "The problem of Safe Areas", describes ongoing work on how React Navigation handles safe areas across devices and platforms, along with the approach the author plans to take.

Background

The post traces safe area handling from the fixed 20pt iOS status bar in 2015, through the StatusBar component, the iPhone X notch and home indicator, React Native's built-in SafeAreaView, and Android display cutouts. React Navigation moved from react-native-iphone-x-helper to react-native-safe-area-context starting with React Navigation 5.

According to the post, newer form factors exposed gaps. Windowed mode in iPadOS introduced window controls that safe areas do not automatically avoid, which led to an UNSTABLE_CornerInset component in React Navigation 8, built with help from Michał Osadnik. The announcement of the iPhone Duo, with its status bar on the right side, led to exploring UIView.LayoutRegion. Conversations with Kacper Kafara highlighted a similar situation on Android with the WindowInsets API.

New components

The author decided to work on two platform-specific components:

  • LayoutRegionView: based on UIView.LayoutRegion on iOS, with cornerAdaptation support and bar, margins, safeArea, readableContent regions.
  • WindowInsetsView: based on WindowInsets on Android, supporting inset types such as systemBars, statusBars, navigationBars, displayCutout, ime and others.

Example usage from the post:

<LayoutRegionView
  region="safeArea"
  cornerAdaptation="horizontal"
  edges={{ top: 'additive', start: 'additive' }}
>
  {/* content */}
</LayoutRegionView>

Both accept edges, which controls which edges receive padding and whether it is additive or maximum, and onInsetsChange for reading actual inset values. The stated requirements include insets that reflect the view's own position automatically, stay stable during transforms, work inside scroll views, and include translucent header and tab bar areas. Unlike SafeAreaProvider, insets depend on where the view itself is rendered.

An OverlayInsetsProvider component abstracts iOS additionalSafeAreaInsets and a custom Android inset type so custom headers and tab bars can supply their own insets.

API implications

The post notes that the useHeaderHeight and useBottomTabBarHeight hooks are tied to headers and tab bars and do not work well with native ones whose height is not known upfront. Using built-in iOS APIs also means contentInsetAdjustmentBehavior="automatic" on ScrollView will work with custom headers and tab bars. Providing insets to components such as FlatList on Android remains an open question. The author describes support for something like CSS env() variables as an ideal API.

Future plans

The React Native Screens team is also working on a SafeAreaView component for iOS and Android. React Navigation plans to work with that team to port this approach to React Native Screens, with SafeAreaView built as an abstraction on top of LayoutRegionView and WindowInsetsView.

Source: reactnavigation.org