Skip to main content

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 with ModalProvider. 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 the ModalController. 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.
How it works:
  • When you call showModal, the provider renders the regular modal.
  • If your content is { render(controller) }, the provider calls your render function and injects a ModalController.
  • 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 Alert inside the modal if your handler throws
    • Closes the modal and resolves the promise when the handler completes successfully
Notes:
  • You can dynamically change title and button labels using setTitle, setConfirmButtonText, and setCancelButtonText.
  • Disable or enable buttons via setConfirmButtonDisabled and setCancelButtonDisabled.
  • 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 Alert in the modal body.
  • LoadingSpinner is 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 Modal components; ensure surrounding UI remains keyboard navigable.

See also