bigskysoftware / htmx

</> htmx - high power tools for HTML
https://htmx.org
Other
38.19k stars 1.3k forks source link

hx-swap="delete" should be allowed to work even with no request at all #2714

Open mlncn opened 3 months ago

mlncn commented 3 months ago

The documentation on swapping says that for delete, hx-swap "deletes the target element regardless of the response"

While #1130 has raised the issue of this not happening for 204 response codes, i think it should go further— if hx-target and hx-swap="delete" are set, the target should be deleted even if none of hx-get, hx-post, hx-put, hx-patch, or hx-delete are set.

Current use case is loading the full size of an image and then, to remove it, deleting it entirely. Of course JavaScript can do this, but lifting this arbitrary constraint on HTMX feels like a consistent approach.

Telroshan commented 3 months ago

Hey, while I understand the concern, imho this doesn't and shouldn't belong to htmx ; htmx's goal is to make interactions with the server, and with a pure client-side interaction like this, it would require a lot of specific if statements in the codebase to skip many parts of the usual process (no XMLHTTPRequest to make, but still fire the settling phase and events after that with the swap logic...)

You may want to look into hyperscript as this one is a pure client-side library.

Note that I would strongly advise using custom JS here, with a simple element.remove() call. Or, if you really want some reusable thing within htmx only, make an extension of your own that sets up elements with a [hx-swap="delete"]|hx-target] selector for example, or something similar.