Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make openModal a promise that resolves when the modal closes #17

Open
c00 opened this issue Aug 6, 2022 · 4 comments
Open

Make openModal a promise that resolves when the modal closes #17

c00 opened this issue Aug 6, 2022 · 4 comments

Comments

@c00
Copy link

c00 commented Aug 6, 2022

Is your feature request related to a problem? Please describe.
Consider a confirmation dialog. A user gets a message "Are you sure?" and clicks yes or no. Currently you'd have to do this with callback props, they would have to exist on any modal component you'd want to wait for or have a return value for.

This works, but isn't optimal.

Describe the solution you'd like
Have the openModal function return a promise. This promise will resolve once the modal is closed.

This could be done by extending the StoredModal interface with a promise, and then on pop() just resolve the promises of the modals you're popping.

Then update closeModal() to accept a parameter returnValue?: unknown. That value will be handed to the eventual pop function so it can be used to resolve the Promise. (To be helpful, add a generic to openModal so we know what to expect when it resolves)

Now what a user could do is:

const confirmation = await openModal(MyFancyConfirmationDialog);
if (confirmation) doStuff();

And in the modal itself, no extra callbacks or boilerplatey stuff would be required, just calling the close method would be sufficient:

    <button type="button" on:click={() => closeModal(false)}> Cancel </button>
    <button type="button" on:click={() => closeModal(true)}> Confirm </button>

Describe alternatives you've considered
Rather than returning the promise from openModal directly, it could also return an object with the promise, so in the future we could add more stuff to it. Either way is fine.

I'd be very willing to make a PR for this. Just let me know if you'd be willing to accept such a PR.

@mattjennings
Copy link
Owner

I think this would be a good idea. This is likely a common scenario, and I don't see any drawbacks to having openModal just be a promise until it closes anyways. Resolving with just the data would be my preference rather than an object containing the data.

A few thoughts:

  • What would you expect to happen in the case of closeAllModals()? Should it also take a response that would resolve all openModal promises with?

  • It would be good if there was a way to infer the response type from a type defined in the modal component, rather than having it be unknown. This might be possible through the $$Generic type, but I'd have to take a closer look. If you have any ideas here let me know, otherwise I'd be happy to tackle this after you create your PR.

This was referenced Aug 17, 2022
@mattjennings
Copy link
Owner

mattjennings commented Aug 20, 2022

This will be added in #20 but with a slightly different API.

Instead of providing the value to closeModal, modal components receive a close prop which is a function that can be used to close itself and return a value.

Reasons why:

  • it allows the return type to be inferred by the component type
  • it tightens the relationship from the modal to its openModal call, making sure that the return type is the expected type and not unexpectedly received from closeModals() (that may not have been intentional).
    • if closeModal/s is called, openModal resolves with undefined.

you can try a repl here https://svelte.dev/repl/82473340d9de47e19e5a850e790042f8?version=3.49.0

@c00
Copy link
Author

c00 commented Aug 24, 2022

I like this a lot! Thank you for working on this!

@jelias
Copy link

jelias commented Mar 26, 2024

Curious what the status of this issue is?!

I saw a couple PRs were merged but wasn't sure if they made it into a proper release.. would love to leverage promises!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants