Skip to content

Commit

Permalink
⭐️ Impl: ModalSheetViewEventEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed Sep 28, 2024
1 parent 114f146 commit 0fac24c
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/components/ModalSheetView/ModalSheetView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ export const ModalSheetView = React.forwardRef<

return await nativeRef.current.getModalMetrics();
},
getEventEmitter: () => {
return nativeRef.current!.getEventEmitter();
},
}));

const shouldEnableDebugBackgroundColors =
Expand Down
29 changes: 29 additions & 0 deletions src/functions/ModalSheetViewEventEmitter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { TSEventEmitter } from '@dominicstop/ts-event-emitter';
import type { RemapObject } from 'react-native-ios-utilities';
import type { OnModalDidHideEventPayload, OnModalDidPresentEventPayload, OnModalDidShowEventPayload, OnModalWillHideEventPayload, OnModalWillPresentEventPayload, OnModalWillShowEventPayload } from '../types/CommonModalEvents';


export enum ModalSheetViewEvents {
onModalWillPresent = "onModalWillPresent",
onModalDidPresent = "onModalDidPresent",
onModalWillShow = "onModalWillShow",
onModalDidShow = "onModalDidShow",
onModalWillHide = "onModalWillHide",
onModalDidHide = "onModalDidHide",
};

export type ModalSheetViewEventKeys = keyof typeof ModalSheetViewEvents;

export type ModalSheetViewEventEmitterMap = RemapObject<typeof ModalSheetViewEvents, {
onModalWillPresent: OnModalWillPresentEventPayload;
onModalDidPresent: OnModalDidPresentEventPayload;
onModalWillShow: OnModalWillShowEventPayload;
onModalDidShow: OnModalDidShowEventPayload;
onModalWillHide: OnModalWillHideEventPayload;
onModalDidHide: OnModalDidHideEventPayload;
}>;

export type ModalSheetViewEventEmitter = TSEventEmitter<
ModalSheetViewEvents,
ModalSheetViewEventEmitterMap
>;
13 changes: 13 additions & 0 deletions src/hooks/useLazyRef.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import React from "react";

export function useLazyRef<T>(
providerCallback: () => T
) {
const ref = React.useRef<T>();

if (ref.current === undefined) {
ref.current = providerCallback();
};

return ref;
};
65 changes: 65 additions & 0 deletions src/native_components/RNIModalSheetVIew/RNIModalSheetView.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import * as React from 'react';
import { StyleSheet } from 'react-native';

import { Helpers, type StateViewID, type StateReactTag } from 'react-native-ios-utilities';
import { TSEventEmitter } from '@dominicstop/ts-event-emitter';

import { RNIModalSheetNativeView } from './RNIModalSheetNativeView';
import { useLazyRef } from '../../hooks/useLazyRef';

import type { RNIModalSheetViewProps, RNIModalSheetViewRef, } from './RNIModalSheetViewTypes';
import type { ModalMetrics } from '../../types/ModalMetrics';
import type { ModalSheetViewEventEmitter } from '../../functions/ModalSheetViewEventEmitter';


export const RNIModalSheetView = React.forwardRef<
Expand All @@ -13,6 +18,9 @@ export const RNIModalSheetView = React.forwardRef<
>((props, ref) => {
const [viewID, setViewID] = React.useState<StateViewID>();
const [reactTag, setReactTag] = React.useState<StateReactTag>();

const modalEventEmitterRef =
useLazyRef<ModalSheetViewEventEmitter>(() => new TSEventEmitter());

const [
cachedModalMetrics,
Expand All @@ -26,6 +34,9 @@ export const RNIModalSheetView = React.forwardRef<
getViewID: () => {
return viewID;
},
getEventEmitter: () => {
return modalEventEmitterRef.current!;
},
presentModal: async (commandArgs) => {
if(viewID == null) return;
const module = Helpers.getRNIUtilitiesModule();
Expand Down Expand Up @@ -82,6 +93,60 @@ export const RNIModalSheetView = React.forwardRef<
props.onDidSetViewID?.(event);
event.stopPropagation();
}}
onModalWillPresent={(event) => {
props.onModalWillPresent?.(event);
event.stopPropagation();

modalEventEmitterRef.current!.emit(
'onModalWillPresent',
event.nativeEvent
);
}}
onModalDidPresent={(event) => {
props.onModalDidPresent?.(event);
event.stopPropagation();

modalEventEmitterRef.current!.emit(
'onModalDidPresent',
event.nativeEvent
);
}}
onModalWillShow={(event) => {
props.onModalWillShow?.(event);
event.stopPropagation();

modalEventEmitterRef.current!.emit(
'onModalWillShow',
event.nativeEvent
);
}}
onModalDidShow={(event) => {
props.onModalDidShow?.(event);
event.stopPropagation();

modalEventEmitterRef.current!.emit(
'onModalDidShow',
event.nativeEvent
);
}}
onModalWillHide={(event) => {
props.onModalWillHide?.(event);
event.stopPropagation();

modalEventEmitterRef.current!.emit(
'onModalWillHide',
event.nativeEvent
);
}}
onModalDidHide={(event) => {
props.onModalDidHide?.(event);
event.stopPropagation();

modalEventEmitterRef.current!.emit(
'onModalDidHide',
event.nativeEvent
);
}}
>
{props.children}
</RNIModalSheetNativeView>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import type React from "react";

import type { PropsWithChildren } from "react";
import type { ViewProps } from "react-native";
import type { StateReactTag, StateViewID } from "react-native-ios-utilities";

import type { RNIModalSheetNativeViewProps } from "./RNIModalSheetNativeView";

import type { ModalMetrics } from "../../types/ModalMetrics";
import type { ModalSheetViewEventEmitter } from "../../functions/ModalSheetViewEventEmitter";


export type RNIModalSheetViewRef = {
getViewID: () => StateViewID;
getReactTag: () => StateReactTag;

getEventEmitter: () => ModalSheetViewEventEmitter;

presentModal: (commandArgs: {
isAnimated: boolean;
}) => Promise<void>;
Expand Down

0 comments on commit 0fac24c

Please sign in to comment.