thoughtbot / superglue

A productive library for Classic Rails, React and Redux
https://thoughtbot.github.io/superglue/
MIT License
327 stars 8 forks source link

Add a data-sg-replace companion to data-sg-visit to replace history instead of push #62

Open jho406 opened 1 month ago

jho406 commented 1 month ago

context

Superglue embraces UJS helpers.

By adding a data attribute to a normal link like the below:

<a href="/posts" data-sg-visit>

Superglue makes a SPA visit from one page to another without reloading the page. This is achieved by grabbing the next page's props at /posts.json, and passing it into the next page's component while pushing history state. data-sg-visit is powered by the visit function, which you can override when you first generate a new superglue app.

Superglue adds a handler to watch out for DOM elements with data-sg-* attributes. These handlers are created here and you may spot that data-sg-visit has a companion handler called data-sg-placeholder. This is documented here.

Issue

data-sg-visit pushes history upon a successful visit using a link/ form. What I'd like to add is a companion data attribute, in the same vien as data-sg-placeholder, that would replace history instead.

The visit function doesn't quite have this ability yet. Instead it passes a suggestedAction to your application_visit here. And that suggested action is determined here.

So to create this functionality we need to allow the visit function to accept a suggestedAction to override the smart defaults https://github.com/thoughtbot/superglue/blob/fd3f027868f9db536db5ebc8b24c02a2f7c1fc62/superglue/lib/action_creators/requests.js#L208

So that it'll be eventually passed to

https://github.com/thoughtbot/superglue/blob/fd3f027868f9db536db5ebc8b24c02a2f7c1fc62/superglue_rails/lib/install/templates/web/application_visit.js#L24

Then finally, add the data attribute handler data-sg-replace to https://github.com/thoughtbot/superglue/blob/main/superglue/lib/utils/ujs.js#L90

I'm open to naming suggestions to the UJS attribute, variables, etc.