diff --git a/src/hooks/useModalSheetViewEvents.ts b/src/hooks/useModalSheetViewEvents.ts new file mode 100644 index 00000000..63d1357c --- /dev/null +++ b/src/hooks/useModalSheetViewEvents.ts @@ -0,0 +1,28 @@ +import React from "react"; +import type { EventListener } from '@dominicstop/ts-event-emitter'; + +import { ModalSheetViewContext } from "../context/ModalSheetViewContext"; +import type { ModalSheetViewEventEmitterMap, ModalSheetViewEventKeys } from "../types/ModalSheetViewEventEmitter"; + + +export function useModalSheetViewEvents< + EventKey extends ModalSheetViewEventKeys +>( + eventName: EventKey, + listener: EventListener +) { + const modalContext = React.useContext(ModalSheetViewContext); + if(modalContext == null){ + return; + }; + + React.useEffect(() => { + const ref = modalContext.getModalSheetViewRef(); + const eventEmitter = ref.getEventEmitter(); + + const listenerHandle = eventEmitter.addListener(eventName, listener); + return () => { + listenerHandle.unsubscribe(); + }; + }); +}; \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 9ee95e59..73c3869c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ export * from './components/ModalSheetView'; export * from './context/ModalSheetViewContext'; +export * from './hooks/useModalSheetViewEvents'; export type * from './types/ModalViewControllerMetrics'; export type * from './types/PresentationControllerMetrics';