From af8088f7c867c4f1791a5c768e63761481890f58 Mon Sep 17 00:00:00 2001 From: Dominic Go Date: Sun, 29 Sep 2024 09:23:24 +0800 Subject: [PATCH] =?UTF-8?q?=E2=AD=90=EF=B8=8F=20Impl:=20`useModalSheetView?= =?UTF-8?q?Events`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hooks/useModalSheetViewEvents.ts | 28 ++++++++++++++++++++++++++++ src/index.ts | 1 + 2 files changed, 29 insertions(+) create mode 100644 src/hooks/useModalSheetViewEvents.ts 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';