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

Choose Between Displaying Multiple Toasts Or Just One #108

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

TrevorSlobodnick
Copy link

Description

This PR will allow users to choose between displaying a single toast on the screen vs. displaying multiple toasts, with one line of code.

Displaying multiple toasts is still the default setting, so anyone using Toastify wouldn't be affected by these changes. The only difference is that with the new "single toast" approach, the toast duration is not honored. Meaning, if a new toast is about to appear, any toast(s) that are currently on the screen are then removed, regardless of how long they were supposed to be displayed for.

How To Use

Toastify.multiple = true; // Allows multiple toasts (default)
Toastify.multiple = false; // Only one toast can be on the screen
Toastify({
    ...
}).showToast()

Super easy! It can be toggled back and forth if it needs to be, but it only needs to be set once at the top of the script, before any toasts are created.

Testing

I want to start off by saying I have not tested this on all browsers. However, can confirm that it is working on these browsers:

  • Google Chrome (version 104.0.5112.102)
  • Firefox (version 80.0.1)

The code I added is very short and sweet so I would not worry too much about compatibility issues. The only functions I use that are worth looking into are:

indexOf  - https://caniuse.com/?search=indexOf
splice   - https://caniuse.com/?search=splice
forEach  - https://caniuse.com/?search=forEach

Based on the results from caniuse.com, I don't think this will cause any issues.

Additional Comments

I took the liberty of updating the demo to include this "toggle" feature by adding a checkbox to toggle between the 2 states. Toggling the checkbox also updates the code displayed in the box. It can be viewed at https://trevorslobodnick.github.io/toastify-js/

I also updated the readme so the documentation now includes Toastify.multiple = true; at the top, and I also added myself to the contributors list.

Im new to this so please let me know if I need to add, edit, or remove anything.

Thanks,
Trevor Slobodnick

…he use of only a single toast at a time

I still need to test the code, but the basics are there. The class Toastify will have a class variable multiple, and when set to false, will only allow one toast to appear on the screen at a time. To accomplish this, there is a class variable activeToasts that is an array of all the toasts that are currently shown on the screen. In the showToast function, check if the multiple variable is false, if it is, call the hideToast function on each toast that is showing, then add the current toast to the activeToasts array. In hideToast, we now also remove the current toast from the activeToasts array.
Looking at the code again, I am not sure what toastiy-es.js is used for, but I figured it wouldnt hurt to add the code to both files.
Only the UI is finished, currently toggling does nothing, but that will change soon.
…he use of only a single toast at a time

I still need to test the code, but the basics are there. The class Toastify will have a class variable multiple, and when set to false, will only allow one toast to appear on the screen at a time. To accomplish this, there is a class variable activeToasts that is an array of all the toasts that are currently shown on the screen. In the showToast function, check if the multiple variable is false, if it is, call the hideToast function on each toast that is showing, then add the current toast to the activeToasts array. In hideToast, we now also remove the current toast from the activeToasts array.
Looking at the code again, I am not sure what toastiy-es.js is used for, but I figured it wouldnt hurt to add the code to both files.
…iple when a checkbox is clicked

There is a small bug, if I declare Toastify.multiple = true to start, then later switch it to false, it will allow 2 toasts on the screen at the same time. But it works as expected if Toast.multiple = false is set from the start... I will look into this bug
…Toastify.multiple = false

In my previous commit I mentioned there was a bug where more than one toast would appear even when Toastify.multiple = false. This is because I thought that the hideToast() function was called every time a toast "ended". After looking into it, it is actually the "removeElement" function that gets called everytime, so I moved my code to that function and now everything is running how it should.
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

Successfully merging this pull request may close these issues.

None yet

1 participant