HTMX Patterns: Dynamic dropdown menu with Bootstrap 5
After doing some more development with HTMX 4.0 , I find myself creating a list of patterns that I reuse, as somehow constructing them from first principles is more fiddly than I initially think, every time. I'm using HTMX 4.0 for the interactivity with Bootstrap 5.3 for the CSS styling. For the showcase, I'm using Perl and Mojolicious, but the approach obviously works elsewhere too.
Dynamic dropdown menu
From time to time, I want or need to provide the user with a more dynamic selection of items. In the below example, I use this technique to populate a list of labels that can be assigned to an item.
Doing so in HTMX (plus Bootstrap) can be done in a simplistic pattern. I put the HTML for the dropdown (or rather, drop-up in this case) into a separate template and URL. This HTML is then loaded when the button is clicked.
```perl
@@ main.html.ep
...
<a class="btn btn-secondary dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-expanded="false">
<%= $agent\_state->{session\_id} %%>
* <a class="dropdown-item" href="#">…</a>
...
@@ available-sessions.html.ep
* \+ New session
*
% for my $sess ($available\_sessions->@\*) {
<a href="">
<%= $sess->{timestamp} // $sess->{last\_modified} %>
% if( $sess->{name} ) {
<%= $sess->{name} %> %
% } else {
<i>(unnamed)</i>
% }
<%= $sess->{id} %%>
</a>
% }
```
Serve available-sessions from /available-sessions.
no-JS fallback
I haven't actually implemented a no-JS fallback for this. This would need some bookkeeping for the application state, which URL I came from, and how I can restore the previous page after selecting from the dropdown, which I skipped on. In principle, it should be fairly easy to render the dropdown template inline, reloading the page on submit, or just have the complete dropdown have its own (no-JS) page.
Pattern: Progress / status poll
For progress updates or status polling, I really like the approach of having two templates, one that reloads itself until we arrive at the final stage, and a template that renders the final state.
@@ message-processing.html.ep
<div id="item"
hx-get="/processing?request_id=123"
hx-trigger="load delay:1s"
hx-swap="outerHTML">
In progress
</div>
The other template is the final template. It has no more HTMX properties related to reloading the item, so the reloading simply stops.
@@ message-done.html.ep
<div id="item">
Item progressed
</div>
If you have multiple items in flight at the same time, it might be worthwhile to reload the complete list of items in one go instead of having each item issue its own separate request, especially as HTMX queues requests.
An alternative approach is to use Server-Sent Events, which HTMX also supports natively. That way, the client still polls, but the server can push multiple updates through a single URL and the client can signal after which update it wants to resume.
no-JS fallback
There is no good fallback without Javascript. You could work with a refreshing page:
<meta http-equiv="refresh" content="5; url=https://example.com/processing?request_id=123">
But that has the problem that the user might want to do other things on the page. Having a manual refresh link is likely the best approach.
Using Server-Sent Events does not update the page via Javascript either.
Using the <progress> element
HTMX does not replace attributes. The HTML <progress> element uses an attribute to fill the progress bar:
<progress value="0" max="100">0 %</progress>
| |
|
| |
|
|