#Disable Mantine Modal Close During Mutation

12 messages · Page 1 of 1 (latest)

hidden rampart
#

I need to disable the close button and outside clicks on a Mantine context modal only while a mutation is pending, but allow closing at all other times.

I am opening the modal like so:

const openDeleteProjectModal = () =>
  openContextModal({
    modal: "deleteProject",
    title: "Delete Project",
    innerProps: { project, onSuccess: () => setSelectedProject(null) },
  });

The delete modal itself is quite simple:

export default function DeleteProjectModal({
  context,
  id,
  innerProps,
}: ContextModalProps<{ project: Project; onSuccess?: () => void }>) {
  const { project, onSuccess } = innerProps;
  const queryClient = useQueryClient();

  const deleteProjectMutation = useDeleteApiProjectsProjectId({
    mutation: {
      onSuccess: async (deletedProject) => {
        await queryClient.invalidateQueries({ queryKey: getGetApiProjectsQueryKey() });
        context.closeModal(id);
        showSuccessNotification(`Project "${deletedProject.name}" deleted successfully!`);
        onSuccess?.();
      },
      onError: () => showErrorNotification("Failed to delete the project. Please try again."),
    },
  });

  const handleOnConfirm = async () => {
    await deleteProjectMutation.mutateAsync({ projectId: project.id });
  };

  return (
    <>
      <Modal.Text>{`Are you sure you want to delete the project "${project.name}?"`}</Modal.Text>

      <Modal.ButtonGroup>
        <Modal.CancelButton disabled={deleteProjectMutation.isPending} onClick={() => context.closeModal(id)}>
          No, don&apos;t delete it
        </Modal.CancelButton>

        <Modal.DeleteButton onClick={handleOnConfirm} loading={deleteProjectMutation.isPending}>
          Delete Project
        </Modal.DeleteButton>
      </Modal.ButtonGroup>
    </>
  );
}

Is there a way to conditionally disable closing behaviors while a mutation is active?
This seems like a common use-case to me, so I'm assuming it's possible somehow.

robust pumice
hidden rampart
#

I was doing that originally, but I ended up having to create a bunch of custom contexts to work around this limitation, which felt wrong. Isn't there a better way?

robust pumice
hidden rampart
robust pumice
hidden rampart
#

@robust pumice I looked a bit at Redux and Zustand and so far it looks like Zustand would work the best for a custom modal management solution built on top of Mantine. What do you think, would that be an OK choice (assuming you have some experience with the two, because I don't)?

hidden rampart
#

@robust pumice I have now implemented update functionality into the Mantine modals package and opened a PR to get this reviewed and hopefullly merged.
I also attached two videos for your convinience:
https://github.com/mantinedev/mantine/pull/7104

GitHub

This PR adds two new functions, updateModal and updateContextModal, enabling dynamic updates to both standard and context modals. These functions allow modals to remain open and update their conten...

hidden rampart
#

@robust pumice Apologies for the ping, but I was wondering if this might be considered for merging at some point? I need it for my application and would prefer not to rely on my own Mantine fork. Totally understand if you haven't had the chance to review it yet - I just wanted to follow up since I didn’t get a response to my last message 😄

robust pumice