bigskysoftware / htmx-extensions

103 stars 30 forks source link

Add `shouldSwap` field to `htmx:sseBeforeMessage` event detail #26

Closed vlence closed 1 month ago

vlence commented 2 months ago

I believe that having a shouldSwap boolean field on the htmx:sseBeforeMessage event detail would make this event more useful. There are situations, I have personally come across them, where we don't want to swap in the SSE message and being able to opt out of that would be very useful.

I can make a PR for this. In my own project I have found that it can be achieved very easily.

// swap the response into the DOM and trigger a notification
event.shouldSwap = true
if (!api.triggerEvent(elt, 'htmx:sseBeforeMessage', event)) {
  return
}

if (event.shouldSwap) {
  swap(elt, event.data)
  api.triggerEvent(elt, 'htmx:sseMessage', event)
}
vlence commented 1 month ago

Closing because I just found out we can call preventDefault() on the htmx:sseBeforeMessage event to stop the swap.