useModal
useModal provides a global, promise-based modal API and a controller for building controlled modals. It is backed by a ModalProvider that renders two Bootstrap modals: a regular modal and a confirm modal.
Source: src/states/useModalContext.tsx
Provider
Wrap your app withModalProvider. The default LocationContext already includes it, so you usually don’t need to add it yourself.
API
Quick use
Controlled modals (advanced)
Controlled modals let the modal content drive button text, loading state, and confirm behavior using theModalController. To opt in, pass a content object with a render(controller) function. Inside, call controller setters to update button state or register an async confirm handler.
- When you call
showModal, the provider renders the regular modal. - If your content is
{ render(controller) }, the provider calls your render function and injects aModalController. - You then call
controller.setOnConfirm(fn)to register the confirm handler; when the user clicks the confirm button (or closes), the provider executes it with error handling:- Shows a LoadingSpinner while the promise is pending (
setConfirmButtonLoading(true)) - Displays an error
Alertinside the modal if your handler throws - Closes the modal and resolves the promise when the handler completes successfully
- Shows a LoadingSpinner while the promise is pending (
- You can dynamically change title and button labels using
setTitle,setConfirmButtonText, andsetCancelButtonText. - Disable or enable buttons via
setConfirmButtonDisabledandsetCancelButtonDisabled. - Call
controller.close()to programmatically close the active modal.
Confirm modals with custom content
confirm(content, options) behaves similarly, but displays a Cancel + Confirm footer. You can also control it via the same controller pattern:
Error handling and loading
- The provider wraps your confirm handler in a try/catch and surfaces errors via a danger
Alertin the modal body. LoadingSpinneris shown automatically on the confirm button while your handler is pending.
Accessibility and visibility
setModalsVisible(false)hides the modal UI but keeps state. Useful for embedded flows where the host temporarily forbids overlays.- Modals are React-Bootstrap
Modalcomponents; ensure surrounding UI remains keyboard navigable.
See also
- LocationContext – Includes the
ModalProviderin the app provider stack