---
title: "Web Components Work Great with htmx"
description: "This essay by Alexander Petros explores how Web Components can be integrated seamlessly with htmx, a library that enables dynamic web pages through HTML. It discusses the flexibility of htmx in handling interactive elements like Web Components alongside traditional server-driven approaches, such as multi-page apps. By using the example of an editable carnival ride table, Alexander demonstrates how Web Components simplify functionality without the need for heavy JavaScript frameworks, highlighting their compatibility with htmx's DOM-based lifecycle. Alexander also addresses potential challenges and how htmx manages them efficiently."
created: 2024-11-13
authors: ["Alexander Petros"]
tags: ["guides"]
---
People interested in htmx often ask us about component libraries.
React and other JavaScript frameworks have great ecosystems of pre-built components that can be imported into your
project; htmx doesn't really have anything similar.
The first and most important thing to understand is that htmx doesn't preclude you from using *anything*.
Because htmx-based websites are [often multi-page apps](https://unplannedobsolescence.com/blog/less-htmx-is-more/), each
page is a blank canvas on which you can import as much or as little JavaScript as you like.
If your app is largely hypermedia, but you want an interactive, React-based calendar for one page, just import it on
that one page with a script tag.
We sometimes call this pattern "Islands of Interactivity", it's referenced in our
explainers [here](/essays/10-tips-for-SSR-HDA-apps#tip-8-when-necessary-create-islands-of-interactivity), [here](/essays/hypermedia-friendly-scripting#islands),
and [here](/essays/you-cant#myth-5-with-htmx-or-mpas-every-user-action-must-happen-on-the-server).
Unlike JS frameworks, which are largely incompatible with each other, using islands with htmx won't lock you into any
specific paradigm.
But there's a second way that you can re-use complex frontend functionality with htmx, and
it's [Web Components](https://developer.mozilla.org/en-US/docs/Web/API/Web_components)!
## Practical Example
Let's say that you have a table that says what carnival rides everyone is signed up for:
```html
Name
Carousel
Roller Coaster
Alex
Yes
No
Sophia
Yes
Yes
```
Alex is willing to go on the carousel but not the roller coaster, because he is scared; Sophia is not scared of either.
I built this as a regular HTML
table ([closing tags are omitted](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/td#technical_summary) for
clarity):
```html
Name
Carousel
Roller Coaster
Alex
Yes
No
Sophia
Yes
Yes
```
Now imagine we want to make those rows editable.
This is a classic situation in which people reach for frameworks, but can we do it with hypermedia?
Sure!
Here's a naive idea:
```html
```
That will give us this table:
```html
Name
Carousel
Roller Coaster
Alex
Sophia
```
That's not too bad!
The save button will submit all the data in the table, and the server will respond with a new table that reflects the
updated state.
We can also use CSS to make the `