From 0d9206ea7a2a942f235733b17b9a35477bb3c54d Mon Sep 17 00:00:00 2001 From: Dominic Go Date: Wed, 2 Oct 2024 13:01:28 +0800 Subject: [PATCH] =?UTF-8?q?=E2=AD=90=EF=B8=8F=20Impl:=20`ModalSheetView`?= =?UTF-8?q?=20Focus=20Events?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../RNIModalSheetViewDelegate.swift | 30 +++++++++++++++++++ .../RNIModalSheetViewManager.mm | 2 ++ .../ModalSheetView/ModalSheetViewTypes.tsx | 1 + src/index.ts | 1 + .../RNIModalSheetNativeView.ts | 4 ++- .../RNIModalSheetViewNativeComponent.ts | 2 ++ .../RNIModalSheetViewTypes.ts | 1 + src/types/CommonModalEvents.ts | 9 ++++++ src/types/ModalFocusState.ts | 6 ++++ 9 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/types/ModalFocusState.ts diff --git a/ios/RNIModalSheetView/RNIModalSheetViewDelegate.swift b/ios/RNIModalSheetView/RNIModalSheetViewDelegate.swift index d6e85c8d..fb084541 100644 --- a/ios/RNIModalSheetView/RNIModalSheetViewDelegate.swift +++ b/ios/RNIModalSheetView/RNIModalSheetViewDelegate.swift @@ -30,6 +30,8 @@ public final class RNIModalSheetViewDelegate: UIView, RNIContentView { case onModalWillHide; case onModalDidHide; + case onModalFocusChange; + case onModalSheetWillDismissViaGesture; case onModalSheetDidDismissViaGesture; case onModalSheetDidAttemptToDismissViaGesture; @@ -113,6 +115,7 @@ public final class RNIModalSheetViewDelegate: UIView, RNIContentView { modalVC.lifecycleEventDelegates.add(self); modalVC.modalLifecycleEventDelegates.add(self); modalVC.sheetPresentationStateMachine.eventDelegates.add(self); + modalVC.modalFocusEventDelegates.add(self); return modalVC; }; @@ -450,3 +453,30 @@ extension RNIModalSheetViewDelegate: ModalSheetViewControllerEventsNotifiable { ); }; }; + +// MARK: - RNIModalSheetViewDelegate+ModalSheetViewControllerEventsNotifiable +// -------------------------------------------------------------------------- + +extension RNIModalSheetViewDelegate: ModalFocusEventNotifiable { + + public func notifyForModalFocusStateChange( + prevState: ModalFocusState?, + currentState: ModalFocusState, + nextState: ModalFocusState + ) { + + var payload: Dictionary = [ + "currentState": currentState.rawValue, + "nextState": nextState.rawValue, + ]; + + payload.unwrapAndMerge(withOther: [ + "prevState": prevState?.rawValue, + ]); + + self.dispatchEvent( + for: .onModalFocusChange, + withPayload: payload + ); + }; +}; diff --git a/ios/RNIModalSheetView/RNIModalSheetViewManager.mm b/ios/RNIModalSheetView/RNIModalSheetViewManager.mm index 07979907..e99bdcdf 100644 --- a/ios/RNIModalSheetView/RNIModalSheetViewManager.mm +++ b/ios/RNIModalSheetView/RNIModalSheetViewManager.mm @@ -45,6 +45,8 @@ - (UIView *)view RNI_EXPORT_VIEW_EVENT(onModalWillHide, RCTBubblingEventBlock); RNI_EXPORT_VIEW_EVENT(onModalDidHide, RCTBubblingEventBlock); +RNI_EXPORT_VIEW_EVENT(onModalFocusChange, RCTBubblingEventBlock); + RNI_EXPORT_VIEW_EVENT(onModalSheetWillDismissViaGesture, RCTBubblingEventBlock); RNI_EXPORT_VIEW_EVENT(onModalSheetDidDismissViaGesture, RCTBubblingEventBlock); RNI_EXPORT_VIEW_EVENT(onModalSheetDidAttemptToDismissViaGesture, RCTBubblingEventBlock); diff --git a/src/components/ModalSheetView/ModalSheetViewTypes.tsx b/src/components/ModalSheetView/ModalSheetViewTypes.tsx index 3ff58e90..0032720b 100644 --- a/src/components/ModalSheetView/ModalSheetViewTypes.tsx +++ b/src/components/ModalSheetView/ModalSheetViewTypes.tsx @@ -46,6 +46,7 @@ export type ModalSheetViewInheritedProps = Pick; onModalDidHide?: BubblingEventHandler<{}>; + onModalFocusChange?: BubblingEventHandler<{}>; + // presentation controller delegate events /** * From `UIAdaptivePresentationControllerDelegate`. diff --git a/src/native_components/RNIModalSheetVIew/RNIModalSheetViewTypes.ts b/src/native_components/RNIModalSheetVIew/RNIModalSheetViewTypes.ts index 879b3bf3..22e865d8 100644 --- a/src/native_components/RNIModalSheetVIew/RNIModalSheetViewTypes.ts +++ b/src/native_components/RNIModalSheetVIew/RNIModalSheetViewTypes.ts @@ -42,6 +42,7 @@ export type RNIModalSheetViewInheritedOptionalProps = Partial; +export type OnModalFocusChangeEventPayload = Readonly<{ + prevState?: ModalFocusState; + currentState: ModalFocusState; + nextState: ModalFocusState; +}>; + // MARK: Events // ------------ @@ -65,3 +72,5 @@ export type OnModalWillHideEvent = export type OnModalDidHideEvent = BubblingEventHandler; +export type OnModalFocusChangeEvent = + BubblingEventHandler; \ No newline at end of file diff --git a/src/types/ModalFocusState.ts b/src/types/ModalFocusState.ts new file mode 100644 index 00000000..a73753ec --- /dev/null +++ b/src/types/ModalFocusState.ts @@ -0,0 +1,6 @@ + +export type ModalFocusState = + | 'blurred' + | 'blurring' + | 'focused' + | 'focusing'; \ No newline at end of file