hx-indicator & Swap Delays




Check out a free preview of the full HTMX & Go course

The "hx-indicator & Swap Delays" Lesson is part of the full, HTMX & Go course featured in this preview video. Here's what you'd learn in this lesson:

ThePrimeagen demonstrates how to add interactivity to a web page using HTMX. They start by adding a time delay to make the interaction more noticeable and then introduce the concept of indicators to show the progress of a request. They also explain how to use swap delays to create smooth transitions when elements are swapped out on the page.


Transcript from the "hx-indicator & Swap Delays" Lesson

>> But still this doesn't feel interactive, right? We want it to feel like something's happening cuz right now it just kind of pops out. You don't know if a request has been made, you don't know what's happening. Are you going to press it multiple times? Are you freaked out?

You could be freaked out. So let's start by adding a simple time delay right here. So I'm gonna go Sleep, and hopefully one second, pretty good. I'm gonna first started off with 3 seconds, so it's like really obvious to see and feel the terribleness of this. So I'm gonna just press this button over this.

Is it requesting? We don't even know what that okay, there we go, it got deleted, it just disappeared. If you had a big list of items, also in something shifted it might be very hard to see. It's not a great experience we should probably fix all of these things at the same time.

So we have two options we have hx-indicator and swap delays. Both are very exciting and both solve different problems. So let's start off with the indicator, okay? I want you to add these two lines to your server which is just saying hey statically serve these directories. I'll paste it in twitch chat boomer websites are the best, I mean fair statement, all right?

So I'm gonna go up to the Tippity top up here and I'm just gonna say, hey. There you go, serve out images, serve out css. That's why I had you kind of use this project to begin with because there is css, there is images. You can look at it, we have some stuff in there, that's gonna be nice.

So pretty simple right there. Next we need to add this css link, so that we have this beautiful css on here. It's only three lines of css, so I'm gonna go back to my template, go up to the tip of the top, go to the head, add that wonderful piece of code right there.

If you wanna use the course website, go for it. There we go, and we also have this little beautiful image that we're gonna be using. I took this directly from the HTMX website, okay? It's my image now, that's mine it's our image, all right? All right, fantastic, I just tried to literally quick, I just typed in colon right Enter on a website.

That's not how you copy something, it's just I can't help but to do that, it's just in my head, but just do it, all right? So let's go and let's add that line. What was the line? Sorry, I got completely confused there. All right, so let's go all the way down here, we want to add one more item to this contact.

I'm going to add in this beautiful image right here. So let's just see what the image looks like, it's a little loading icon right there. How nice is that little loading icon? Our icon, that is, all right, so I'm gonna add one more item to it. It's gonna call it div class = htmx-indicator.

Now that is what that css is for. This is a generalized way to solve this problem, and so you can use this for all indicators, so let's look at that css one more time. If you have indicator on your class, we set your opacity to 0 and we have a transition of 500 milliseconds if it were to become visible.

Then remember how there's all those events that happen with htmx? Well, when an element is a part of a request, htmx-request will be added to its class list. So notice that I say this, if one of our parents has an htmx-request than our opacity becomes one. If we have that, our opacity becomes 1, so we can kind of define both possibilities.

And so that way we can load an indicator and show it upon request. So now we have that nice delay, everything should be good. So let's refresh it, obviously nothing's there. When I click Delete, What happened? Well, nothing, we didn't actually add the indicator. So let's go back over here.

And when we do a delete, I'm gonna add one more attribute here and go hx-indicator =, now we need to have some way to target it. So we'll add a simple id, id = contact indicator, id, yeah, look at that beauty. That is beautiful, isn't it? There we go.

We now have, hey, the indicator for this action exists in this location. And so when that happens, it's going to do all the addition of things you needed to do. So pretty nice. So I'm gonna hit Refresh, and now when I hit the Delete, that thing will nicely show up during the duration of the request, and then be removed when it's done.

If we look at the actual elements, let's look at the actual elements so we can see what happens here. So, when I hit that delete, notice that htmx-request is just added automatically to that class for you. So you don't have to think about it. It allows you to have these things added and there's actually more classes that we can we can get, which we will get here in just one second.

So that is one way to add some level of interactivity. It's good, but it's still, I mean, it's shockingly declarative, right? We pretty much declared exactly how our element behaves, including where our indicator is and how it should behave. So that's nice, but it's still not as good.

So now we're gonna add something called a swap delay. So I want you to update your CSS with this small little thing right here, contact.htmx-swapping. It's beautiful, okay. And you could obviously just make this generalized. You could have a single way you do this, or you could have it a unique way for each one.

We could use the max height thing, though trying to animate on max height is extremely difficult. And there's all these CSS hacks you have to do just to get height to go downwards. My goodness., I hate CSS sometimes, I just wanna just punch, hate it. Anyways okay so we have contact we have this I'm going to add it to the CSS.

So I'm just gonna walk over to the CSS and I'm just gonna paste this little bit in, yes my CSS is clearly a skill issue. So we have this beautiful little piece of CSS right here, so I'm gonna back to this. And now we obviously want to add, I mean, now we're getting into like full front end here.

Now we have this nice little class contact on it, so we know how to say, hey, which one's gonna have this. Now, when we do this right here, what's gonna end up happening, let's see, Is that, let's go back here. So now you can see it, this right here, we have a target on it.

When the target happens before the swap happens, it adds an additional class htmx swap or swapping. And it's gonna say, hey, we're swapping out this element. So if you need to do something with it, you now have one visual hook in your CSS to do something with it.

And it also has something else, which is I can go swap for 500 milliseconds. I want you to just give me 500 milliseconds on this so I can make something pretty myself. So when we go back here, and I click Delete, it will do the whole indication for three seconds and then the swap will happen.

We'll do that nice little fade out and then it goes away. Now you imagine again you can do the whole height thing, you can make that too much smoother experience, you can kind of choose what you want to do here. And so it's just another way in which case mix just gives you a bunch of events to tap into.

Its kind of your responsibility to go the nine yards on this. And so I don't know I like that, I think it's really, really simple and I think it really shows a great way to make nice tools fairly simple, right? Cuz most of our life is making tools very few things are maintaining very wonderful awesome websites.

All the little ancillary things around it you don't need to have an entire monolithic application for just the simple stuff.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now