sezaru

sezaru

I have a LiveView with a form that has 2 buttons, a skip button and a submitbutton.

Both buttons have a CSS that checks if the button has the phx-submit-loading and shows a loading svg.

The issue is that I only want to change the loading in the button that I clicked, so, if I click in the skip button, I want to show the loading SVG in that button until the server replies. Same thing for the submit button.

But right now, it seems that LiveView will add the phx-submit-loading to all buttons instead of the one I clicked, and it doesn’t add any other class to the button I clicked. meaning that I don’t have a way to differentiate between them, and the loading svg ends up showing in both buttons which seems confusing for the user.

Showing Posts 1 to 10

sodapopcan

sodapopcan

I think you’re going to have to use a client side hook or get fancy with JS commands here.

The simplest non-hook approach I can come up with is:

def click do
  %JS{}
  |> JS.remove_class("clicked", to: "button") # Will remove `clicked` class from all buttons
  |> JS.add_class("clicked") # Add `clicked` class to clicked button
end

Then add phx-click={click()} to the appropriate buttons and target in CSS with:

.phx-submit-loading.clicked .icon {
  display: block;
}

This will leave the clicked class on the button even after loading. Not super clean but also not a big deal if you target properly in CSS (it’s more of a “last clicked” class).

You could also use a client-side hook and put it somewhere where it will apply to all buttons if that is what you’re after.

sezaru

sezaru OP

Yeah, that is a good workaround, thanks for the tip I will use it in my project :slight_smile:

It still sucks. I mean, AFAIK, LiveView knows exactly the button that was clicked, it could, for example, add phx-submit-loading to all buttons inside the form, but also add phx-click-loading to the button that was clicked.

It does already add the phx-click-loading class to a button that is outside a form.

sodapopcan

sodapopcan

Gah, I gotta reread the docs, I wasn’t aware of phx-click-loading. I guess you could just put some sort of no-op click on the buttons you want this to apply to. You could always request it get added (or something similar to it). I think it’s far more common to have a global loader in conjunction with phx-disable-with so my guess is that it just hasn’t come up yet.

sezaru

sezaru OP

I just opened a issue to see if they can add that, let’s see what the devs say :slight_smile:

sodapopcan

sodapopcan

Unless we’re unaware of it and you already can, it would actually be nice to be able to add a temporary class with some variant of phx-disable-with. Though having phx-click-loading is probably enough.

sezaru

sezaru OP

Oh, for sure, that would be even better, I would even go further and add options to customize all the phx-* classes in some way.

sezaru

sezaru OP

Now that you mentioned phx-disable-with, that confirmed to me that LiveView does know what button was clicked since it will only apply that attribute change to the clicked button, not the others :slight_smile:

sodapopcan

sodapopcan

Oh, weird, I tested before I answered and thought it applied to all. Though I actually only looked for the disabled attribute and nothing else but ya, of course that makes sense it only applies to the one, haha. Since that’s the case you can actually do what you want now with:

[phx-disable-with] .loader-icon {
  /* ... */
}
sezaru

sezaru OP

Oh, actually.. I talked too soon… I forgot to add the phx-disable-with to my other button :sweat_smile:

It does apply to all of them :smiling_face_with_tear:

sodapopcan

sodapopcan

Ah ok, I thought so but was tired of spinning up Phoenix apps to check :joy:

Where Next? Top

Trending in Questions Top

RSP87
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
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
velrest
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
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
samoloth
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

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews