KonnorRogers / mrujs

UJS for modern javascript.
https://mrujs.netlify.app
MIT License
164 stars 16 forks source link

Add configuration option to disable DOM morphing #213

Open tcannonfodder opened 1 year ago

tcannonfodder commented 1 year ago

Describe Problem

While I do appreciate the default behavior of using DOM morphing on to render form errors, it would be great to be able to disable it at both the element level using data- attributes, and library-wide. It's actually causing a FOUC with my font-awesome icons. I also don't want to add Turbo to my project, because it mucks around with the DOM & navigation flow too much for my liking

Suggest Solution

2 configuration options that stop calling morphResponse:

Additional Details

👋 Thanks for this library, I'm glad folks are carrying the torch of UJS!

KonnorRogers commented 1 year ago

@tcannonfodder I don't think I documented it anywhere, but technically you can stop morphs like this:

<form data-ujs-morph="false">
</form>

https://github.com/KonnorRogers/mrujs/blob/315a4b8725816455c8a82425f2692d2281a7d862/src/navigationAdapter.ts#L168

The other option is to add an event listener for the ajax:beforeNavigate event.

document.addEventListener("ajax:beforeNavigate", (e) => {
  if (e.detail.fetchResponse.ok) return

  // We're in 4xx -> 5xx territory here.
  e.preventDefault()
})

A config option would make it easier, but it's at least possible today!

tcannonfodder commented 1 year ago

This is great to know! I'll submit a PR to document this!

KonnorRogers commented 1 year ago

Thanks! ❤️

May also be worth double checking that it works as welll 🙈

tcannonfodder commented 10 months ago

Note for future detectives: the event is ajax:beforeNavigation

KonnorRogers commented 10 months ago

🕵️ thanks!

tcannonfodder commented 10 months ago

Of course!

An alternative: is there documentation on how to write a different navigation adapter? I might try making my own, where it checks for 322 and a JSON body with location and uses that to redirect using window.location.replace

KonnorRogers commented 10 months ago

@tcannonfodder it's largely internal, but you should be able to stop the adapter and do a simple replace for your use-case.

document.addEventListener("ajax:beforeNavigate", (e) => {
  if (e.detail.fetchResponse.status !== 322) return

  // We're in 322 territory here.
  e.preventDefault()
  window.location.replace(e.detail.fetchResponse.url)
})

I think that would cover your use-case