Skip to content

fix: measureInWindow calculations for Modal with formSheet presentation - #59098

Open
kirillzyusko wants to merge 4 commits into
react:mainfrom
kirillzyusko:fix/use-window-dimensions-in-form-sheet-modal-on-ios
Open

kirillzyusko wants to merge 4 commits into
react:mainfrom
kirillzyusko:fix/use-window-dimensions-in-form-sheet-modal-on-ios

Conversation

@kirillzyusko

@kirillzyusko kirillzyusko commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

Coming from #56062 but after fork re-sync

Fixed measureInWindow calculations for Modal with formSheet presentation.

This is only Fabric problem (paper works well because it relies on UIKit implementation).

Problem

The core issue is in how measureInWindow works 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 a formSheet modal is presented, the modal's view controller lives in a separate UIWindow or has a known offset from the top of the screen. UIKit's convertRect:fromView: correctly accounts for this because it works with the real rendered view positions.

Fabric's measureInWindow (in DOM.cpp) works entirely in the shadow tree, not the native view hierarchy. It calls getLayoutMetricsFromRoot which walks up the shadow node ancestors accumulating frame origins. The critical logic is at LayoutableShadowNode.cpp:

auto shouldApplyTransformation = (policy.includeTransform && !isRootNode) ||
    (policy.includeViewportOffset && isRootNode);

The includeViewportOffset flag only applies the root node's transform. For a formSheet modal, 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 the UIViewController presentation that positions the modal content on screen, not a shadow tree transform.

The key is at ModalHostViewShadowNode.h:

traits.set(ShadowNodeTraits::Trait::RootNodeKind);

The ModalHostViewShadowNode has the RootNodeKind trait. This means when computeRelativeLayoutMetrics walks up from the view, it stops at the modal node (see LayoutableShadowNode.cpp):

if (shadowNode.getTraits().check(ShadowNodeTraits::Trait::RootNodeKind)) {
  // If this is a node with a `RootNodeKind` trait, we need to stop right there.
  break;
}

Then at the "apply transform" step:

auto shouldApplyTransformation = (policy.includeTransform && !isRootNode) ||
    (policy.includeViewportOffset && isRootNode);

For the Modal's root node, includeViewportOffset && isRootNode is true, so it applies getTransform(). But ModalHostViewShadowNode doesn't override getTransform() — only RootShadowNode does (which translates by viewportOffset). The default getTransform() returns Transform::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. In boundsDidChange, when the modal's bounds change, the state is updated with just the size:

auto newState = ModalHostViewState{RCTSizeFromCGSize(newBounds.size)};

The fix needs to:

  1. Extend ModalHostViewState to include a viewportOffset (the modal's origin on screen)
  2. In boundsDidChange compute the modal view controller's actual screen position (e.g., self.viewController.view.frame.origin in window coordinates) and pass it as the viewport offset
  3. Override getTransform() on ModalHostViewShadowNode to apply the viewport offset from state.
  4. Skip the viewport offset of a RootNodeKind node when this node itself is measured (see Measuring the Modal itself below).

This way, when measureInWindow walks up to the modal (which is a RootNodeKind), includeViewportOffset triggers getTransform(), which now returns the actual screen offset instead of identity.

Measuring the Modal itself

Important

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 on main.

<Modal ref> points at the ModalHostView host node. As @zoontek noticed in the review, with the getTransform() override alone measureInWindow() 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:

// Even if this is a node with a `RootNodeKind` trait, we don't treat it as
// root because we measure it from an outside tree perspective.
shadowNodeList.push_back(descendantNode);

But the isRootNode check 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 RootNodeKind node when this node is the one being measured:

auto shouldApplyTransformation = (policy.includeTransform && !isRootNode) ||
    (policy.includeViewportOffset && isRootNode && i != 0);

The overflow clipping rect (used by IntersectionObserver) follows the same decision now, otherwise the modal would be clipped by its own viewport offset.

What is measured Chain of nodes Applied viewport offset
A view inside the modal view → … → modal (stop) The one of the modal
The modal itself (<Modal ref>) modal → … → main root (stop) The one of the main root (same as on main)

Why this approach

  1. Why not walk through the modal up to the main root? The position of the host node in the main tree is the place where <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 the RootNodeKind trait was introduced for (fdd133e, c381518), and it is covered by the relativeLayoutMetricsOnNodesCrossingRootKindNode test.
  2. Why not return the own viewport offset when the modal itself is measured? The host node is intentionally measured from the outside tree perspective (caab26e). Returning the own viewport offset instead would change <Modal ref> measurements for all presentation styles, which is out of scope of this PR.
  3. Why the state and getTransform()? This is the mechanism that the main root already uses (154ce78): the native side reports where the root is located in the window, and includeViewportOffset applies getTransform() of a RootNodeKind node. 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

InputAccessoryView is the only other core component with the RootNodeKind trait that can be the measured node. Its own transform style is not applied anymore when InputAccessoryView itself is measured with measureInWindow/getBoundingClientRect (measure already ignored it), which should have no practical effect.

Changelog:

[IOS] [FIXED] measureInWindow calculations for Modal with formSheet presentation

Test Plan:

I used this code in RNTester:

Click to expand
/**
 * Copyright (c) Meta Platforms, Inc. and affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @flow strict-local
 * @format
 */

/* eslint-disable no-alert */

import type {RNTesterModuleExample} from '../../types/RNTesterTypes';
import type {ModalProps} from 'react-native';

import RNTesterButton from '../../components/RNTesterButton';
import RNTesterText from '../../components/RNTesterText';
import {RNTesterThemeContext} from '../../components/RNTesterTheme';
import RNTOption from '../../components/RNTOption';
import * as React from 'react';
import {useCallback, useContext, useState, useRef} from 'react';
import {Modal, Platform, StyleSheet, Switch, Text, TouchableOpacity, View} from 'react-native';

const animationTypes = ['slide', 'none', 'fade'] as const;
const presentationStyles = [
  'fullScreen',
  'pageSheet',
  'formSheet',
  'overFullScreen',
] as const;
const supportedOrientations = [
  'portrait',
  'portrait-upside-down',
  'landscape',
  'landscape-left',
  'landscape-right',
] as const;

const backdropColors = ['red', 'blue', undefined];

function ModalPresentation() {
  const onDismiss = useCallback(() => {
    alert('onDismiss');
  }, []);

  const onShow = useCallback(() => {
    alert('onShow');
  }, []);

  const onRequestClose = useCallback(() => {
    console.log('onRequestClose');
    setProps(prev => ({...prev, visible: false}));
  }, []);

  const [props, setProps] = useState<ModalProps>({
    allowSwipeDismissal: false,
    animationType: 'none',
    backdropColor: undefined,
    hardwareAccelerated: false,
    navigationBarTranslucent: false,
    onDismiss: undefined,
    onShow: undefined,
    presentationStyle: Platform.select({
      default: undefined,
      ios: 'fullScreen',
    }),
    statusBarTranslucent: false,
    supportedOrientations: Platform.select({
      default: undefined,
      ios: ['portrait'],
    }),
    transparent: false,
    visible: false,
  });
  const presentationStyle = props.presentationStyle;
  const hardwareAccelerated = props.hardwareAccelerated;
  const statusBarTranslucent = props.statusBarTranslucent;
  const navigationBarTranslucent = props.navigationBarTranslucent;
  const allowSwipeDismissal = props.allowSwipeDismissal;
  const backdropColor = props.backdropColor;
  const backgroundColor = useContext(RNTesterThemeContext).BackgroundColor;

  const [currentOrientation, setCurrentOrientation] = useState('unknown');

  type OrientationChangeEvent = Parameters<
    NonNullable<ModalProps['onOrientationChange']>,
  >[0];
  const onOrientationChange = (event: OrientationChangeEvent) =>
    setCurrentOrientation(event.nativeEvent.orientation);

  const controls = (
    <>
      <View style={styles.inlineBlock}>
        <RNTesterText style={styles.title}>
          Status Bar Translucent 🟢
        </RNTesterText>
        <Switch
          value={statusBarTranslucent}
          onValueChange={enabled =>
            setProps(prev => ({
              ...prev,
              navigationBarTranslucent: false,
              statusBarTranslucent: enabled,
            }))
          }
        />
      </View>
      <View style={styles.inlineBlock}>
        <RNTesterText style={styles.title}>
          Navigation Bar Translucent 🟢
        </RNTesterText>
        <Switch
          value={navigationBarTranslucent}
          onValueChange={enabled => {
            setProps(prev => ({
              ...prev,
              navigationBarTranslucent: enabled,
              statusBarTranslucent: enabled,
            }));
          }}
        />
      </View>
      <View style={styles.inlineBlock}>
        <RNTesterText style={styles.title}>
          Hardware Acceleration 🟢
        </RNTesterText>
        <Switch
          value={hardwareAccelerated}
          onValueChange={enabled =>
            setProps(prev => ({
              ...prev,
              hardwareAccelerated: enabled,
            }))
          }
        />
      </View>

      <View style={styles.inlineBlock}>
        <RNTesterText style={styles.title}>
          Allow Swipe Dismissal ⚫️
        </RNTesterText>
        <Switch
          value={allowSwipeDismissal}
          onValueChange={enabled =>
            setProps(prev => ({
              ...prev,
              allowSwipeDismissal: enabled,
            }))
          }
        />
      </View>
      <View style={styles.block}>
        <RNTesterText style={styles.title}>Presentation Style ⚫️</RNTesterText>
        <View style={styles.row}>
          {presentationStyles.map(type => (
            <RNTOption
              key={type}
              disabled={Platform.OS !== 'ios'}
              style={styles.option}
              label={type}
              multiSelect={true}
              onPress={() =>
                setProps(prev => {
                  if (type === 'overFullScreen' && prev.transparent === true) {
                    return {
                      ...prev,
                      presentationStyle: type,
                      transparent: false,
                    };
                  }
                  return {
                    ...prev,
                    presentationStyle:
                      type === prev.presentationStyle ? undefined : type,
                  };
                })
              }
              selected={type === presentationStyle}
            />
          ))}
        </View>
      </View>
      <View style={styles.block}>
        <View style={styles.rowWithSpaceBetween}>
          <RNTesterText style={styles.title}>Transparent</RNTesterText>
          <Switch
            value={props.transparent}
            onValueChange={enabled =>
              setProps(prev => ({...prev, transparent: enabled}))
            }
          />
        </View>
        {Platform.OS === 'ios' && presentationStyle !== 'overFullScreen' ? (
          <RNTesterText style={styles.warning}>
            iOS Modal can only be transparent with 'overFullScreen' Presentation
            Style
          </RNTesterText>
        ) : null}
      </View>
      <View style={styles.block}>
        <RNTesterText style={styles.title}>
          Supported Orientation ⚫️
        </RNTesterText>
        <View style={styles.row}>
          {supportedOrientations.map(orientation => (
            <RNTOption
              key={orientation}
              disabled={Platform.OS !== 'ios'}
              style={styles.option}
              label={orientation}
              multiSelect={true}
              onPress={() =>
                setProps(prev => {
                  if (prev.supportedOrientations?.includes(orientation)) {
                    return {
                      ...prev,
                      supportedOrientations: prev.supportedOrientations?.filter(
                        o => o !== orientation,
                      ),
                    };
                  }
                  return {
                    ...prev,
                    supportedOrientations: [
                      ...(prev.supportedOrientations ?? []),
                      orientation,
                    ],
                  };
                })
              }
              selected={props.supportedOrientations?.includes(orientation)}
            />
          ))}
        </View>
      </View>
      <View style={styles.block}>
        <RNTesterText style={styles.title}>Actions</RNTesterText>
        <View style={styles.row}>
          <RNTOption
            key="onShow"
            style={styles.option}
            label="onShow"
            multiSelect={true}
            onPress={() =>
              setProps(prev => ({
                ...prev,
                onShow: prev.onShow ? undefined : onShow,
              }))
            }
            selected={!!props.onShow}
          />
          <RNTOption
            key="onDismiss"
            style={styles.option}
            label="onDismiss ⚫️"
            disabled={Platform.OS !== 'ios'}
            onPress={() =>
              setProps(prev => ({
                ...prev,
                onDismiss: prev.onDismiss ? undefined : onDismiss,
              }))
            }
            selected={!!props.onDismiss}
          />
        </View>
      </View>
      <View style={styles.block}>
        <RNTesterText style={styles.title}>Backdrop Color ⚫️</RNTesterText>
        <View style={styles.row}>
          {backdropColors.map(type => (
            <RNTOption
              key={type ?? 'default'}
              style={styles.option}
              label={type ?? 'default'}
              multiSelect={true}
              onPress={() =>
                setProps(prev => ({
                  ...prev,
                  backdropColor: type,
                }))
              }
              selected={type === backdropColor}
            />
          ))}
        </View>
      </View>
    </>
  );

  const measureRef = useRef<View>(null);

  return (
    <View>
      <RNTesterButton
        onPress={() => setProps(prev => ({...prev, visible: true}))}>
        Show Modal
      </RNTesterButton>
      <Modal
        {...props}
        onRequestClose={onRequestClose}
        onOrientationChange={onOrientationChange}>
        <View style={styles.modalContainer}>
          <View style={[styles.modalInnerContainer, {backgroundColor}]}>
            <TouchableOpacity onPress={() => measureRef.current.measureInWindow((x, y, width, height) => {
              console.log(x, y, width, height);
            })}>
              <View ref={measureRef} style={{width: 200, height: 50, backgroundColor: "red"}} />
            </TouchableOpacity>
            <Text testID="modal_animationType_text">
              This modal was presented with animationType: '
              {props.animationType}'
            </Text>
            {Platform.OS === 'ios' ? (
              <Text>
                It is currently displayed in {currentOrientation} mode.
              </Text>
            ) : null}
            <RNTesterButton
              onPress={() => setProps(prev => ({...prev, visible: false}))}>
              Close
            </RNTesterButton>
            {controls}
          </View>
        </View>
      </Modal>
      <View style={styles.block}>
        <RNTesterText style={styles.title}>Animation Type</RNTesterText>
        <View style={styles.row}>
          {animationTypes.map(type => (
            <RNTOption
              key={type}
              style={styles.option}
              label={type}
              onPress={() => setProps(prev => ({...prev, animationType: type}))}
              selected={type === props.animationType}
            />
          ))}
        </View>
      </View>
      {controls}
    </View>
  );
}

const styles = StyleSheet.create({
  block: {
    borderBottomWidth: 1,
    borderColor: 'rgba(0,0,0, 0.1)',
    padding: 6,
  },
  inlineBlock: {
    alignItems: 'center',
    borderBottomWidth: 1,
    borderColor: 'rgba(0,0,0, 0.1)',
    flexDirection: 'row',
    justifyContent: 'space-between',
    padding: 6,
  },
  modalContainer: {
    flex: 1,
    justifyContent: 'center',
    padding: 20,
  },
  modalInnerContainer: {
    borderRadius: 10,
    padding: 10,
  },
  option: {
    marginRight: 8,
    marginTop: 6,
  },
  row: {
    flexDirection: 'row',
    flexWrap: 'wrap',
  },
  rowWithSpaceBetween: {
    flexDirection: 'row',
    justifyContent: 'space-between',
  },
  title: {
    fontWeight: 'bold',
    margin: 3,
  },
  warning: {
    color: 'red',
    fontSize: 12,
    margin: 3,
  },
});

export default ({
  description: 'Modals can be presented with or without animation',
  name: 'basic',
  render: (): React.Node => <ModalPresentation />,
  title: 'Modal Presentation',
}: RNTesterModuleExample);

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):

Screenshot 2026-03-11 at 18 46 03

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.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Oct 15, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Oct 15, 2026
@kirillzyusko
kirillzyusko force-pushed the fix/use-window-dimensions-in-form-sheet-modal-on-ios branch from 042dfcb to eb9d254 Compare October 15, 2026 11:13
@kirillzyusko

Copy link
Copy Markdown
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
kirillzyusko force-pushed the fix/use-window-dimensions-in-form-sheet-modal-on-ios branch from eb9d254 to 952bf83 Compare November 2, 2026 18:22

extern const char ModalHostViewComponentName[] = "ModalHostView";

Transform ModalHostViewShadowNode::getTransform() const {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

<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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed it!

@kirillzyusko
kirillzyusko force-pushed the fix/use-window-dimensions-in-form-sheet-modal-on-ios branch from 9b35ae9 to 7f30936 Compare November 18, 2026 10:26
@github-actions

Copy link
Copy Markdown

Visual regression

1 visual difference(s) at 7f30936f779c. Visual differences need maintainer approval. Review the report, then apply visual-change-approved.

ios touchable/feedback-events/pressed: Changed, 1620 px (0.285%)

diff

Expected and Captured
Expected Captured
expected captured

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants