Skip to content

Commit

Permalink
⭐️ Impl: ModalSheetViewContext - Add Sheet State Metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed Sep 29, 2024
1 parent 65b307a commit 2662f62
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/components/ModalSheetView/ModalSheetView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import { RNIModalSheetView, type RNIModalSheetViewRef } from '../../native_compo
import { ModalSheetViewContext, type ModalSheetViewContextType } from '../../context/ModalSheetViewContext';
import { useLazyRef } from '../../hooks/useLazyRef';

import { DEFAULT_MODAL_SHEET_VIEW_METRICS as MODAL_SHEET_STATE_METRICS_DEFAULT } from './ModalSheetViewConstants';
import type { RNIModalSheetStateMetrics } from '../../types/RNIModalSheetStateMetrics';


export const ModalSheetView = React.forwardRef<
ModalSheetViewRef,
Expand All @@ -18,6 +21,16 @@ export const ModalSheetView = React.forwardRef<

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

const [
prevModalSheetStateMetrics,
setPrevModalSheetStateMetrics,
] = React.useState<RNIModalSheetStateMetrics | undefined>();

const [
currentModalSheetStateMetrics,
setCurrentModalSheetStateMetrics,
] = React.useState<RNIModalSheetStateMetrics>(MODAL_SHEET_STATE_METRICS_DEFAULT);

const callbacksToInvokeOnNextRender = React.useRef<Array<() => void>>([]);
React.useEffect(() => {
const totalItems = callbacksToInvokeOnNextRender.current.length;
Expand Down Expand Up @@ -117,6 +130,8 @@ export const ModalSheetView = React.forwardRef<
});

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

Expand All @@ -132,6 +147,13 @@ export const ModalSheetView = React.forwardRef<
props.onModalDidHide?.(event);
event.stopPropagation();
}}
onModalSheetStateDidChange={(event) => {
setPrevModalSheetStateMetrics(event.nativeEvent.prevState);
setCurrentModalSheetStateMetrics(event.nativeEvent.currentState);

props.onModalSheetStateDidChange?.(event);
event.stopPropagation();
}}
>
{children}
</RNIModalSheetView>
Expand Down
10 changes: 10 additions & 0 deletions src/components/ModalSheetView/ModalSheetViewConstants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { RNIModalSheetStateMetrics } from "../../types/RNIModalSheetStateMetrics";

export const DEFAULT_MODAL_SHEET_VIEW_METRICS: RNIModalSheetStateMetrics = {
state: 'dismissed',
isDismissed: true,
isDismissing: false,
isPresented: false,
isPresenting: false,
simplified: 'dismissed'
};
4 changes: 4 additions & 0 deletions src/context/ModalSheetViewContext.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import React from 'react';
import type { ModalSheetViewRef } from '../components/ModalSheetView';
import type { RNIModalSheetStateMetrics } from '../types/RNIModalSheetStateMetrics';

export type ModalSheetViewContextType = {
prevModalSheetStateMetrics: RNIModalSheetStateMetrics | undefined;
currentModalSheetStateMetrics: RNIModalSheetStateMetrics;

getModalSheetViewRef: () => ModalSheetViewRef;
};

Expand Down

0 comments on commit 2662f62

Please sign in to comment.