Skip to content

Commit

Permalink
⭐️ Impl: ModalSheetView.isModalContentLazy
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed Sep 27, 2024
1 parent 7d73674 commit ae4054d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
38 changes: 38 additions & 0 deletions src/components/ModalSheetView/ModalSheetView.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import { StyleSheet } from 'react-native';
import { Helpers } from 'react-native-ios-utilities';

import type { ModalSheetViewProps, ModalSheetViewRef } from './ModalSheetViewTypes';
import type { ModalSheetViewContentProps } from './ModalSheetViewContentTypes';
Expand All @@ -15,17 +16,51 @@ export const ModalSheetView = React.forwardRef<

const nativeRef = React.useRef<RNIModalSheetViewRef | null>();

const callbacksToInvokeOnNextRender = React.useRef<Array<() => void>>([]);
React.useEffect(() => {
const totalItems = callbacksToInvokeOnNextRender.current.length;

for (let index = 0; index < totalItems; index++) {
const callback = callbacksToInvokeOnNextRender.current.pop();
callback!();
};
});

const [
shouldExplicitlyMountModalContents,
setShouldExplicitlyMountModalContents
] = React.useState(false);

const [
modalSheetContentMap,
setModalSheetContentMap
] = React.useState<ModalSheetContentMap>({});


const isModalContentLazy = props.isModalContentLazy ?? true;

const shouldMountModalContents =
!isModalContentLazy
|| shouldExplicitlyMountModalContents;

React.useImperativeHandle(ref, () => ({
presentModal: async (commandArgs) => {
if(nativeRef.current == null) {
throw Error("Unable to get ref to native sheet");
};

const shouldWaitToMount =
!shouldExplicitlyMountModalContents
&& isModalContentLazy;

setShouldExplicitlyMountModalContents(true);

if(shouldWaitToMount){
await Helpers.promiseWithTimeout(300, new Promise<void>(resolve => {
callbacksToInvokeOnNextRender.current.push(resolve);
}));
};

await nativeRef.current.presentModal({
isAnimated: true,
...commandArgs,
Expand Down Expand Up @@ -56,11 +91,14 @@ export const ModalSheetView = React.forwardRef<
const shouldEnableDebugBackgroundColors =
props.shouldEnableDebugBackgroundColors ?? false;



const children = React.Children.map(props.children, (child) => {
return React.cloneElement(
child as React.ReactElement<ModalSheetViewContentProps>,
{
modalSheetContentMap,
shouldMountModalContent: shouldMountModalContents,
}
);
});
Expand Down
6 changes: 4 additions & 2 deletions src/components/ModalSheetView/ModalSheetViewContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export function ModalSheetViewContent(
props.contentContainerStyle,
];

const shouldMountModalContent = props.shouldMountModalContent ?? false;

const detachedSubviewEntry =
(viewID != null ? props.modalSheetContentMap?.[viewID] : undefined )
?? DEFAULT_SHEET_CONTENT_ENTRY;
Expand Down Expand Up @@ -50,13 +52,13 @@ export function ModalSheetViewContent(
}}
>
{IS_USING_NEW_ARCH ? (
props.children
shouldMountModalContent && props.children
) : (
<View style={[
styles.innerWrapperContainerForPaper,
...wrapperStyle,
]}>
{props.children}
{shouldMountModalContent && props.children}
</View>
)}
</RNIWrapperView>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type ModalSheetViewContentBaseProps = {
contentContainerStyle?: ViewStyle;
isParentDetached?: boolean;
modalSheetContentMap?: ModalSheetContentMap;
shouldMountModalContent?: boolean;
};

export type ModalSheetViewContentProps =
Expand Down
5 changes: 3 additions & 2 deletions src/components/ModalSheetView/ModalSheetViewTypes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ export type ModalSheetViewRef =
& ModalSheetViewRefInheritedRemapped;

export type ModalSheetViewInheritedProps = Pick<RNIModalSheetViewProps,
| 'onDidSetViewID'
| 'shouldEnableDebugBackgroundColors'
>;

export type ModalSheetViewBaseProps = {};
export type ModalSheetViewBaseProps = {
isModalContentLazy?: boolean;
};

export type ModalSheetViewProps = PropsWithChildren<
ModalSheetViewInheritedProps
Expand Down

0 comments on commit ae4054d

Please sign in to comment.