From 76bf5961b510ef3c30eaf88d51992ecd76b6a1d8 Mon Sep 17 00:00:00 2001 From: GCyganek Date: Thu, 3 Dec 2026 13:32:54 +0100 Subject: [PATCH 1/6] Move modal header back buttons into the iPhone Duo vertical bar --- .../ModalVerticalBarContext.ts | 11 ++ .../Modal/ModalVerticalBar/index.ios.tsx | 100 ++++++++++ .../Modal/ModalVerticalBar/index.tsx | 8 + .../Modal/ModalVerticalBar/types.ts | 11 ++ .../Modal/ReanimatedModal/index.tsx | 45 +++-- .../useVerticalBarBackButton/index.ios.ts | 17 +- src/styles/index.ts | 8 + src/styles/variables.ts | 6 + tests/unit/ModalVerticalBarTest.tsx | 187 ++++++++++++++++++ tests/unit/useVerticalBarBackButtonTest.tsx | 57 ++++-- 10 files changed, 414 insertions(+), 36 deletions(-) create mode 100644 src/components/Modal/ModalVerticalBar/ModalVerticalBarContext.ts create mode 100644 src/components/Modal/ModalVerticalBar/index.ios.tsx create mode 100644 src/components/Modal/ModalVerticalBar/index.tsx create mode 100644 src/components/Modal/ModalVerticalBar/types.ts create mode 100644 tests/unit/ModalVerticalBarTest.tsx diff --git a/src/components/Modal/ModalVerticalBar/ModalVerticalBarContext.ts b/src/components/Modal/ModalVerticalBar/ModalVerticalBarContext.ts new file mode 100644 index 000000000000..d312208e0178 --- /dev/null +++ b/src/components/Modal/ModalVerticalBar/ModalVerticalBarContext.ts @@ -0,0 +1,11 @@ +import {createContext} from 'react'; + +type ModalVerticalBarContextType = { + /** Shows a back button in the vertical bar while the modal is shown, for a header in the modal. Returns the cleanup. */ + addBackButton: (onPress: () => void) => () => void; +}; + +/** Given by a modal that has a bar of its own in the vertical bar of a foldable iPhone, and undefined elsewhere. */ +const ModalVerticalBarContext = createContext(undefined); + +export default ModalVerticalBarContext; diff --git a/src/components/Modal/ModalVerticalBar/index.ios.tsx b/src/components/Modal/ModalVerticalBar/index.ios.tsx new file mode 100644 index 000000000000..d38dc3b669ed --- /dev/null +++ b/src/components/Modal/ModalVerticalBar/index.ios.tsx @@ -0,0 +1,100 @@ +/** + * Gives a modal a bar of its own in the vertical bar of a foldable iPhone. A modal covers the screen it is shown over and + * that screen's bar items, so a back button in the modal's header can only move into the vertical bar through a bar that + * belongs to the modal. UIKit lays out the items of a shown navigation bar in the vertical bar, so the modal gets a native + * stack of one empty screen, whose bar has no title and no background and only shows the back button. + * + * The stack only covers the vertical bar, next to the content rather than around it. A screen mounted outside a React root + * handles touches itself, which would take them away from the modal's own handler, and its native views take touches + * wherever they lie, so the content would stop responding under it. + */ +import VerticalBarBackButton from '@components/VerticalBarBackButton'; + +import useHasVerticalBar from '@hooks/useHasVerticalBar'; +import useSafeAreaInsets from '@hooks/useSafeAreaInsets'; +import useThemeStyles from '@hooks/useThemeStyles'; + +import variables from '@styles/variables'; + +import noop from 'lodash/noop'; +import React, {useState} from 'react'; +import {ScreenStack, ScreenStackHeaderLeftView, ScreenStackItem} from 'react-native-screens'; + +import type ModalVerticalBarProps from './types'; + +import ModalVerticalBarContext from './ModalVerticalBarContext'; + +/** A header in the modal that has a back button. */ +type BackButtonOwner = { + /** What a press on the button does */ + onPress: () => void; +}; + +function ModalVerticalBar({children, isClosing = false}: ModalVerticalBarProps) { + const styles = useThemeStyles(); + const hasVerticalBar = useHasVerticalBar(); + const insets = useSafeAreaInsets(); + const [backButtonOwners, setBackButtonOwners] = useState([]); + + const addBackButton = (onPress: () => void) => { + const owner = {onPress}; + setBackButtonOwners((owners) => [...owners, owner]); + return () => setBackButtonOwners((owners) => owners.filter((currentOwner) => currentOwner !== owner)); + }; + const contextValue = {addBackButton}; + + // The header that came last is the one on top, e.g. a page of a modal that shows one page after another. + const currentOwner = backButtonOwners.at(-1); + const [lastOwner, setLastOwner] = useState(currentOwner); + if (currentOwner && currentOwner !== lastOwner) { + setLastOwner(currentOwner); + } + // A closing modal drops its content, and with it the header, at once, but stays over the screen and its bar until the + // transition ends. The button stays until then, so the vertical bar is never left without one, and a press does + // nothing, as the header already went back. + const shownOwner = currentOwner ?? (isClosing ? lastOwner : undefined); + const onBackButtonPress = currentOwner?.onPress ?? noop; + // The vertical bar takes the side safe area inset of its edge. The bar reaches a little past it, over the edge of the + // content, so UIKit lays the button out as on a screen. + const isOnLeftEdge = insets.left > insets.right; + const sideInset = isOnLeftEdge ? insets.left : insets.right; + const width = Math.max(sideInset, variables.iosVerticalBarNavigationBarMinWidth); + const verticalBarStyle = isOnLeftEdge ? {left: 0, width} : {right: 0, width}; + // In Split View the window leaves the vertical bar out of its safe area, so the modal lays its content over the bar, + // and its headers keep their own button. + const hasBar = hasVerticalBar && sideInset > 0; + + return ( + + {children} + {hasBar && !!shownOwner && ( + + + + + ), + }} + > + {null} + + + )} + + ); +} + +export default ModalVerticalBar; diff --git a/src/components/Modal/ModalVerticalBar/index.tsx b/src/components/Modal/ModalVerticalBar/index.tsx new file mode 100644 index 000000000000..995315225f8b --- /dev/null +++ b/src/components/Modal/ModalVerticalBar/index.tsx @@ -0,0 +1,8 @@ +import type ModalVerticalBarProps from './types'; + +/** Only iOS has a vertical bar, so elsewhere a modal shows just its content. */ +function ModalVerticalBar({children}: ModalVerticalBarProps) { + return children; +} + +export default ModalVerticalBar; diff --git a/src/components/Modal/ModalVerticalBar/types.ts b/src/components/Modal/ModalVerticalBar/types.ts new file mode 100644 index 000000000000..5b75c5eed66a --- /dev/null +++ b/src/components/Modal/ModalVerticalBar/types.ts @@ -0,0 +1,11 @@ +import type {ReactNode} from 'react'; + +type ModalVerticalBarProps = { + /** The modal's backdrop and content, which the bar is laid over along the edge of the vertical bar */ + children: ReactNode; + + /** Whether the modal is on its way out: its content is gone, but the modal still covers the screen */ + isClosing?: boolean; +}; + +export default ModalVerticalBarProps; diff --git a/src/components/Modal/ReanimatedModal/index.tsx b/src/components/Modal/ReanimatedModal/index.tsx index 764241f3bb35..e8eb98d46036 100644 --- a/src/components/Modal/ReanimatedModal/index.tsx +++ b/src/components/Modal/ReanimatedModal/index.tsx @@ -1,5 +1,6 @@ import FocusTrapForModal from '@components/FocusTrap/FocusTrapForModal'; import KeyboardAvoidingView from '@components/KeyboardAvoidingView'; +import ModalVerticalBar from '@components/Modal/ModalVerticalBar'; import useThemeStyles from '@hooks/useThemeStyles'; import useWindowDimensions from '@hooks/useWindowDimensions'; @@ -242,27 +243,29 @@ function ReanimatedModal({ style={modalStyle} {...props} > - {isBackdropMounted && hasBackdrop && backdropView} - {avoidKeyboard ? ( - - {isVisibleState && containerView} - - ) : ( - - {isVisibleState && containerView} - - )} + + {isBackdropMounted && hasBackdrop && backdropView} + {avoidKeyboard ? ( + + {isVisibleState && containerView} + + ) : ( + + {isVisibleState && containerView} + + )} + ); diff --git a/src/hooks/useVerticalBarBackButton/index.ios.ts b/src/hooks/useVerticalBarBackButton/index.ios.ts index 13dbeea4c7cf..5c22dda91d04 100644 --- a/src/hooks/useVerticalBarBackButton/index.ios.ts +++ b/src/hooks/useVerticalBarBackButton/index.ios.ts @@ -3,6 +3,7 @@ * navigation bar: at the top, under the status items. */ import ModalContext from '@components/Modal/ModalContext'; +import ModalVerticalBarContext from '@components/Modal/ModalVerticalBar/ModalVerticalBarContext'; import useHasVerticalBar from '@hooks/useHasVerticalBar'; import useIsInSidePanel from '@hooks/useIsInSidePanel'; @@ -18,22 +19,30 @@ const useVerticalBarBackButton: UseVerticalBarBackButton = (goBack, shouldShowBa // useNavigation() throws outside a navigator, and headers are rendered there too, so read the context, which is then empty. const screenNavigation = useContext(NavigationContext); const hasVerticalBar = useHasVerticalBar(); - // A modal covers the screen and its bars, so a button there could not be reached. + // A modal covers the screen and its bars, so a header in a modal puts its button in the modal's own bar. const isInModal = !useContext(ModalContext).default; + const modalVerticalBar = useContext(ModalVerticalBarContext); // The side panel is not a screen with a bar of its own, so a header in it keeps its button. const isInSidePanel = useIsInSidePanel(); - const isInVerticalBar = shouldShowBackButton && hasVerticalBar && !isInModal && !isInSidePanel && !!screenNavigation; + const hasBar = isInModal ? !!modalVerticalBar : !!screenNavigation; + const isInVerticalBar = shouldShowBackButton && hasVerticalBar && !isInSidePanel && hasBar; // The handler gets a new identity on most renders. Read through an effect event, a press runs the latest one // without the button being handed over again each time. const pressBackButton = useEffectEvent(goBack); // Not useLayoutEffect: a covered screen is frozen and loses its layout effects, so the button would blink on return. useEffect(() => { - if (!isInVerticalBar || !screenNavigation) { + if (!isInVerticalBar) { + return; + } + if (isInModal) { + return modalVerticalBar?.addBackButton(() => pressBackButton()); + } + if (!screenNavigation) { return; } return addVerticalBarBackButton(screenNavigation, () => pressBackButton()); - }, [isInVerticalBar, screenNavigation]); + }, [isInVerticalBar, isInModal, modalVerticalBar, screenNavigation]); return isInVerticalBar; }; diff --git a/src/styles/index.ts b/src/styles/index.ts index 079bf12173e3..17c1c7a28c66 100644 --- a/src/styles/index.ts +++ b/src/styles/index.ts @@ -2178,6 +2178,14 @@ const staticStyles = (theme: ThemeColors) => height: variables.iosVerticalBarItemSize - 2 * variables.iosVerticalBarItemViewPadding, }, + // The bar of a modal in the vertical bar: the full height of the window along the edge the vertical bar is on, over the modal's content. + modalVerticalBar: { + position: 'absolute', + top: 0, + bottom: 0, + zIndex: variables.modalVerticalBarZIndex, + }, + // A floating button is larger than the items UIKit draws in the vertical bar, so there it takes their size. Its icon keeps its own. verticalBarItemButton: { width: variables.iosVerticalBarItemSize, diff --git a/src/styles/variables.ts b/src/styles/variables.ts index aac5802560bc..efe22954cc23 100644 --- a/src/styles/variables.ts +++ b/src/styles/variables.ts @@ -174,6 +174,11 @@ export default { iosVerticalBarItemViewPadding: 5, /** The size UIKit draws its own items at in the vertical bar. The floating buttons take it there so they match the items of other apps. */ iosVerticalBarItemSize: 48, + /** + * The narrowest navigation bar that keeps an item of the vertical bar at its size. UIKit lays the items of a narrower + * bar out narrower and off center, and the vertical bar of the iPhone Duo is 84 wide. + */ + iosVerticalBarNavigationBarMinWidth: 92, iconHeader: 48, iconSection: 68, iouAmountTextSize: 40, @@ -435,6 +440,7 @@ export default { reportActionImagesMultipleImageHeight: 110, reportActionItemImagesMoreCornerTriangleWidth: 40, growlNotificationZIndex: 10001, + modalVerticalBarZIndex: 10002, popoverZIndex: 10000, modalBaseZIndex: 9999, autoCompleteSuggestionsZIndex: 9999, diff --git a/tests/unit/ModalVerticalBarTest.tsx b/tests/unit/ModalVerticalBarTest.tsx new file mode 100644 index 000000000000..318e2b557898 --- /dev/null +++ b/tests/unit/ModalVerticalBarTest.tsx @@ -0,0 +1,187 @@ +import {fireEvent, render, screen} from '@testing-library/react-native'; + +import ModalVerticalBar from '@components/Modal/ModalVerticalBar/index.ios'; +import ModalVerticalBarContext from '@components/Modal/ModalVerticalBar/ModalVerticalBarContext'; + +import variables from '@styles/variables'; + +import type {ReactNode} from 'react'; +import type ReactNative from 'react-native'; +import type {StyleProp, ViewStyle} from 'react-native'; +import type {EdgeInsets} from 'react-native-safe-area-context'; +import type {ScreenStackHeaderConfigProps} from 'react-native-screens'; + +import React, {useContext, useEffect} from 'react'; +import {StyleSheet} from 'react-native'; + +let mockHasVerticalBar = true; +let mockInsets: EdgeInsets = {top: 0, right: 84, bottom: 34, left: 0}; + +jest.mock('@hooks/useHasVerticalBar', () => () => mockHasVerticalBar); +jest.mock('@hooks/useSafeAreaInsets', () => () => mockInsets); +jest.mock('@components/VerticalBarBackButton', () => { + const {Pressable: MockPressable} = jest.requireActual('react-native'); + return ({onPress}: {onPress: () => void}) => ( + + ); +}); +// The native stack is only a host for the bar here, so it renders the views it is given, and what its bar holds. +jest.mock('react-native-screens', () => { + const {View: MockView} = jest.requireActual('react-native'); + return { + ScreenStack: ({children, style}: {children: ReactNode; style: StyleProp}) => ( + + {children} + + ), + ScreenStackItem: ({children, headerConfig}: {children: ReactNode; headerConfig: ScreenStackHeaderConfigProps}) => ( + <> + {headerConfig.children} + {children} + + ), + ScreenStackHeaderLeftView: ({children}: {children: ReactNode}) => children, + }; +}); + +/** Stands in for a header in the modal that has a back button. */ +function Header({goBack}: {goBack: () => void}) { + const modalVerticalBar = useContext(ModalVerticalBarContext); + useEffect(() => modalVerticalBar?.addBackButton(goBack), [modalVerticalBar, goBack]); + return null; +} + +describe('ModalVerticalBar', () => { + beforeEach(() => { + mockHasVerticalBar = true; + mockInsets = {top: 0, right: 84, bottom: 34, left: 0}; + }); + + it('shows a back button in a bar over the vertical bar while a header in the modal has one, and presses go to the header shown last', () => { + // Given a modal on a foldable iPhone whose vertical bar is on the right, with a header that has a back button + const goBackFromFirst = jest.fn(); + const goBackFromSecond = jest.fn(); + const {rerender} = render( + +
+ , + ); + + // When the button is pressed, and a second header mounts over the first and the button is pressed again + fireEvent.press(screen.getByTestId('verticalBarBackButton')); + rerender( + +
+
+ , + ); + fireEvent.press(screen.getByTestId('verticalBarBackButton')); + + // Then the bar covers the right edge, a little wider than the vertical bar so UIKit keeps the button at its size, + // and each press went to the header shown at the time + expect(StyleSheet.flatten(screen.getByTestId('modalVerticalBarStack').props.style)).toEqual( + expect.objectContaining({right: 0, width: variables.iosVerticalBarNavigationBarMinWidth}), + ); + expect(goBackFromFirst).toHaveBeenCalledTimes(1); + expect(goBackFromSecond).toHaveBeenCalledTimes(1); + }); + + it('puts the bar on the left edge when the vertical bar is there', () => { + // Given a folded iPhone in landscape, whose vertical bar is on the left + mockInsets = {top: 0, right: 0, bottom: 21, left: 84}; + + // When a header in the modal has a back button + render( + +
+ , + ); + + // Then the bar covers the left edge + expect(StyleSheet.flatten(screen.getByTestId('modalVerticalBarStack').props.style)).toEqual(expect.objectContaining({left: 0})); + }); + + it('removes the bar once no header in the modal has a back button', () => { + // Given a modal with a header that has a back button + const {rerender} = render( + +
+ , + ); + + // When the header unmounts + rerender({null}); + + // Then the modal has no bar, so nothing covers its edge + expect(screen.queryByTestId('modalVerticalBarStack')).toBeNull(); + }); + + it('keeps the button while the modal closes, so the vertical bar is not left empty, and ignores presses on it', () => { + // Given a modal with a header that has a back button + const goBack = jest.fn(); + const {rerender} = render( + +
+ , + ); + + // When the modal starts closing, which drops its content and the header with it, while it still covers the screen, + // and the button is pressed + rerender({null}); + fireEvent.press(screen.getByTestId('verticalBarBackButton')); + + // Then the button stays until the modal is gone, and the press does not go back a second time + expect(screen.getByTestId('modalVerticalBarStack')).toBeTruthy(); + expect(goBack).not.toHaveBeenCalled(); + }); + + it('gives headers no bar without a vertical bar, so they keep their own back button', () => { + // Given a window without a vertical bar + mockHasVerticalBar = false; + const onRead = jest.fn]>(); + function Reader() { + onRead(useContext(ModalVerticalBarContext)); + return null; + } + + // When a header in the modal reads what the modal gives it + render( + + + , + ); + + // Then the modal gives it no bar, and draws none + expect(onRead).toHaveBeenLastCalledWith(undefined); + expect(screen.queryByTestId('modalVerticalBarStack')).toBeNull(); + }); + + it('gives headers no bar in Split View, where the modal lays its content over the vertical bar', () => { + // Given the left half of Split View, whose window has a vertical bar but leaves it out of its safe area + mockInsets = {top: 0, right: 0, bottom: 34, left: 0}; + const onRead = jest.fn]>(); + function Reader() { + onRead(useContext(ModalVerticalBarContext)); + return null; + } + + // When a header in the modal reads what the modal gives it + render( + + + , + ); + + // Then the modal gives it no bar, so it keeps its own button rather than one floating over the content + expect(onRead).toHaveBeenLastCalledWith(undefined); + expect(screen.queryByTestId('modalVerticalBarStack')).toBeNull(); + }); +}); diff --git a/tests/unit/useVerticalBarBackButtonTest.tsx b/tests/unit/useVerticalBarBackButtonTest.tsx index 41e5a9b8f444..c41d720195e6 100644 --- a/tests/unit/useVerticalBarBackButtonTest.tsx +++ b/tests/unit/useVerticalBarBackButtonTest.tsx @@ -1,6 +1,7 @@ import {render} from '@testing-library/react-native'; import ModalContext from '@components/Modal/ModalContext'; +import ModalVerticalBarContext from '@components/Modal/ModalVerticalBar/ModalVerticalBarContext'; import {IsInSidePanelContext} from '@hooks/useIsInSidePanel'; import useVerticalBarBackButton from '@hooks/useVerticalBarBackButton/index.ios'; @@ -68,6 +69,8 @@ function moveFocus(from: Screen, to?: Screen) { type HeaderProps = { screenNavigation: NavigationProp; isInModal?: boolean; + /** What the modal the header is in gives a header for its back button, when the modal has a bar of its own. */ + modalVerticalBar?: React.ContextType; isInSidePanel?: boolean; goBack?: () => void; /** Gets what the hook returns: whether the header leaves its own back button out. */ @@ -80,17 +83,19 @@ function BackButtonOwner({goBack, onRender}: Required {}, onRender = () => {}}: HeaderProps) { +function Header({screenNavigation, isInModal = false, modalVerticalBar, isInSidePanel = false, goBack = () => {}, onRender = () => {}}: HeaderProps) { return ( - - - - - + + + + + + + ); } @@ -222,8 +227,38 @@ describe('useVerticalBarBackButton', () => { expect(rootScreen.setOptions.mock.calls.filter(([options]) => 'unstable_headerLeftItems' in options)).toHaveLength(1); }); - it('adds no back button for a header inside a modal, which keeps its own', () => { - // Given a header rendered in a modal, which covers the screen and the bars of the screen under it + it("shows the back button of a header inside a modal in the modal's own bar, in place of the header's own", () => { + // Given a header rendered in a modal that has a bar of its own, as the modal covers the bars of the screen under it + const screen = createNavigation(); + const removeBackButton = jest.fn(); + const addBackButton = jest.fn<() => void, [() => void]>(() => removeBackButton); + const goBack = jest.fn(); + const onRender = jest.fn(); + + // When the header is rendered, the button the modal shows is pressed and the header unmounts + const {unmount} = render( +
, + ); + addBackButton.mock.calls.at(0)?.[0](); + unmount(); + + // Then the button went to the modal's bar and not to the screen under the modal, the header was told to leave + // its own button out, the press went to the header's own handler, and the button left with the header + expect(addBackButton).toHaveBeenCalledTimes(1); + expect(screen.setOptions).not.toHaveBeenCalled(); + expect(onRender).toHaveBeenLastCalledWith(true); + expect(goBack).toHaveBeenCalledTimes(1); + expect(removeBackButton).toHaveBeenCalledTimes(1); + }); + + it('adds no back button for a header inside a modal without a bar of its own, which keeps its own', () => { + // Given a header rendered in a modal with no bar of its own, which covers the screen and the bars of the screen under it const screen = createNavigation(); const onRender = jest.fn(); From 9ec5d4caab7f0ae7e13e4c1181ab2cb4b494b1f4 Mon Sep 17 00:00:00 2001 From: GCyganek Date: Thu, 3 Dec 2026 14:24:44 +0100 Subject: [PATCH 2/6] Address review of modal vertical bar back button --- .../ModalVerticalBarContext.ts | 7 +- .../Modal/ModalVerticalBar/index.ios.tsx | 49 +++++++---- src/hooks/useHasVerticalBar/index.ios.ts | 9 +- .../useVerticalBarBackButton/index.ios.ts | 8 +- src/hooks/useVerticalBarEdge/index.ios.ts | 19 +++++ src/hooks/useVerticalBarEdge/index.ts | 11 +++ tests/unit/ModalVerticalBarTest.tsx | 84 +++++++++++++++---- tests/unit/useVerticalBarBackButtonTest.tsx | 30 ++++++- 8 files changed, 172 insertions(+), 45 deletions(-) create mode 100644 src/hooks/useVerticalBarEdge/index.ios.ts create mode 100644 src/hooks/useVerticalBarEdge/index.ts diff --git a/src/components/Modal/ModalVerticalBar/ModalVerticalBarContext.ts b/src/components/Modal/ModalVerticalBar/ModalVerticalBarContext.ts index d312208e0178..1b553b5da09c 100644 --- a/src/components/Modal/ModalVerticalBar/ModalVerticalBarContext.ts +++ b/src/components/Modal/ModalVerticalBar/ModalVerticalBarContext.ts @@ -1,8 +1,11 @@ import {createContext} from 'react'; type ModalVerticalBarContextType = { - /** Shows a back button in the vertical bar while the modal is shown, for a header in the modal. Returns the cleanup. */ - addBackButton: (onPress: () => void) => () => void; + /** + * Shows a back button in the vertical bar while the modal is shown, for a header in the modal. The header passes the + * same ID each time, so it keeps its place under the headers that came after it. Returns the cleanup. + */ + addBackButton: (headerID: string, onPress: () => void) => () => void; }; /** Given by a modal that has a bar of its own in the vertical bar of a foldable iPhone, and undefined elsewhere. */ diff --git a/src/components/Modal/ModalVerticalBar/index.ios.tsx b/src/components/Modal/ModalVerticalBar/index.ios.tsx index d38dc3b669ed..c984c6aacf0c 100644 --- a/src/components/Modal/ModalVerticalBar/index.ios.tsx +++ b/src/components/Modal/ModalVerticalBar/index.ios.tsx @@ -10,14 +10,15 @@ */ import VerticalBarBackButton from '@components/VerticalBarBackButton'; -import useHasVerticalBar from '@hooks/useHasVerticalBar'; import useSafeAreaInsets from '@hooks/useSafeAreaInsets'; import useThemeStyles from '@hooks/useThemeStyles'; +import useVerticalBarEdge from '@hooks/useVerticalBarEdge'; import variables from '@styles/variables'; +import {VERTICAL_BAR_EDGE} from '@expensify/nitro-utils'; import noop from 'lodash/noop'; -import React, {useState} from 'react'; +import React, {useRef, useState} from 'react'; import {ScreenStack, ScreenStackHeaderLeftView, ScreenStackItem} from 'react-native-screens'; import type ModalVerticalBarProps from './types'; @@ -26,49 +27,67 @@ import ModalVerticalBarContext from './ModalVerticalBarContext'; /** A header in the modal that has a back button. */ type BackButtonOwner = { + /** The order the header first added its button in. A header that adds it again keeps this place. */ + order: number; + /** What a press on the button does */ onPress: () => void; }; function ModalVerticalBar({children, isClosing = false}: ModalVerticalBarProps) { const styles = useThemeStyles(); - const hasVerticalBar = useHasVerticalBar(); + const verticalBarEdge = useVerticalBarEdge(); const insets = useSafeAreaInsets(); const [backButtonOwners, setBackButtonOwners] = useState([]); + // A header adds its button again whenever what its effect depends on changes. It must not move above the headers + // that came after it then, or a press would go back from a page that is covered. + const headerOrders = useRef(new Map()); - const addBackButton = (onPress: () => void) => { - const owner = {onPress}; - setBackButtonOwners((owners) => [...owners, owner]); + const addBackButton = (headerID: string, onPress: () => void) => { + let order = headerOrders.current.get(headerID); + if (order === undefined) { + order = headerOrders.current.size; + headerOrders.current.set(headerID, order); + } + const owner = {order, onPress}; + setBackButtonOwners((owners) => [...owners, owner].sort((first, second) => first.order - second.order)); return () => setBackButtonOwners((owners) => owners.filter((currentOwner) => currentOwner !== owner)); }; const contextValue = {addBackButton}; // The header that came last is the one on top, e.g. a page of a modal that shows one page after another. const currentOwner = backButtonOwners.at(-1); - const [lastOwner, setLastOwner] = useState(currentOwner); - if (currentOwner && currentOwner !== lastOwner) { - setLastOwner(currentOwner); - } // A closing modal drops its content, and with it the header, at once, but stays over the screen and its bar until the // transition ends. The button stays until then, so the vertical bar is never left without one, and a press does - // nothing, as the header already went back. - const shownOwner = currentOwner ?? (isClosing ? lastOwner : undefined); + // nothing, as the header already went back. A modal whose headers had no button left before it closed keeps none. + const [previousShownOwner, setPreviousShownOwner] = useState(currentOwner); + const shownOwner = currentOwner ?? (isClosing ? previousShownOwner : undefined); + if (shownOwner !== previousShownOwner) { + setPreviousShownOwner(shownOwner); + } const onBackButtonPress = currentOwner?.onPress ?? noop; // The vertical bar takes the side safe area inset of its edge. The bar reaches a little past it, over the edge of the // content, so UIKit lays the button out as on a screen. - const isOnLeftEdge = insets.left > insets.right; + const isOnLeftEdge = verticalBarEdge === VERTICAL_BAR_EDGE.LEADING; const sideInset = isOnLeftEdge ? insets.left : insets.right; const width = Math.max(sideInset, variables.iosVerticalBarNavigationBarMinWidth); const verticalBarStyle = isOnLeftEdge ? {left: 0, width} : {right: 0, width}; + // The native views of the stack take every touch inside it, so the part over the content would stop the content + // under it from responding. A negative hit slop leaves that part out of the stack's touch area. + const contentOverlap = width - sideInset; + const hitSlop = isOnLeftEdge ? {right: -contentOverlap} : {left: -contentOverlap}; // In Split View the window leaves the vertical bar out of its safe area, so the modal lays its content over the bar, // and its headers keep their own button. - const hasBar = hasVerticalBar && sideInset > 0; + const hasBar = verticalBarEdge !== VERTICAL_BAR_EDGE.UNSPECIFIED && sideInset > 0; return ( {children} {hasBar && !!shownOwner && ( - + void) => WindowBarsNitroModule.addChangeListener(onChange); - -const getHasVerticalBar = () => WindowBarsNitroModule.verticalBarEdge !== VERTICAL_BAR_EDGE.UNSPECIFIED; +import {VERTICAL_BAR_EDGE} from '@expensify/nitro-utils'; /** * Whether the window has a vertical bar: the column a foldable iPhone gives the status bar, the tab bar and the bar items @@ -11,7 +8,7 @@ const getHasVerticalBar = () => WindowBarsNitroModule.verticalBarEdge !== VERTIC * is the side safe area inset of that edge. */ function useHasVerticalBar(): boolean { - return useSyncExternalStore(subscribe, getHasVerticalBar, getHasVerticalBar); + return useVerticalBarEdge() !== VERTICAL_BAR_EDGE.UNSPECIFIED; } export default useHasVerticalBar; diff --git a/src/hooks/useVerticalBarBackButton/index.ios.ts b/src/hooks/useVerticalBarBackButton/index.ios.ts index 5c22dda91d04..e727db57c8df 100644 --- a/src/hooks/useVerticalBarBackButton/index.ios.ts +++ b/src/hooks/useVerticalBarBackButton/index.ios.ts @@ -11,7 +11,7 @@ import useIsInSidePanel from '@hooks/useIsInSidePanel'; import {addVerticalBarBackButton} from '@libs/Navigation/verticalBarHost'; import {NavigationContext} from '@react-navigation/native'; -import {useContext, useEffect, useEffectEvent} from 'react'; +import {useContext, useEffect, useEffectEvent, useId} from 'react'; import type UseVerticalBarBackButton from './types'; @@ -29,6 +29,8 @@ const useVerticalBarBackButton: UseVerticalBarBackButton = (goBack, shouldShowBa // The handler gets a new identity on most renders. Read through an effect event, a press runs the latest one // without the button being handed over again each time. const pressBackButton = useEffectEvent(goBack); + // Lets a modal keep this header under the headers that came after it each time the header adds its button again. + const headerID = useId(); // Not useLayoutEffect: a covered screen is frozen and loses its layout effects, so the button would blink on return. useEffect(() => { @@ -36,13 +38,13 @@ const useVerticalBarBackButton: UseVerticalBarBackButton = (goBack, shouldShowBa return; } if (isInModal) { - return modalVerticalBar?.addBackButton(() => pressBackButton()); + return modalVerticalBar?.addBackButton(headerID, () => pressBackButton()); } if (!screenNavigation) { return; } return addVerticalBarBackButton(screenNavigation, () => pressBackButton()); - }, [isInVerticalBar, isInModal, modalVerticalBar, screenNavigation]); + }, [isInVerticalBar, isInModal, modalVerticalBar, screenNavigation, headerID]); return isInVerticalBar; }; diff --git a/src/hooks/useVerticalBarEdge/index.ios.ts b/src/hooks/useVerticalBarEdge/index.ios.ts new file mode 100644 index 000000000000..af37b4a4fc84 --- /dev/null +++ b/src/hooks/useVerticalBarEdge/index.ios.ts @@ -0,0 +1,19 @@ +import type {VerticalBarEdge} from '@expensify/nitro-utils'; + +import {WindowBarsNitroModule} from '@expensify/nitro-utils'; +import {useSyncExternalStore} from 'react'; + +const subscribe = (onChange: () => void) => WindowBarsNitroModule.addChangeListener(onChange); + +const getVerticalBarEdge = () => WindowBarsNitroModule.verticalBarEdge; + +/** + * The side edge of the vertical bar: the column a foldable iPhone gives the status bar, the tab bar and the bar items, + * folded or open in landscape. It is unspecified whenever the window's trait collection names no edge for it. The app is + * always laid out left to right, so the leading edge is the left one. + */ +function useVerticalBarEdge(): VerticalBarEdge { + return useSyncExternalStore(subscribe, getVerticalBarEdge, getVerticalBarEdge); +} + +export default useVerticalBarEdge; diff --git a/src/hooks/useVerticalBarEdge/index.ts b/src/hooks/useVerticalBarEdge/index.ts new file mode 100644 index 000000000000..801c5d519b7d --- /dev/null +++ b/src/hooks/useVerticalBarEdge/index.ts @@ -0,0 +1,11 @@ +import type {VerticalBarEdge} from '@expensify/nitro-utils'; + +/** + * Only a foldable iPhone has a vertical bar, so elsewhere it never has an edge. The value is written out because importing + * VERTICAL_BAR_EDGE would load the native modules of the package, which this platform doesn't have. + */ +function useVerticalBarEdge(): VerticalBarEdge { + return 'unspecified'; +} + +export default useVerticalBarEdge; diff --git a/tests/unit/ModalVerticalBarTest.tsx b/tests/unit/ModalVerticalBarTest.tsx index 318e2b557898..2651e9e70d71 100644 --- a/tests/unit/ModalVerticalBarTest.tsx +++ b/tests/unit/ModalVerticalBarTest.tsx @@ -5,19 +5,20 @@ import ModalVerticalBarContext from '@components/Modal/ModalVerticalBar/ModalVer import variables from '@styles/variables'; +import type {VerticalBarEdge} from '@expensify/nitro-utils'; import type {ReactNode} from 'react'; import type ReactNative from 'react-native'; -import type {StyleProp, ViewStyle} from 'react-native'; +import type {Insets, StyleProp, ViewStyle} from 'react-native'; import type {EdgeInsets} from 'react-native-safe-area-context'; import type {ScreenStackHeaderConfigProps} from 'react-native-screens'; -import React, {useContext, useEffect} from 'react'; +import React, {useContext, useEffect, useId} from 'react'; import {StyleSheet} from 'react-native'; -let mockHasVerticalBar = true; +let mockVerticalBarEdge: VerticalBarEdge = 'trailing'; let mockInsets: EdgeInsets = {top: 0, right: 84, bottom: 34, left: 0}; -jest.mock('@hooks/useHasVerticalBar', () => () => mockHasVerticalBar); +jest.mock('@hooks/useVerticalBarEdge', () => () => mockVerticalBarEdge); jest.mock('@hooks/useSafeAreaInsets', () => () => mockInsets); jest.mock('@components/VerticalBarBackButton', () => { const {Pressable: MockPressable} = jest.requireActual('react-native'); @@ -34,10 +35,11 @@ jest.mock('@components/VerticalBarBackButton', () => { jest.mock('react-native-screens', () => { const {View: MockView} = jest.requireActual('react-native'); return { - ScreenStack: ({children, style}: {children: ReactNode; style: StyleProp}) => ( + ScreenStack: ({children, style, hitSlop}: {children: ReactNode; style: StyleProp; hitSlop: Insets}) => ( {children} @@ -55,13 +57,14 @@ jest.mock('react-native-screens', () => { /** Stands in for a header in the modal that has a back button. */ function Header({goBack}: {goBack: () => void}) { const modalVerticalBar = useContext(ModalVerticalBarContext); - useEffect(() => modalVerticalBar?.addBackButton(goBack), [modalVerticalBar, goBack]); + const headerID = useId(); + useEffect(() => modalVerticalBar?.addBackButton(headerID, goBack), [modalVerticalBar, headerID, goBack]); return null; } describe('ModalVerticalBar', () => { beforeEach(() => { - mockHasVerticalBar = true; + mockVerticalBarEdge = 'trailing'; mockInsets = {top: 0, right: 84, bottom: 34, left: 0}; }); @@ -86,17 +89,18 @@ describe('ModalVerticalBar', () => { fireEvent.press(screen.getByTestId('verticalBarBackButton')); // Then the bar covers the right edge, a little wider than the vertical bar so UIKit keeps the button at its size, - // and each press went to the header shown at the time - expect(StyleSheet.flatten(screen.getByTestId('modalVerticalBarStack').props.style)).toEqual( - expect.objectContaining({right: 0, width: variables.iosVerticalBarNavigationBarMinWidth}), - ); + // but takes touches only over the vertical bar, and each press went to the header shown at the time + const stack = screen.getByTestId('modalVerticalBarStack'); + expect(StyleSheet.flatten(stack.props.style)).toEqual(expect.objectContaining({right: 0, width: variables.iosVerticalBarNavigationBarMinWidth})); + expect(stack.props.hitSlop).toEqual({left: 84 - variables.iosVerticalBarNavigationBarMinWidth}); expect(goBackFromFirst).toHaveBeenCalledTimes(1); expect(goBackFromSecond).toHaveBeenCalledTimes(1); }); - it('puts the bar on the left edge when the vertical bar is there', () => { - // Given a folded iPhone in landscape, whose vertical bar is on the left - mockInsets = {top: 0, right: 0, bottom: 21, left: 84}; + it('puts the bar on the edge UIKit names for the vertical bar, whatever the other side inset is', () => { + // Given a folded iPhone in landscape, whose vertical bar is on the left, and a right inset as large as the left one + mockVerticalBarEdge = 'leading'; + mockInsets = {top: 0, right: 84, bottom: 21, left: 84}; // When a header in the modal has a back button render( @@ -105,8 +109,38 @@ describe('ModalVerticalBar', () => { , ); - // Then the bar covers the left edge - expect(StyleSheet.flatten(screen.getByTestId('modalVerticalBarStack').props.style)).toEqual(expect.objectContaining({left: 0})); + // Then the bar covers the left edge, and takes touches only over the vertical bar + const stack = screen.getByTestId('modalVerticalBarStack'); + expect(StyleSheet.flatten(stack.props.style)).toEqual(expect.objectContaining({left: 0})); + expect(stack.props.hitSlop).toEqual({right: 84 - variables.iosVerticalBarNavigationBarMinWidth}); + }); + + it('keeps a header under the headers that came after it when it adds its button again', () => { + // Given a modal with a second page over the first, each with a header that has a back button + const goBackFromFirst = jest.fn(); + const goBackFromSecond = jest.fn(); + const {rerender} = render( + +
+
+ , + ); + + // When the covered first header adds its button again, as it does when what its effect depends on changes, and + // the button is pressed + const newGoBackFromFirst = jest.fn(); + rerender( + +
+
+ , + ); + fireEvent.press(screen.getByTestId('verticalBarBackButton')); + + // Then the press still goes back from the second page, the one shown + expect(goBackFromSecond).toHaveBeenCalledTimes(1); + expect(goBackFromFirst).not.toHaveBeenCalled(); + expect(newGoBackFromFirst).not.toHaveBeenCalled(); }); it('removes the bar once no header in the modal has a back button', () => { @@ -143,9 +177,25 @@ describe('ModalVerticalBar', () => { expect(goBack).not.toHaveBeenCalled(); }); + it('shows no button while the modal closes when no header in it had one left', () => { + // Given a modal whose header had a back button, then moved to a page whose header has none + const {rerender} = render( + +
+ , + ); + rerender({null}); + + // When the modal starts closing + rerender({null}); + + // Then the button the modal had earlier does not come back + expect(screen.queryByTestId('modalVerticalBarStack')).toBeNull(); + }); + it('gives headers no bar without a vertical bar, so they keep their own back button', () => { // Given a window without a vertical bar - mockHasVerticalBar = false; + mockVerticalBarEdge = 'unspecified'; const onRead = jest.fn]>(); function Reader() { onRead(useContext(ModalVerticalBarContext)); diff --git a/tests/unit/useVerticalBarBackButtonTest.tsx b/tests/unit/useVerticalBarBackButtonTest.tsx index c41d720195e6..e389a39d616b 100644 --- a/tests/unit/useVerticalBarBackButtonTest.tsx +++ b/tests/unit/useVerticalBarBackButtonTest.tsx @@ -231,7 +231,7 @@ describe('useVerticalBarBackButton', () => { // Given a header rendered in a modal that has a bar of its own, as the modal covers the bars of the screen under it const screen = createNavigation(); const removeBackButton = jest.fn(); - const addBackButton = jest.fn<() => void, [() => void]>(() => removeBackButton); + const addBackButton = jest.fn<() => void, [string, () => void]>(() => removeBackButton); const goBack = jest.fn(); const onRender = jest.fn(); @@ -245,7 +245,7 @@ describe('useVerticalBarBackButton', () => { onRender={onRender} />, ); - addBackButton.mock.calls.at(0)?.[0](); + addBackButton.mock.calls.at(0)?.[1](); unmount(); // Then the button went to the modal's bar and not to the screen under the modal, the header was told to leave @@ -257,6 +257,32 @@ describe('useVerticalBarBackButton', () => { expect(removeBackButton).toHaveBeenCalledTimes(1); }); + it('gives the modal the same header ID each time the header adds its button again, so the header keeps its place', () => { + // Given a header in a modal that has a bar of its own + const screen = createNavigation(); + const addBackButton = jest.fn<() => void, [string, () => void]>(() => jest.fn()); + const {rerender} = render( +
, + ); + + // When the header adds its button again, as it does when the screen under the modal changes + rerender( +
, + ); + + // Then both times the modal was given the same ID for the header + expect(addBackButton).toHaveBeenCalledTimes(2); + expect(addBackButton.mock.calls.at(1)?.[0]).toBe(addBackButton.mock.calls.at(0)?.[0]); + }); + it('adds no back button for a header inside a modal without a bar of its own, which keeps its own', () => { // Given a header rendered in a modal with no bar of its own, which covers the screen and the bars of the screen under it const screen = createNavigation(); From 865343ddf4e1e029104fb45398472f753abb44c0 Mon Sep 17 00:00:00 2001 From: GCyganek Date: Thu, 3 Dec 2026 16:10:36 +0100 Subject: [PATCH 3/6] Keep useHasVerticalBar subscribed to a boolean --- src/hooks/useHasVerticalBar/index.ios.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/hooks/useHasVerticalBar/index.ios.ts b/src/hooks/useHasVerticalBar/index.ios.ts index 481c0d296db6..2eb6062838cf 100644 --- a/src/hooks/useHasVerticalBar/index.ios.ts +++ b/src/hooks/useHasVerticalBar/index.ios.ts @@ -1,6 +1,9 @@ -import useVerticalBarEdge from '@hooks/useVerticalBarEdge'; +import {VERTICAL_BAR_EDGE, WindowBarsNitroModule} from '@expensify/nitro-utils'; +import {useSyncExternalStore} from 'react'; -import {VERTICAL_BAR_EDGE} from '@expensify/nitro-utils'; +const subscribe = (onChange: () => void) => WindowBarsNitroModule.addChangeListener(onChange); + +const getHasVerticalBar = () => WindowBarsNitroModule.verticalBarEdge !== VERTICAL_BAR_EDGE.UNSPECIFIED; /** * Whether the window has a vertical bar: the column a foldable iPhone gives the status bar, the tab bar and the bar items @@ -8,7 +11,7 @@ import {VERTICAL_BAR_EDGE} from '@expensify/nitro-utils'; * is the side safe area inset of that edge. */ function useHasVerticalBar(): boolean { - return useVerticalBarEdge() !== VERTICAL_BAR_EDGE.UNSPECIFIED; + return useSyncExternalStore(subscribe, getHasVerticalBar, getHasVerticalBar); } export default useHasVerticalBar; From c67aaab282b100fd0e74ec6c91bc2c94e5170597 Mon Sep 17 00:00:00 2001 From: GCyganek Date: Fri, 4 Dec 2026 11:16:15 +0100 Subject: [PATCH 4/6] Read the vertical bar only in modals whose header has a back button Every modal is wrapped in ModalVerticalBar, but most never show a back button and only a foldable iPhone has a vertical bar. The provider now only tracks headers; the edge, insets and native stack move to ModalVerticalBarStrip, which mounts once a header adds its button. --- .../ModalVerticalBarStrip.tsx | 78 +++++++++++++++++++ .../Modal/ModalVerticalBar/index.ios.tsx | 74 ++++-------------- tests/unit/ModalVerticalBarTest.tsx | 41 ++++++---- 3 files changed, 120 insertions(+), 73 deletions(-) create mode 100644 src/components/Modal/ModalVerticalBar/ModalVerticalBarStrip.tsx diff --git a/src/components/Modal/ModalVerticalBar/ModalVerticalBarStrip.tsx b/src/components/Modal/ModalVerticalBar/ModalVerticalBarStrip.tsx new file mode 100644 index 000000000000..7919bbab7da0 --- /dev/null +++ b/src/components/Modal/ModalVerticalBar/ModalVerticalBarStrip.tsx @@ -0,0 +1,78 @@ +/** + * The bar of a modal in the vertical bar of a foldable iPhone. UIKit lays out the items of a shown navigation bar in the + * vertical bar, so this is a native stack of one empty screen, whose bar has no title and no background and only shows the + * back button. + * + * The stack only covers the vertical bar, next to the content rather than around it. A screen mounted outside a React root + * handles touches itself, which would take them away from the modal's own handler, and its native views take touches + * wherever they lie, so the content would stop responding under it. + */ +import VerticalBarBackButton from '@components/VerticalBarBackButton'; + +import useSafeAreaInsets from '@hooks/useSafeAreaInsets'; +import useThemeStyles from '@hooks/useThemeStyles'; +import useVerticalBarEdge from '@hooks/useVerticalBarEdge'; + +import variables from '@styles/variables'; + +import {VERTICAL_BAR_EDGE} from '@expensify/nitro-utils'; +import React from 'react'; +import {ScreenStack, ScreenStackHeaderLeftView, ScreenStackItem} from 'react-native-screens'; + +type ModalVerticalBarStripProps = { + /** What a press on the back button does */ + onBackButtonPress: () => void; +}; + +function ModalVerticalBarStrip({onBackButtonPress}: ModalVerticalBarStripProps) { + const styles = useThemeStyles(); + const verticalBarEdge = useVerticalBarEdge(); + const insets = useSafeAreaInsets(); + + // The vertical bar takes the side safe area inset of its edge. The bar reaches a little past it, over the edge of the + // content, so UIKit lays the button out as on a screen. + const isOnLeftEdge = verticalBarEdge === VERTICAL_BAR_EDGE.LEADING; + const sideInset = isOnLeftEdge ? insets.left : insets.right; + // The header takes its button back once the window has no vertical bar, but it does so in an effect, after this render. + if (verticalBarEdge === VERTICAL_BAR_EDGE.UNSPECIFIED || sideInset <= 0) { + return null; + } + const width = Math.max(sideInset, variables.iosVerticalBarNavigationBarMinWidth); + const verticalBarStyle = isOnLeftEdge ? {left: 0, width} : {right: 0, width}; + // The native views of the stack take every touch inside it, so the part over the content would stop the content + // under it from responding. A negative hit slop leaves that part out of the stack's touch area. + const contentOverlap = width - sideInset; + const hitSlop = isOnLeftEdge ? {right: -contentOverlap} : {left: -contentOverlap}; + + return ( + + + + + ), + }} + > + {null} + + + ); +} + +export default ModalVerticalBarStrip; diff --git a/src/components/Modal/ModalVerticalBar/index.ios.tsx b/src/components/Modal/ModalVerticalBar/index.ios.tsx index c984c6aacf0c..3cc48f0eae4f 100644 --- a/src/components/Modal/ModalVerticalBar/index.ios.tsx +++ b/src/components/Modal/ModalVerticalBar/index.ios.tsx @@ -1,29 +1,21 @@ /** * Gives a modal a bar of its own in the vertical bar of a foldable iPhone. A modal covers the screen it is shown over and * that screen's bar items, so a back button in the modal's header can only move into the vertical bar through a bar that - * belongs to the modal. UIKit lays out the items of a shown navigation bar in the vertical bar, so the modal gets a native - * stack of one empty screen, whose bar has no title and no background and only shows the back button. + * belongs to the modal. * - * The stack only covers the vertical bar, next to the content rather than around it. A screen mounted outside a React root - * handles touches itself, which would take them away from the modal's own handler, and its native views take touches - * wherever they lie, so the content would stop responding under it. + * Every modal is wrapped, while most never have a header with a back button, and only a foldable iPhone has a vertical bar. + * So the modal only keeps track of the headers here, and reads the vertical bar and draws the bar once one of them adds + * its button, which a header only does when the window has a vertical bar. */ -import VerticalBarBackButton from '@components/VerticalBarBackButton'; - import useSafeAreaInsets from '@hooks/useSafeAreaInsets'; -import useThemeStyles from '@hooks/useThemeStyles'; -import useVerticalBarEdge from '@hooks/useVerticalBarEdge'; - -import variables from '@styles/variables'; -import {VERTICAL_BAR_EDGE} from '@expensify/nitro-utils'; import noop from 'lodash/noop'; import React, {useRef, useState} from 'react'; -import {ScreenStack, ScreenStackHeaderLeftView, ScreenStackItem} from 'react-native-screens'; import type ModalVerticalBarProps from './types'; import ModalVerticalBarContext from './ModalVerticalBarContext'; +import ModalVerticalBarStrip from './ModalVerticalBarStrip'; /** A header in the modal that has a back button. */ type BackButtonOwner = { @@ -35,15 +27,16 @@ type BackButtonOwner = { }; function ModalVerticalBar({children, isClosing = false}: ModalVerticalBarProps) { - const styles = useThemeStyles(); - const verticalBarEdge = useVerticalBarEdge(); const insets = useSafeAreaInsets(); const [backButtonOwners, setBackButtonOwners] = useState([]); // A header adds its button again whenever what its effect depends on changes. It must not move above the headers // that came after it then, or a press would go back from a page that is covered. - const headerOrders = useRef(new Map()); + const headerOrders = useRef | null>(null); const addBackButton = (headerID: string, onPress: () => void) => { + if (!headerOrders.current) { + headerOrders.current = new Map(); + } let order = headerOrders.current.get(headerID); if (order === undefined) { order = headerOrders.current.size; @@ -65,53 +58,16 @@ function ModalVerticalBar({children, isClosing = false}: ModalVerticalBarProps) if (shownOwner !== previousShownOwner) { setPreviousShownOwner(shownOwner); } - const onBackButtonPress = currentOwner?.onPress ?? noop; - // The vertical bar takes the side safe area inset of its edge. The bar reaches a little past it, over the edge of the - // content, so UIKit lays the button out as on a screen. - const isOnLeftEdge = verticalBarEdge === VERTICAL_BAR_EDGE.LEADING; - const sideInset = isOnLeftEdge ? insets.left : insets.right; - const width = Math.max(sideInset, variables.iosVerticalBarNavigationBarMinWidth); - const verticalBarStyle = isOnLeftEdge ? {left: 0, width} : {right: 0, width}; - // The native views of the stack take every touch inside it, so the part over the content would stop the content - // under it from responding. A negative hit slop leaves that part out of the stack's touch area. - const contentOverlap = width - sideInset; - const hitSlop = isOnLeftEdge ? {right: -contentOverlap} : {left: -contentOverlap}; // In Split View the window leaves the vertical bar out of its safe area, so the modal lays its content over the bar, - // and its headers keep their own button. - const hasBar = verticalBarEdge !== VERTICAL_BAR_EDGE.UNSPECIFIED && sideInset > 0; + // and its headers keep their own button. The side that meets the other app has no inset either, while the vertical + // bar's side always has one outside Split View, so no side inset at all means there is no room for the bar. The + // vertical bar itself is only read once a header adds a button, so a modal doesn't watch it for nothing. + const hasSideInset = insets.left > 0 || insets.right > 0; return ( - + {children} - {hasBar && !!shownOwner && ( - - - - - ), - }} - > - {null} - - - )} + {hasSideInset && !!shownOwner && } ); } diff --git a/tests/unit/ModalVerticalBarTest.tsx b/tests/unit/ModalVerticalBarTest.tsx index 2651e9e70d71..032446939efc 100644 --- a/tests/unit/ModalVerticalBarTest.tsx +++ b/tests/unit/ModalVerticalBarTest.tsx @@ -18,7 +18,9 @@ import {StyleSheet} from 'react-native'; let mockVerticalBarEdge: VerticalBarEdge = 'trailing'; let mockInsets: EdgeInsets = {top: 0, right: 84, bottom: 34, left: 0}; -jest.mock('@hooks/useVerticalBarEdge', () => () => mockVerticalBarEdge); +const mockUseVerticalBarEdge = jest.fn(() => mockVerticalBarEdge); + +jest.mock('@hooks/useVerticalBarEdge', () => () => mockUseVerticalBarEdge()); jest.mock('@hooks/useSafeAreaInsets', () => () => mockInsets); jest.mock('@components/VerticalBarBackButton', () => { const {Pressable: MockPressable} = jest.requireActual('react-native'); @@ -193,24 +195,35 @@ describe('ModalVerticalBar', () => { expect(screen.queryByTestId('modalVerticalBarStack')).toBeNull(); }); - it('gives headers no bar without a vertical bar, so they keep their own back button', () => { - // Given a window without a vertical bar - mockVerticalBarEdge = 'unspecified'; - const onRead = jest.fn]>(); - function Reader() { - onRead(useContext(ModalVerticalBarContext)); - return null; - } + it('does not read the vertical bar while no header in the modal has a back button', () => { + // Given a modal whose content has no header with a back button, as most modals, and every modal off a foldable iPhone + mockUseVerticalBarEdge.mockClear(); - // When a header in the modal reads what the modal gives it - render( + // When the modal renders + render({null}); + + // Then it neither watches the vertical bar nor draws a bar, so it costs nothing there + expect(mockUseVerticalBarEdge).not.toHaveBeenCalled(); + expect(screen.queryByTestId('modalVerticalBarStack')).toBeNull(); + }); + + it('draws no bar once the window has no vertical bar, before the header takes its button back', () => { + // Given a modal with a header that has a back button + const {rerender} = render( - +
, ); - // Then the modal gives it no bar, and draws none - expect(onRead).toHaveBeenLastCalledWith(undefined); + // When the window loses its vertical bar, as when the phone unfolds in portrait, and the modal renders again + mockVerticalBarEdge = 'unspecified'; + rerender( + +
+ , + ); + + // Then the modal draws no bar over its content expect(screen.queryByTestId('modalVerticalBarStack')).toBeNull(); }); From 3068f1929f09ceeeb97a36224ac42ea828dc4c98 Mon Sep 17 00:00:00 2001 From: GCyganek Date: Fri, 4 Dec 2026 11:28:32 +0100 Subject: [PATCH 5/6] Shorter comments, check useHasVerticalBar in ModalVerticalBar --- .../ModalVerticalBarContext.ts | 7 ++-- .../ModalVerticalBarStrip.tsx | 24 +++++-------- .../Modal/ModalVerticalBar/index.ios.tsx | 34 +++++++------------ .../Modal/ModalVerticalBar/types.ts | 4 +-- .../useVerticalBarBackButton/index.ios.ts | 2 +- src/hooks/useVerticalBarEdge/index.ios.ts | 6 +--- src/hooks/useVerticalBarEdge/index.ts | 5 +-- src/styles/index.ts | 2 +- src/styles/variables.ts | 5 +-- tests/unit/ModalVerticalBarTest.tsx | 27 +++++++++++++-- 10 files changed, 56 insertions(+), 60 deletions(-) diff --git a/src/components/Modal/ModalVerticalBar/ModalVerticalBarContext.ts b/src/components/Modal/ModalVerticalBar/ModalVerticalBarContext.ts index 1b553b5da09c..6766f7a75a3f 100644 --- a/src/components/Modal/ModalVerticalBar/ModalVerticalBarContext.ts +++ b/src/components/Modal/ModalVerticalBar/ModalVerticalBarContext.ts @@ -1,14 +1,11 @@ import {createContext} from 'react'; type ModalVerticalBarContextType = { - /** - * Shows a back button in the vertical bar while the modal is shown, for a header in the modal. The header passes the - * same ID each time, so it keeps its place under the headers that came after it. Returns the cleanup. - */ + /** Shows a header's back button in the vertical bar. Same ID on each call keeps the header's place. Returns cleanup. */ addBackButton: (headerID: string, onPress: () => void) => () => void; }; -/** Given by a modal that has a bar of its own in the vertical bar of a foldable iPhone, and undefined elsewhere. */ +/** Set by a modal with its own vertical bar strip; undefined elsewhere. */ const ModalVerticalBarContext = createContext(undefined); export default ModalVerticalBarContext; diff --git a/src/components/Modal/ModalVerticalBar/ModalVerticalBarStrip.tsx b/src/components/Modal/ModalVerticalBar/ModalVerticalBarStrip.tsx index 7919bbab7da0..33143be2df05 100644 --- a/src/components/Modal/ModalVerticalBar/ModalVerticalBarStrip.tsx +++ b/src/components/Modal/ModalVerticalBar/ModalVerticalBarStrip.tsx @@ -1,11 +1,7 @@ /** - * The bar of a modal in the vertical bar of a foldable iPhone. UIKit lays out the items of a shown navigation bar in the - * vertical bar, so this is a native stack of one empty screen, whose bar has no title and no background and only shows the - * back button. - * - * The stack only covers the vertical bar, next to the content rather than around it. A screen mounted outside a React root - * handles touches itself, which would take them away from the modal's own handler, and its native views take touches - * wherever they lie, so the content would stop responding under it. + * A modal's bar in the vertical bar: a native stack of one empty screen whose navigation bar shows only the back button, + * since UIKit puts a shown navigation bar's items in the vertical bar. It covers only the vertical bar, not the content, + * because its native views would take the content's touches. */ import VerticalBarBackButton from '@components/VerticalBarBackButton'; @@ -29,18 +25,16 @@ function ModalVerticalBarStrip({onBackButtonPress}: ModalVerticalBarStripProps) const verticalBarEdge = useVerticalBarEdge(); const insets = useSafeAreaInsets(); - // The vertical bar takes the side safe area inset of its edge. The bar reaches a little past it, over the edge of the - // content, so UIKit lays the button out as on a screen. + // The bar is a bit wider than the side inset, so UIKit sizes the button as on a screen. const isOnLeftEdge = verticalBarEdge === VERTICAL_BAR_EDGE.LEADING; const sideInset = isOnLeftEdge ? insets.left : insets.right; - // The header takes its button back once the window has no vertical bar, but it does so in an effect, after this render. + // Headers take their button back in an effect, after this render. if (verticalBarEdge === VERTICAL_BAR_EDGE.UNSPECIFIED || sideInset <= 0) { return null; } const width = Math.max(sideInset, variables.iosVerticalBarNavigationBarMinWidth); const verticalBarStyle = isOnLeftEdge ? {left: 0, width} : {right: 0, width}; - // The native views of the stack take every touch inside it, so the part over the content would stop the content - // under it from responding. A negative hit slop leaves that part out of the stack's touch area. + // Negative hit slop keeps the part over the content from taking its touches. const contentOverlap = width - sideInset; const hitSlop = isOnLeftEdge ? {right: -contentOverlap} : {left: -contentOverlap}; @@ -51,18 +45,18 @@ function ModalVerticalBarStrip({onBackButtonPress}: ModalVerticalBarStripProps) > diff --git a/src/components/Modal/ModalVerticalBar/index.ios.tsx b/src/components/Modal/ModalVerticalBar/index.ios.tsx index 3cc48f0eae4f..8c0a6d2b0ee0 100644 --- a/src/components/Modal/ModalVerticalBar/index.ios.tsx +++ b/src/components/Modal/ModalVerticalBar/index.ios.tsx @@ -1,12 +1,8 @@ /** - * Gives a modal a bar of its own in the vertical bar of a foldable iPhone. A modal covers the screen it is shown over and - * that screen's bar items, so a back button in the modal's header can only move into the vertical bar through a bar that - * belongs to the modal. - * - * Every modal is wrapped, while most never have a header with a back button, and only a foldable iPhone has a vertical bar. - * So the modal only keeps track of the headers here, and reads the vertical bar and draws the bar once one of them adds - * its button, which a header only does when the window has a vertical bar. + * Gives a modal its own bar in the vertical bar of a foldable iPhone, since a modal covers the screen's bar items. + * Offered only while the window has a vertical bar; drawn only once a header adds its back button. */ +import useHasVerticalBar from '@hooks/useHasVerticalBar'; import useSafeAreaInsets from '@hooks/useSafeAreaInsets'; import noop from 'lodash/noop'; @@ -19,7 +15,7 @@ import ModalVerticalBarStrip from './ModalVerticalBarStrip'; /** A header in the modal that has a back button. */ type BackButtonOwner = { - /** The order the header first added its button in. A header that adds it again keeps this place. */ + /** Order of the header's first add, kept on re-adds */ order: number; /** What a press on the button does */ @@ -27,10 +23,10 @@ type BackButtonOwner = { }; function ModalVerticalBar({children, isClosing = false}: ModalVerticalBarProps) { + const hasVerticalBar = useHasVerticalBar(); const insets = useSafeAreaInsets(); const [backButtonOwners, setBackButtonOwners] = useState([]); - // A header adds its button again whenever what its effect depends on changes. It must not move above the headers - // that came after it then, or a press would go back from a page that is covered. + // Keeps a re-adding header under later ones, so a press never goes back from a covered page. const headerOrders = useRef | null>(null); const addBackButton = (headerID: string, onPress: () => void) => { @@ -48,26 +44,22 @@ function ModalVerticalBar({children, isClosing = false}: ModalVerticalBarProps) }; const contextValue = {addBackButton}; - // The header that came last is the one on top, e.g. a page of a modal that shows one page after another. + // Last header is the one on top. const currentOwner = backButtonOwners.at(-1); - // A closing modal drops its content, and with it the header, at once, but stays over the screen and its bar until the - // transition ends. The button stays until then, so the vertical bar is never left without one, and a press does - // nothing, as the header already went back. A modal whose headers had no button left before it closed keeps none. + // A closing modal drops its header at once but covers the screen until the transition ends, so the button stays until + // then, doing nothing on press. const [previousShownOwner, setPreviousShownOwner] = useState(currentOwner); const shownOwner = currentOwner ?? (isClosing ? previousShownOwner : undefined); if (shownOwner !== previousShownOwner) { setPreviousShownOwner(shownOwner); } - // In Split View the window leaves the vertical bar out of its safe area, so the modal lays its content over the bar, - // and its headers keep their own button. The side that meets the other app has no inset either, while the vertical - // bar's side always has one outside Split View, so no side inset at all means there is no room for the bar. The - // vertical bar itself is only read once a header adds a button, so a modal doesn't watch it for nothing. - const hasSideInset = insets.left > 0 || insets.right > 0; + // No side inset means Split View, where content lies over the vertical bar and headers keep their own button. + const hasRoomForBar = hasVerticalBar && (insets.left > 0 || insets.right > 0); return ( - + {children} - {hasSideInset && !!shownOwner && } + {hasRoomForBar && !!shownOwner && } ); } diff --git a/src/components/Modal/ModalVerticalBar/types.ts b/src/components/Modal/ModalVerticalBar/types.ts index 5b75c5eed66a..2abe20ec9ea4 100644 --- a/src/components/Modal/ModalVerticalBar/types.ts +++ b/src/components/Modal/ModalVerticalBar/types.ts @@ -1,10 +1,10 @@ import type {ReactNode} from 'react'; type ModalVerticalBarProps = { - /** The modal's backdrop and content, which the bar is laid over along the edge of the vertical bar */ + /** The modal's backdrop and content */ children: ReactNode; - /** Whether the modal is on its way out: its content is gone, but the modal still covers the screen */ + /** Whether the modal is closing: content gone, modal still covers the screen */ isClosing?: boolean; }; diff --git a/src/hooks/useVerticalBarBackButton/index.ios.ts b/src/hooks/useVerticalBarBackButton/index.ios.ts index e727db57c8df..31d729ec1e8d 100644 --- a/src/hooks/useVerticalBarBackButton/index.ios.ts +++ b/src/hooks/useVerticalBarBackButton/index.ios.ts @@ -29,7 +29,7 @@ const useVerticalBarBackButton: UseVerticalBarBackButton = (goBack, shouldShowBa // The handler gets a new identity on most renders. Read through an effect event, a press runs the latest one // without the button being handed over again each time. const pressBackButton = useEffectEvent(goBack); - // Lets a modal keep this header under the headers that came after it each time the header adds its button again. + // Keeps this header's place in a modal across re-adds. const headerID = useId(); // Not useLayoutEffect: a covered screen is frozen and loses its layout effects, so the button would blink on return. diff --git a/src/hooks/useVerticalBarEdge/index.ios.ts b/src/hooks/useVerticalBarEdge/index.ios.ts index af37b4a4fc84..34e5b7e087f0 100644 --- a/src/hooks/useVerticalBarEdge/index.ios.ts +++ b/src/hooks/useVerticalBarEdge/index.ios.ts @@ -7,11 +7,7 @@ const subscribe = (onChange: () => void) => WindowBarsNitroModule.addChangeListe const getVerticalBarEdge = () => WindowBarsNitroModule.verticalBarEdge; -/** - * The side edge of the vertical bar: the column a foldable iPhone gives the status bar, the tab bar and the bar items, - * folded or open in landscape. It is unspecified whenever the window's trait collection names no edge for it. The app is - * always laid out left to right, so the leading edge is the left one. - */ +/** Edge of a foldable iPhone's vertical bar, or unspecified when there is none. The app is LTR, so leading = left. */ function useVerticalBarEdge(): VerticalBarEdge { return useSyncExternalStore(subscribe, getVerticalBarEdge, getVerticalBarEdge); } diff --git a/src/hooks/useVerticalBarEdge/index.ts b/src/hooks/useVerticalBarEdge/index.ts index 801c5d519b7d..40f52c87fa7c 100644 --- a/src/hooks/useVerticalBarEdge/index.ts +++ b/src/hooks/useVerticalBarEdge/index.ts @@ -1,9 +1,6 @@ import type {VerticalBarEdge} from '@expensify/nitro-utils'; -/** - * Only a foldable iPhone has a vertical bar, so elsewhere it never has an edge. The value is written out because importing - * VERTICAL_BAR_EDGE would load the native modules of the package, which this platform doesn't have. - */ +/** No vertical bar off iOS. Literal value: importing VERTICAL_BAR_EDGE would load native modules missing here. */ function useVerticalBarEdge(): VerticalBarEdge { return 'unspecified'; } diff --git a/src/styles/index.ts b/src/styles/index.ts index 17c1c7a28c66..71062e354c65 100644 --- a/src/styles/index.ts +++ b/src/styles/index.ts @@ -2178,7 +2178,7 @@ const staticStyles = (theme: ThemeColors) => height: variables.iosVerticalBarItemSize - 2 * variables.iosVerticalBarItemViewPadding, }, - // The bar of a modal in the vertical bar: the full height of the window along the edge the vertical bar is on, over the modal's content. + // A modal's bar: full height along the vertical bar's edge, over the content. modalVerticalBar: { position: 'absolute', top: 0, diff --git a/src/styles/variables.ts b/src/styles/variables.ts index efe22954cc23..21d0c20ebd3b 100644 --- a/src/styles/variables.ts +++ b/src/styles/variables.ts @@ -174,10 +174,7 @@ export default { iosVerticalBarItemViewPadding: 5, /** The size UIKit draws its own items at in the vertical bar. The floating buttons take it there so they match the items of other apps. */ iosVerticalBarItemSize: 48, - /** - * The narrowest navigation bar that keeps an item of the vertical bar at its size. UIKit lays the items of a narrower - * bar out narrower and off center, and the vertical bar of the iPhone Duo is 84 wide. - */ + /** Narrowest navigation bar that keeps vertical bar items full size and centered (Duo's bar is 84 wide). */ iosVerticalBarNavigationBarMinWidth: 92, iconHeader: 48, iconSection: 68, diff --git a/tests/unit/ModalVerticalBarTest.tsx b/tests/unit/ModalVerticalBarTest.tsx index 032446939efc..0f9d00831b6a 100644 --- a/tests/unit/ModalVerticalBarTest.tsx +++ b/tests/unit/ModalVerticalBarTest.tsx @@ -21,6 +21,7 @@ let mockInsets: EdgeInsets = {top: 0, right: 84, bottom: 34, left: 0}; const mockUseVerticalBarEdge = jest.fn(() => mockVerticalBarEdge); jest.mock('@hooks/useVerticalBarEdge', () => () => mockUseVerticalBarEdge()); +jest.mock('@hooks/useHasVerticalBar', () => () => mockVerticalBarEdge !== 'unspecified'); jest.mock('@hooks/useSafeAreaInsets', () => () => mockInsets); jest.mock('@components/VerticalBarBackButton', () => { const {Pressable: MockPressable} = jest.requireActual('react-native'); @@ -195,14 +196,14 @@ describe('ModalVerticalBar', () => { expect(screen.queryByTestId('modalVerticalBarStack')).toBeNull(); }); - it('does not read the vertical bar while no header in the modal has a back button', () => { + it("does not read the vertical bar's edge while no header in the modal has a back button", () => { // Given a modal whose content has no header with a back button, as most modals, and every modal off a foldable iPhone mockUseVerticalBarEdge.mockClear(); // When the modal renders render({null}); - // Then it neither watches the vertical bar nor draws a bar, so it costs nothing there + // Then it neither watches the edge of the vertical bar nor draws a bar expect(mockUseVerticalBarEdge).not.toHaveBeenCalled(); expect(screen.queryByTestId('modalVerticalBarStack')).toBeNull(); }); @@ -227,6 +228,28 @@ describe('ModalVerticalBar', () => { expect(screen.queryByTestId('modalVerticalBarStack')).toBeNull(); }); + it('gives headers no bar on a device without a vertical bar, even when a rotation gives the window side insets', () => { + // Given an iPhone that doesn't fold, in landscape, whose notch gives the window side insets + mockVerticalBarEdge = 'unspecified'; + mockInsets = {top: 0, right: 59, bottom: 21, left: 59}; + const onRead = jest.fn]>(); + function Reader() { + onRead(useContext(ModalVerticalBarContext)); + return null; + } + + // When a header in the modal reads what the modal gives it + render( + + + , + ); + + // Then the modal gives it no bar, so the header keeps its own button and isn't rendered again by the rotation + expect(onRead).toHaveBeenLastCalledWith(undefined); + expect(screen.queryByTestId('modalVerticalBarStack')).toBeNull(); + }); + it('gives headers no bar in Split View, where the modal lays its content over the vertical bar', () => { // Given the left half of Split View, whose window has a vertical bar but leaves it out of its safe area mockInsets = {top: 0, right: 0, bottom: 34, left: 0}; From bae8774b2fc054f847a32c71e6af5aca00fddddb Mon Sep 17 00:00:00 2001 From: GCyganek Date: Fri, 4 Dec 2026 12:59:23 +0100 Subject: [PATCH 6/6] Leave room for the vertical bar in full-height modals in Split View --- .../ModalVerticalBarStrip.tsx | 6 +- src/components/Modal/index.ios.tsx | 12 +++- .../ScreenSafeAreaInsets/index.ios.tsx | 40 ++++++++----- src/components/ScreenSafeAreaInsets/index.tsx | 4 +- src/components/ScreenSafeAreaInsets/types.ts | 8 +++ tests/unit/ScreenSafeAreaInsetsTest.tsx | 56 +++++++++++++++++++ 6 files changed, 108 insertions(+), 18 deletions(-) create mode 100644 src/components/ScreenSafeAreaInsets/types.ts create mode 100644 tests/unit/ScreenSafeAreaInsetsTest.tsx diff --git a/src/components/Modal/ModalVerticalBar/ModalVerticalBarStrip.tsx b/src/components/Modal/ModalVerticalBar/ModalVerticalBarStrip.tsx index 33143be2df05..344379ca5b7d 100644 --- a/src/components/Modal/ModalVerticalBar/ModalVerticalBarStrip.tsx +++ b/src/components/Modal/ModalVerticalBar/ModalVerticalBarStrip.tsx @@ -16,7 +16,7 @@ import React from 'react'; import {ScreenStack, ScreenStackHeaderLeftView, ScreenStackItem} from 'react-native-screens'; type ModalVerticalBarStripProps = { - /** What a press on the back button does */ + /** Goes back in the header shown on top; does nothing while the modal closes */ onBackButtonPress: () => void; }; @@ -25,13 +25,13 @@ function ModalVerticalBarStrip({onBackButtonPress}: ModalVerticalBarStripProps) const verticalBarEdge = useVerticalBarEdge(); const insets = useSafeAreaInsets(); - // The bar is a bit wider than the side inset, so UIKit sizes the button as on a screen. const isOnLeftEdge = verticalBarEdge === VERTICAL_BAR_EDGE.LEADING; const sideInset = isOnLeftEdge ? insets.left : insets.right; - // Headers take their button back in an effect, after this render. + // Without a vertical bar the modal stops offering its bar, but headers only show their own button again in an effect. if (verticalBarEdge === VERTICAL_BAR_EDGE.UNSPECIFIED || sideInset <= 0) { return null; } + // The bar is a bit wider than the side inset, so UIKit sizes the button as on a screen. const width = Math.max(sideInset, variables.iosVerticalBarNavigationBarMinWidth); const verticalBarStyle = isOnLeftEdge ? {left: 0, width} : {right: 0, width}; // Negative hit slop keeps the part over the content from taking its touches. diff --git a/src/components/Modal/index.ios.tsx b/src/components/Modal/index.ios.tsx index 063ab7c5cc37..b954bfe62c39 100644 --- a/src/components/Modal/index.ios.tsx +++ b/src/components/Modal/index.ios.tsx @@ -3,6 +3,7 @@ import {WindowSafeAreaInsets} from '@components/ScreenSafeAreaInsets'; import useResponsiveLayout from '@hooks/useResponsiveLayout'; import CONST from '@src/CONST'; +import type ModalType from '@src/types/utils/ModalType'; import React from 'react'; @@ -10,13 +11,22 @@ import type BaseModalProps from './types'; import BaseModal from './BaseModal'; +/** Full-height modals, which leave room for their back button in the vertical bar, like screens. Sheets, dialogs, popovers and full-screen modals cover it. */ +const MODAL_TYPES_BESIDE_VERTICAL_BAR = new Set([ + CONST.MODAL.MODAL_TYPE.RIGHT_DOCKED, + CONST.MODAL.MODAL_TYPE.CENTERED, + CONST.MODAL.MODAL_TYPE.CENTERED_SMALL, + CONST.MODAL.MODAL_TYPE.CENTERED_UNSWIPEABLE, + CONST.MODAL.MODAL_TYPE.CENTERED_SWIPEABLE_TO_RIGHT, +]); + function Modal({children, ...rest}: BaseModalProps) { const {isInNarrowPaneModal} = useResponsiveLayout(); const animationInTiming = rest.animationInTiming ?? (isInNarrowPaneModal ? CONST.MODAL.ANIMATION_TIMING.DEFAULT_RIGHT_DOCKED_IOS_IN : undefined); const animationOutTiming = rest.animationOutTiming ?? (isInNarrowPaneModal ? CONST.MODAL.ANIMATION_TIMING.DEFAULT_RIGHT_DOCKED_IOS_OUT : undefined); return ( - + (null); +const ScreenAndWindowInsetsContext = createContext(null); function isSameInsets(first: EdgeInsets | null, second: EdgeInsets) { return first?.top === second.top && first.right === second.right && first.bottom === second.bottom && first.left === second.left; } function ScreenSafeAreaInsets({children}: ChildrenProps) { - const windowInsetsOfOuterScreen = useContext(WindowSafeAreaInsetsContext); + const insetsOfOuterScreen = useContext(ScreenAndWindowInsetsContext); const windowInsets = useContext(SafeAreaInsetsContext); // Starts from the window's insets, so the first layout doesn't wait for the native view to report its own. const [screenInsets, setScreenInsets] = useState(windowInsets); - if (windowInsetsOfOuterScreen) { + if (insetsOfOuterScreen) { return children; } + const screenAndWindowInsets = windowInsets && screenInsets ? {window: windowInsets, screen: screenInsets} : null; return ( setScreenInsets((previousInsets) => (isSameInsets(previousInsets, insets) ? previousInsets : insets))}> - + {children} - + ); } -/** Gives a modal opened from a screen the window's insets again, as it covers the vertical bar the screen keeps clear of. */ -function WindowSafeAreaInsets({children}: ChildrenProps) { - const windowInsets = useContext(WindowSafeAreaInsetsContext); +/** Gives a modal opened from a screen the window's insets again, as the screen's include e.g. the vertical bar in Split View. */ +function WindowSafeAreaInsets({children, shouldKeepScreenSideInsets = false}: WindowSafeAreaInsetsProps) { + // Read from the screen, not the library's context, which a modal this one is opened from has set to the window's. + const insetsOfScreen = useContext(ScreenAndWindowInsetsContext); - if (!windowInsets) { + if (!insetsOfScreen) { return children; } - return {children}; + const {window: windowInsets, screen: screenInsets} = insetsOfScreen; + const modalInsets = shouldKeepScreenSideInsets ? {...windowInsets, left: screenInsets.left, right: screenInsets.right} : windowInsets; + return {children}; } export default ScreenSafeAreaInsets; diff --git a/src/components/ScreenSafeAreaInsets/index.tsx b/src/components/ScreenSafeAreaInsets/index.tsx index c7069d4cac03..708b00d6ed2e 100644 --- a/src/components/ScreenSafeAreaInsets/index.tsx +++ b/src/components/ScreenSafeAreaInsets/index.tsx @@ -1,12 +1,14 @@ import type ChildrenProps from '@src/types/utils/ChildrenProps'; +import type WindowSafeAreaInsetsProps from './types'; + /** Only a foldable iPhone gives its screens a safe area the window doesn't have (its vertical bar), so elsewhere the window's insets are the screen's. */ function ScreenSafeAreaInsets({children}: ChildrenProps) { return children; } /** Elsewhere the screen's insets are already the window's, so a modal keeps them. */ -function WindowSafeAreaInsets({children}: ChildrenProps) { +function WindowSafeAreaInsets({children}: WindowSafeAreaInsetsProps) { return children; } diff --git a/src/components/ScreenSafeAreaInsets/types.ts b/src/components/ScreenSafeAreaInsets/types.ts new file mode 100644 index 000000000000..6039584042ab --- /dev/null +++ b/src/components/ScreenSafeAreaInsets/types.ts @@ -0,0 +1,8 @@ +import type ChildrenProps from '@src/types/utils/ChildrenProps'; + +type WindowSafeAreaInsetsProps = ChildrenProps & { + /** Keeps the screen's side insets, so the modal leaves room for its bar in the vertical bar, also in Split View */ + shouldKeepScreenSideInsets?: boolean; +}; + +export default WindowSafeAreaInsetsProps; diff --git a/tests/unit/ScreenSafeAreaInsetsTest.tsx b/tests/unit/ScreenSafeAreaInsetsTest.tsx new file mode 100644 index 000000000000..fa7ae9c9fad9 --- /dev/null +++ b/tests/unit/ScreenSafeAreaInsetsTest.tsx @@ -0,0 +1,56 @@ +import {render} from '@testing-library/react-native'; + +import ScreenSafeAreaInsets, {WindowSafeAreaInsets} from '@components/ScreenSafeAreaInsets/index.ios'; + +import type {EdgeInsets, SafeAreaListenerProps} from 'react-native-safe-area-context'; + +import React, {useContext} from 'react'; +// eslint-disable-next-line no-restricted-imports -- The test reads the insets the components put in the library's own context. +import {SafeAreaInsetsContext} from 'react-native-safe-area-context'; + +// The app in the left half of Split View on a foldable iPhone: the window has no left inset, while UIKit gives the screen +// the width of the vertical bar. +const WINDOW_INSETS: EdgeInsets = {top: 0, right: 0, bottom: 34, left: 0}; +const mockScreenInsets: EdgeInsets = {top: 0, right: 0, bottom: 34, left: 84}; + +// The listener reports the insets of its native view, i.e. the screen's, once it lays out. +jest.mock('react-native-safe-area-context', () => { + const {createContext, useEffect} = jest.requireActual('react'); + return { + SafeAreaInsetsContext: createContext(null), + SafeAreaListener: ({onChange, children}: SafeAreaListenerProps) => { + useEffect(() => onChange({insets: mockScreenInsets, frame: {x: 0, y: 0, width: 469, height: 669}}), [onChange]); + return children; + }, + }; +}); + +describe('ScreenSafeAreaInsets', () => { + it('gives a full-height modal opened from a sheet the side insets of the screen, not the ones the sheet set', () => { + // Given a screen in Split View with a sheet opened from it, which covers the vertical bar, like the date picker + const onReadInSheet = jest.fn(); + const onReadInNestedModal = jest.fn(); + function Reader({onRead}: {onRead: (insets: EdgeInsets | null) => void}) { + onRead(useContext(SafeAreaInsetsContext)); + return null; + } + + // When a full-height modal opens from the sheet, like the date picker's month list + render( + + + + + + + + + + , + ); + + // Then the sheet keeps the window's insets, while the nested modal leaves room for its bar like the screen does + expect(onReadInSheet).toHaveBeenLastCalledWith(WINDOW_INSETS); + expect(onReadInNestedModal).toHaveBeenLastCalledWith({...WINDOW_INSETS, left: 84}); + }); +});