Add an onSafeAreaInsetsChange view prop - #57967
Draft
janicduplessis wants to merge 17 commits into
Draft
Conversation
|
Warning JavaScript API change detected This PR commits an update to
This change was flagged as: |
Reports the part of a view that is covered by the system UI, dispatched synchronously so that layout depending on the insets lands in the frame the insets changed in. Replaces every use of the deprecated SafeAreaView inside core (LogBox, the element inspector, InputAccessoryView) with a JS implementation built on the prop.
Triggers now mark the view as needing layout instead of emitting inline, so the synchronous React render never re-enters from inside the mounting transaction (updateProps / didMoveToWindow). The layout pass runs before the frame is displayed, so the same-frame guarantee is unchanged.
Dimensions.get('window').safeAreaInsets exposes the part of the window
covered by the system UI, available synchronously at startup and updated
through the existing change event. Uses the same native inset
computation as the onSafeAreaInsetsChange view prop.
janicduplessis
force-pushed
the
safe-area-insets-view-prop
branch
from
August 14, 2026 22:02
979132a to
66de1bd
Compare
A freshly mounted view cannot receive its first inset event before its first frame is presented, even with synchronous dispatch — the event requires the view to be mounted and laid out. Seeding the padding from Dimensions makes the first frame correct; the event keeps it correct, relative to the view, from then on.
… frame experimental_flushSync only requests a synchronous beat; the queue is still processed at the next induce, one frame boundary later. For a freshly mounted view that lands the inset padding one frame after the view is first presented. An opt-in immediate mode processes the queue at the call site instead: the emit happens during the layout pass of the frame, the resulting commit mounts inline through the mounting manager's follow-up transaction loop, and the padding is part of the first presented frame. Verified with a full-bleed view mounted with no animation and null initial insets: zero unpadded frames. Existing experimental_flushSync callers are unchanged.
Demonstrates the synchronous layout directly: the modal opens without the prop, and applying it pads the content in the same frame.
The state between attaching onSafeAreaInsetsChange and receiving the first event renders with a yellow background: with synchronous dispatch it is committed but never presented, so any displayed yellow frame means the dispatch was not synchronous.
Contributor
|
This is amazing!! Been missing this for years |
The frame is part of the event payload but no longer part of the trigger: a view that moves (scrolling, layout) without its overlap with the system UI changing stays silent. Benchmarked with 50 observing rows inside a scroll view; the previous frame-based trigger emitted a synchronous event per view per frame while scrolling, and sustained a feedback storm afterwards (the synchronous render produces a new frame, which runs the pre-draw listener again) — ~5,000 events and ~55 rendered frames per second on an idle screen. With the inset-only trigger the same scene emits one event per row as it becomes visible and nothing afterwards, and scroll frame times match a scene with no observers. Also treat views fully clipped by an ancestor as having no insets on Android: getGlobalVisibleRect leaves the rect undefined for them, which fed garbage into the inset math and oscillated the computed values. Adds a scroll benchmark section to the RNTester example.
Covers the immediate mode used by the safe area inset event: a synchronous request processed by calling induce at the call site, and the guarantee that an induce issued from within the beat callback does not re-enter it.
The keyboard does not change the reported insets on either platform: iOS safeAreaInsets do not include the keyboard for a regular full screen view, and Android excludes the ime() inset type.
The deprecated SafeAreaView component is now backed by the JS implementation built on onSafeAreaInsetsChange, which behaves identically (verified with the RNTester SafeAreaView example) and works on every platform instead of iOS only. Deletes the C++ shadow node, state and component descriptor, the iOS component view, the Android view and view manager, the codegen spec, and their registrations.
…uests Replaces the opt-in immediate mode on experimental_flushSync with a fix at the platform level, restoring the plain API. The run loop observer that ordinarily induces the beat runs before Core Animation commits the frame, so a synchronous request made while Core Animation is already laying out (an event emitted from layoutSubviews) was only processed on the next frame. AppleEventBeat now also schedules an induce in the display phase of the current commit cycle — Core Animation runs display after the whole layout pass but before committing — via a zero-sized layer attached to the key window. This makes iOS structurally match Android, where the beat already runs within the frame before drawing, and batches for free: all synchronous requests made during one layout pass are processed in a single beat. Mounting 10 observing views previously ran 10 separate flushes of ~0.9ms each with the immediate mode; it now runs one ~2.7ms beat. The marker probe still shows zero unpadded frames on a bare mount, and rotation still updates the padding within the transition.
- Views with onSafeAreaInsetsChange now form a stacking context so view flattening cannot optimize away the host view the observer needs (with a Fantom test covering it) - Remove the stale React-FabricSafeAreaView target from Package.swift, missed by the SafeAreaView removal - iOS: fix observation state on recycled views (compare against current state, not stale oldViewProps); guard the display-phase flusher layer against use after the EventBeat owner is gone and tear it down on the main queue - EventBeat: a synchronous request is no longer stranded behind an already-scheduled asynchronous beat (with a test) - Rewrite the nested-request EventBeat test to model how platform implementations actually defer the induce - Android: only attach the observer under Fabric; record lastInsets only once the event is actually dispatched; detach the observer when a view is recycled - Docs: the frame in the payload is a snapshot at the time of the event, not a trigger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
Prototype, opened for discussion rather than for landing as-is.
SafeAreaViewis deprecated in favour ofreact-native-safe-area-context(per react-native-community/discussions-and-proposals#827), but core surfaces like LogBox and the element inspector cannot depend on the library, so core keeps a private copy of the deprecated component alive. The smallest primitive that would let both sides go away is native code reporting inset values to JS — today the library'sRNCSafeAreaProvidercomponent. This adds that primitive as a view prop instead:The payload is identical to the library's
onInsetsChange, soSafeAreaProvidercan swap its native component for a plainViewwith no API change on its side, and the inset math follows the library's so the semantics match. Insets are relative to the view: a view laid out inside the safe area reports zeros, which is what makes it composable and stops nested providers from double-padding.Window insets in
Dimensions.Dimensions.get('window').safeAreaInsets(anduseWindowDimensions) reports the safe area insets of the window using the same native computation — available synchronously at startup and updated through the existingchangeevent. This replaces the library's last native module (initialWindowMetrics); the library-side prototype consuming all of this is appandflow/react-native-safe-area-context#752.The native
SafeAreaViewimplementations are deleted entirely — the payoff of having the primitive in core. The deprecated public component is now backed by a JS implementation built on the prop, renders identically (verified with the RNTesterSafeAreaViewexample), and works on every platform instead of iOS only. The removal commit is -797/+92 lines: C++ shadow node/state/descriptor, iOS component view, Android view + view manager, codegen spec, and registrations. Two behaviour changes fall out: LogBox, the element inspector andInputAccessoryViewnow apply safe area padding on Android too (they previously fell back to a plainView), and these surfaces re-render when insets arrive instead of being padded natively.Synchronous dispatch. The event goes out through
EventEmitter::experimental_flushSyncas aDiscreteevent, the same mechanismVirtualViewuses: the UI and JS threads block until React has re-rendered, so inset-driven layout is mounted in the frame the insets changed in — first mount included, and on rotation the padding animates with the transition instead of jumping after it. One platform fix was needed:experimental_flushSynconly requests a beat, processed at the nextEventBeat::induce. Android induces within the frame before drawing, so it is already same-frame; on iOS the beat's run-loop observer runs before Core Animation's commit observer, while the inset events are emitted fromlayoutSubviews— inside CA's commit cycle — landing them one frame late.AppleEventBeatnow additionally schedules an induce in the display phase of the current commit cycle (CA runs layout → display → commit; a zero-sized layer marked dirty during layout getsdisplayafter the whole layout pass, before the commit). Theexperimental_flushSyncAPI is unchanged, iOS becomes structurally the same as Android, and batching falls out: all requests in one layout pass are processed in a single beat — mounting 10 observing views is one ~2.7 ms beat, where a per-callsite flush measured a perfectly linear 10 × ~0.9 ms. The beat semantics are covered by new unit tests (EventBeatTest.cpp).Cost when unused. The prop is a
boolinBaseViewProps(likeonLayout); native only observes the safe area when it is set. The only unconditional cost is one ivar check inlayoutSubviews/didMoveToWindow, now overridden onRCTViewComponentView— worth a look from someone who profiles that path.Cost when used. Benchmarked with 50 observing rows inside a
ScrollView(the "Scroll benchmark" section of the example), since a view sliding around the screen is the worst case:framein the payload is "as of the last inset change" — a consumer wanting continuously fresh frames (the library'sSafeAreaFrameContextduring scrolling) doesn't get them.useWindowDimensionsconsumer) blocks for the full batch — up to ~24 ms in debug during rotation in RNTester.safeAreaInsetsexclude it for full-screen views; Android excludesime()) — consistent, matching the library.Open questions I'd like input on:
onSafeAreaInsetsChangeright, and should it ship prefixed (experimental_/unstable_) first?framebe in the payload at all? The library needs it forSafeAreaFrameContext, but it's derivable withmeasureInWindow.Changelog:
[GENERAL] [ADDED] - Add an
onSafeAreaInsetsChangeview prop andDimensions.get('window').safeAreaInsets, reporting the part of a view / the window covered by the system UITest Plan:
RNTester, new "Safe area insets" example, on an iPhone 17 Pro simulator and an Android 16 emulator.
A view inside the safe area reports zero insets and its frame (iOS left, Android right):
A full screen view padding itself by its own insets — the unpadded (pink) area lines up exactly with the system UI on both platforms, including in landscape:
LogBox (a converted call site) still clears the home indicator, and
Dimensions.get('window').safeAreaInsetsreports the same values as the prop on both platforms:Synchronous rendering. The modal in the example opens without the prop; pressing "Apply insets" attaches it. The example renders a loud marker for the in-between state — yellow background while the view observes the safe area but no inset event has been received — so the dispatch timing is directly visible: any displayed yellow frame means the event was not synchronous.
With synchronous dispatch the marker state is committed but never presented. Consecutive captured frames, no yellow frame anywhere in the capture:
Full capture (apply → landscape → portrait), decomposed with ffmpeg and checked frame by frame — zero yellow frames, no stale insets, padding animating with the rotation:
sync-final3.mp4
The same sequence with sync dispatch disabled (plain async
dispatchEvent): the marker is presented for one frame on apply, and during rotations the incoming layout renders with the previous orientation's insets, correcting itself over the following frames (scrub the rotations):async-marker-v.mp4
Known gaps, noted but not addressed here:
FabricUIManager's per-frame synchronous-event dedupe can drop a second inset change for the same view landing within one frame; in practice insets don't change twice per frame.getGlobalVisibleRectmixes coordinate spaces for partially clipped views (inherited from safe-area-context's implementation).