emilkowalski / sonner

An opinionated toast component for React.
https://sonner.emilkowal.ski
MIT License
8.4k stars 264 forks source link

Enhanced Configuration for toast.promise States #464

Open bakikucukcakiroglu opened 3 months ago

bakikucukcakiroglu commented 3 months ago

Feature Request: Enhanced Configuration for toast.promise States

Problem Statement: Currently, toast.promise supports basic customization of messages for different states (loading, success, error), but lacks support for detailed configurations like different actions and descriptions for each state. If a user tries to add a custom component to implement description and buttons, they lose the consistent styling provided by the library(also code becomes cumbersome).

Using data and error parameters to show a toast description, supplying an action based on the result of the promise would be really elegant. You can make user go to the output's page on success while open logs on error.

Note: Please let me know if there is an easy way of doing that but I'm missing.

Example Usage:

toast.promise(
    new Promise((resolve, reject) => {
        setTimeout(() => {
            if (Math.random() > 0.2) {
                resolve("Transcription successful");
            } else {
                reject("Transcription failed");
            }
        }, 1000);
    }), {
        loading: {
           "Loading", {
               description: "Please wait for a few seconds..."
            }
        },
        success: {
            "Transcription successful", {
            description: "Your transcription has been completed successfully.",
               action: {
                   label: "View transcription",
                   onClick: () => {
                       console.log("View transcription");
                   }
               }
           }
        },
        error: {
            "Transcription failed", {
               description: "Unable to complete your transcription at this time."
            }
        }
    }
);
vinhloc30796 commented 3 months ago

It's looking like this would be the place to implement the thing

https://github.com/emilkowalski/sonner/blob/2b99cd8920f984664cb805d2a197fd0f04b6c9bb/src/state.ts#L107-L165

Jkense commented 2 months ago

I've been struggling with the same thing here. I would love to have the same action buttons in my promise messages, but it lacks a lot of flexibility.

Jkense commented 2 months ago

Just found this:

https://github.com/emilkowalski/sonner/issues/413

bakikucukcakiroglu commented 2 months ago

Just found this:

https://github.com/emilkowalski/sonner/issues/413

Actually I was aware of that. 'If a user tries to add a custom component to implement description and buttons, they lose the consistent styling provided by the library(also code becomes cumbersome).'

I think implementing description and buttons for promised toasts is a better approach since it also make the developers use the same structure for showing a toast regardless of it is promised or not.

Jkense commented 2 months ago

Just found this:

https://github.com/emilkowalski/sonner/issues/413

Actually I was aware of that. 'If a user tries to add a custom component to implement description and buttons, they lose the consistent styling provided by the library(also code becomes cumbersome).'

I think implementing description and buttons for promised toasts is a better approach since it also make the developers use the same structure for showing a toast regardless of it is promised or not.

Agreed, the whole point should be to reduce code and take it out of our components

raunaq-iprally commented 2 months ago

I was just trying to figure this out myself. I was wondering if there is way to add a description and action to loading state?

Just showing the label: 'Loading...' is not sufficient at times. For example: for my usecase, loading state can take minutes, in that case I would want the user to be able to say 'notify me when this completes' or something

Is there a better way to achieve this? Apologies, I am not advance in coding