josefrichter
Is there a way to have a flash message disappear (fade out) after a few seconds, please? Can’t find anything in docs nor on internets. Feels like this could be easy in LiveView, maybe I’m just missing the obvious. Thank you.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
sfusato
You can achieve that with something like this:
Send a message to
selfwhen you useput_flashThen, in
handle_info:baldwindavid
That will remove the flash immediately, right? I use some css to accomplish fading out though it is for a different case than flash messages.
josefrichter
It appears to me, that the process that put the flash message is dead after it e.g. creates a record. Maybe the send needs to go to different process?
sfusato
That’s true. If you program the sending to a
pid, thenpush_redirectfrom the live component, the live view will be shut down and a new one created in its place, thus the old process won’t exist anymore. Changing topush_patchshould work.Also, forgot to mention that you also need to make sure the socket is connected before sending the message:
@baldwindavid, it will just be removed from the DOM. You could chain together 2 events: 1st fade-out the element (add a css class), then remove it altogether. Worth mentioning that animations are best kept for Javascript, rather than Liveview. You could have a hook on the flash message that will remove itself in 5 seconds with animation effects and everything.
josefrichter
Bingo! I’ve combined both of your solutions, and tweaked it a bit. I kept
push_redirectrather thanpush_patchas I was “returning” to index and with push_patch my index of items was not updated.My workaround is that I actually put the
Process.send_afterinto mymountfunction, because that’s where I always get back to – so if there’s any new flash message displayed, it gets cleared after 5s. Not sure it’s super robust solution, but this way I need the clear function in one place only, and it works nicely.And I applied the css animation to nicely fade out the flash, no need for javascript, nor desirable for this simple animation, I think.
GumptionWare
Hey guys. This thread was very helpful. Many thanks. I see many other threads out there asking this question, and this one seems to be the closest to a clean solution.
I wanted to revisit this topic given more recent Phoenix & LiveView releases. Frankly, I am baffled that such a standard UI behavior expectation is still so hard to find a clean solution for in Phoenix/LiveView. If I am missing that solution, please point me to it.
I just created a fresh Phoenix app and ran
mix phx.gen.authto create a typical starting point to see how Flash messages currently behave. (FYI, it looks like my generator runs created a Phoenix 1.7.10 app running LiveView 0.19.5).Alas, it appears that nothing has changed. Out of the box, Flash message behavior remains static. The Flash box appears, and it just stays there–until the user clicks the “x” in the box to dismiss it.
The (partial) Solution So Far
Adapting the advice in this thread history, I have been able to get the Flash box to go away after a specified number of milliseconds. In my example app, I have a (generated) LiveView scaffolding for an Alerts table and its LiveViews, so I have a
MyApp_web/live/alert_live/index.exand aMyApp_web/live/form_component.exthat are involved in making updates to Alert records via LiveView interactions.My list of alerts is driven by this logic in
MyApp_web/live/alert_live/index.ex:Because each CRUD action on an Alert gets us back here, I have added the
Process.send_after(self(), :clear_flash, 3000)in this function as shown above.That
:clear_flashhandle_event function is also inMyApp_web/live/alert_live/index.ex:This works. I’m not (yet) using CSS to create a graceful fade-out; the Flash box disappears abruptly after 3 seconds. It is not slick, but it is a huge improvement.
But How Do We Generalize This?
I haven’t wired this up for all the other places CRUD activities take place (like User Settings, for example). This solution will require repeating this logic in all the places
put_flashis invoked. To say the most, that is very not DRY.I asked my AI intern for suggestions, and that advice included writing JavaScript to create a more generalized solution. That strikes me as antithetical to the LiveView vision–particularly for UI behavior users expect–and behavior that is common to most of the other web frameworks out there.
We need to do better than this. I need a solution that is credible to SaaS users, and our community needs a solution that is credible to UI framework adopters.
I’m not just whining here; I am eager to do the work required to help create, document and publicize a generalized solution.
Thanks in advance for your help.
horizon0708
To keep it dry, maybe have a look at attach_hook/4 in conjunction with on_mount?
To give an example, I have something like this:
And this is mounted to live_session in
router.exNow the logic to hide the flash is in one place.
I do agree that more could be done for flash though!
GumptionWare
This looks promising! I’ll give it a shot and report back.
HUGE thanks!
sezaru
Hey @GumptionWare maybe you will be interested in my library which supports what you want to do and much more: Flashy - A small library to extend LV's flash notifications - #17 by sezaru
GumptionWare
Thank @sezaru!! Our team will check that out tomorrow. It looks pretty complete.