live_toast icon indicating copy to clipboard operation
live_toast copied to clipboard

Inconsistencies and issues with LiveToast 0.8 and Phoenix 1.8

Open luislhsc opened this issue 9 months ago • 4 comments

Hey guys

I'm having issues with LiveToast 0.8 on both Phoenix 1.7 and Phoenix 1.8

I followed the docs and created this repo to reproduce the issues: https://github.com/luislhsc/live-toast-test

There are instructions on how to test on README The main test I did are in ib/hello_phoenix_web/live/conversation_live/index.ex and lib/hello_phoenix_web/live/conversation_live/form.ex

The main questions are: 1 - When should I used put_toast vs send_toast vs put_flash? The docs are not clear about when a toast or a flash message will be displayed. Seems like LiveToast.put_toast always shows flash instead of toast for push_patch and push_navigate Is this the expected behavior? When an actual toast will be displayed?

Also, on whenever it falls back to flash it doesn't show the title

2 - Why LiveToast.send_toast never works on this test? Am I using it wrong or the docs missing some setup step?

Thanks in advance

luislhsc avatar Apr 05 '25 18:04 luislhsc

Hi @luislhsc, yes what you are seeing is expected, let me explain:

The idea would be that you would probably have no need to ever use put_flash, cause put_toast should fall back to flashes in all situations where it matters. That being said, the difference is this:

send_toast is asynchronous. It's actually sending an update to the live component with send_update.

put_toast is synchronous: it assigns the data directly onto the socket.

When would you want to use send_toast

The primary reason is sometimes you don't have access to the socket. In those scenarios you'd want to use send_toast.

When would you want to use put_toast

In most other scenarios, you probably want put_toast. This is because put_toast has been designed to fall back to flashes during a navigation event. This is not possible with send_toast, because it's asynchronous, so the message never gets to the LC when the navigation starts.

This should explain your question about send_toast not working in the scenario you described: It's likely because you were doing a navigation at the same time.

I realize it's not pointed out the best in the docs. If you'd like to make a PR taking a stab at clarifying, it'd be greatly appreciated!

As for titles on flashes, yes, some have asked for this as well. I think there may be an open issue.

srcrip avatar Apr 07 '25 20:04 srcrip

Closing now as I think it's explained but please reopen if you have more questions!

srcrip avatar Apr 17 '25 15:04 srcrip

So, put_toast will never add a Toast with patch/navigate? If thats the case, what is the point of supporting put_toast?

IMO it is misleading since when you call a put_toast, you expect it to a real toast, not a flash. Fallback to flash make you lose all benefits of the toast, like title, custom components etc.

Another example of how misleading it is, is the issue #45 that essentially talks about same problem.

Also, based on the discussion on the issue #9 it sounded like it was going to be solved.

Is there a plan to support real toasts on patch/navigations? I remember reading a while ago that it would work if you first call patch/navigate and then do the put_toast. I think if this requeriment is well documented is not a problem.

The lib is awesome, but this limitation is a deal breaker for me (and I guess for others too)

Looking forward to help, just let me know how

luislhsc avatar Apr 21 '25 00:04 luislhsc

I'm certainly willing to consider changes to make toasts work truely in all situations--it would just require a lot more javascript which is what I'm kind of leary about. One of the nice things about how it works right now is it's very easily testable in regular phoenix unit tests because it doesn't really rely on javascript for a lot (besides animations and stuff).

The other problem is the frontend parts of LiveView don't really feel like a very stable API to me yet. We'd have to hook into details of the page lifecycle at a fundamental level and that feels really brittle where as the current system is conceptually pretty simple, with the caveat that flashes still show up across navigations...

srcrip avatar May 20 '25 19:05 srcrip