agentleFS
4444J994444J99/a-i--skills/skills/development/htmx-interaction-patterns/SKILL.md

htmx-interaction-patterns

Skill17 starsChanged 5 months agoApache-2.0Archived repositoryView on GitHub

Build dynamic web interfaces with htmx using hypermedia-driven patterns for partial page updates, lazy loading, infinite scroll, and server-sent events without writing JavaScript. Triggers on htmx usage, hypermedia architecture, or progressive enhancement requests.

1 other project uses a version of this.

The file

We split this skill into the rules it gives an agent

1 rule. About 0 are common, 1 is distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈1

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Update multiple elements from one response:

    line 206

≈ “Common” is an estimate: at least 1 in 5 of the 364,980 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

htmx Interaction Patterns

Build dynamic interfaces with HTML-over-the-wire — server renders HTML fragments, htmx swaps them in.

Core Concepts

htmx extends HTML with attributes that make AJAX requests and swap content — no JavaScript required.

<!-- Click button → GET /items → replace #item-list content -->
<button hx-get="/items" hx-target="#item-list" hx-swap="innerHTML">
  Load Items
</button>
<div id="item-list"></div>

Key Attributes

AttributePurposeExample
hx-getGET requesthx-get="/api/items"
hx-postPOST requesthx-post="/api/items"
hx-putPUT requesthx-put="/api/items/1"
hx-deleteDELETE requesthx-delete="/api/items/1"
hx-targetWhere to put responsehx-target="#results"
hx-swapHow to inserthx-swap="outerHTML"
hx-triggerWhen to firehx-trigger="click"
hx-indicatorLoading indicatorhx-indicator="#spinner"

Swap Strategies

StrategyBehavior
innerHTMLReplace children (default)
outerHTMLReplace entire target element
afterbeginInsert as first child
beforeendInsert as last child
beforebeginInsert before target
afterendInsert after target
deleteRemove target
noneNo DOM update

Common Patterns

Search with Debounce

<input type="search" name="q"
  hx-get="/search"
  hx-trigger="input changed delay:300ms"
  hx-target="#results"
  hx-indicator="#search-spinner"
  placeholder="Search skills...">
<span id="search-spinner" class="htmx-indicator">Searching...</span>
<div id="results"></div>

Server returns an HTML fragment:

@app.get("/search")
async def search(q: str = ""):
    results = search_skills(q)
    return HTMLResponse(render_results(results))

Inline Editing

<!-- View mode -->
<div id="skill-42" hx-get="/skills/42/edit" hx-trigger="dblclick" hx-swap="outerHTML">
  <h3>testing-patterns</h3>
  <p>Write effective tests across the stack...</p>
</div>

<!-- Edit mode (returned by server) -->
<form id="skill-42" hx-put="/skills/42" hx-swap="outerHTML">
  <input name="name" value="testing-patterns">
  <textarea name="description">Write effective tests...</textarea>
  <button type="submit">Save</button>
  <button hx-get="/skills/42" hx-swap="outerHTML">Cancel</button>
</form>

Infinite Scroll

<div id="item-list">
  <!-- Items rendered here -->
  <div hx-get="/items?page=2"
       hx-trigger="revealed"
       hx-swap="outerHTML">
    Loading more...
  </div>
</div>

Server returns items + next trigger:

<div class="item">Item 11</div>
<div class="item">Item 12</div>
<!-- Next page trigger -->
<div hx-get="/items?page=3" hx-trigger="revealed" hx-swap="outerHTML">
  Loading more...
</div>

Lazy Loading

<div hx-get="/dashboard/metrics" hx-trigger="load" hx-swap="innerHTML">
  <div class="skeleton-loader"></div>
</div>

Active Search with URL State

<input type="search" name="q"
  hx-get="/search"
  hx-trigger="input changed delay:300ms"
  hx-target="#results"
  hx-push-url="true">

Confirmation Dialogs

<button hx-delete="/items/42"
        hx-confirm="Delete this item?"
        hx-target="#item-42"
        hx-swap="outerHTML swap:500ms">
  Delete
</button>

Server-Sent Events

<div hx-ext="sse" sse-connect="/events" sse-swap="message">
  Waiting for updates...
</div>
from sse_starlette.sse import EventSourceResponse

@app.get("/events")
async def events():
    async def generate():
        while True:
            data = await get_update()
            yield {"data": render_update(data)}
    return EventSourceResponse(generate())

Server-Side Patterns

Partial vs Full Responses

@app.get("/items")
async def list_items(request: Request):
    items = await get_items()
    if "HX-Request" in request.headers:
        # htmx request: return fragment
        return HTMLResponse(render_items_fragment(items))
    else:
        # Normal request: return full page
        return HTMLResponse(render_full_page(items))

Response Headers

from starlette.responses import HTMLResponse

@app.post("/items")
async def create_item(request: Request):
    item = await save_item(request)
    response = HTMLResponse(render_item(item))
    response.headers["HX-Trigger"] = "itemCreated"  # Client-side event
    response.headers["HX-Retarget"] = "#item-list"
    response.headers["HX-Reswap"] = "afterbegin"
    return response

OOB (Out-of-Band) Swaps

Update multiple elements from one response:

<!-- Primary swap -->
<div id="item-42">Updated item content</div>
<!-- OOB: also update the counter -->
<span id="item-count" hx-swap-oob="true">43 items</span>

CSS Integration

/* Loading indicator */
.htmx-indicator { opacity: 0; transition: opacity 200ms; }
.htmx-request .htmx-indicator { opacity: 1; }

/* Settling animation */
.htmx-settling { opacity: 0; }
.htmx-settled { opacity: 1; transition: opacity 300ms; }

/* Added items */
.htmx-added { opacity: 0; }
.htmx-settled.htmx-added { opacity: 1; transition: opacity 300ms; }

Anti-Patterns

  • Returning JSON from htmx endpoints — Always return rendered HTML fragments
  • Large page replacements — Return small, targeted fragments
  • No loading indicators — Always show feedback during requests
  • Ignoring non-htmx requests — Support full-page loads for bookmarkability
  • Client-side state management — Keep state on the server; let HTML be the state

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.