Repository navigation
fix: measureInWindow calculations for Modal with formSheet presentation - #59098
Open
kirillzyusko wants to merge 4 commits into
Open
kirillzyusko wants to merge 4 commits into
kirillzyusko wants to merge 4 commits into
Conversation
kirillzyusko
force-pushed
the
fix/use-window-dimensions-in-form-sheet-modal-on-ios
branch
from
October 15, 2026 11:13
042dfcb to
eb9d254
Compare
Contributor
Author
|
Hey @cipolleschi I asked you to review #56062 but then Meta moved repo to react foundation and I had to re-open PR. Would it be possible to import/review this PR too? This problem still actual and haven't been fixed in recent RN versions 😢 |
kirillzyusko
force-pushed
the
fix/use-window-dimensions-in-form-sheet-modal-on-ios
branch
from
November 2, 2026 18:22
eb9d254 to
952bf83
Compare
zoontek
suggested changes
Nov 17, 2026
|
|
||
| extern const char ModalHostViewComponentName[] = "ModalHostView"; | ||
|
|
||
| Transform ModalHostViewShadowNode::getTransform() const { |
Contributor
There was a problem hiding this comment.
<Modal ref> points at this host node, so measureInWindow() on it now adds the offset twice. The translate is applied here, then again by the main root.
Contributor
Author
There was a problem hiding this comment.
Thanks, fixed it!
kirillzyusko
force-pushed
the
fix/use-window-dimensions-in-form-sheet-modal-on-ios
branch
from
November 18, 2026 10:26
9b35ae9 to
7f30936
Compare
This branch has not been deployed
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:
Coming from #56062 but after fork re-sync
Fixed
measureInWindowcalculations forModalwithformSheetpresentation.This is only Fabric problem (paper works well because it relies on UIKit implementation).
Problem
The core issue is in how
measureInWindowworks differently between Paper and Fabric.Paper calls iOS native
[view.window convertRect:view.frame fromView:view.superview]which uses UIKit's actual view hierarchy. When aformSheetmodal is presented, the modal's view controller lives in a separateUIWindowor has a known offset from the top of the screen. UIKit'sconvertRect:fromView:correctly accounts for this because it works with the real rendered view positions.Fabric's
measureInWindow(inDOM.cpp) works entirely in the shadow tree, not the native view hierarchy. It callsgetLayoutMetricsFromRootwhich walks up the shadow node ancestors accumulating frame origins. The critical logic is atLayoutableShadowNode.cpp:auto shouldApplyTransformation = (policy.includeTransform && !isRootNode) || (policy.includeViewportOffset && isRootNode);The
includeViewportOffsetflag only applies the root node's transform. For aformSheetmodal, the Y offset from the top of the screen (the ~62pt gap where the modal doesn't cover the full screen) would need to be represented as a transform on the root shadow node. But the modal's screen position is a UIKit concern — it's theUIViewControllerpresentation that positions the modal content on screen, not a shadow tree transform.The key is at
ModalHostViewShadowNode.h:The
ModalHostViewShadowNodehas theRootNodeKindtrait. This means whencomputeRelativeLayoutMetricswalks up from the view, it stops at the modal node (seeLayoutableShadowNode.cpp):Then at the "apply transform" step:
auto shouldApplyTransformation = (policy.includeTransform && !isRootNode) || (policy.includeViewportOffset && isRootNode);For the Modal's root node,
includeViewportOffset && isRootNodeistrue, so it appliesgetTransform(). ButModalHostViewShadowNodedoesn't overridegetTransform()— onlyRootShadowNodedoes (which translates byviewportOffset). The defaultgetTransform()returnsTransform::Identity().So the modal's screen position (the formSheet top inset) is simply never encoded anywhere in the shadow tree.
Solution
The fix is in
RCTModalHostViewComponentView.mm. InboundsDidChange, when the modal's bounds change, the state is updated with just the size:The fix needs to:
ModalHostViewStateto include aviewportOffset(the modal's origin on screen)boundsDidChangecompute the modal view controller's actual screen position (e.g.,self.viewController.view.frame.originin window coordinates) and pass it as the viewport offsetgetTransform()onModalHostViewShadowNodeto apply the viewport offset from state.RootNodeKindnode when this node itself is measured (see Measuring theModalitself below).This way, when
measureInWindowwalks up to the modal (which is aRootNodeKind),includeViewportOffsettriggersgetTransform(), which now returns the actual screen offset instead of identity.Measuring the
ModalitselfImportant
The viewport offset of the modal is applied only to the views inside of it. Measuring
<Modal ref>itself returns exactly the same values as onmain.<Modal ref>points at theModalHostViewhost node. As @zoontek noticed in the review, with thegetTransform()override alonemeasureInWindow()on that node would add two viewport offsets: the one of the modal and the one of the main root.It happens in the same loop of
computeRelativeLayoutMetrics. When the modal is the measured node, it is the first node of the chain (i == 0) and the walk continues through its ancestors up to the main root:But the
isRootNodecheck is applied to every node of the chain, including the measured one:auto shouldApplyTransformation = (policy.includeTransform && !isRootNode) || (policy.includeViewportOffset && isRootNode);It was harmless before, because the modal host never had a transform. Now its transform is the viewport offset, so the result was the position of the host in the main tree + the viewport offset of the main root + the viewport offset of the modal.
The fix is to skip the viewport offset of a
RootNodeKindnode when this node is the one being measured:The overflow clipping rect (used by
IntersectionObserver) follows the same decision now, otherwise the modal would be clipped by its own viewport offset.<Modal ref>)main)Why this approach
<Modal>is declared in JSX, and it has nothing to do with the place where UIKit presents the modal. Measuring across the modal is exactly the bug that theRootNodeKindtrait was introduced for (fdd133e, c381518), and it is covered by therelativeLayoutMetricsOnNodesCrossingRootKindNodetest.<Modal ref>measurements for all presentation styles, which is out of scope of this PR.getTransform()? This is the mechanism that the main root already uses (154ce78): the native side reports where the root is located in the window, andincludeViewportOffsetappliesgetTransform()of aRootNodeKindnode. The modal is a root of the same kind that just never reported its offset. Measuring natively (as Paper did) is not an option, because Fabric measures synchronously from the shadow tree.Note
InputAccessoryViewis the only other core component with theRootNodeKindtrait that can be the measured node. Its owntransformstyle is not applied anymore whenInputAccessoryViewitself is measured withmeasureInWindow/getBoundingClientRect(measurealready ignored it), which should have no practical effect.Changelog:
[IOS] [FIXED]
measureInWindowcalculations forModalwithformSheetpresentationTest Plan:
I used this code in RNTester:
Click to expand
If you press on a red rectangle with a fix you will see a correct
y=72(10pt padding in container + 62pt safe area padding):Without a fix the
y=10(which is incorrect).The measurement of the views inside the modal and of the modal itself is also covered by two new unit tests in
LayoutableShadowNodeTest.cpp:includeViewportOffsetOnNodesCrossingRootKindNode- a view inside the modal is offset only by the viewport offset of the modal;includeViewportOffsetOnRootKindNode- the modal itself is offset only by the viewport offset of the main root, and it is not clipped by its own viewport offset.