Replies: 2 comments 6 replies
-
Hey, One thing to keep in mind is that htmx's core purpose is to handle communication with the server. As such, in a usecase like yours when you want pure client side logic still relying on some htmx syntax, an extension is indeed the right way to tackle this. The internal API exposes Lines 2908 to 2912 in 407408b See how it simply calls So, you should be able to use both these functions to run your own event firing logic. This way, you'd use no magic trick, no workarounds, just 2 functions from the internal API. Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
…or you could simply use hyperscript. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
As I'm learning HTMX, I'm trying to find the "way" and the "rough edges" to build up a mental model for how HTMX works best. One thought that I recently had was to create an extension,
hx-event
, that would allowhx-trigger
bindings to emit a synthetic event instead of triggering an AJAX request. An example of this might be to emit aclose-modal
event on the[x]
button in a modal window (and then have some parent element listening for this event to handle the closing). This way, it could listen for both theclick
and perhaps akeydown[key === 'Escape']
event:Then, presumably, I might have a parent element that is listening for it:
This turned out to be harder than I expected because it looks like HTMX doesn't want to allow "naked triggers" on an element. When it encounters an
hx-trigger
in the DOM with nohx-get
(for example), it binds a no-op event-handler, which in turn, seems to prevent any otherhx-trigger
from being bound to the same node (such as by my extension). So I had to jump through hoops to hide thehx-trigger
before node-processing, then re-attach it. All of which makes me think that maybe this (my extension) is just a terrible idea to begin with :DI know Carson Gross talks about how much the event-system in the browser is helpful, so I'm trying to bend my mind to think about event-based communication. But maybe this particular context is not the way.
The better way might be to just bind the
hx-get
to the close button and have it respond to bothclick
and closeModalevents. Then, the server could always respond with an
HX-Trigger: close-modal` HTTP response header.Anyway, just thinking out loud and seeing if anyone has any thoughts on "there be dragons" and how to think about wiring up an HTMX application.
For what it's worth, here's my write-up of the extension: https://www.bennadel.com/blog/4803-creating-an-hx-event-extension-for-triggering-events-in-htmx.htm
Beta Was this translation helpful? Give feedback.
All reactions