Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {createContext} from 'react';

type ModalVerticalBarContextType = {
/** 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;
};

/** Set by a modal with its own vertical bar strip; undefined elsewhere. */
const ModalVerticalBarContext = createContext<ModalVerticalBarContextType | undefined>(undefined);

export default ModalVerticalBarContext;
72 changes: 72 additions & 0 deletions src/components/Modal/ModalVerticalBar/ModalVerticalBarStrip.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* 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';

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 = {
/** Goes back in the header shown on top; does nothing while the modal closes */
onBackButtonPress: () => void;
};

function ModalVerticalBarStrip({onBackButtonPress}: ModalVerticalBarStripProps) {
const styles = useThemeStyles();
const verticalBarEdge = useVerticalBarEdge();
const insets = useSafeAreaInsets();

const isOnLeftEdge = verticalBarEdge === VERTICAL_BAR_EDGE.LEADING;
const sideInset = isOnLeftEdge ? insets.left : insets.right;
// 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.
const contentOverlap = width - sideInset;
const hitSlop = isOnLeftEdge ? {right: -contentOverlap} : {left: -contentOverlap};

return (
<ScreenStack
style={[styles.modalVerticalBar, verticalBarStyle]}
hitSlop={hitSlop}
>
<ScreenStackItem
screenId="modalVerticalBar"
// On top: UIKit only shows a visible navigation bar's items.
activityState={2}
style={styles.flex1}
headerConfig={{
// Untitled and transparent, so it takes no room along the top.
translucent: true,
title: '',
hideBackButton: true,
hideShadow: true,
backgroundColor: 'transparent',
children: (
// The button has its own glass.
<ScreenStackHeaderLeftView hidesSharedBackground>
<VerticalBarBackButton onPress={onBackButtonPress} />
</ScreenStackHeaderLeftView>
),
}}
>
{null}
</ScreenStackItem>
</ScreenStack>
);
}

export default ModalVerticalBarStrip;
67 changes: 67 additions & 0 deletions src/components/Modal/ModalVerticalBar/index.ios.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* 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';
import React, {useRef, useState} from 'react';

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 = {
/** Order of the header's first add, kept on re-adds */
order: number;

/** What a press on the button does */
onPress: () => void;
};

function ModalVerticalBar({children, isClosing = false}: ModalVerticalBarProps) {
const hasVerticalBar = useHasVerticalBar();
const insets = useSafeAreaInsets();
const [backButtonOwners, setBackButtonOwners] = useState<BackButtonOwner[]>([]);
// Keeps a re-adding header under later ones, so a press never goes back from a covered page.
const headerOrders = useRef<Map<string, number> | 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;
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};

// Last header is the one on top.
const currentOwner = backButtonOwners.at(-1);
// 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<BackButtonOwner | undefined>(currentOwner);
const shownOwner = currentOwner ?? (isClosing ? previousShownOwner : undefined);
if (shownOwner !== previousShownOwner) {
setPreviousShownOwner(shownOwner);
}
// 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 (
<ModalVerticalBarContext.Provider value={hasRoomForBar ? contextValue : undefined}>
{children}
{hasRoomForBar && !!shownOwner && <ModalVerticalBarStrip onBackButtonPress={currentOwner?.onPress ?? noop} />}
</ModalVerticalBarContext.Provider>
);
}

export default ModalVerticalBar;
8 changes: 8 additions & 0 deletions src/components/Modal/ModalVerticalBar/index.tsx
Original file line number Diff line number Diff line change
@@ -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;
11 changes: 11 additions & 0 deletions src/components/Modal/ModalVerticalBar/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type {ReactNode} from 'react';

type ModalVerticalBarProps = {
/** The modal's backdrop and content */
children: ReactNode;

/** Whether the modal is closing: content gone, modal still covers the screen */
isClosing?: boolean;
};

export default ModalVerticalBarProps;
45 changes: 24 additions & 21 deletions src/components/Modal/ReanimatedModal/index.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -242,27 +243,29 @@ function ReanimatedModal({
style={modalStyle}
{...props}
>
{isBackdropMounted && hasBackdrop && backdropView}
{avoidKeyboard ? (
<KeyboardAvoidingView
behavior="padding"
pointerEvents="box-none"
style={[style, {margin: 0}]}
>
{isVisibleState && containerView}
</KeyboardAvoidingView>
) : (
<FocusTrapForModal
active={modalVisibility}
initialFocus={initialFocus}
shouldReturnFocus={shouldReturnFocus ?? !shouldEnableNewFocusManagement}
shouldPreventScroll={shouldPreventScrollOnFocus}
shouldDeactivateOnOutsideInteraction={shouldDeactivateFocusTrapOnOutsideInteraction}
launcherRef={launcherRef}
>
{isVisibleState && containerView}
</FocusTrapForModal>
)}
<ModalVerticalBar isClosing={!isVisibleState}>
{isBackdropMounted && hasBackdrop && backdropView}
{avoidKeyboard ? (
<KeyboardAvoidingView
behavior="padding"
pointerEvents="box-none"
style={[style, styles.m0]}
>
{isVisibleState && containerView}
</KeyboardAvoidingView>
) : (
<FocusTrapForModal
active={modalVisibility}
initialFocus={initialFocus}
shouldReturnFocus={shouldReturnFocus ?? !shouldEnableNewFocusManagement}
shouldPreventScroll={shouldPreventScrollOnFocus}
shouldDeactivateOnOutsideInteraction={shouldDeactivateFocusTrapOnOutsideInteraction}
launcherRef={launcherRef}
>
{isVisibleState && containerView}
</FocusTrapForModal>
)}
</ModalVerticalBar>
</Modal>
</LayoutAnimationConfig>
);
Expand Down
12 changes: 11 additions & 1 deletion src/components/Modal/index.ios.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,30 @@ 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';

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<ModalType | undefined>([
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 (
<WindowSafeAreaInsets>
<WindowSafeAreaInsets shouldKeepScreenSideInsets={MODAL_TYPES_BESIDE_VERTICAL_BAR.has(rest.type)}>
<BaseModal
{...rest}
animationInTiming={animationInTiming}
Expand Down
40 changes: 27 additions & 13 deletions src/components/ScreenSafeAreaInsets/index.ios.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Gives a screen the safe area insets UIKit gives its view, rather than the window's. On a foldable iPhone the vertical
* bar only reaches the safe area of the screens: with the app in the left half of a split screen, the bar sits on the
* left edge, where the window has no inset. Modals cover the bar, so they go back to the window's insets.
* left edge, where the window has no inset. Full-height modals keep the screen's side insets, so their own bar fits too.
*/
import type ChildrenProps from '@src/types/utils/ChildrenProps';

Expand All @@ -11,44 +11,58 @@ import React, {createContext, useContext, useState} from 'react';
// eslint-disable-next-line no-restricted-imports -- The screen's insets replace the window's for everything under it, so they go in the library's own context.
import {SafeAreaInsetsContext, SafeAreaListener} from 'react-native-safe-area-context';

import type WindowSafeAreaInsetsProps from './types';

type ScreenAndWindowInsets = {
/** The window's insets */
window: EdgeInsets;

/** The screen's insets, which a modal inside another modal can no longer read from the library's context */
screen: EdgeInsets;
};

/**
* The window's insets, kept for the modals of a screen. Every screen of a stack inside a screen shares the outer screen's
* insets, so only the outermost one, where this is still null, measures them.
* The window's and the screen's insets, kept for the modals of a screen. Every screen of a stack inside a screen shares
* the outer screen's insets, so only the outermost one, where this is still null, measures them.
*/
const WindowSafeAreaInsetsContext = createContext<EdgeInsets | null>(null);
const ScreenAndWindowInsetsContext = createContext<ScreenAndWindowInsets | null>(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 (
<SafeAreaListener onChange={({insets}) => setScreenInsets((previousInsets) => (isSameInsets(previousInsets, insets) ? previousInsets : insets))}>
<WindowSafeAreaInsetsContext.Provider value={windowInsets}>
<ScreenAndWindowInsetsContext.Provider value={screenAndWindowInsets}>
<SafeAreaInsetsContext.Provider value={screenInsets}>{children}</SafeAreaInsetsContext.Provider>
</WindowSafeAreaInsetsContext.Provider>
</ScreenAndWindowInsetsContext.Provider>
</SafeAreaListener>
);
}

/** 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 <SafeAreaInsetsContext.Provider value={windowInsets}>{children}</SafeAreaInsetsContext.Provider>;
const {window: windowInsets, screen: screenInsets} = insetsOfScreen;
const modalInsets = shouldKeepScreenSideInsets ? {...windowInsets, left: screenInsets.left, right: screenInsets.right} : windowInsets;
return <SafeAreaInsetsContext.Provider value={modalInsets}>{children}</SafeAreaInsetsContext.Provider>;
}

export default ScreenSafeAreaInsets;
Expand Down
4 changes: 3 additions & 1 deletion src/components/ScreenSafeAreaInsets/index.tsx
Original file line number Diff line number Diff line change
@@ -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;
}

Expand Down
8 changes: 8 additions & 0 deletions src/components/ScreenSafeAreaInsets/types.ts
Original file line number Diff line number Diff line change
@@ -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;
Loading
Loading