Skip to content

Commit

Permalink
⭐️ Impl: ModalSheetViewContext
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed Sep 28, 2024
1 parent 57c146e commit 0c84b2b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/components/ModalSheetView/ModalSheetView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type { ModalSheetViewContentProps } from './ModalSheetViewContentTypes';
import type { ModalSheetContentMap } from './ModalSheetContentMap';

import { RNIModalSheetView, type RNIModalSheetViewRef } from '../../native_components/RNIModalSheetVIew';
import { ModalSheetViewContext, type ModalSheetViewContextType } from '../../context/ModalSheetViewContext';
import { useLazyRef } from '../../hooks/useLazyRef';


export const ModalSheetView = React.forwardRef<
Expand Down Expand Up @@ -42,14 +44,14 @@ export const ModalSheetView = React.forwardRef<
!isModalContentLazy
|| shouldExplicitlyMountModalContents;

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

const shouldWaitToMount =
!shouldExplicitlyMountModalContents
!shouldExplicitlyMountModalContents
&& isModalContentLazy;

setShouldExplicitlyMountModalContents(true);
Expand Down Expand Up @@ -99,6 +101,8 @@ export const ModalSheetView = React.forwardRef<
},
}));

React.useImperativeHandle(ref, () => rawRef.current!);

const shouldEnableDebugBackgroundColors =
props.shouldEnableDebugBackgroundColors ?? false;

Expand All @@ -112,20 +116,26 @@ export const ModalSheetView = React.forwardRef<
);
});

const contextValue: ModalSheetViewContextType = {
getModalSheetViewRef: () => rawRef.current!
};

return (
<RNIModalSheetView
{...props}
ref={ref => nativeRef.current = ref}
style={styles.nativeModalSheet}
onModalDidHide={(event) => {
setShouldExplicitlyMountModalContents(false);

props.onModalDidHide?.(event);
event.stopPropagation();
}}
>
{children}
</RNIModalSheetView>
<ModalSheetViewContext.Provider value={contextValue}>
<RNIModalSheetView
{...props}
ref={ref => nativeRef.current = ref}
style={styles.nativeModalSheet}
onModalDidHide={(event) => {
setShouldExplicitlyMountModalContents(false);

props.onModalDidHide?.(event);
event.stopPropagation();
}}
>
{children}
</RNIModalSheetView>
</ModalSheetViewContext.Provider>
);
});

Expand Down
9 changes: 9 additions & 0 deletions src/context/ModalSheetViewContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';
import type { ModalSheetViewRef } from '../components/ModalSheetView';

export type ModalSheetViewContextType = {
getModalSheetViewRef: () => ModalSheetViewRef;
};

export const ModalSheetViewContext =
React.createContext<ModalSheetViewContextType | undefined>(undefined);
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

export * from './components/ModalSheetView';
export * from './context/ModalSheetViewContext';

export type * from './types/ModalViewControllerMetrics';
export type * from './types/PresentationControllerMetrics';
Expand Down

0 comments on commit 0c84b2b

Please sign in to comment.