Skip to content

Commit

Permalink
⭐️ Impl: useModalSheetViewEvents
Browse files Browse the repository at this point in the history
  • Loading branch information
dominicstop committed Sep 29, 2024
1 parent db266f0 commit af8088f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/hooks/useModalSheetViewEvents.ts
Original file line number Diff line number Diff line change
@@ -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<ModalSheetViewEventEmitterMap[EventKey]>
) {
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();
};
});
};
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down

0 comments on commit af8088f

Please sign in to comment.