Skip to content

async submission scenario #22

@PavelZhuravlev

Description

@PavelZhuravlev

Hey, thank you for developing this interesting library! The idea behind it is excellent, and the library's compact size is impressive.

I’m currently exploring the possibility of integrating it but have encountered a challenge with one particular scenario: displaying a modal in a loading state while processing an async operation upon confirmation. Perhaps I’m missing something, but based on what I see in the source code, the library seems to immediately resolve the provided promise and unmount the modal when call.end() is executed. The unmount is a result of the internal stack update (which is iterated to render instances).

While the current call.end() approach works perfectly for common scenarios, introducing additional API options to allow more granular control could expand the library’s range of use cases. Specifically, decoupling the instance's removal from the stack from promise resolution could help address this scenario.

For example, here’s a potential API that could provide more control:

type Props = {
  message: string;
  loading: boolean;
};

export const Confirm = createCallable<Props, Response>(({ call, message, loading }) => (
  <div role="dialog">
    <p>{message}</p>
    <button onClick={() => call.resolve(true)}>Yes</button> //resolve without unmount (detaching from the stack)
    <button onClick={() => call.end(false)}>No</button>
  </div>
));

Usage:

const accepted = await Confirm.call({ message: 'Continue?' });

if (!accepted) {
  return;
}

// Possibly could be achieved without .update() in favor of passing prop through Confirm.Root
Confirm.update({ loading: true }); 
await someAsyncOperation();
Confirm.dismiss(); // unmount (by detaching from the stack)

This is just an illustrative proposal, and I’m sure there are better ways to implement such functionality. A more granular API, layered on top of the current one, could also cater to feature requests for animation support (as an alternative to using timeouts).

Please let me know if I’ve overlooked anything and if the mentioned use case is achievable with the current API.

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions