r/htmx • • 2d ago

Menus, dialogs and tabs with no JavaScript, for htmx pages

Most examples for a dropdown, a modal or tabs still need a JavaScript library. But plain HTML can do most of this on its own now, and that fits htmx pages well, since they're rendered on the server anyway.

  • Menus: popover
  • Modals: <dialog> with commandfor
  • Tabs: radio inputs

No script to load, nothing to set up again after an htmx swap, and it keeps working with JavaScript off.

I wrote up how each one works, with the HTML: https://alloyhtml.com/articles/dropdown-dialog-tabs-no-javascript/

Disclosure: the examples use Alloy CSS, a free stylesheet I made. The HTML works without it, so you can copy it into your own pages.

Curious what you use for this today: Alpine, hyperscript, or plain HTML?

26 Upvotes

21 comments sorted by

4

u/CuriousCapsicum 2d ago

I like doing as much as possible in HTML and CSS, and then using Stimulus.js to add richness and handle any other client side state. It works really well with server rendered HTML and HTMX replacement.

Unfortunately, HTML doesn’t provide total parity even now. For example, there are accessibility issues with using radio buttons to implement tabs.

2

u/prafandy 2d ago

Agreed, tabs are the weak spot. A screen reader hears a group of radio buttons, not a tab list. The keyboard still works and the picked one is announced, but it's not the real tab pattern, and the article says so in the tabs part. Where that matters, a few lines of JS for the tab roles is the honest fix, and Stimulus is a nice fit for exactly that.

2

u/kygyty 1d ago

I found the same accessibility issues when making Minium (https://minium.style). For page-level tabs, I prefer semantic navigation with `aria-current` and server-side routing. It avoids the radio button workaround and fits nicely with htmx. For actual interactive tab panels, I agree that a little JS makes sense.

1

u/TheRealUprightMan 1d ago

I just have the tab fetch the tabbody from the server as an hx-get. The tabbody outputs an htmx event trigger in the response. The tabbar recieves the event and uses htmx.takeClass() to set the "active" css class. Its all in the markup.

No radio buttons. Tab is just a clickable div

0

u/CuriousCapsicum 1d ago

AKA do it with JavaScript

1

u/TheRealUprightMan 1d ago

WTF? By that logic, HTMX is also doing it with JavaScript. Thanks for your brilliant feedback.

0

u/CuriousCapsicum 1d ago

There are loads of ways to implement tabs with JavaScript, with or without HTMX. The goal is to do it only with HTML.

3

u/Shrink_Laureate 2d ago

Wonderful.

It's a strange irony that the rise of the super-heavy SPA has coincided with HTML quietly getting better at doing these things with little or no Javascript at all.

3

u/prafandy 2d ago

Yeah, it's funny. popover and dialog alone now cover most of what I used to see people pull a whole library in for.

1

u/StaticFanatic3 2d ago

Pretty sure we’re well over the hump on that heavy SPA peak. No one is talking about JS frameworks anymore. AI coding has people shipping native or building all their own server rendered components

1

u/Shrink_Laureate 1d ago

Big organisations are often stuck with the choices they've made. I know my employer is stuck with Vue.js for the foreseeable future. Heavy SPAs may not be what people are talking about any more, but they're not going away any time soon.

2

u/_HMCB_ 2d ago

So Alloy is. CSS framework? Quickly looking at the docs, I really like how you’ve broken out the CSS layers. It seems logical.

2

u/prafandy 2d ago

Yes, a CSS framework, utilities plus a few ready parts like menus, dialogs and tabs, and every value (spacing, sizes, colours) comes from a fixed set I chose on purpose, so pages stay consistent. No JavaScript and no build step: link the file and go.

If you want to see it in practice, I rebuilt a few real sites (Hacker News, GitHub, Wikipedia) using only Alloy's classes: alloyhtml.com/remakes. Thanks for looking!

2

u/_HMCB_ 2d ago

Awesome. I am going to take a hard look at this this week.

2

u/ShotgunPayDay 2d ago

The tabs part would be really cool if the css could be made generic like:

.tabs:has(input:checked) {
  --selected-index: sibling-index();
}

.panels > div:nth-child(var(--selected-index)) {
  display: block;
}

But this doesn't exist just yet sadly, because computed values can't participate in the matching process yet.

3

u/prafandy 2d ago

Yes, I wanted exactly that too. Until sibling-index() works in selectors, Alloy just writes the rule out for each position (a Sass loop, up to 12 tabs), so the HTML stays generic: tab 3 shows panel 3, no ids or extra classes.

1

u/erroneousbosh 2d ago

This looks like something I'd use, thanks!

1

u/prafandy 2d ago

Thanks! If you try it and something breaks, tell me, I'd like to know.

0

u/TheRealUprightMan 1d ago

Now? Most of this has been javascript free for decades. I mean, <dialog> was introduced in 2013. That was 13 years ago.

Why are people acting like basic HTML is some sort of new thing? Do we really need this every few days? Glad you finally figured out HTML. Go stand over there with the rest of them

1

u/prafandy 2h ago

<dialog> is old, true. But opening one without a line of JS needs \`commandfor\`, which only landed in all three browsers this year (Chrome 135, Firefox 144, Safari 26.2). Same for popover, which became Baseline in 2024. That's the new part.

1

u/TheRealUprightMan 2h ago

There is a huge difference between one line of javascript to control a model, and firing up boostrap or React and a huge javascript framework. You can use a <details> element as a sidebar but people keep grabbing massive javascript frameworks anyway.