Introduction to Magic Modal
mainMagic Modal is a library designed to turn modal interactions into typed, awaitable async results. Instead of managing complex state to track if a modal is open, you can await the result of a modal interaction directly within your business logic (events, effects, or services).
Key features include:
- One root portal:
MagicModalPortalmanages the modal stack. - Typed close results:
show<T>()andhide(data)allow you to pass typed data from the modal back to the caller. - Readable async flows: You can await a modal, branch on its result, and immediately trigger the next step in a linear function.
- Cross-platform: A single API for Web, iOS, and Android.
const result = await magicModal.show<ConfirmationResult>(ConfirmationModal, {
accessibilityLabel: "Confirm purchase",
});
if (
result.reason === MagicModalHideReason.INTENTIONAL_HIDE &&
result.data.confirmed
) {
completePurchase();
}