colorfy-software / react-native-modalfy

🥞 Modal citizen of React Native.
https://colorfy-software.gitbook.io/react-native-modalfy
MIT License
1.06k stars 42 forks source link

How to subscribe to all modal close events? #65

Closed cmaycumber closed 2 years ago

cmaycumber commented 2 years ago

I'm currently using shallow routing on Next Js with this library on the web. I'd like to track the modal close event so whenever a modal is closed by clicking on the backdrop I can trigger a change in the URL to remove the shallow route that's added when a modal is pushed.

Is this currently possible? Or should I extend the current modal hook functions to include my custom functionality?

CharlesMangwa commented 2 years ago

Hey @cmaycumber! This was not possible up until now indeed. I just published v3.1.0 that adds this feature via the existing addListener method. The documentation has been updated to reflect this, just as the Expo playground (look at ./components/DemoModal.tsx#L51-53 if you want to quickly check it out).

Let me know if this works for you and addresses your use case!

cmaycumber commented 2 years ago

Thanks for adding this!

Still curious, would it be possible to add the onClose listeners to all the modals in the stack without having to declare it across every modal component?

Something like passing listeners to the ModalProvider would run for every modal.

CharlesMangwa commented 2 years ago

I would have to think about a way to do so that wouldn't be costly.

In the meantime, if you absolutely need this behavior, you can have all of your modals use a common wrapper component. That way you'd only use addListener with 'onClose' once in that wrapper component and trigger whatever logic you need from there.

cmaycumber commented 2 years ago

I would have to think about a way to do so that wouldn't be costly.

Totally get this. Thanks for the direction.