Use with htmx
Coverup listens for htmx events, so the common patterns need no extra JavaScript. The demos below call a mock server that lives in your browser.
Confirm before a request
Add hx-confirm. Coverup replaces the browser dialog with a styled one, and the request is sent only after the user confirms.
With hx-confirm
Add hx-confirm to any htmx element. Coverup intercepts it and shows the styled dialog; the request fires only after the user confirms. Optional data-confirm-message, data-confirm-label and data-confirm-tone="neutral".
| Ada Lovelace | |
| Grace Hopper |
<table class="tbl" style="max-width:520px">
<tbody id="confirm-demo">
<tr><td>Ada Lovelace</td><td style="text-align:right"><button class="btn btn-ghost btn-sm" hx-delete="/api/users/1" hx-confirm="Delete Ada Lovelace?" data-confirm-message="This can't be undone." hx-target="closest tr" hx-swap="outerHTML swap:150ms"><svg class="i i-sm"><use href="#i-trash"/></svg>Delete</button></td></tr>
<tr><td>Grace Hopper</td><td style="text-align:right"><button class="btn btn-ghost btn-sm" hx-delete="/api/users/2" hx-confirm="Delete Grace Hopper?" data-confirm-message="This can't be undone." hx-target="closest tr" hx-swap="outerHTML swap:150ms"><svg class="i i-sm"><use href="#i-trash"/></svg>Delete</button></td></tr>
</tbody>
</table>Loading state
htmx adds .htmx-request to the element that fired the request. Buttons turn it into a spinner and ignore clicks until the response lands.
Loading with htmx
htmx adds .htmx-request to the element that fired the request. .btn turns that into a spinner and ignores clicks until the response lands. This demo calls a mock endpoint.
<button class="btn btn-primary" hx-post="/api/save">Save changes</button>Toasts from the server
Return an HX-Trigger header with a toast event. Coverup shows it. Errors work too: htmx processes the header even on a 500.
From the server
Send a toast with a response header. No client code needed. This button hits mock endpoints that set the header.
<button class="btn btn-primary" hx-post="/save">Save</button>
<!-- response header -->
HX-Trigger: {"toast": {"type": "ok", "title": "Saved", "message": "Your changes are live."}}Live search
Server-side filter with htmx
The same box, but htmx asks the server for matching rows. This demo is answered by a mock endpoint.
| Name | Role | Status |
|---|
<div class="panel">
<div class="panel-head">
<div class="search"><svg class="i"><use href="#i-search"/></svg><input class="input" style="width:240px" name="q" placeholder="Search users" aria-label="Search users" autocomplete="off"
hx-get="/api/users" hx-trigger="input changed delay:250ms" hx-target="#remote-users"></div>
</div>
<div class="table-scroll">
<table class="tbl" style="min-width:560px">
<thead><tr><th>Name</th><th>Role</th><th>Status</th><th></th></tr></thead>
<tbody id="remote-users" hx-get="/api/users" hx-trigger="load"></tbody>
</table>
</div>
</div>Modals
Swap markup into a dialog.modal and it opens. Send HX-Trigger: closeModal to close it.
Load a modal with htmx
The dialog opens by itself when htmx swaps content into it. The server can close it with HX-Trigger: closeModal.
<button class="btn" hx-get="/users/new" hx-target="#modal-body">New user</button>
<dialog class="modal" id="modal">
<div id="modal-body"></div>
</dialog>
<!-- server returns .modal-head / .modal-body / .modal-foot markup;
the form can hx-post and the response can send HX-Trigger: closeModal -->Events Coverup handles
| Event | Behavior |
|---|---|
htmx:confirm | Shows the styled confirm dialog instead of window.confirm |
toast (HX-Trigger) | Shows a toast. Detail: {type, title, message} |
closeModal (HX-Trigger) | Closes any open dialog.modal |
htmx:afterSwap | Opens a closed dialog.modal that just received content |
The mock server is assets/mock.js. You do not need it in a real project.